From 3c58d96db548e8efee89df6f1595eecff12a4c18 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Fri, 24 Oct 2025 14:45:07 +0800 Subject: [PATCH] feat: add configuration for final knowledge base retrieval count and update related components --- astrbot/core/config/default.py | 16 +- astrbot/core/pipeline/process_stage/utils.py | 3 +- .../shared/KnowledgeBaseSelector.vue | 178 +++++++++--------- 3 files changed, 103 insertions(+), 94 deletions(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index d23364b19..36ac616ae 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -137,6 +137,7 @@ DEFAULT_CONFIG = { "default_kb_collection": "", # 默认知识库名称, 已经过时 "plugin_set": ["*"], # "*" 表示使用所有可用的插件, 空列表表示不使用任何插件 "kb_names": [], # 默认知识库名称列表 + "kb_final_top_k": 5, # 知识库检索的最终返回结果数量 } @@ -2001,6 +2002,8 @@ CONFIG_METADATA_2 = { "default_kb_collection": { "type": "string", }, + "kb_names": {"type": "list", "items": {"type": "string"}}, + "kb_final_top_k": {"type": "int", "default": 5}, }, }, } @@ -2079,10 +2082,17 @@ CONFIG_METADATA_3 = { "description": "知识库", "type": "object", "items": { - "default_kb_collection": { - "description": "默认使用的知识库", - "type": "string", + "kb_names": { + "description": "知识库列表", + "type": "list", + "items": {"type": "string"}, "_special": "select_knowledgebase", + "hint": "支持多选", + }, + "kb_final_top_k": { + "description": "最终返回结果数", + "type": "int", + "hint": "从知识库中检索到的结果数量,越大可能获得越多相关信息,但也可能引入噪音。建议根据实际需求调整", }, }, }, diff --git a/astrbot/core/pipeline/process_stage/utils.py b/astrbot/core/pipeline/process_stage/utils.py index bf40b3cae..8fe34e3ee 100644 --- a/astrbot/core/pipeline/process_stage/utils.py +++ b/astrbot/core/pipeline/process_stage/utils.py @@ -7,7 +7,6 @@ async def inject_kb_context( umo: str, p_ctx: PipelineContext, req: ProviderRequest, - top_k: int = 5, ) -> None: """inject knowledge base context into the provider request @@ -17,6 +16,7 @@ async def inject_kb_context( """ kb_mgr = p_ctx.plugin_manager.context.kb_manager kb_names = p_ctx.astrbot_config.get("kb_names", []) + top_k = p_ctx.astrbot_config.get("kb_final_top_k", 5) if not kb_names: return @@ -24,6 +24,7 @@ async def inject_kb_context( kb_context = await kb_mgr.retrieve( query=req.prompt, kb_names=kb_names, + top_m_final=top_k, ) if not kb_context: return diff --git a/dashboard/src/components/shared/KnowledgeBaseSelector.vue b/dashboard/src/components/shared/KnowledgeBaseSelector.vue index 3e0a8be15..a90e6f5e2 100644 --- a/dashboard/src/components/shared/KnowledgeBaseSelector.vue +++ b/dashboard/src/components/shared/KnowledgeBaseSelector.vue @@ -1,11 +1,21 @@