From 8f31236303c8c33ea7d6478d1ef47843b3e66ba5 Mon Sep 17 00:00:00 2001 From: Timebomb2018 <18868801967@163.com> Date: Fri, 24 Apr 2026 17:48:15 +0800 Subject: [PATCH] fix(app_chat_service,draft_run_service): move system_prompt augmentation before LangChainAgent instantiation --- api/app/services/app_chat_service.py | 46 +++++++++++------------- api/app/services/draft_run_service.py | 50 +++++++++++---------------- 2 files changed, 41 insertions(+), 55 deletions(-) diff --git a/api/app/services/app_chat_service.py b/api/app/services/app_chat_service.py index 407a1cfc..39a3d4d4 100644 --- a/api/app/services/app_chat_service.py +++ b/api/app/services/app_chat_service.py @@ -399,24 +399,6 @@ class AppChatService: # 获取模型参数 model_parameters = config.model_parameters - # 创建 LangChain Agent - agent = LangChainAgent( - model_name=api_key_obj.model_name, - api_key=api_key_obj.api_key, - provider=api_key_obj.provider, - api_base=api_key_obj.api_base, - is_omni=api_key_obj.is_omni, - temperature=model_parameters.get("temperature", 0.7), - max_tokens=model_parameters.get("max_tokens", 2000), - system_prompt=system_prompt, - tools=tools, - streaming=True, - deep_thinking=model_parameters.get("deep_thinking", False), - thinking_budget_tokens=model_parameters.get("thinking_budget_tokens"), - json_output=model_parameters.get("json_output", False), - capability=api_key_obj.capability or [], - ) - model_info = ModelInfo( model_name=api_key_obj.model_name, provider=api_key_obj.provider, @@ -471,16 +453,28 @@ class AppChatService: f.type == FileType.DOCUMENT for f in files ): from langchain.agents import create_agent - agent.system_prompt += ( - "\n\n文档中包含图片,图片位置已在文本中以 [第N页 第M张图片]: URL 标记。" - "请在回答中用 Markdown 格式 ![描述](URL) 展示相关图片,做到图文并茂。" - ) - agent.agent = create_agent( - model=agent.llm, - tools=agent._wrap_tools_with_tracking(agent.tools) if agent.tools else None, - system_prompt=agent.system_prompt + system_prompt += ( + "\n\n文档文字中包含图片位置标记如 [图片 第2页 第1张]: http://...,请在回答中用 Markdown 格式 ![图片描述](url) 展示对应图片。" ) + # 创建 LangChain Agent + agent = LangChainAgent( + model_name=api_key_obj.model_name, + api_key=api_key_obj.api_key, + provider=api_key_obj.provider, + api_base=api_key_obj.api_base, + is_omni=api_key_obj.is_omni, + temperature=model_parameters.get("temperature", 0.7), + max_tokens=model_parameters.get("max_tokens", 2000), + system_prompt=system_prompt, + tools=tools, + streaming=True, + deep_thinking=model_parameters.get("deep_thinking", False), + thinking_budget_tokens=model_parameters.get("thinking_budget_tokens"), + json_output=model_parameters.get("json_output", False), + capability=api_key_obj.capability or [], + ) + # 为需要运行时上下文的工具注入上下文 for t in tools: if hasattr(t, 'tool_instance') and hasattr(t.tool_instance, 'set_runtime_context'): diff --git a/api/app/services/draft_run_service.py b/api/app/services/draft_run_service.py index 9d04369b..d3cb12db 100644 --- a/api/app/services/draft_run_service.py +++ b/api/app/services/draft_run_service.py @@ -875,24 +875,6 @@ class AgentRunService: user_rag_memory_id) tools.extend(memory_tools) - # 4. 创建 LangChain Agent - agent = LangChainAgent( - model_name=api_key_config["model_name"], - api_key=api_key_config["api_key"], - provider=api_key_config.get("provider", "openai"), - api_base=api_key_config.get("api_base"), - is_omni=api_key_config.get("is_omni", False), - temperature=effective_params.get("temperature", 0.7), - max_tokens=effective_params.get("max_tokens", 2000), - system_prompt=system_prompt, - tools=tools, - streaming=True, - deep_thinking=effective_params.get("deep_thinking", False), - thinking_budget_tokens=effective_params.get("thinking_budget_tokens"), - json_output=effective_params.get("json_output", False), - capability=api_key_config.get("capability", []), - ) - # 5. 处理会话ID(创建或验证),新会话时写入开场白 is_new_conversation = not conversation_id opening, suggested_questions = None, None @@ -948,18 +930,28 @@ class AgentRunService: and any(f.type == FileType.DOCUMENT for f in files) ) if has_doc_with_images: - agent.system_prompt += ( - "\n\n文档中包含图片,图片位置已在文本中以 [图片 第N页 第M张图片]: URL 标记。" - "请在回答中用 Markdown 格式 ![描述](URL) 展示相关图片,做到图文并茂。" - "**规则1:图片URL必须原封不动、一字不差地复制,禁止修改、禁止省略任何字符**" - "**规则2:禁止修改URL中UUID里的任何数字和字母**" - "**规则3:直接使用 ![描述](完整URL) 格式输出**" - ) - agent.agent = create_agent( - model=agent.llm, - tools=agent._wrap_tools_with_tracking(agent.tools) if agent.tools else None, - system_prompt=agent.system_prompt + system_prompt += ( + "\n\n文档文字中包含图片位置标记如 [图片 第2页 第1张]: http://...,请在回答中用 Markdown 格式 ![图片描述](url) 展示对应图片。" ) + + # 创建 LangChain Agent + agent = LangChainAgent( + model_name=api_key_config["model_name"], + api_key=api_key_config["api_key"], + provider=api_key_config.get("provider", "openai"), + api_base=api_key_config.get("api_base"), + is_omni=api_key_config.get("is_omni", False), + temperature=effective_params.get("temperature", 0.7), + max_tokens=effective_params.get("max_tokens", 2000), + system_prompt=system_prompt, + tools=tools, + streaming=True, + deep_thinking=effective_params.get("deep_thinking", False), + thinking_budget_tokens=effective_params.get("thinking_budget_tokens"), + json_output=effective_params.get("json_output", False), + capability=api_key_config.get("capability", []), + ) + # 为需要运行时上下文的工具注入上下文 for t in tools: if hasattr(t, 'tool_instance') and hasattr(t.tool_instance, 'set_runtime_context'):