From 3911f7449bdc4e9a227de5e43c8f998f2e9b4bf9 Mon Sep 17 00:00:00 2001 From: z30057876 Date: Wed, 17 Dec 2025 01:07:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Thinking=E8=BF=87=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/scheduler/executor/agent.py | 10 +------ apps/scheduler/mcp_agent/plan.py | 30 +++++++++++++++----- data/prompts/system/mcp/agent_thinking.en.md | 15 ++++++++++ data/prompts/system/mcp/agent_thinking.zh.md | 15 ++++++++++ data/prompts/system/mcp/gen_step.en.md | 10 +++---- data/prompts/system/mcp/gen_step.zh.md | 10 +++---- pyproject.toml | 2 +- 7 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 data/prompts/system/mcp/agent_thinking.en.md create mode 100644 data/prompts/system/mcp/agent_thinking.zh.md diff --git a/apps/scheduler/executor/agent.py b/apps/scheduler/executor/agent.py index e6650f8fb..499d0a6d4 100644 --- a/apps/scheduler/executor/agent.py +++ b/apps/scheduler/executor/agent.py @@ -112,14 +112,6 @@ class MCPAgentExecutor(BaseExecutor): mcp_ids = app_metadata.mcp_service for mcp_id in mcp_ids: - if not await MCPServiceManager.is_user_actived(self.task.metadata.userId, mcp_id): - _logger.warning( - "[MCPAgentExecutor] 用户 %s 未启用MCP %s", - self.task.metadata.userId, - mcp_id, - ) - continue - mcp_service = await MCPServiceManager.get_mcp_service(mcp_id) if mcp_service: self._mcp_list.append(mcp_service) @@ -422,7 +414,7 @@ class MCPAgentExecutor(BaseExecutor): step = None for _ in range(max_retry): try: - step = await self._planner.create_next_step(list(self._tool_list.values()), self.task) + step = await self._planner.create_next_step(list(self._tool_list.values()), self.task, self.llm) if step.tool_name in self._tool_list: break except Exception: diff --git a/apps/scheduler/mcp_agent/plan.py b/apps/scheduler/mcp_agent/plan.py index 6f9697161..8cbba10c0 100644 --- a/apps/scheduler/mcp_agent/plan.py +++ b/apps/scheduler/mcp_agent/plan.py @@ -57,23 +57,39 @@ class MCPPlanner(MCPBase): ) return FlowName.model_validate(result) - async def create_next_step(self, tools: list[MCPTools], task: TaskData) -> Step: + async def create_next_step(self, tools: list[MCPTools], task: TaskData, llm: LLM) -> Step: """创建下一步的执行步骤""" - template = _env.from_string(await self._load_prompt("gen_step")) - prompt = template.render(goal=self._goal, tools=tools) + history = await self.assemble_memory(task) + + # 第一步:让大模型思考分析 + thinking_template = _env.from_string(await self._load_prompt("agent_thinking")) + thinking_prompt = thinking_template.render(goal=self._goal, tools=tools) + + thinking_messages = [ + {"role": "system", "content": "You are a helpful assistant."}, + *history, + {"role": "user", "content": thinking_prompt}, + ] + + thinking_content = "" + async for chunk in llm.call(thinking_messages, streaming=False): + thinking_content += chunk.content or "" + logger.info("[MCPPlanner] 思考分析: %s", thinking_content) + + step_template = _env.from_string(await self._load_prompt("gen_step")) + step_prompt = step_template.render(goal=self._goal, tools=tools) function = deepcopy(CREATE_NEXT_STEP_FUNCTION[self._language]) function["parameters"]["properties"]["tool_name"]["enum"] = [tool.toolName for tool in tools] - history = await self.assemble_memory(task) - step = await json_generator.generate( function=function, conversation=[ {"role": "system", "content": "You are a helpful assistant."}, - *history, + {"role": "user", "content": thinking_prompt}, + {"role": "assistant", "content": thinking_content}, ], - prompt=prompt, + prompt=step_prompt, ) logger.info("[MCPPlanner] 创建下一步的执行步骤: %s", step) return Step.model_validate(step) diff --git a/data/prompts/system/mcp/agent_thinking.en.md b/data/prompts/system/mcp/agent_thinking.en.md new file mode 100644 index 000000000..f89d0cbec --- /dev/null +++ b/data/prompts/system/mcp/agent_thinking.en.md @@ -0,0 +1,15 @@ +Now let's review the step history context. My goal is: {{goal}} + +Available tools: + +{% for tool in tools %} +- {{tool.toolName}}: {{tool.description}} +{% endfor %} + +Please think step by step: + +- How far along is my goal now +- What should be done next to achieve the goal +- Which tool should be used and why + +Think and analyze first, then provide a specific plan for the next step. \ No newline at end of file diff --git a/data/prompts/system/mcp/agent_thinking.zh.md b/data/prompts/system/mcp/agent_thinking.zh.md new file mode 100644 index 000000000..2ba00aa85 --- /dev/null +++ b/data/prompts/system/mcp/agent_thinking.zh.md @@ -0,0 +1,15 @@ +现在回顾一下步骤历史上下文,我的目标是:{{goal}} + +可用的工具有: + +{% for tool in tools %} +- {{tool.toolName}}: {{tool.description}} +{% endfor %} + +请你一步一步分析思考: + +- 我的目标现在完成到什么程度了 +- 接下来应该做什么才能达成目标 +- 应该用哪个工具,为什么 + +先思考分析,然后再给出下一步的具体方案。 diff --git a/data/prompts/system/mcp/gen_step.en.md b/data/prompts/system/mcp/gen_step.en.md index a511d8327..ec2b1eb19 100644 --- a/data/prompts/system/mcp/gen_step.en.md +++ b/data/prompts/system/mcp/gen_step.en.md @@ -2,15 +2,15 @@ ## Role -You are a professional workflow planning assistant who can intelligently plan the next execution step based on user goals and conversation history. +You are a professional workflow planning assistant who can intelligently plan the next execution step based on user goals and thinking analysis. ## Task Objective -Analyze the conversation history and user goal, then call the `create_next_step` function to plan the next execution step. +Based on the previous thinking analysis and user goal, call the `create_next_step` function to plan the next execution step. ## Planning Requirements -1. **Accuracy**: Plan based on the results of executed steps in the conversation history +1. **Accuracy**: Plan based on the conclusions from the thinking analysis 2. **Relevance**: Select the most appropriate tool for the current stage 3. **Clarity**: Step description should be concise and clear, explaining specific operations 4. **Completeness**: Ensure each step is independent and complete, directly executable @@ -52,7 +52,7 @@ Calling Specifications: Usage Example: -- Scenario: User goal is "Analyze MySQL database performance", conversation history shows database connection established, need to perform performance analysis +- Scenario: User goal is "Analyze MySQL database performance", thinking analysis shows database connection established, need to perform performance analysis - Next step should be: Call performance analysis tool to retrieve database performance metrics {% raw %}{% if use_xml_format %}{% endraw %} @@ -72,4 +72,4 @@ Usage Example: **User Goal**: {{goal}} -Now begin responding to user instructions, call the `create_next_step` function to plan the next step based on the results of executed steps in the conversation history above. +Now based on the thinking and analysis above, call the `create_next_step` function to plan the next step. diff --git a/data/prompts/system/mcp/gen_step.zh.md b/data/prompts/system/mcp/gen_step.zh.md index 9848dddd0..ba235d914 100644 --- a/data/prompts/system/mcp/gen_step.zh.md +++ b/data/prompts/system/mcp/gen_step.zh.md @@ -2,15 +2,15 @@ ## 角色 -你是一个专业的工作流程规划助手,能够根据用户目标和对话历史,智能规划下一个执行步骤。 +你是一个专业的工作流程规划助手,能够根据用户目标和思考分析,智能规划下一个执行步骤。 ## 任务目标 -分析对话历史和用户目标,然后调用 `create_next_step` 函数来规划下一个执行步骤。 +基于前面的思考分析和用户目标,调用 `create_next_step` 函数来规划下一个执行步骤。 ## 规划要求 -1. **准确性**:基于对话历史中已执行步骤的结果进行规划 +1. **准确性**:基于思考分析中的结论进行规划 2. **针对性**:选择最适合当前阶段的工具 3. **清晰性**:步骤描述简洁明确,说明具体操作内容 4. **完整性**:确保每个步骤独立完整,可直接执行 @@ -52,7 +52,7 @@ 用法示例: -- 场景:用户目标是"分析MySQL数据库性能",对话历史显示已连接到数据库,需要进行性能分析 +- 场景:用户目标是"分析MySQL数据库性能",思考分析显示已连接到数据库,需要进行性能分析 - 下一步应为:调用性能分析工具,获取数据库的性能指标 {% raw %}{% if use_xml_format %}{% endraw %} @@ -72,4 +72,4 @@ **用户目标**: {{goal}} -现在开始响应用户指令,根据上方对话历史中已执行步骤的结果,调用 `create_next_step` 函数规划下一步。 +现在基于上面的思考分析结果,调用 `create_next_step` 函数规划下一步。 diff --git a/pyproject.toml b/pyproject.toml index b5709e9f3..794119ca8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,7 @@ dependencies = [ "jsonschema==4.21.1", "mcp==1.17.0", "minio==7.2.15", - "ollama==0.5.3", + "ollama==0.6.1", "openai==2.3.0", "pandas==2.2.2", "pgvector==0.4.1", -- Gitee