Knowledge graph¶
Some questions need passages that share no words with the question. In "Where did Marie Curie work?" one chunk says Marie Curie worked at the University of Paris, and the chunk that answers the follow-up, the University of Paris opened the Radium Institute, never names her. A vector or keyword search finds the first chunk and misses the second.
A knowledge graph connects them. Pagelore extracts the entities each chunk names (people, organizations, places, products, ...) and the relations between them. At query time it walks the graph from the entities in the question and from the search's best chunks, and adds the chunks it reaches to the results.
# 1. Turn it on for a project
curl -X PATCH localhost:8000/api/v1/projects/1 -H 'Content-Type: application/json' \
-d '{"graph": {"enabled": true, "extractor": "llm"}}'
# 2. Build it (a background job)
curl -X POST localhost:8000/api/v1/projects/1/graph/build -H 'Content-Type: application/json' -d '{}'
# 3. Use it in retrieval
curl -X POST localhost:8000/api/v1/projects/1/retrieve -H 'Content-Type: application/json' \
-d '{"query": "Where did Marie Curie work?", "mode": "hybrid", "graph": {}}'
The approach follows HippoRAG and HippoRAG 2. Retrieval still returns chunks, with their usual citations, so your app does not change. Pagelore does not write community summaries or answer "global" questions about the whole corpus (the GraphRAG global search).
Settings¶
The graph is off by default. Turn it on per project with graph in
POST /projects or PATCH /projects/{id}. A PATCH changes only the fields
you send.
| Setting | Default | |
|---|---|---|
enabled |
false |
Allow builds. Retrieval with graph uses whatever has been built |
extractor |
llm |
llm, gliner or rules (see below) |
entity_types |
person, organization, location, product, event, concept |
The types the llm and gliner extractors look for (1 to 30). The llm extractor gives other entities other; gliner finds only these types. Legal Case is stored as legal_case |
max_entities_per_chunk |
20 |
The most entities kept per chunk (1 to 100), most frequent first |
auto_build |
false |
Queue a build after each ingestion that adds or changes documents (see Build it) |
Extractors¶
llm |
gliner |
rules |
|
|---|---|---|---|
| How | The LLM reads each chunk and lists its entities (name, type, a one-sentence description) and the relations it states ("acquired", "works for") | A local GLiNER model finds the entities of your entity_types in each chunk. Two entities in the same sentence are related |
Capitalized names ("Bank of Egypt"), acronyms ("NASA"), quoted terms and code identifiers (camelCase, snake_case). Two entities in the same sentence are related |
| Cost | One LLM call per chunk, LLM_CONCURRENCY at a time |
No API calls. A ~1 GB model, downloaded on first use; slower than rules on a CPU |
Free, fast, no model |
| Quality | Typed entities, labelled relations, pronouns resolved, any language | Typed entities, in the languages the model was trained on (the default model is multilingual: check it on yours); unlabelled relations, no descriptions | Untyped entities, unlabelled relations. Scripts without capital letters (Arabic, Chinese, ...) yield only quoted terms and identifiers |
The llm extractor needs LLM_PROVIDER. Building a large project with it
costs about one call per chunk: check the chunk count first
(pending_chunks in the status) and try a small project to estimate the
tokens. The rules extractor is a good free start for English and other
cased-script documents.
The gliner extractor needs the gliner extra
(uv sync --extra gliner, or PAGELORE_EXTRAS=gliner in Docker). The
first build downloads GRAPH_GLINER_MODEL from Hugging Face (or set it to
a local folder) and keeps it loaded. GRAPH_GLINER_THRESHOLD (0.5) is the
lowest confidence kept and GRAPH_GLINER_DEVICE (cpu) where it runs
(cuda, mps). The model reads chunks in windows of whole sentences, up
to 200 words each.
Build it¶
curl -X POST localhost:8000/api/v1/projects/1/graph/build -H 'Content-Type: application/json' \
-d '{"background": true, "rebuild": false}'
| Field | Default | |
|---|---|---|
background |
true |
Run as a background job of kind graph (202 with job_id). With false, the request waits and returns the result (200) |
rebuild |
false |
Delete the graph and extract every chunk again, for example after changing the extractor or entity types |
A build reads only the chunks of indexed documents that are not extracted yet, so building again after adding documents is incremental. When you re-process or delete a document, its part of the graph is removed at once, and the next build extracts its new chunks and removes entities that no chunk names any more.
By default, ingestion does not update the graph: build again after you add
documents. With auto_build: true, every upload, text, web, S3, sync,
re-index or re-process (inline or as a job) that leaves chunks to extract
queues a graph job when it ends. While one waits in the queue, more
ingestion adds no second one: the queued build reads every pending chunk
when it starts. It is off by default because with the llm extractor each
ingestion then costs LLM calls; it suits rules and gliner best.
The job (or inline) result:
{"extractor": "llm", "rebuild": false, "total": 240, "done": 240,
"extracted": 238, "failed": 2, "entities": 3120, "relations": 2210,
"input_tokens": 182400, "output_tokens": 61300,
"orphans_deleted": 0, "pending": 2}
entities and relations count what each chunk yielded, before entities
with the same name are merged. A chunk whose extraction failed (a bad LLM
reply) stays pending for the next build. When every chunk of a batch fails
(the LLM is down), the build fails. Cancel a running build like any job: the
chunks saved so far are kept.
Only one build per project can be queued or running: a manual build gets a 409 then. An automatic build may queue behind a running one, and waits for it to end before it starts.
A rebuild job records "cleared": true in its progress once it has
deleted the graph. If it is interrupted and runs again, it does not delete
the graph again: it goes on with the chunks still pending, so the LLM calls
already made are not paid twice.
One entity per name¶
Entities are merged by their normalized name: case, punctuation, possessives, a leading "the" and leading titles ("Dr.", "Prof.", "المهندسة", "الدكتور") are ignored, so "The Bank of Egypt's" and "bank of egypt" are one entity, and so are "Dr. Mona Fawzy" and "Mona Fawzy". Different spellings ("IBM" and "International Business Machines") stay separate. An entity keeps the first type it was given.
A name inside a longer one ("Colossus" and "Colossus computer", "Champions League" and "2019 Champions League final") stays a separate entity, but retrieval links the two as name variants (see below). A name found in more than 8 longer names of the project ("League" in "Cairo League", "Giza League", ...) is a common word, and is linked to none.
Graphs built before titles were ignored keep their old entities: build with
"rebuild": true to merge them.
Use it in retrieval¶
Add graph to a retrieve request ({} for the defaults). It works in every
mode, with POST /retrieve across projects, and with the other options.
| Option | Default | |
|---|---|---|
weight |
1.0 |
Weight of the graph's list in the fusion; the search's list weighs 1 (above 0, at most 10) |
seed_chunks |
1 |
How many of the search's best results seed the walk (0 to 50). More seeds spread the walk over more topics, so the chunks it reaches through entities rank lower; raise it when the best result is often off target |
entity_seeds |
true |
Entities named in the query seed the walk too |
hops |
2 |
How far the walk reaches (1 to 3). Entity → its chunks → their entities is one hop |
damping |
0.5 |
The chance of following an edge rather than jumping back to a seed (0.05 to 0.95). Higher reaches further from the seeds |
seed_chunks: 0 with entity_seeds: false is a 422: the walk needs seeds.
How it ranks¶
flowchart LR
Q["query"] --> S["search<br/>(vector / keyword / hybrid)"]
Q --> E["entities named<br/>in the query"]
S --> C["best chunks"]
E --> G["subgraph: entities,<br/>chunks, relations"]
C --> G
G --> P["Personalized<br/>PageRank"]
P --> F["fuse (RRF)"]
S --> F
F --> R["rerank, diversity, top_k ..."]
- Seeds. Every run of up to six query words is matched against the
entity names, and the longest matches are kept ("Bank of America", not
"America"). In the rest of the query, a name that is part of a longer
entity name seeds that entity ("VERA-2" seeds "VERA-2 trial") when it
has two words or more, or a capital or a digit (a lone lower-case word
like "computer" is not taken for a name). The search's best
seed_chunkschunks are seeds too. - Subgraph. From the seeds, Pagelore collects the chunks that name the
seed entities, the other entities in those chunks, and so on for
hopshops (at most 100 chunks per entity, 5000 nodes). Name variants are added without spending a hop, so a chunk about "Colossus" reaches the chunks about the "Colossus computer". Edges join each entity to the chunks that name it, related entities to each other (weighted by how often the relation is stated), and name variants to each other (a weaker edge, as they may not be the same thing). - Personalized PageRank. A random walk starts from the seeds and follows the edges. Rare seed entities weigh more than common ones, and a better-ranked seed chunk more than a worse one (half the start weight for each kind of seed). Chunks the walk visits often rank high.
- Fusion. The graph's list is fused with the search's list by weighted
reciprocal rank fusion (
rrf_k), and the rest of the pipeline (parents, dedupe, reranking, diversity,top_k) runs as usual.
Chunks only the graph found pass the same filters, document access and
max_per_document as the others. score_threshold applies to the search's
list only, so it does not drop them.
The response¶
Each result gains graph, and the response a graph block:
{
"results": [{
"rank": 2,
"score": 0.0325,
"file_name": "paris.txt",
"text": "The University of Paris opened the Radium Institute in 1914.",
"graph": {"score": 0.081, "rank": 2,
"entities": [{"entity_id": 7, "name": "University of Paris", "type": "organization"}]},
...
}],
"graph": {
"seed_entities": [{"entity_id": 3, "name": "Marie Curie", "type": "person"}],
"seed_chunks": [41],
"entities": 12, "chunks": 9, "edges": 31, "relations": 8,
"ranked": 9, "graph_only": 1, "took_ms": 6.4
}
}
| Field | |
|---|---|
results[].graph.score, rank |
The result's PageRank score and place in the graph's list (absent if the graph did not rank it) |
results[].graph.base_score, base_rank |
Its score and place in the search's list (absent if only the graph found it) |
results[].graph.entities |
Up to 5 entities that tie the chunk to the query, most relevant first |
graph.seed_entities, seed_chunks |
What the walk started from |
graph.entities, chunks, edges, relations |
The size of the subgraph |
graph.ranked, graph_only |
Chunks the graph ranked, and results only the graph found |
score is the fused RRF score. If a project has no graph yet, the request
still succeeds with a warning and the plain search results. If nothing in
the query or the results is in the graph, a warning says the walk found no
seed.
Browse it¶
| Endpoint | |
|---|---|
GET /projects/{id}/graph |
Settings, the number of entities, relations, mentions and extracted chunks, and pending_chunks |
DELETE /projects/{id}/graph |
Delete the graph; the settings stay (204) |
GET /projects/{id}/graph/entities |
Entities, most mentioned first: q (part of the name), type, page, page_size |
GET /projects/{id}/graph/entities/{entity} |
An entity, the chunks naming it (with what each says about it) and its related entities, strongest first: limit (50) |
GET /projects/{id}/graph/entities/{entity}/subgraph |
Its neighbourhood, to draw it: entities up to hops (1 or 2) relations away, at most limit (50), and the relations among them |
GET /projects/{id}/graph/relations |
Relations, newest first: filter by entity_id or document_id; page, page_size |
Relations from the rules extractor have relation: null (they mean "named
in the same sentence").
Permissions¶
The graph respects document permissions.
The browse endpoints take user and groups, and retrieval takes user,
like the other endpoints. An entity, mention or relation is shown only if
the user may see a document it comes from. Counts (mentions,
documents) count only those documents. An entity named only in hidden
documents is a 404.
The status (GET /projects/{id}/graph) takes user and groups too:
its counts and pending_chunks then cover only the documents the user may
see (entities counts the entities named in them). Build and delete change
the whole project's graph, so
keys bound to a user
get a 403 there.
Evaluate it¶
Evaluation configs accept graph, so you can measure what it adds on your
own questions:
{"questions": [...], "top_k": 5,
"configs": [
{"name": "hybrid", "options": {"mode": "hybrid"}},
{"name": "hybrid + graph", "options": {"mode": "hybrid", "graph": {}}},
{"name": "graph x2", "options": {"mode": "hybrid", "graph": {"weight": 2}}}
]}
The graph helps most with questions that connect facts across chunks or
documents (multi-hop questions) and with names the embeddings handle
poorly. On simple fact lookups it can push a good search result down: try
a lower weight.
Errors¶
| Status | When |
|---|---|
| 403 | A key bound to a user calls the build or delete endpoint |
| 404 | The project or entity does not exist, or the entity is hidden from the user |
| 409 | A build when the graph is not enabled, when the llm extractor has no LLM, when the gliner extractor lacks the gliner extra, or while another build is queued or running |
| 422 | Invalid settings or retrieval options |
| 502 | The LLM failed on every chunk of a batch, or the GLiNER model could not be loaded or run, during an inline build (a background job fails with the same error) |
Limits¶
- Entities are not embedded. The query's entities are found by name, so "the Curie lab" does not match "Marie Curie". The search's best chunks seed the walk too, which covers most of these cases.
- Automatic builds are off by default. Without
auto_build, call build after adding documents. - No community summaries or global search.