Knowledge graph¶
Build a project's knowledge graph (entities and the relations between them,
extracted from its chunks) and browse it. Retrieval uses the graph through
its graph option. The graph's settings live on the
project: see Graph settings. For the concepts,
see Knowledge graph.
Paths are relative to /api/v1. The examples use BASE=http://localhost:8000/api/v1
and an API key in $KEY.
Browsing shows only what the caller may see. An entity exists for the
caller through the chunks of the documents they may see; mentions, counts
and relations are counted over those documents only. The browsing
endpoints take the user and groups query parameters described in
Searching as a user.
| Query parameter | Type | Allowed values | |
|---|---|---|---|
user |
string | 1 to 256 characters | Read as this user: only documents they may see (their stored groups count) |
groups |
string, repeatable | up to the server's group limit | Read as members of these groups (?groups=hr&groups=legal), with or without user |
A key bound to a user always reads as that user; it gets 403 if it sends
another user or any groups, and it can't build or delete the graph.
GET /projects/{project_id}/graph¶
The graph's settings, its size and the chunks not extracted yet.
Query: user, groups (above).
Response: 200
| Field | Type | |
|---|---|---|
project_id |
integer | |
settings |
object | The project's graph settings, every field filled in |
entities |
integer | Entities the caller may see |
relations |
integer | Relations stated in visible chunks |
mentions |
integer | Entity mentions (entity, chunk pairs) |
extracted_chunks |
integer | Chunks extracted so far |
pending_chunks |
integer | Chunks of indexed documents not extracted yet. Build to extract them |
Errors: 403 (user / groups with a bound key), 404 (no project).
{
"project_id": 1,
"settings": {"enabled": true, "extractor": "llm", "max_entities_per_chunk": 20,
"auto_build": false,
"entity_types": ["person", "organization", "location", "product", "event", "concept"]},
"entities": 412,
"relations": 988,
"mentions": 1730,
"extracted_chunks": 640,
"pending_chunks": 12
}
POST /projects/{project_id}/graph/build¶
Extract the entities and relations of the chunks not extracted yet, or of
every chunk with rebuild. Run it after adding documents, unless the
graph's auto_build queues it for you. Re-processed or deleted documents
drop their part of the graph at once, so a build only needs to extract
what is new.
Needs a write key not bound to a user.
Body (JSON, unknown fields refused; {} uses the defaults):
| Field | Type | Default | |
|---|---|---|---|
background |
boolean | true |
Run as a job and return its job_id at once (202). false: extract before answering (200), for small projects |
rebuild |
boolean | false |
Clear the graph first and extract every chunk again. Use it after changing the extractor or entity_types. Default: only chunks not extracted yet (new or re-processed documents) |
Response:
- 202 with
background: true:{"project_id": 1, "processing": "background", "job_id": 57}. The job'skindisgraph. Itsprogresshasstage(cleared, thenextracting),total,done,extracted,failed,input_tokensandoutput_tokens; itsresultis the object below. - 200 with
background: false:{"project_id": 1, "processing": "inline", "result": {...}}.
result:
| Field | |
|---|---|
extractor |
llm, gliner or rules |
rebuild |
Whether the graph was cleared first |
total |
Chunks to extract when the build started |
done |
Chunks handled |
extracted |
Chunks extracted |
failed |
Chunks whose extraction failed. They stay pending, for the next build |
entities, relations |
Entities and relations found (before merging names already in the graph) |
input_tokens, output_tokens |
LLM tokens used (llm extractor) |
orphans_deleted |
Entities no chunk names any more, deleted |
pending |
Chunks still not extracted afterwards |
When a whole batch of chunks fails (for example, the LLM is down), the build stops with that error; chunks extracted before it are kept.
Errors:
| Status | When |
|---|---|
| 403 | The key is bound to a user |
| 404 | The project does not exist |
| 409 | The graph is not enabled on the project, the llm extractor has no LLM configured, the gliner extractor's package is not installed, or a graph build of the project is already queued or running |
| 422 | An unknown field |
| 502 | The LLM failed (inline), or the GLiNER model could not be loaded |
# Enable the graph, then build it in the background
curl -X PATCH "$BASE/projects/1" -H "Authorization: Bearer $KEY" \
-H 'Content-Type: application/json' -d '{"graph": {"enabled": true, "extractor": "llm"}}'
curl -X POST "$BASE/projects/1/graph/build" -H "Authorization: Bearer $KEY" \
-H 'Content-Type: application/json' -d '{}'
# After changing entity_types: extract everything again, inline
curl -X POST "$BASE/projects/1/graph/build" -H "Authorization: Bearer $KEY" \
-H 'Content-Type: application/json' -d '{"background": false, "rebuild": true}'
{
"project_id": 1,
"processing": "inline",
"result": {"extractor": "llm", "rebuild": true, "total": 652, "done": 652, "extracted": 650,
"failed": 2, "entities": 3120, "relations": 2410, "input_tokens": 512330,
"output_tokens": 98012, "orphans_deleted": 0, "pending": 2}
}
DELETE /projects/{project_id}/graph¶
Delete the project's graph: entities, mentions and relations. The settings stay; build again to extract it.
Needs a write key not bound to a user.
Response: 204, no body. Errors: 403 (bound key), 404.
GET /projects/{project_id}/graph/entities¶
The entities, most mentioned first (then by name).
Query:
| Parameter | Type | Allowed values | Default | |
|---|---|---|---|---|
q |
string | up to 256 characters | Part of the name, in any case. It is normalized like stored names: case, punctuation, possessives, a leading article and leading titles such as "Dr." are ignored | |
type |
string | up to 64 characters | Only this entity type, such as person |
|
page |
integer | 1 or more | 1 |
|
page_size |
integer | 1 to 200 | 50 |
|
user, groups |
See above |
Response: 200
{
"entities": [
{"entity_id": 17, "name": "Novagen", "type": "organization", "mentions": 48, "documents": 9}
],
"total": 1, "page": 1, "page_size": 50
}
Each entity: entity_id, name, type, mentions (chunks naming it
that the caller may see) and documents (documents naming it).
Errors: 403, 404 (no project), 422.
curl "$BASE/projects/1/graph/entities?q=nova&type=organization&page_size=20" \
-H "Authorization: Bearer $KEY"
GET /projects/{project_id}/graph/entities/{entity_id}¶
An entity, the chunks that name it (with what each says about it), and the entities related to it, strongest first.
Query:
| Parameter | Type | Allowed values | Default | |
|---|---|---|---|---|
limit |
integer | 1 to 200 | 50 |
The most mentions, and the most related entities, to return |
user, groups |
See above |
Response: 200
| Field | |
|---|---|
entity_id, name, type, mentions, documents |
As in the list |
mentioned_in |
{chunk_id, document_id, file_name, chunk_index, description} per chunk naming it, by document then chunk. description is what the chunk says about it (llm extractor), else null |
related |
{entity_id, name, type, weight, count, relations}: the entities it has relations with in visible chunks, the strongest (highest total weight) first. count: relations stated between the two; relations: their distinct labels, such as ["leads", "works_for"] (empty for co-occurrence) |
Errors: 403; 404 when the project or entity does not exist, or no chunk the caller may see names it; 422.
{
"entity_id": 17, "name": "Novagen", "type": "organization", "mentions": 48, "documents": 9,
"mentioned_in": [
{"chunk_id": 311, "document_id": 12, "file_name": "trials-2025.pdf", "chunk_index": 4,
"description": "Sponsor of the NV-2 phase II trial"}
],
"related": [
{"entity_id": 52, "name": "Dr. Lina Rahman", "type": "person", "weight": 6.0, "count": 6,
"relations": ["leads_trial_for"]}
]
}
GET /projects/{project_id}/graph/entities/{entity_id}/subgraph¶
The entity's neighbourhood, to draw it: the entities up to hops relations
away (at most limit in all, the entity included) and the relations among
them.
Query:
| Parameter | Type | Allowed values | Default | |
|---|---|---|---|---|
hops |
integer | 1 or 2 | 1 |
How many relations away to go |
limit |
integer | 2 to 200 | 50 |
The most entities (nodes) to return |
user, groups |
See above |
Response: 200
| Field | |
|---|---|
entity_id |
The center |
nodes |
{entity_id, name, type, hop}. hop is 0 for the center, then 1 or 2. Nearer and stronger neighbours come first |
edges |
{source, target, weight} between the nodes (entity ids); weight adds up the relations stated between the two |
Errors: as for the entity.
curl "$BASE/projects/1/graph/entities/17/subgraph?hops=2&limit=30" \
-H "Authorization: Bearer $KEY"
{
"entity_id": 17,
"nodes": [
{"entity_id": 17, "name": "Novagen", "type": "organization", "hop": 0},
{"entity_id": 52, "name": "Dr. Lina Rahman", "type": "person", "hop": 1},
{"entity_id": 80, "name": "NV-2", "type": "product", "hop": 2}
],
"edges": [
{"source": 52, "target": 17, "weight": 6.0},
{"source": 52, "target": 80, "weight": 3.0}
]
}
GET /projects/{project_id}/graph/relations¶
The relations the chunks state, newest first; of one entity, of one document, or all.
Query:
| Parameter | Type | Allowed values | Default | |
|---|---|---|---|---|
entity_id |
integer | 1 or more | Relations from or to this entity | |
document_id |
integer | 1 or more | Relations stated in this document | |
page |
integer | 1 or more | 1 |
|
page_size |
integer | 1 to 200 | 50 |
|
user, groups |
See above |
Response: 200
{
"relations": [
{"relation_id": 905, "source": {"entity_id": 52, "name": "Dr. Lina Rahman", "type": "person"},
"target": {"entity_id": 17, "name": "Novagen", "type": "organization"},
"relation": "leads_trial_for", "description": "Rahman is principal investigator of NV-2",
"weight": 1.0, "chunk_id": 311, "document_id": 12}
],
"total": 1, "page": 1, "page_size": 50
}
Each relation: relation_id, source and target ({entity_id, name,
type}), relation (its label; null for a co-occurrence found by the
rules or gliner extractor), description (what the chunk says, llm
extractor), weight, and the chunk_id and document_id stating it.
Errors: 403, 404 (no project), 422.