Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 12.4 — Retrieval over a Graph
RAG and Vector Search · Example 1 of 2

Entities and relations from text, by hand

Fixed phrase patterns stand in for a model extractor. Note the skipped sentence: a missed pattern is a missing link, and nothing fails loudly.

Some questions need facts that sit in different passages and are connected by who or what they mention. Extract entities and relations into a graph, then follow the links hop by hop to collect the passages a chain of facts needs.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write neighbours(node, triples) that returns a list of (relation, other) pairs for every triple whose left side is node, in the order they appear. Print the neighbours of Harbour shop and of Northside.

02

Write hops_between(start, goal, triples, max_hops) that follows outgoing links breadth first and returns the number of hops needed to reach goal, or None if it takes more than max_hops. Print the result for Maria to "charging station" with limits 2 and 3.

Explain it without notes

01

What is a multi-hop question, and why does ordinary chunk search handle it badly?

02

Describe how a graph is built from text and how a question uses it.

03

What goes wrong with extraction, and how would you notice?

04

When would you decide a graph is not worth building?

Entities and relations from text, by hand
Sign in to run this example in your browser.

Expected output

(Maria) -manages-> (Harbour shop)
(Harbour shop) -is_in-> (Northside)
(Northside) -has-> (charging station)
(Dev) -manages-> (Hilltop shop)
(Hilltop shop) -is_in-> (Eastgate)
skipped: Priya looks after the Docks shop.
5 triples from 6 sentences