A multi-hop question needs several facts joined
A multi-hop question is one whose answer needs several facts joined in a row. GraphRAG answers such questions by storing facts as a graph and following its links, rather than only searching text.
It matters because ordinary retrieval finds the one passage most like the question. Which building are the Databases lectures in? No single sentence says.
By the end of this lesson
Note
I can answer a multi-hop question by following a knowledge graph.
Key words
| Word | What it means | Example |
|---|---|---|
| Hop | one step from a fact to the next | |
| Knowledge graph | facts stored as things joined by named links | |
| Triple | one fact as subject, relation and object | Room B12, in_building, Turing Building |
| Path | the chain of links from one thing to another | |
| Community | a group of things linked closely together | |
| GraphRAG | retrieval that uses a graph of facts |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsOne search finds one passage
The answer here needs three facts. Databases is taught by Dr Okafor. Dr Okafor teaches in Room B12. Room B12 is in the Turing Building.
A search for the question's words finds the first sentence, because it shares the word Databases. That sentence names a lecturer, not a building. The search has done its job and the question is still unanswered.
The same facts as a graph
flowchart LR
D["Databases"] -->|taught_by| O["Dr Okafor"]
O -->|teaches_in| B["Room B12"]
B -->|in_building| T["Turing Building"]
N["Networks"] -->|taught_by| P["Ms Patel"]
P -->|teaches_in| A["Room A3"]
A -->|in_building| L["Lovelace Building"]Check
Have a go
Check
3 questionsWrite the graph and the search, part 1
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'const TRIPLES = [' ' [\"Databases\",\"taught_by\",\"Dr Okafor\"],' ' [\"Dr Okafor\",\"teaches_in\",\"Room B12\"],' ' [\"Room B12\",\"in_building\",\"Turing Building\"],' ' [\"Networks\",\"taught_by\",\"Ms Patel\"],' ' [\"Ms Patel\",\"teaches_in\",\"Room A3\"],' ' [\"Room A3\",\"in_building\",\"Lovelace Building\"],' ' [\"Library\",\"in_building\",\"Lovelace Building\"],' ' [\"Sports Hall\",\"open_until\",\"22:00\"],' '];' 'const CHUNKS = [' ' \"Databases is taught by Dr Okafor.\",' ' \"Dr Okafor teaches in Room B12.\",' ' \"Room B12 is in the Turing Building.\",' ' \"Networks is taught by Ms Patel.\",' ' \"Ms Patel teaches in Room A3.\",' ' \"Room A3 is in the Lovelace Building.\",' ' \"The Library is in the Lovelace Building.\",' ' \"The Sports Hall is open until 22:00.\",' '];' > graph5.js"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the graph and the search, part 2
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'console.log(\"Question: which building are the Databases lectures in?\");' 'const WORDS = [\"databases\",\"building\"];' 'let best = 0;' 'let top = -1;' 'CHUNKS.forEach((c, i) => {' ' let n = 0;' ' for (const word of WORDS) if (c.toLowerCase().includes(word)) n = n + 1;' ' if (n > top) { top = n; best = i; }' '});' 'console.log(\"Keyword search, best sentence\" + \": \" + CHUNKS[best]);' 'function next(at, relation) {' ' for (const t of TRIPLES) if (t[0] === at && t[1] === relation) return t[2];' ' return null;' '}' 'let at = \"Databases\";' 'const hops = [at];' 'for (const r of [\"taught_by\",\"teaches_in\",\"in_building\"]) {' ' at = at === null ? null : next(at, r);' ' hops.push(at === null ? \"?\" : r + \" -> \" + at);' '}' >> graph5.js"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the graph and the search, part 3
docker exec -w /home/node/project agent sh -c "printf '%s\n' 'console.log(\"Graph, three hops\" + \": \" + hops.join(\" | \"));' 'function path(from, goal) {' ' const queue = [[from]];' ' const seen = [from];' ' while (queue.length > 0) {' ' const p = queue.shift();' ' const end = p[p.length - 1];' ' if (end === goal) return p;' ' for (const t of TRIPLES) {' ' if (t[0] === end && seen.includes(t[2]) === false) { seen.push(t[2]); queue.push(p.concat([t[2]])); }' ' }' ' }' ' return [];' '}' 'const found = path(\"Networks\", \"Lovelace Building\");' 'console.log(\"Path from Networks to Lovelace Building\" + \": \" + (found.length ? found.join(\" > \") + \" (\" + (found.length - 1) + \")\" : \"no path\"));' 'const groups = [];' 'for (const t of TRIPLES) {' ' const hit = groups.filter((g) => g.includes(t[0]) || g.includes(t[2]));' ' const merged = [t[0], t[2]];' >> graph5.js"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the graph and the search, part 4
docker exec -w /home/node/project agent sh -c "printf '%s\n' ' for (const g of hit) for (const n of g) if (merged.includes(n) === false) merged.push(n);' ' for (const g of hit) groups.splice(groups.indexOf(g), 1);' ' groups.push(merged);' '}' 'console.log(\"Groups\" + \": \" + groups.length);' 'for (const g of groups) console.log(\" \" + g.join(\", \"));' >> graph5.js"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Search the text, then follow the graph
docker exec -w /home/node/project agent node graph5.jsShown here, not run. Press Start lab to type it into your own lab’s terminal.
What the Run printed
| Line | What it shows |
|---|---|
| Keyword search, best sentence: Databases is taught by Dr Okafor. | One search finds a lecturer, not a building |
| Graph, three hops: Databases | taught_by -> Dr Okafor | teaches_in -> Room B12 | in_building -> Turing Building | Three hops reach the answer |
| Path from Networks to Lovelace Building: Networks > Ms Patel > Room A3 > Lovelace Building (3) | A search finds the shortest path |
| Groups: 3 | The graph falls into three groups of linked things |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsGraphRAG builds the graph with a model
Here the graph is written by hand. In GraphRAG, an approach published by Microsoft Research in 2024, a model reads every document and writes out the things and links it finds. Groups of closely linked things are found, and a model summarises each group.
A question about one thing follows links from it. A question about the whole collection, such as the main themes, reads the group summaries.
A common mistake
Warning
Many people think a graph built by a model is a set of checked facts. The model can miss a link or invent one, and building the graph costs a call per passage. Keep the source passage beside each link, so an answer can show where it came from.
Match each word to its meaning
Have a go
Loading…
Your answers are marked here and are not kept. An account keeps your marks.
Assignment: follow a knowledge graph
Answer multi-hop questions over a list of triples, each [subject, relation, object].
neighbours(triples, node) returns [relation, object] for every triple whose subject is node, in list order. follow(triples, start, relations) follows one relation at a time, taking the first matching triple each hop, and returns where it ends, or None when a hop has no match.
path(triples, start, goal) returns the shortest list of nodes from start to goal, following links forwards only, with a breadth-first search that tries triples in list order. It returns [start] when they are the same and [] when there is no path. hops(triples, start, goal) returns the number of links on that path, or -1.
def neighbours(triples, node):
return None
def follow(triples, start, relations):
return None
def path(triples, start, goal):
return None
def hops(triples, start, goal):
return None
Make a free account to save your work and track your progress.
Exit quiz
Have a go