Lesson 25.2 · 2 of 3 · 4 min read · Reviewed 24 Nov 2026
Building a graph from documents
By the end, you can
- Build a knowledge graph from documents with a model.
- Control quality when extracting entities and relations.
The build pipeline
documents --> chunk --> model extracts (entities, relations) per chunk
--> entity resolution (merge duplicates)
--> graph store
--> optional: detect communities, summarise each
Extraction with a schema
Define allowed types so output is consistent (chapter 14):
{
"entities": [{"name": "Team Atlas", "type": "Team"},
{"name": "Billing service", "type": "Service"}],
"relations": [{"from": "Team Atlas", "type": "OWNS", "to": "Billing service",
"source_chunk": "doc7#3"}]
}
Keep the source chunkChunking: Splitting documents into smaller passages before embedding and indexing them. on every edge so answers can cite it.
Entity resolution
Merge by normalised names, aliasesModel alias: A short name that points to the latest version of a model, so it can change over time., ids where available, and model judgement for hard cases. Without this, the graph fragments and traversals miss connections.
Community summaries
The GraphRAGGraphRAG: RAG that builds a knowledge graph of entities and relationships from documents and retrieves through it. approach described by Edge and colleagues clusters the graph into communities of closely linked entities and writes a summary for each. Broad questions (“What are the main themes across these reports?”) are then answered from community summaries rather than from a handful of chunks.
Costs
Extraction runs a model over every chunk, often more than once. Use batches (68.4), cache shared prompts, and rebuild incrementally as documents change.
Key points
- A model can extract entities and relationships from text into a schema you define.
- Entity resolution merges duplicates ('R. Mensah', 'Rita Mensah') into one node.
- Some GraphRAG approaches also cluster the graph into communities and summarise each for broad questions.
Resolve entities in extracted triples
Merge duplicate entity names from model extraction into single nodes, so graph queries stop missing connections.
You will have: A resolve() function, node counts before and after, and a query that only works after resolution.
Steps
- Read the extracted triples: the same person and team appear under several spellings.
- Write a key function: lowercase, trim, drop full stops, and apply an alias table for initials and 'Surname, First' forms.
- Rewrite every triple with resolved names, keeping each edge's source chunk id.
- Count distinct nodes before and after, and list the teams Rita Mensah leads.
Done when
- Distinct nodes drop from 10 to 5.
- After resolution, Rita Mensah leads 2 teams.
- Every resolved edge still has its source chunk id.
Open in the Playground No API key needed.
Starter code (Python)
TRIPLES = [ # (from, relation, to, source_chunk), as a model might extract them
("R. Mensah", "LEADS", "Team Atlas", "doc1#2"),
("Rita Mensah", "LEADS", "team atlas ", "doc3#1"),
("Mensah, Rita", "LEADS", "Team Orion", "doc4#5"),
("Team Atlas", "OWNS", "Billing", "doc1#3"),
("Team Orion", "OWNS", "Payments", "doc4#6"),
("team orion", "OWNS", "payments.", "doc5#1"),
]
ALIASES = {"r mensah": "rita mensah", "mensah, rita": "rita mensah"}
def nodes(triples):
return {t[0] for t in triples} | {t[2] for t in triples}
def key(name):
# TODO: lowercase, strip spaces and full stops, then map through ALIASES
return name
def resolve(triples):
# TODO: return triples with key() applied to both ends, keeping relation and source
return triples
print("nodes before:", len(nodes(TRIPLES)))
print("nodes after:", len(nodes(resolve(TRIPLES))))
Show a solution
TRIPLES = [ # (from, relation, to, source_chunk), as a model might extract them
("R. Mensah", "LEADS", "Team Atlas", "doc1#2"),
("Rita Mensah", "LEADS", "team atlas ", "doc3#1"),
("Mensah, Rita", "LEADS", "Team Orion", "doc4#5"),
("Team Atlas", "OWNS", "Billing", "doc1#3"),
("Team Orion", "OWNS", "Payments", "doc4#6"),
("team orion", "OWNS", "payments.", "doc5#1"),
]
ALIASES = {"r mensah": "rita mensah", "mensah, rita": "rita mensah"}
def nodes(triples):
return {t[0] for t in triples} | {t[2] for t in triples}
def key(name):
k = " ".join(name.lower().replace(".", " ").split())
return ALIASES.get(k, k)
def resolve(triples):
return [(key(a), rel, key(b), src) for a, rel, b, src in triples]
resolved = resolve(TRIPLES)
print("nodes before:", len(nodes(TRIPLES)))
print("nodes after:", len(nodes(resolved)))
teams = sorted({b for a, rel, b, _ in resolved if a == "rita mensah" and rel == "LEADS"})
print("rita mensah leads:", teams)
print("every edge keeps its source:", all(src for *_, src in resolved))