From 3e4a1fa8879af4ec0b9ed2ca7cac779e48377850 Mon Sep 17 00:00:00 2001 From: Wang Qi Date: Wed, 12 Aug 2026 09:37:25 +0800 Subject: [PATCH] Add doc_name for compilation generated chunk (#18126) --- rag/advanced_rag/knowlege_compile/runner.py | 8 ++++++++ .../knowlege_compile/structure.py | 19 +++++++++++++++++++ rag/flow/compiler/compiler.py | 10 ++++++++++ .../chunk_post_processor.py | 6 ++++++ 4 files changed, 43 insertions(+) diff --git a/rag/advanced_rag/knowlege_compile/runner.py b/rag/advanced_rag/knowlege_compile/runner.py index 3f855b5d5c..84aadf5458 100644 --- a/rag/advanced_rag/knowlege_compile/runner.py +++ b/rag/advanced_rag/knowlege_compile/runner.py @@ -194,6 +194,7 @@ async def _upsert_dataset_nav_from_page_index( tenant_id: str, kb_id: str, doc_id: str, + doc_name: str, progress_cb: Callable[..., None], cancel_check: Callable[[], bool], ) -> None: @@ -215,6 +216,7 @@ async def _upsert_dataset_nav_from_page_index( tenant_id, kb_id, doc_id, + doc_name, "page_index", compilation_template_id=template_id, ) @@ -224,6 +226,7 @@ async def _upsert_dataset_nav_from_page_index( tenant_id, kb_id, doc_id, + doc_name, "timeline", compilation_template_id=template_id, ) @@ -277,6 +280,7 @@ async def run_structure_compile_over_batches( tenant_id: str, kb_id: str, doc_id: str, + doc_name: str, language: str, chunk_batches: AsyncIterator[list[dict]], progress_cb: Callable[..., None], @@ -375,6 +379,7 @@ async def run_structure_compile_over_batches( doc_storage_waiter=_wait_for_doc_storage, doc_storage_releaser=_release_doc_storage, merge_scope=merge_scope_by_tid[template_id], + doc_name=doc_name, ) finally: if not doc_storage_released: @@ -408,6 +413,7 @@ async def run_structure_compile_over_batches( compile_chat_mdl, embedding_model, doc_id, + doc_name=doc_name, language=language, callback=progress_cb, max_workers=3, @@ -604,6 +610,7 @@ async def run_structure_compile_over_batches( tenant_id=tenant_id, kb_id=kb_id, doc_id=doc_id, + doc_name=doc_name, progress_cb=progress_cb, cancel_check=cancel_check, ) @@ -618,6 +625,7 @@ async def run_structure_compile_over_batches( tenant_id, kb_id, doc_id, + doc_name, compilation_template_id=template_id, ) except Exception: diff --git a/rag/advanced_rag/knowlege_compile/structure.py b/rag/advanced_rag/knowlege_compile/structure.py index 558dc427fe..663149dfee 100644 --- a/rag/advanced_rag/knowlege_compile/structure.py +++ b/rag/advanced_rag/knowlege_compile/structure.py @@ -720,6 +720,7 @@ def _struct_to_doc_storage_doc( payload: dict, compile_kwd: str, doc_id: str, + doc_name: str, chunk_ids: list[str], vec, kind: str, @@ -772,6 +773,7 @@ def _struct_to_doc_storage_doc( "knowledge_graph_kwd": kind, "scope_kwd": scope, "doc_id": doc_id_str, + "docnm_kwd": doc_name, "source_chunk_ids": list(chunk_ids or []), "content_ltks": content_ltks, "content_sm_ltks": content_sm_ltks, @@ -827,6 +829,7 @@ async def _struct_process_batch( chat_mdl, embd_mdl, doc_id: str, + doc_name: str, language: str, callback, semaphore, @@ -895,6 +898,7 @@ async def _struct_process_batch( payload, autotype, doc_id, + doc_name, _struct_payload_chunk_ids(payload, payload_chunk_ids), vec, kind, @@ -923,6 +927,7 @@ async def compile_structure_from_text( chat_mdl, embd_mdl, doc_id: str, + doc_name: str = "", language: str = "en", callback=None, max_workers: int = 10, @@ -1003,6 +1008,7 @@ async def compile_structure_from_text( chat_mdl=chat_mdl, embd_mdl=embd_mdl, doc_id=doc_id, + doc_name=doc_name, language=language, callback=callback, semaphore=None, @@ -1309,6 +1315,7 @@ def _struct_rebuild_doc_storage_doc( payload=payload, compile_kwd=base_doc.get("compile_kwd"), doc_id=base_doc.get("doc_id"), + doc_name=base_doc.get("docnm_kwd") or "", chunk_ids=chunk_ids, vec=vec, kind=kind, @@ -1686,6 +1693,7 @@ async def _struct_doc_storage_dedup_batch( "knowledge_graph_kwd", "compile_kwd", "doc_id", + "docnm_kwd", "from_entity_kwd", "to_entity_kwd", "compilation_template_ids", @@ -1907,6 +1915,7 @@ async def _struct_doc_storage_dedup_batch( "knowledge_graph_kwd", "compile_kwd", "doc_id", + "docnm_kwd", "from_entity_kwd", "to_entity_kwd", "compilation_template_ids", @@ -2293,6 +2302,7 @@ async def cleanup_timeline_isolated_entities( tenant_id: str, kb_id: str, doc_id: str, + doc_name: str, compilation_template_id: str | None = None, ) -> int: """Remove timeline entity rows that are not used by any relation. @@ -2363,6 +2373,7 @@ async def cleanup_timeline_isolated_entities( tenant_id, kb_id, doc_id, + doc_name, "timeline", compilation_template_id, ) @@ -2374,6 +2385,7 @@ async def _struct_upsert_graph_json( tenant_id: str, kb_id: str, doc_id: str, + doc_name: str, compile_kwd: str, compilation_template_id: str | None = None, ) -> None: @@ -2388,6 +2400,7 @@ async def _struct_upsert_graph_json( "compile_kwd": compile_kwd, "knowledge_graph_kwd": "graph", "doc_id": doc_id, + "docnm_kwd": doc_name, "kb_id": kb_id, "available_int": 0, } @@ -2411,6 +2424,7 @@ async def _struct_upsert_tree_graph_rows( tenant_id: str, kb_id: str, doc_id: str, + doc_name: str, embedding_model, compilation_template_id: str | None = None, ) -> None: @@ -2441,6 +2455,7 @@ async def _struct_upsert_tree_graph_rows( payload=payload, compile_kwd="tree", doc_id=doc_id, + doc_name=doc_name, chunk_ids=source_chunk_ids, vec=vector, kind=kind, @@ -2467,6 +2482,7 @@ async def rebuild_structure_graph_json( tenant_id: str, kb_id: str, doc_id: str, + doc_name: str, compile_kwd: str, compilation_template_id: str | None = None, ) -> dict: @@ -2484,6 +2500,7 @@ async def rebuild_structure_graph_json( tenant_id, kb_id, doc_id, + doc_name, compile_kwd, compilation_template_id, ) @@ -2999,6 +3016,7 @@ async def merge_compiled_structures( doc_storage_waiter: Callable[[], Awaitable[None]] | None = None, doc_storage_releaser: Callable[[], Awaitable[None]] | None = None, merge_scope: str = MERGE_SCOPE_DOC, + doc_name: str = "", ) -> dict: """Merge ``docs`` (the output of ``compile_structure_from_text``) before inserting them into ES. @@ -3144,6 +3162,7 @@ async def merge_compiled_structures( tenant_id, kb_id, doc_id, + doc_name, compile_kwd, compilation_template_id=template_id or None, ) diff --git a/rag/flow/compiler/compiler.py b/rag/flow/compiler/compiler.py index a92ffdd383..a8741bdb8f 100644 --- a/rag/flow/compiler/compiler.py +++ b/rag/flow/compiler/compiler.py @@ -370,6 +370,7 @@ class Compiler(ProcessBase, LLM): tenant_id: str, kb_id: str, doc_id: str, + doc_name: str, ) -> None: """Build and persist tree graphs from the pipeline's in-memory chunks. @@ -452,6 +453,7 @@ class Compiler(ProcessBase, LLM): tenant_id, kb_id, doc_id, + doc_name, embedding_model, compilation_template_id=template_id, ) @@ -460,6 +462,7 @@ class Compiler(ProcessBase, LLM): tenant_id, kb_id, doc_id, + doc_name, compile_kwd="tree", compilation_template_id=template_id, ) @@ -516,6 +519,11 @@ class Compiler(ProcessBase, LLM): tenant_id = self._canvas.get_tenant_id() doc_id = self._canvas._doc_id + doc_name = "" + if doc_id: + found, document = DocumentService.get_by_id(doc_id) + if found and document: + doc_name = document.name kb_id = getattr(self._canvas, "_kb_id", None) or DocumentService.get_knowledgebase_id(doc_id) language = self._compile_language(kwargs) @@ -650,6 +658,7 @@ class Compiler(ProcessBase, LLM): tenant_id, kb_id, doc_id, + doc_name, ) if non_tree_templates: @@ -681,6 +690,7 @@ class Compiler(ProcessBase, LLM): tenant_id=tenant_id, kb_id=kb_id, doc_id=doc_id, + doc_name=doc_name, language=language, chunk_batches=_chunk_batches(), progress_cb=self._compile_progress, diff --git a/rag/svr/task_executor_refactor/chunk_post_processor.py b/rag/svr/task_executor_refactor/chunk_post_processor.py index ed33f7d813..16b458204a 100644 --- a/rag/svr/task_executor_refactor/chunk_post_processor.py +++ b/rag/svr/task_executor_refactor/chunk_post_processor.py @@ -893,6 +893,7 @@ async def run_tree_templates( templates: list[tuple[str, dict]], chat_mdl_by_tid: dict[str, "LLMBundle"], embedding_model, + doc_name: str, ) -> None: """Run the ``tree``-kind compilation templates for the current doc. Each pair runs RAPTOR with ``is_tree=True`` via @@ -984,6 +985,7 @@ async def run_tree_templates( ctx.tenant_id, ctx.kb_id, doc_id, + doc_name, compile_kwd="tree", compilation_template_id=template_id, ) @@ -1035,6 +1037,8 @@ async def run_document_structure_compile(handler, embedding_model: LLMBundle) -> from api.apps.restful_apis.chunk_api import _compilation_template_kind ctx = handler._task_context + found, document = DocumentService.get_by_id(ctx.doc_id) + doc_name = document.name if found and document else "" template_ids = _parser_config_compilation_template_ids(ctx.parser_config, ctx.tenant_id) if not template_ids: return @@ -1088,6 +1092,7 @@ async def run_document_structure_compile(handler, embedding_model: LLMBundle) -> tree_templates, chat_mdl_by_tid, embedding_model, + doc_name, ) if not non_tree_templates: @@ -1109,6 +1114,7 @@ async def run_document_structure_compile(handler, embedding_model: LLMBundle) -> tenant_id=ctx.tenant_id, kb_id=ctx.kb_id, doc_id=ctx.doc_id, + doc_name=doc_name, language=ctx.language, chunk_batches=_stream_doc_batches(), progress_cb=ctx.progress_cb,