Skip to content

feat(kb): 知识库无匹配结果时告知用户#16205

Open
MichaelYu9135 wants to merge 1 commit into
CherryHQ:mainfrom
MichaelYu9135:feat/kb-no-match-user-notification
Open

feat(kb): 知识库无匹配结果时告知用户#16205
MichaelYu9135 wants to merge 1 commit into
CherryHQ:mainfrom
MichaelYu9135:feat/kb-no-match-user-notification

Conversation

@MichaelYu9135

Copy link
Copy Markdown

Summary

当助手配置了知识库时,用户期望回答来自他们的存储文档。但此前如果 kb_search 返回空结果,LLM 仅收到一条建议重试的提示,没有指令要求告知用户。模型会静默回退到通用知识回答,用户完全无法得知回答是否来自知识库。

两层修复:

  1. 系统提示层:当助手有 knowledgeBaseIds 时,注入一段 KB 通知指令,要求模型:

    • 始终先尝试 kb_search 再用通用知识回答
    • 若无相关结果,必须明确告知用户知识库中未找到相关信息
    • 不得静默回退到通用知识而不告知用户
  2. 工具输出层:更新 knowledgeSearchModelOutput 中空结果时的提示文本,在保留重试建议的同时,增加"必须告知用户"的指令。

Changed files

File Change
src/main/ai/tools/knowledgeLookup.ts 空结果提示文本增加 "必须告知用户" 指令
src/main/ai/runtime/aiSdk/params/assembleSystemPrompt.ts 新增 KB_SYSTEM_INSTRUCTION 常量,当 kbSearchActive=true 注入系统提示
src/main/ai/runtime/aiSdk/params/buildAgentParams.ts 传递 kbSearchActive 参数(基于 assistant.knowledgeBaseIds 判断)
assembleSystemPrompt.test.ts 新增 4 个测试覆盖 KB 指令注入逻辑
KnowledgeSearchTool.test.ts 更新空结果测试以验证新增的告知用户指令

Test plan

  • 新增 assembleSystemPrompt 测试:kbSearchActive=false 时不注入 KB 指令
  • 新增 assembleSystemPrompt 测试:kbSearchActive=true 时注入 KB 指令
  • 新增 assembleSystemPrompt 测试:KB 指令与助手 prompt 并存
  • 更新 KnowledgeSearchTool 空结果测试验证 "inform the user" 指令
  • 手动测试:配置助手关联知识库后提问不在知识库中的内容,确认 LLM 回答中包含"知识库中未找到相关信息"的提示

🤖 Generated with Claude Code

When an assistant has knowledge bases configured, users expect answers
to come from their stored documents. Previously, if kb_search returned
empty results, the model received a retry-oriented hint but no
instruction to inform the user. The model would silently fall back to
general knowledge, leaving the user unaware that the answer did not
come from their knowledge base.

Two-layer fix:
1. System prompt: inject a KB notification instruction when the
   assistant has knowledgeBaseIds, telling the model to always attempt
   kb_search first and explicitly inform the user when no relevant
   results are found.
2. Tool output: update the empty-result message in
   knowledgeSearchModelOutput to instruct the model to tell the user
   (in addition to the existing retry hint).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@MichaelYu9135 MichaelYu9135 requested a review from a team June 19, 2026 01:55
@MichaelYu9135 MichaelYu9135 requested a review from DeJeune as a code owner June 19, 2026 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant