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 2 of 2

Multi-hop: follow the links

A breadth-first walk with a seen-set and a hop limit. Change the start to Dev and the limit to 2.

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?

Multi-hop: follow the links
Sign in to run this example in your browser.

Expected output

start at Maria, up to 1 hop(s)
  hop 1: Maria -manages-> Harbour shop
start at Maria, up to 3 hop(s)
  hop 1: Maria -manages-> Harbour shop
  hop 2: Harbour shop -is_in-> Northside
  hop 3: Northside -has-> charging station