From e4e4dcbd1e3c89d9f5004a0be207130bb872456e Mon Sep 17 00:00:00 2001 From: Wang Jiyuan <59059173+EurFelux@users.noreply.github.com> Date: Sat, 14 Jun 2025 13:35:32 +0800 Subject: [PATCH] fix: model_name prompt var always use default model (#7178) * fix: model_name prompt var always use default mode * fix: incorrect model name --- .../aiCore/clients/anthropic/AnthropicAPIClient.ts | 2 +- .../src/aiCore/clients/gemini/GeminiAPIClient.ts | 2 +- .../src/aiCore/clients/openai/OpenAIApiClient.ts | 2 +- .../aiCore/clients/openai/OpenAIResponseAPIClient.ts | 2 +- src/renderer/src/utils/prompt.ts | 12 ++++++++---- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts b/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts index ffbda737c6..29cf86399a 100644 --- a/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts +++ b/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts @@ -453,7 +453,7 @@ export class AnthropicAPIClient extends BaseApiClient< }) if (this.useSystemPromptForTools) { - systemPrompt = await buildSystemPrompt(systemPrompt, mcpTools) + systemPrompt = await buildSystemPrompt(systemPrompt, mcpTools, assistant) } const systemMessage: TextBlockParam | undefined = systemPrompt diff --git a/src/renderer/src/aiCore/clients/gemini/GeminiAPIClient.ts b/src/renderer/src/aiCore/clients/gemini/GeminiAPIClient.ts index bc848df7f9..1a67696e33 100644 --- a/src/renderer/src/aiCore/clients/gemini/GeminiAPIClient.ts +++ b/src/renderer/src/aiCore/clients/gemini/GeminiAPIClient.ts @@ -452,7 +452,7 @@ export class GeminiAPIClient extends BaseApiClient< }) if (this.useSystemPromptForTools) { - systemInstruction = await buildSystemPrompt(assistant.prompt || '', mcpTools) + systemInstruction = await buildSystemPrompt(assistant.prompt || '', mcpTools, assistant) } let messageContents: Content diff --git a/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts b/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts index 07359c837f..f9c4372c7b 100644 --- a/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts +++ b/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts @@ -420,7 +420,7 @@ export class OpenAIAPIClient extends OpenAIBaseClient< }) if (this.useSystemPromptForTools) { - systemMessage.content = await buildSystemPrompt(systemMessage.content || '', mcpTools) + systemMessage.content = await buildSystemPrompt(systemMessage.content || '', mcpTools, assistant) } // 3. 处理用户消息 diff --git a/src/renderer/src/aiCore/clients/openai/OpenAIResponseAPIClient.ts b/src/renderer/src/aiCore/clients/openai/OpenAIResponseAPIClient.ts index 0fdd65f709..a0f4d8077d 100644 --- a/src/renderer/src/aiCore/clients/openai/OpenAIResponseAPIClient.ts +++ b/src/renderer/src/aiCore/clients/openai/OpenAIResponseAPIClient.ts @@ -290,7 +290,7 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient< }) if (this.useSystemPromptForTools) { - systemMessageInput.text = await buildSystemPrompt(systemMessageInput.text || '', mcpTools) + systemMessageInput.text = await buildSystemPrompt(systemMessageInput.text || '', mcpTools, assistant) } systemMessageContent.push(systemMessageInput) systemMessage.content = systemMessageContent diff --git a/src/renderer/src/utils/prompt.ts b/src/renderer/src/utils/prompt.ts index 05b10748d7..7ae0b7327f 100644 --- a/src/renderer/src/utils/prompt.ts +++ b/src/renderer/src/utils/prompt.ts @@ -1,5 +1,6 @@ import store from '@renderer/store' -import { MCPTool } from '@renderer/types' +import { Assistant, MCPTool } from '@renderer/types' + 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 tool 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. @@ -147,7 +148,11 @@ ${availableTools} ` } -export const buildSystemPrompt = async (userSystemPrompt: string, tools?: MCPTool[]): Promise => { +export const buildSystemPrompt = async ( + userSystemPrompt: string, + tools?: MCPTool[], + assistant?: Assistant +): Promise => { if (typeof userSystemPrompt === 'string') { const now = new Date() if (userSystemPrompt.includes('{{date}}')) { @@ -197,8 +202,7 @@ export const buildSystemPrompt = async (userSystemPrompt: string, tools?: MCPToo if (userSystemPrompt.includes('{{model_name}}')) { try { - const modelName = store.getState().llm.defaultModel.name - userSystemPrompt = userSystemPrompt.replace(/{{model_name}}/g, modelName) + userSystemPrompt = userSystemPrompt.replace(/{{model_name}}/g, assistant?.model?.name || 'Unknown Model') } catch (error) { console.error('Failed to get model name:', error) userSystemPrompt = userSystemPrompt.replace(/{{model_name}}/g, 'Unknown Model')