Knowledge graphs and graph RAG
Be able to combine graph traversal with retrieval — like the platform's own knowledge graph.
Prerequisites
Intuition
Ordinary RAG fetches independent pieces of text. That works badly for questions requiring you to follow relationships:
- «What do I need to know before I start on LoRA?» → it requires traversing the prerequisite chain.
- «Which projects are affected if this source changes?» → it requires the dependency graph.
- «Summarise everything we know about X» → it requires aggregating over many documents.
Graph RAG combines two steps: find entry nodes with a vector search, and traverse the graph from there to collect the context.
AI-grafen is itself an example: (:KnowledgeNode)-[:REQUIRES]->(:KnowledgeNode) plus embeddings over the source texts. The learning path is a graph traversal; the explanations are fetched with a vector search.
Code
def graph_rag(question, vector_index, graph, k_entries=3, depth=2, max_nodes=15):
"""1) find entry nodes semantically 2) expand in the graph 3) build the context"""
entries = [h["node_slug"] for h in vector_index.search(question, k=k_entries)]
visited, frontier = set(entries), list(entries)
for _ in range(depth):
nxt = []
for slug in frontier:
for neighbour in graph.neighbours(slug, relations=("REQUIRES", "TEACHES")):
if neighbour not in visited and len(visited) < max_nodes:
visited.add(neighbour); nxt.append(neighbour)
frontier = nxt
# Sort topologically so that the context follows the learning order
order = graph.topological(visited)
parts = []
for slug in order:
n = graph.node(slug)
parts.append(f"### {n['title']} (level {n['level']})\nPrerequisites: {', '.join(n['requires']) or '—'}\n{n['summary']}")
return "\n\n".join(parts), order
context, path = graph_rag("What do I need to know before LoRA?", index, graph)
# path: ['vektorer', 'matriser', 'matrisfaktorisering', 'neuronnat', ..., 'lora']
The cost is the building. A knowledge graph requires entities and relationships that are correct — which means editorial work or LLM extraction with review. Microsoft's GraphRAG extracts entities and builds community summaries automatically, which costs many LLM calls across the whole corpus.
Choose graph RAG when: the relationships already exist in structured form (as here), or when the questions systematically require several steps and aggregation. Choose ordinary RAG when: the questions are answered by individual passages — which is most of them.
Mastery means
- Combines graph traversal with vector search
- Knows when graph RAG beats ordinary RAG
- Judges the cost of building the graph
Sign in to do the exercises and build your mastery up.
Sources
- arXiv — From Local to Global: A Graph RAG Approach to Query-Focused Summarization — arXiv (open access; licence per article)
- Neo4j — Graph data modeling — free to read