From 5545b852c866e1320f55ed67d35eb957d85722d5 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 9 Apr 2026 12:27:50 +0800 Subject: [PATCH] feat: Add Wiki Researcher agent and system prompt for enhanced knowledge retrieval - Introduced a new built-in agent, "Wiki Researcher," designed for navigating and answering questions based on Wiki knowledge bases, complete with multilingual support. - Added a corresponding system prompt that outlines the agent's role, mission, and workflow for effective knowledge graph traversal. - Updated the agent configuration to include specific tools and parameters tailored for the Wiki Researcher, enhancing its functionality and user interaction. - Removed deprecated wiki tools from the agent service to streamline the toolset and improve performance. These changes significantly enhance the capabilities of the agent framework, providing users with a specialized tool for in-depth Wiki-based research. --- .../prompt_templates/agent_system_prompt.yaml | 20 +++++----- internal/agent/observe.go | 38 ++++++++++++++++++- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/config/prompt_templates/agent_system_prompt.yaml b/config/prompt_templates/agent_system_prompt.yaml index f9a94fbf..59b4c3c4 100644 --- a/config/prompt_templates/agent_system_prompt.yaml +++ b/config/prompt_templates/agent_system_prompt.yaml @@ -75,8 +75,8 @@ templates: ### Critical Constraints (ABSOLUTE RULES) 1. **Evidence-Based Facts:** For factual claims about documents or domain knowledge, rely on KB/Web retrieval rather than internal knowledge. However, you MAY answer directly when the user's question is about image content you can see, conversational context, or general interaction. 2. **Mandatory Deep Read:** Whenever grep_chunks or knowledge_search returns matched knowledge_ids or chunk_ids, you **MUST** immediately call list_knowledge_chunks to read the full content of those specific chunks. Do not rely on search snippets alone. - 3. **KB First, Web Second:** When retrieval IS needed, always exhaust KB strategies (including the Deep Read) before attempting Web Search (if enabled). - 4. **Strict Plan Adherence:** If a todo_write plan exists, execute it sequentially. No skipping. + 3. **Knowledge Base Priority:** When retrieval IS needed, always exhaust knowledge base strategies (including the Deep Read) before attempting Web Search (if enabled). + 4. **Always Re-Retrieve for Each New Question:** You MUST perform fresh knowledge base retrieval for EVERY new user question that requires factual or domain-specific information, even if a similar or identical question was asked earlier in the conversation. NEVER rely on previously retrieved knowledge base content from the conversation history — the knowledge base may have been updated, switched, or had content removed since the last retrieval. Treat each new question as if you have no prior knowledge from previous retrievals. 5. **User-Friendly Communication:** In ALL outputs visible to users (including your thinking/reasoning process), you MUST: - Use natural language descriptions instead of internal tool names (e.g., say "搜索知识库" not "knowledge_search", "文本搜索" not "grep_chunks", "阅读文档内容" not "list_knowledge_chunks"). - Never expose internal IDs (knowledge_base_id, knowledge_id, chunk_id, etc.) in thinking or answers. Refer to documents by their title or name instead. @@ -85,18 +85,16 @@ templates: ### Workflow: The "Assess-Reconnaissance-Plan-Execute" Cycle - #### Phase 0: Intent Assessment (Before Any Retrieval) - Before initiating any KB search, briefly evaluate the user's request in your think block: - * **Direct Answer Path (skip retrieval):** ONLY when the request is: - - Pure conversational interaction (greetings, thanks, farewells) - - Summarizing or continuing previous discussion from conversation context - - Explicitly asking to describe/read image content with no deeper question (e.g., "帮我读一下图片上的文字", "Describe this image") - → Proceed directly to **final_answer**. - * **Retrieval Path (default for image + question):** In most cases, especially when the user uploads an image with a question (e.g., "这是为啥", "这是什么意思", "这张图说的啥"), the user likely wants you to **combine the image content with knowledge base information** to provide an informed answer. Use the image content (OCR text or visual description) as search keywords and proceed to Phase 1. - Also proceed to Phase 1 when: + #### Intent Assessment + Before initiating any search, briefly evaluate the user's request: + * **If retrieval is unnecessary** — the request is purely conversational (greetings, thanks, farewells), or explicitly asking to describe/read image content with no deeper question (e.g., "帮我读一下图片上的文字", "Describe this image") — proceed directly to **final_answer**. + * **Otherwise, proceed to retrieval.** Even if the user asks a question similar to a previous one, you MUST perform a fresh retrieval — do NOT reuse or summarize answers from earlier in the conversation. The knowledge base content may have changed. + In most cases, especially when the user uploads an image with a question (e.g., "这是为啥", "这是什么意思", "这张图说的啥"), the user likely wants you to **combine the image content with knowledge base information** to provide an informed answer. Use the image content (OCR text or visual description) as search keywords. + Also proceed to retrieval when: - The question involves factual, technical, or domain-specific knowledge - The user asks to find related documents - You are uncertain whether the image alone can fully answer the question + - The user asks the same or a similar question as before (knowledge base may have been updated) #### Phase 1: Preliminary Reconnaissance Perform a "Deep Read" test of the KB to gain preliminary cognition. diff --git a/internal/agent/observe.go b/internal/agent/observe.go index 10bf3585..c10bfa2c 100644 --- a/internal/agent/observe.go +++ b/internal/agent/observe.go @@ -324,6 +324,37 @@ func countTotalToolCalls(steps []types.AgentStep) int { return total } +// kbToolNames lists tools whose results contain knowledge base content that +// may become stale across turns (KB can be switched, updated, or deleted). +// Historical results from these tools are redacted to force fresh retrieval. +var kbToolNames = map[string]bool{ + agenttools.ToolKnowledgeSearch: true, + agenttools.ToolGrepChunks: true, + agenttools.ToolListKnowledgeChunks: true, + agenttools.ToolQueryKnowledgeGraph: true, + agenttools.ToolGetDocumentInfo: true, +} + +// redactHistoryKBResults replaces full KB tool results in historical context +// with brief markers. This prevents the LLM from reusing stale retrieval data +// when the knowledge base has been modified or switched between turns. +func redactHistoryKBResults(llmContext []chat.Message) []chat.Message { + redacted := make([]chat.Message, 0, len(llmContext)) + for _, msg := range llmContext { + if msg.Role == "tool" && kbToolNames[msg.Name] { + redacted = append(redacted, chat.Message{ + Role: msg.Role, + Content: "[Previous retrieval result omitted — knowledge base may have changed. Please perform a fresh search.]", + ToolCallID: msg.ToolCallID, + Name: msg.Name, + }) + } else { + redacted = append(redacted, msg) + } + } + return redacted +} + // buildMessagesWithLLMContext builds the message array with LLM context func (e *AgentEngine) buildMessagesWithLLMContext( systemPrompt, currentQuery, sessionID string, @@ -335,7 +366,10 @@ func (e *AgentEngine) buildMessagesWithLLMContext( } if len(llmContext) > 0 { - for _, msg := range llmContext { + // Redact KB tool results from previous turns to prevent the LLM + // from reusing stale retrieval data when the KB has been modified. + sanitized := redactHistoryKBResults(llmContext) + for _, msg := range sanitized { if msg.Role == "system" { continue } @@ -343,7 +377,7 @@ func (e *AgentEngine) buildMessagesWithLLMContext( messages = append(messages, msg) } } - logger.Infof(context.Background(), "Added %d history messages to context", len(llmContext)) + logger.Infof(context.Background(), "Added %d history messages to context (KB tool results redacted)", len(llmContext)) } // Build user message with runtime context safety tag