Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix knowledge compilation to use pipeline LLM instead of default tenant LLM (#18572)

Wang Qi169822105fe3
diff --git a/api/apps/restful_apis/compilation_template_api.py b/api/apps/restful_apis/compilation_template_api.py
index 6a8deeabd4..ada0e5fd6d 100644
--- a/api/apps/restful_apis/compilation_template_api.py
+++ b/api/apps/restful_apis/compilation_template_api.py
@@ -16,7 +16,7 @@
from quart import Response
-from api.apps import current_user, login_required
+from api.apps import login_required
from api.apps.restful_apis.utils.compilation_template_validation import validate_template_payload
from api.db.services.compilation_template_service import CompilationTemplateService
from api.utils.api_utils import get_json_result, server_error_response
@@ -48,7 +48,6 @@ def list_builtin_templates() -> Response:
}
for template in CompilationTemplateService.load_builtins_from_files()
]
- templates = CompilationTemplateService.fill_default_llm_for_templates(templates, current_user.id)
return get_json_result(data=templates)
except Exception as exc:
return server_error_response(exc)
diff --git a/api/db/services/compilation_template_group_service.py b/api/db/services/compilation_template_group_service.py
index 7ea104c79a..ff0066ded0 100644
--- a/api/db/services/compilation_template_group_service.py
+++ b/api/db/services/compilation_template_group_service.py
@@ -327,9 +327,6 @@ class CompilationTemplateGroupService(CommonService):
raise GroupValidationError(f"Template name '{name}' is duplicated in this group.")
seen_names.add(name)
- from api.db.services.compilation_template_service import CompilationTemplateService
-
- config = CompilationTemplateService.fill_config_default_llm(config, tenant_id)
duplicate_query = CompilationTemplate.select().where(
CompilationTemplate.tenant_id == tenant_id,
CompilationTemplate.group_id == group_id,
@@ -411,9 +408,6 @@ class CompilationTemplateGroupService(CommonService):
config = (child or {}).get("config") or {}
if not kind or not name or not isinstance(config, dict):
raise GroupValidationError("Each template must include a name, kind, and config object.")
- from api.db.services.compilation_template_service import CompilationTemplateService
-
- config = CompilationTemplateService.fill_config_default_llm(config, tenant_id)
duplicate = CompilationTemplate.select().where(
CompilationTemplate.tenant_id == tenant_id,
CompilationTemplate.group_id == group_id,
diff --git a/api/db/services/compilation_template_service.py b/api/db/services/compilation_template_service.py
index db3baa046e..4677600988 100644
--- a/api/db/services/compilation_template_service.py
+++ b/api/db/services/compilation_template_service.py
@@ -29,35 +29,6 @@ from common.file_utils import get_project_base_directory
class CompilationTemplateService(CommonService):
model = CompilationTemplate
- @classmethod
- def fill_config_default_llm(cls, config: dict, tenant_id: str | None) -> dict:
- if not isinstance(config, dict) or config.get("llm_id") or not tenant_id:
- return config
- try:
- from api.db.services.user_service import TenantService
-
- ok, tenant = TenantService.get_by_id(tenant_id)
- if ok and getattr(tenant, "tenant_llm_id", None):
- config = dict(config)
- config["llm_id"] = tenant.tenant_llm_id
- except Exception:
- logging.exception(
- "compilation_template: llm_id default-fill lookup failed for tenant=%s",
- tenant_id,
- )
- return config
-
- @classmethod
- def fill_default_llm_for_templates(cls, templates: list[dict], tenant_id: str | None) -> list[dict]:
- if not tenant_id:
- return templates
- filled = []
- for template in templates:
- item = dict(template)
- item["config"] = cls.fill_config_default_llm(item.get("config") or {}, tenant_id)
- filled.append(item)
- return filled
-
@classmethod
def _sort_builtins(cls, templates: list[dict]) -> list[dict]:
return sorted(
@@ -78,33 +49,15 @@ class CompilationTemplateService(CommonService):
def _to_saved_dict(cls, template: CompilationTemplate) -> dict:
data = template.to_dict()
config = data.get("config") or {}
- # Lazy-fill llm_id with the tenant's default chat model so the
- # frontend always sees a value (legacy templates predate the
- # field). The DB row is left untouched — this is a read-side
- # default. If the tenant has no default chat model set,
- # silently leave llm_id absent and let the caller fall back
- # however it likes.
- if isinstance(config, dict) and not config.get("llm_id"):
- tenant_id = data.get("tenant_id")
- if tenant_id:
- try:
- from api.db.services.user_service import TenantService
-
- ok, tenant = TenantService.get_by_id(tenant_id)
- if ok and getattr(tenant, "tenant_llm_id", None):
- config = dict(config)
- config["llm_id"] = tenant.tenant_llm_id
- except Exception:
- logging.exception(
- "compilation_template: llm_id lazy-fill lookup failed for tenant=%s",
- tenant_id,
- )
+ if isinstance(config, dict):
+ config = dict(config)
+ config.pop("llm_id", None)
return {
"id": data["id"],
"name": data["name"],
"description": data.get("description") or "",
"kind": data["kind"],
- "config": cls.fill_config_default_llm(config, data.get("tenant_id")),
+ "config": config,
"create_time": data.get("create_time"),
"update_time": data.get("update_time"),
}
diff --git a/rag/flow/compiler/compiler.py b/rag/flow/compiler/compiler.py
index b57cf433c6..6ced075951 100644
--- a/rag/flow/compiler/compiler.py
+++ b/rag/flow/compiler/compiler.py
@@ -552,52 +552,16 @@ class Compiler(ProcessBase, LLM):
self.set_output("chunks", chunks)
return
- # Per-template chat model: a template may pin its own ``llm_id``;
- # otherwise fall back to this component's configured chat model.
- llm_bundle_cache: dict[str, LLMBundle] = {}
- chat_mdl_by_tid: dict[str, LLMBundle] = {}
- filtered_templates: list[tuple[str, dict]] = []
- default_chat_mdl = None
- for template_id, parser_cfg in active_templates:
- tpl_llm_id = parser_cfg.get("llm_id") if isinstance(parser_cfg, dict) else None
- if isinstance(tpl_llm_id, str) and tpl_llm_id.strip():
- chat_llm_id = tpl_llm_id.strip()
- if chat_llm_id not in llm_bundle_cache:
- try:
- cfg = resolve_model_config(tenant_id, LLMType.CHAT, chat_llm_id)
- llm_bundle_cache[chat_llm_id] = LLMBundle(
- tenant_id,
- cfg,
- lang=language,
- max_retries=self._param.max_retries,
- retry_interval=self._param.delay_after_error,
- )
- except Exception:
- logging.exception(
- "Compiler: cannot resolve chat model %s for template %s; skipping",
- chat_llm_id,
- template_id,
- )
- continue
- chat_mdl_by_tid[template_id] = llm_bundle_cache[chat_llm_id]
- else:
- if default_chat_mdl is None:
- default_chat_mdl = LLMBundle(
- tenant_id,
- self.chat_mdl.model_config,
- lang=language,
- max_retries=self._param.max_retries,
- retry_interval=self._param.delay_after_error,
- )
- chat_mdl_by_tid[template_id] = default_chat_mdl
- filtered_templates.append((template_id, parser_cfg))
-
- if not filtered_templates:
- if chunks is None:
- chunks = self._normalize_upstream_chunks(kwargs)
- self.set_output("chunks", chunks)
- return
- active_templates = filtered_templates
+ # Compilation templates describe output structure, not model selection.
+ # Every template in this pipeline must use the Compiler component's LLM.
+ chat_mdl = LLMBundle(
+ tenant_id,
+ self.chat_mdl.model_config,
+ lang=language,
+ max_retries=self._param.max_retries,
+ retry_interval=self._param.delay_after_error,
+ )
+ chat_mdl_by_tid = {template_id: chat_mdl for template_id, _ in active_templates}
def _template_requests_rechunk(cfg: dict) -> bool:
if not isinstance(cfg, dict):
diff --git a/rag/svr/task_executor_refactor/chunk_post_processor.py b/rag/svr/task_executor_refactor/chunk_post_processor.py
index e9e5fd799b..ff2dc6b2a2 100644
--- a/rag/svr/task_executor_refactor/chunk_post_processor.py
+++ b/rag/svr/task_executor_refactor/chunk_post_processor.py
@@ -422,16 +422,8 @@ def _parser_config_compilation_template_ids(parser_config, tenant_id: str) -> li
return template_ids
-def _resolve_template_chat_llm_id(parser_cfg: dict, ctx) -> str:
- """Pick the chat model id for a knowledge-compilation template.
-
- Resolution order: template ``llm_id`` → doc ``parser_config.llm_id``
- → ``ctx.llm_id`` (the chunking task's default).
- """
- if isinstance(parser_cfg, dict):
- tid = parser_cfg.get("llm_id")
- if isinstance(tid, str) and tid.strip():
- return tid.strip()
+def _resolve_ingestion_chat_llm_id(ctx) -> str:
+ """Pick the ingestion model id for a knowledge-compilation template."""
doc_cfg = getattr(ctx, "parser_config", None) or {}
if isinstance(doc_cfg, dict):
did = doc_cfg.get("llm_id")
@@ -1058,36 +1050,14 @@ async def run_document_structure_compile(handler, embedding_model: LLMBundle) ->
if not active_templates:
return
- llm_bundle_cache: dict[str, LLMBundle] = {}
- chat_mdl_by_tid: dict[str, LLMBundle] = {}
- filtered_templates: list[tuple[str, dict]] = []
- for template_id, parser_cfg in active_templates:
- chat_llm_id = _resolve_template_chat_llm_id(parser_cfg, ctx)
- if chat_llm_id not in llm_bundle_cache:
- try:
- cfg = resolve_model_config(
- ctx.tenant_id,
- LLMType.CHAT,
- chat_llm_id,
- )
- llm_bundle_cache[chat_llm_id] = LLMBundle(
- ctx.tenant_id,
- cfg,
- lang=ctx.language,
- )
- except Exception:
- logging.exception(
- "document_structure_compile: cannot resolve chat model %s for template %s; skipping",
- chat_llm_id,
- template_id,
- )
- continue
- chat_mdl_by_tid[template_id] = llm_bundle_cache[chat_llm_id]
- filtered_templates.append((template_id, parser_cfg))
-
- if not filtered_templates:
+ chat_llm_id = _resolve_ingestion_chat_llm_id(ctx)
+ try:
+ cfg = resolve_model_config(ctx.tenant_id, LLMType.CHAT, chat_llm_id)
+ chat_mdl = LLMBundle(ctx.tenant_id, cfg, lang=ctx.language)
+ except Exception:
+ logging.exception("document_structure_compile: cannot resolve ingestion chat model %s", chat_llm_id)
return
- active_templates = filtered_templates
+ chat_mdl_by_tid = {template_id: chat_mdl for template_id, _ in active_templates}
tree_templates: list[tuple[str, dict]] = []
non_tree_templates: list[tuple[str, dict]] = []
diff --git a/rag/svr/task_executor_refactor/dataset_skill_generator.py b/rag/svr/task_executor_refactor/dataset_skill_generator.py
index e592dbe87e..560cc6c19f 100644
--- a/rag/svr/task_executor_refactor/dataset_skill_generator.py
+++ b/rag/svr/task_executor_refactor/dataset_skill_generator.py
@@ -396,32 +396,15 @@ async def run_corpus2skill(
# much lives under ``api.db.services``.
from api.db.services.document_service import DocumentService
from api.db.services.llm_service import LLMBundle
- from api.db.joint_services.tenant_model_service import (
- get_tenant_default_model_by_type,
- )
+ from api.db.joint_services.tenant_model_service import resolve_model_config
from rag.advanced_rag.knowlege_compile.raptor import (
RecursiveAbstractiveProcessing4TreeOrganizedRetrieval as Raptor,
)
+ from rag.svr.task_executor_refactor.dataset_wiki_generator import _pipeline_compiler_llm_id
progress = ctx.progress_cb
progress(0.0, "skill: loading documents")
- # ---- Phase 0: chat model + RAPTOR instance for summarization/clustering.
- chat_model_config = get_tenant_default_model_by_type(ctx.tenant_id, LLMType.CHAT)
- chat_mdl = LLMBundle(ctx.tenant_id, chat_model_config, lang=ctx.language)
-
- raptor = Raptor(
- max_cluster=128,
- llm_model=chat_mdl,
- embd_model=embedding_model,
- prompt="Please write a concise summary of the following texts:\n{cluster_content}",
- max_token=512,
- max_errors=3,
- clustering_threshold=0.3,
- clustering_ratio=0.5,
- )
-
- # ---- Phase 1: per-doc summaries.
all_docs, _ = await thread_pool_exec(
DocumentService.get_by_kb_id,
kb_id=ctx.kb_id,
@@ -440,6 +423,41 @@ async def run_corpus2skill(
progress(1.0, "skill: no documents in KB")
return
+ # ---- Phase 0: chat models + RAPTOR instances for summarization/clustering.
+ # Per-document summaries use each document's ingestion pipeline Compiler
+ # model. The first document's model deterministically owns KB-wide cluster
+ # summaries and labels.
+ chat_models: dict[str, LLMBundle] = {}
+ raptors: dict[str, Raptor] = {}
+ doc_llm_ids: dict[str, str] = {}
+ for doc in eligible_docs:
+ doc_id = str(doc["id"])
+ pipeline_id = str(doc.get("pipeline_id") or "").strip()
+ if not pipeline_id:
+ raise ValueError(f"Skill document {doc_id} must use a pipeline")
+ llm_id = _pipeline_compiler_llm_id(pipeline_id)
+ if not llm_id:
+ raise ValueError(f"Skill document {doc_id} pipeline Compiler must configure an LLM")
+ doc_llm_ids[doc_id] = llm_id
+ if llm_id in chat_models:
+ continue
+ chat_model_config = resolve_model_config(ctx.tenant_id, LLMType.CHAT, llm_id)
+ chat_models[llm_id] = LLMBundle(ctx.tenant_id, chat_model_config, lang=ctx.language)
+ raptors[llm_id] = Raptor(
+ max_cluster=128,
+ llm_model=chat_models[llm_id],
+ embd_model=embedding_model,
+ prompt="Please write a concise summary of the following texts:\n{cluster_content}",
+ max_token=512,
+ max_errors=3,
+ clustering_threshold=0.3,
+ clustering_ratio=0.5,
+ )
+ first_llm_id = doc_llm_ids[str(eligible_docs[0]["id"])]
+ chat_mdl = chat_models[first_llm_id]
+ raptor = raptors[first_llm_id]
+
+ # ---- Phase 1: per-doc summaries.
# Phase-1 gate: bail before spinning up N per-doc summarizations.
if ctx.has_canceled_func(ctx.id):
progress(-1, "skill: task has been canceled")
@@ -452,10 +470,11 @@ async def run_corpus2skill(
async def _summarize_doc(d: Dict) -> Optional[SkillNode]:
async with doc_sem:
try:
+ llm_id = doc_llm_ids[str(d["id"])]
return await doc_summary_for_skill(
d["id"],
- raptor,
- chat_mdl,
+ raptors[llm_id],
+ chat_models[llm_id],
ctx,
load_chunks_for_doc,
)
diff --git a/rag/svr/task_executor_refactor/dataset_wiki_generator.py b/rag/svr/task_executor_refactor/dataset_wiki_generator.py
index 739a54cba1..6df9170128 100644
--- a/rag/svr/task_executor_refactor/dataset_wiki_generator.py
+++ b/rag/svr/task_executor_refactor/dataset_wiki_generator.py
@@ -269,18 +269,21 @@ def _pipeline_compiler_llm_id(pipeline_id: str) -> str | None:
return None
-def _validate_wiki_eligible_docs(eligible: list[tuple[dict, str]]) -> dict[str, str | None]:
+def _validate_wiki_eligible_docs(eligible: list[tuple[dict, str]]) -> dict[str, str]:
"""Validate one Wiki template and return each doc's pipeline chat model."""
template_ids = {template_id for _, template_id in eligible}
if len(template_ids) > 1:
raise ValueError("Eligible Wiki documents must use the same template")
- pipeline_chat_llm_ids: dict[str, str | None] = {}
+ pipeline_chat_llm_ids: dict[str, str] = {}
for doc, _ in eligible:
doc_id = str(doc.get("id") or "")
pipeline_id = (doc.get("pipeline_id") or "").strip()
if not pipeline_id:
raise ValueError(f"Wiki document {doc_id} must use a pipeline")
- pipeline_chat_llm_ids[doc_id] = _pipeline_compiler_llm_id(pipeline_id)
+ llm_id = _pipeline_compiler_llm_id(pipeline_id)
+ if not llm_id:
+ raise ValueError(f"Wiki document {doc_id} pipeline Compiler must configure an LLM")
+ pipeline_chat_llm_ids[doc_id] = llm_id
return pipeline_chat_llm_ids
@@ -1273,10 +1276,7 @@ async def run_wiki(
from api.db.services.knowledgebase_service import KnowledgebaseService
from api.db.services.compilation_template_service import CompilationTemplateService
from api.db.services.llm_service import LLMBundle
- from api.db.joint_services.tenant_model_service import (
- get_tenant_default_model_by_type,
- resolve_model_config,
- )
+ from api.db.joint_services.tenant_model_service import resolve_model_config
progress = ctx.progress_cb
progress(0.0, "Loading documents for wiki compilation...")
@@ -1331,31 +1331,16 @@ async def run_wiki(
# pipeline Compiler's ``llm_id`` as the canonical KB chat model.
llm_bundle_cache: dict[str, LLMBundle] = {}
- def _bundle_for(llm_id: str | None) -> LLMBundle:
- key = (llm_id or "").strip() or "__tenant_default__"
+ def _bundle_for(llm_id: str) -> LLMBundle:
+ key = llm_id.strip()
cached = llm_bundle_cache.get(key)
if cached is not None:
return cached
- try:
- if key == "__tenant_default__":
- cfg = get_tenant_default_model_by_type(ctx.tenant_id, LLMType.CHAT)
- else:
- cfg = resolve_model_config(
- ctx.tenant_id,
- LLMType.CHAT,
- key,
- )
- except Exception:
- logging.exception(
- "wiki: chat model resolution failed for llm_id=%s (kb=%s); falling back to tenant default",
- key,
- ctx.kb_id,
- )
- cfg = get_tenant_default_model_by_type(ctx.tenant_id, LLMType.CHAT)
- key = "__tenant_default__"
- cached = llm_bundle_cache.get(key)
- if cached is not None:
- return cached
+ cfg = resolve_model_config(
+ ctx.tenant_id,
+ LLMType.CHAT,
+ key,
+ )
bundle = LLMBundle(ctx.tenant_id, cfg, lang=ctx.language)
llm_bundle_cache[key] = bundle
return bundle
@@ -1404,7 +1389,7 @@ async def run_wiki(
first_doc = resolved_eligible[0][0]
first_parser_cfg = resolved_eligible[0][2]
first_parser_cfg = first_parser_cfg if isinstance(first_parser_cfg, dict) else {}
- kb_chat_llm_id = pipeline_chat_llm_ids.get(str(first_doc.get("id") or ""))
+ kb_chat_llm_id = pipeline_chat_llm_ids[str(first_doc.get("id") or "")]
first_instruction = first_parser_cfg.get("instruction")
first_example = first_parser_cfg.get("example")
kb_writer_instruction: Optional[str] = first_instruction if isinstance(first_instruction, str) and first_instruction.strip() else None
@@ -1452,7 +1437,7 @@ async def run_wiki(
i, doc, template_id, parser_cfg, batch, batch_no = item
doc_id = doc["id"]
stats = doc_stats[i]
- map_llm_id = pipeline_chat_llm_ids.get(str(doc_id))
+ map_llm_id = pipeline_chat_llm_ids[str(doc_id)]
phase1 = await wiki_map_from_chunks(
chunks=batch,
chat_mdl=map_llm_pool.wrap(
@@ -1624,10 +1609,7 @@ async def run_wiki_incremental(
from api.db.services.document_service import DocumentService
from api.db.services.compilation_template_service import CompilationTemplateService
from api.db.services.llm_service import LLMBundle
- from api.db.joint_services.tenant_model_service import (
- get_tenant_default_model_by_type,
- resolve_model_config,
- )
+ from api.db.joint_services.tenant_model_service import resolve_model_config
from rag.advanced_rag.knowlege_compile.wiki_incremental import (
wiki_compile_incremental,
)
@@ -1752,22 +1734,12 @@ async def run_wiki_incremental(
# 3. Resolve chat model
llm_bundle_cache: dict[str, LLMBundle] = {}
- def _bundle_for(llm_id: str | None) -> LLMBundle:
- key = (llm_id or "").strip() or "__tenant_default__"
+ def _bundle_for(llm_id: str) -> LLMBundle:
+ key = llm_id.strip()
cached = llm_bundle_cache.get(key)
if cached is not None:
return cached
- try:
- if key == "__tenant_default__":
- cfg = get_tenant_default_model_by_type(ctx.tenant_id, LLMType.CHAT)
- else:
- cfg = resolve_model_config(ctx.tenant_id, LLMType.CHAT, key)
- except Exception:
- cfg = get_tenant_default_model_by_type(ctx.tenant_id, LLMType.CHAT)
- key = "__tenant_default__"
- cached = llm_bundle_cache.get(key)
- if cached is not None:
- return cached
+ cfg = resolve_model_config(ctx.tenant_id, LLMType.CHAT, key)
bundle = LLMBundle(ctx.tenant_id, cfg, lang=ctx.language)
llm_bundle_cache[key] = bundle
return bundle
@@ -1789,7 +1761,7 @@ async def run_wiki_incremental(
doc_configs[d["id"]] = cfg
if not first_template_found and isinstance(cfg, dict):
first_template_found = True
- kb_chat_llm_id = pipeline_chat_llm_ids.get(str(d.get("id") or ""))
+ kb_chat_llm_id = pipeline_chat_llm_ids[str(d.get("id") or "")]
except Exception:
logging.exception("wiki: config resolve failed for doc %s", d["id"])
doc_configs[d["id"]] = {}
@@ -1817,7 +1789,7 @@ async def run_wiki_incremental(
return
_, doc, template_id, parser_cfg, batch = item
doc_id = doc["id"]
- map_llm_id = pipeline_chat_llm_ids.get(str(doc_id))
+ map_llm_id = pipeline_chat_llm_ids[str(doc_id)]
await wiki_map_from_chunks(
chunks=batch,
@@ -1922,7 +1894,9 @@ async def run_wiki_incremental(
logging.info("wiki: MAP rows exist but no pages found for kb=%s; rebuilding from stored extracts.", ctx.kb_id)
# 5. Run incremental wiki compilation (Mode A or Mode B)
- kb_chat_mdl = _bundle_for(kb_chat_llm_id) if kb_chat_llm_id else _bundle_for(None)
+ if not kb_chat_llm_id:
+ raise ValueError("Wiki compilation requires an ingestion pipeline Compiler with an LLM configured")
+ kb_chat_mdl = _bundle_for(kb_chat_llm_id)
progress(0.65, f"Wiki {mode} incremental compilation ...")
summary = await wiki_compile_incremental(
diff --git a/rag/svr/task_executor_refactor/task_handler.py b/rag/svr/task_executor_refactor/task_handler.py
index dc92a55de6..7d83c1cb8c 100644
--- a/rag/svr/task_executor_refactor/task_handler.py
+++ b/rag/svr/task_executor_refactor/task_handler.py
@@ -87,28 +87,6 @@ def _parser_config_compilation_template_ids(parser_config, tenant_id: str) -> li
return template_ids
-def _resolve_template_chat_llm_id(parser_cfg: dict, ctx) -> str:
- """Pick the chat model id for a knowledge-compilation template.
-
- Resolution order:
- 1. The template's own ``llm_id`` (what the user picked in the
- compilation-template panel).
- 2. The doc's ``parser_config.llm_id`` (the doc-level chunking
- model).
- 3. ``ctx.llm_id`` (the chunking task's default).
- """
- if isinstance(parser_cfg, dict):
- tid = parser_cfg.get("llm_id")
- if isinstance(tid, str) and tid.strip():
- return tid.strip()
- doc_cfg = getattr(ctx, "parser_config", None) or {}
- if isinstance(doc_cfg, dict):
- did = doc_cfg.get("llm_id")
- if isinstance(did, str) and did.strip():
- return did.strip()
- return ctx.llm_id
-
-
# Document-structure compilation tunables
# (DOC_STRUCTURE_COMPILE_BATCH_CHUNKS, DOC_STRUCTURE_MERGE_MAX_DOCS,
# STRUCTURE_CHAIN_CORRECTION_TIMEOUT_S) moved to