feat(kb): 知识库无匹配结果时告知用户#16205
Open
MichaelYu9135 wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
当助手配置了知识库时,用户期望回答来自他们的存储文档。但此前如果
kb_search返回空结果,LLM 仅收到一条建议重试的提示,没有指令要求告知用户。模型会静默回退到通用知识回答,用户完全无法得知回答是否来自知识库。两层修复:
系统提示层:当助手有
knowledgeBaseIds时,注入一段 KB 通知指令,要求模型:kb_search再用通用知识回答工具输出层:更新
knowledgeSearchModelOutput中空结果时的提示文本,在保留重试建议的同时,增加"必须告知用户"的指令。Changed files
src/main/ai/tools/knowledgeLookup.tssrc/main/ai/runtime/aiSdk/params/assembleSystemPrompt.tsKB_SYSTEM_INSTRUCTION常量,当kbSearchActive=true注入系统提示src/main/ai/runtime/aiSdk/params/buildAgentParams.tskbSearchActive参数(基于assistant.knowledgeBaseIds判断)assembleSystemPrompt.test.tsKnowledgeSearchTool.test.tsTest plan
assembleSystemPrompt测试:kbSearchActive=false时不注入 KB 指令assembleSystemPrompt测试:kbSearchActive=true时注入 KB 指令assembleSystemPrompt测试:KB 指令与助手 prompt 并存KnowledgeSearchTool空结果测试验证 "inform the user" 指令🤖 Generated with Claude Code