From 51835e32c5e146f295fe308d355531c2a08c9654 Mon Sep 17 00:00:00 2001 From: SuYao Date: Mon, 28 Jul 2025 23:54:41 +0800 Subject: [PATCH] fix(think-tool): update prompt handling logic and improve comments (#8538) * fix(think-tool): update prompt handling logic and improve comments * fix: ut --- src/renderer/src/services/ApiService.ts | 2 +- .../src/utils/__tests__/prompt.test.ts | 10 ++--- src/renderer/src/utils/prompt.ts | 43 +------------------ 3 files changed, 6 insertions(+), 49 deletions(-) diff --git a/src/renderer/src/services/ApiService.ts b/src/renderer/src/services/ApiService.ts index 14547690c5..841d9de934 100644 --- a/src/renderer/src/services/ApiService.ts +++ b/src/renderer/src/services/ApiService.ts @@ -379,7 +379,7 @@ async function fetchExternalTool( // 根据toolUseMode决定如何构建系统提示词 const basePrompt = assistant.prompt if (assistant.settings?.toolUseMode === 'prompt' || mcpTools.length > SYSTEM_PROMPT_THRESHOLD) { - // 提示词模式:需要完整的工具定义和思考指令 + // 提示词模式:需要完整的工具定义,思考工具返回会打乱提示词的返回(先去掉) assistant.prompt = buildSystemPromptWithTools(basePrompt, mcpTools) } else { // 原生函数调用模式:仅需要注入思考指令 diff --git a/src/renderer/src/utils/__tests__/prompt.test.ts b/src/renderer/src/utils/__tests__/prompt.test.ts index 4e90685811..3ae21dcaa4 100644 --- a/src/renderer/src/utils/__tests__/prompt.test.ts +++ b/src/renderer/src/utils/__tests__/prompt.test.ts @@ -169,7 +169,7 @@ describe('prompt', () => { System Information: - Date: {{date}} - User: {{username}} - + Instructions: Be helpful. ` const assistant = createMockAssistant('Test Assistant', 'Advanced-AI-Model') @@ -178,7 +178,7 @@ describe('prompt', () => { System Information: - Date: ${mockDate.toLocaleDateString()} - User: MockUser - + Instructions: Be helpful. ` tools = [createMockTool('web_search', 'Search the web')] @@ -192,7 +192,6 @@ describe('prompt', () => { expect(finalPrompt).toEqual(expectedFinalPrompt) expect(finalPrompt).toContain('## Tool Use Formatting') - expect(finalPrompt).toContain('## Using the think tool') }) it('should build a think-only prompt for native function calling mode', () => { @@ -223,7 +222,6 @@ describe('prompt', () => { expect(finalPrompt).toEqual(expectedFinalPrompt) expect(finalPrompt).toContain('## Tool Use Formatting') - expect(finalPrompt).toContain('## Using the think tool') }) }) @@ -234,7 +232,7 @@ describe('prompt', () => { System Information: - Date: {{date}} - User: {{username}} - + Instructions: Be helpful. ` const assistant = createMockAssistant('Test Assistant', 'Advanced-AI-Model') @@ -243,7 +241,7 @@ describe('prompt', () => { System Information: - Date: ${mockDate.toLocaleDateString()} - User: MockUser - + Instructions: Be helpful. ` diff --git a/src/renderer/src/utils/prompt.ts b/src/renderer/src/utils/prompt.ts index 88ec126a1c..0d17eb0e52 100644 --- a/src/renderer/src/utils/prompt.ts +++ b/src/renderer/src/utils/prompt.ts @@ -7,47 +7,6 @@ const logger = loggerService.withContext('Utils:Prompt') export const SYSTEM_PROMPT = `In this environment you have access to a set of tools you can use to answer the user's question. \ You can use one or more tools per message, and will receive the result of that tool use in the user's response. You use tools step-by-step to accomplish a given task, with each tool use informed by the result of the previous tool use. -## Using the think tool - -Before taking any action or responding to the user after receiving tool results, use the think tool as a scratchpad to: -- List the specific rules that apply to the current request -- Check if all required information is collected -- Verify that the planned action complies with all policies -- Iterate over tool results for correctness - -Here are some examples of what to iterate over inside the think tool: - -User wants to cancel flight ABC123 -- Need to verify: user ID, reservation ID, reason -- Check cancellation rules: - * Is it within 24h of booking? - * If not, check ticket class and insurance -- Verify no segments flown or are in the past -- Plan: collect missing info, verify rules, get confirmation - - - -User wants to book 3 tickets to NYC with 2 checked bags each -- Need user ID to check: - * Membership tier for baggage allowance - * Which payments methods exist in profile -- Baggage calculation: - * Economy class × 3 passengers - * If regular member: 1 free bag each → 3 extra bags = $150 - * If silver member: 2 free bags each → 0 extra bags = $0 - * If gold member: 3 free bags each → 0 extra bags = $0 -- Payment rules to verify: - * Max 1 travel certificate, 1 credit card, 3 gift cards - * All payment methods must be in profile - * Travel certificate remainder goes to waste -- Plan: -1. Get user ID -2. Verify membership level for bag fees -3. Check which payment methods in profile and if their combination is allowed -4. Calculate total: ticket price + any bag fees -5. Get explicit confirmation for booking - - ## Tool Use Formatting Tool use is formatted using XML-style tags. The tool name is enclosed in opening and closing tags, and each parameter is similarly enclosed within its own set of tags. Here's the structure: @@ -109,7 +68,7 @@ Before taking any action or responding to the user after receiving tool results, - List the specific rules that apply to the current request - Check if all required information is collected - Verify that the planned action complies with all policies -- Iterate over tool results for correctness +- Iterate over tool results for correctness - Response in user query language Here are some examples of what to iterate over inside the think tool: