Knowledge Compilation Pipeline#
Overview#
Introduced in v0.27.0 (PR #16515), the Knowledge Compilation Pipeline replaced the older GraphRAG engine as RAGFlow's primary mechanism for producing structured knowledge artifacts from a Knowledge Base (KB). It lives under rag/advanced_rag/knowlege_compile/ and is a template-driven, multi-phase pipeline that processes all eligible documents in a KB to produce wiki pages, entity/relation graphs, topics, and other artifact types.
The pipeline is not limited to Knowledge Graphs. The active template determines what artifact is built :
| YAML 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 (wiki) |
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 .
Pipeline Phases: MAP → REDUCE → PLAN → REFINE#
The public entry point is run_wiki() in rag/svr/task_executor_refactor/dataset_wiki_generator.py. It orchestrates four sequential phases implemented in rag/advanced_rag/knowlege_compile/wiki.py .
MAP (per doc, parallel batches) → REDUCE (KB-wide dedup) → PLAN (page reconciliation) → REFINE (page writing)
MAP — Per-document extraction#
wiki_map_from_chunks() processes document chunks in batches of 64 across all eligible documents. For each batch, it calls the LLM via gen_json() to extract entities, concepts, claims, relations, and topics as JSON.
Incremental logic: Each chunk is fingerprinted by hash. MAP compares current chunk hashes against stored artifact_map_extract ES rows: UNCHANGED chunks are skipped; NEW and CHANGED are re-extracted; DELETED rows are purged . Extracts are persisted to ES as non-searchable artifact_map_extract rows.
Progress is reported at 5%–65% of overall task progress .
REDUCE — KB-wide canonicalization#
wiki_reduce_from_extracts() deduplicates entities and concepts across all documents in three sub-phases:
- Exact dedup — normalize by
(normalize(name), type)for entities,normalize(term)for concepts. - Embedding dedup — cosine similarity; pairs ≥ 0.95 auto-merge; borderline pairs escalate.
- LLM disambiguation — batched LLM calls resolve ambiguous pairs.
A resume cache (artifact_reduce_result) is keyed by an input hash fingerprint of the MAP state — re-runs skip REDUCE if MAP produced no delta .
PLAN — Page reconciliation#
wiki_plan_from_reduction() compares the canonicalized entities/concepts against existing artifact_page ES rows using top-1 KNN embedding search. Each item is classified as UPDATE (cosine ≥ 0.95), MAYBE, or CREATE. MAYBE items go to an LLM call for final classification. A single planning LLM call then produces {pages: [...], estimated_page_count, compilation_notes}.
Resume cache: returns cached plan if the stored input hash equals the current REDUCE output hash .
REFINE — Page writing#
wiki_refine_from_plan() writes the final wiki pages in parallel (bounded by asyncio.Semaphore). For each page:
- Assembles evidence from claims matching entity names, with fallback to
chunk_idsfrom the entity/concept itself. - Builds a context budget capped at 60k chars.
- Calls the writer LLM; for UPDATE actions, merges new and existing content with an LLM, falling back to new content if merged output shrinks below 70% of the max input.
- Transforms
[[slug]]wikilinks toartifact/{kb_id}/{slug}URLs.
Final pages land in ES as searchable artifact_page rows . Entity and relation rows (artifact_entity, artifact_relation) are also materialized for the canvas graph view via build_wiki_page_graph().
Document Eligibility: parser_config Template Assignment#
A document participates in the wiki/artifact pipeline only if its parser_config.compilation_template_group_id resolves to at least one template with kind == "artifacts" .
Resolution chain :
- Read
parser_config.compilation_template_group_id(orparser_config.ext.compilation_template_group_id). - Call
CompilationTemplateGroupService.resolve_template_ids(group_id, tenant_id)for each group_id. - Load each template via
CompilationTemplateService.get_saved()and checkconfig.kind. - Only templates where
kind == "artifacts"make the document eligible.
First-template-wins: The first eligible template's llm_id and example field are used as the canonical chat model and writer example for the KB-wide REDUCE / PLAN / REFINE phases . Documents without a matching template are silently skipped.
Compilation progress is tracked on the Knowledgebase model via artifact_task_id / artifact_task_finish_at fields .
Multi-level Error Handling and Fault Isolation#
The pipeline applies graceful degradation at three scopes:
Chunk/batch level (MAP): Individual batch failures are caught and logged; the loop continues to the next batch. Aggregate MAP results remain usable .
LLM call level: Timeouts or failures return empty extracts / empty JSON rather than crashing. Evidence fallback ensures REFINE can still attribute pages to source chunks via entity chunk_ids when claims are sparse .
Phase level (REDUCE / PLAN / REFINE): All three are wrapped in a single try/except . Failure stops the pipeline at that phase and sets progress to -1 (error state), but prior phases' ES rows remain intact for the next run's resume caches.
ES I/O: Batch search failures fall back to per-ID get(). Insert failures for artifact_page, artifact_entity, and artifact_relation rows log the exception but do not fail the task — the user still receives a success message . Progress callbacks are also wrapped in try/except to prevent UI feedback from crashing the task .
Model resolution: Chat model resolution errors fall back to the tenant default model .
Key Source References#
| Component | File |
|---|---|
| Pipeline orchestrator (entry point) | rag/svr/task_executor_refactor/dataset_wiki_generator.py |
| MAP / REDUCE / PLAN / REFINE logic | rag/advanced_rag/knowlege_compile/wiki.py |
| Document structure compilation | rag/advanced_rag/knowlege_compile/structure.py |
| Handler-free core (doc-scoped) | rag/advanced_rag/knowlege_compile/runner.py |
| Artifact page API surface | api/apps/services/dataset_api_service.py |
| KB model fields (task IDs) | api/db/db_models.py L837–876 |
| Template REST API | api/apps/restful_apis/compilation_template_api.py |
| Template YAML definitions | api/db/init_data/compilation_templates/ |
| Introducing PR | PR #16515 |