Knowledge Graph#
RAGFlow's Knowledge Graph (KG) feature lets you enrich a knowledge base with structured entity/relation/community data extracted from documents, then inject that graph context into retrieval. Queries routed through the KG path receive a synthesized result block (labeled "Related content in Knowledge Graph" with similarity: 1.0) prepended to the normal chunk results .
Status as of v0.27.0: GraphRAG is superseded by the Knowledge Compilation Engine, but the existing GraphRAG code remains in the repository and is still used by datasets created before the migration.
Architecture: Document-Store–Backed Graph#
Rather than a dedicated graph database, all graph data is stored as special chunks in the same document store (Elasticsearch / Infinity) used for ordinary chunks. Each chunk carries a knowledge_graph_kwd field that identifies its type:
knowledge_graph_kwd value | Content |
|---|---|
entity | A single extracted entity (name, type, description) |
relation | A relationship between two entities |
community_report | A summary report for a detected graph community |
The KGSearch class queries all three types at retrieval time, combining entity rows, relation rows, and community reports into a single returned block. Community reports are ordered by weight_flt and their entities are used as the community-matching key .
Graph indexing constants (batch sizes, timeouts, retry logic) live at the top of rag/graphrag/general/index.py:
- Default batch chunk token size: 4 096 tokens
- Entity resolution timeout: 1 800 s
- Community detection timeout: 1 800 s
Three built-in extraction methods are registered in the same file :
| Method | Class |
|---|---|
general | GeneralKGExt |
light | LightKGExt |
ner | NerKGExt (not yet enabled in UI; benchmarked at ~5 % lift over naive RAG) |
Four-Step User Workflow#
The "Knowledge Graph" chunk method was removed from the dataset-creation dialog in September 2025 to fix race conditions, inefficient reparsing, and premature community detection . KG generation is now a separate post-parsing task. The required order is strict — doing steps out of order means the KG won't be used during retrieval :
- Configure — Set entity types in the dataset's Configuration page before uploading files. Settings are locked at import time.
- Parse — Upload and parse documents until visible chunks appear.
- Generate — Click Generate → Knowledge graph on the Files page.
- Enable — Toggle "Use Knowledge Graph in search" when creating or editing a retrieval query / chat assistant.
KG task progress is tracked via the graphrag_task_id and graphrag_task_finish_at fields on the Knowledgebase model .
The global kg_retriever singleton (a KGSearch instance backed by the shared docStoreConn) is initialized in common/settings.py init_settings() .
Knowledge Compilation Engine (v0.27.0)#
As of v0.27.0, the GraphRAG system is replaced by the Knowledge Compilation Engine, introduced in PR #16515 . This new engine lives in rag/advanced_rag/knowlege_compile/ and provides a template-driven, concurrent compilation pipeline that covers multiple artifact types — not just knowledge graphs:
| Template | Artifact produced |
|---|---|
knowledge_graph.yaml | Knowledge graph (entities + relations) |
tree.yaml | RAPTOR hierarchical summary tree |
timeline.yaml | Chronological event extraction |
page_index.yaml | Hierarchical table-of-contents |
mind_map.yaml | Mind map |
artifacts.yaml | General structured artifacts |
Key modules:
structure.py— Compiles document structure (list/set/hypergraph chain kinds) usingLLMCallPoolfor concurrent LLM calls.wiki.py— KB-wide MAP → REDUCE → PLAN → REFINE pipeline for artifact/wiki compilation.runner.py— Handler-free core for document-scoped compilation.
Task orchestration runs through rag/svr/task_executor_refactor/ — the dataset_wiki_generator.py handles KB-wide artifact tasks (MAP batch size: 64 chunks) and the chunk_post_processor.py handles structure compilation .
Compilation templates are YAML files stored under api/db/init_data/compilation_templates/ and managed via REST endpoints at api/apps/restful_apis/compilation_template_api.py. The Knowledgebase model tracks compilation progress via artifact_task_id / artifact_task_finish_at .
Key Source References#
| Area | Entry point |
|---|---|
| GraphRAG extraction & indexing | rag/graphrag/general/index.py |
| GraphRAG retrieval | rag/graphrag/search.py |
| Knowledge Compilation structure | rag/advanced_rag/knowlege_compile/structure.py |
| Knowledge Compilation wiki/artifact | rag/advanced_rag/knowlege_compile/wiki.py |
| KB model fields (task IDs) | api/db/db_models.py L837–876 |
| Global KG retriever init | common/settings.py L370–374 |
| Sept 2025 refactoring (UI change) | PR #10315 · PR #10584 |
| v0.27.0 Compilation Engine | PR #16515 |