diff --git a/apps/scheduler/executor/agent.py b/apps/scheduler/executor/agent.py index 4114d76d1952df0a3462ff0d503b07c57ff989a1..c879339038f146c0c78a58a543749b62ef7ffbd2 100644 --- a/apps/scheduler/executor/agent.py +++ b/apps/scheduler/executor/agent.py @@ -34,7 +34,7 @@ logger = logging.getLogger(__name__) class MCPAgentExecutor(BaseExecutor): """MCP Agent执行器""" - max_steps: int = Field(default=20, description="最大步数") + max_steps: int = Field(default=40, description="最大步数") servers_id: list[str] = Field(description="MCP server id") agent_id: str = Field(default="", description="Agent ID") agent_description: str = Field(default="", description="Agent描述") @@ -176,6 +176,7 @@ class MCPAgentExecutor(BaseExecutor): self.task.state.step_status = StepStatus.ERROR self.task.state.error_message = str(e) return + logger.error(f"当前工具名称: {mcp_tool.name}, 输出参数: {output_params}") if output_params.isError: err = "" for output in output_params.content: @@ -465,9 +466,9 @@ class MCPAgentExecutor(BaseExecutor): # 初始化状态 try: self.task.state.flow_id = str(uuid.uuid4()) - self.task.state.flow_name = await MCPPlanner.get_flow_name( + self.task.state.flow_name = (await MCPPlanner.get_flow_name( self.task.runtime.question, self.resoning_llm, self.task.language - ) + )).flow_name await TaskManager.save_task(self.task.id, self.task) await self.get_next_step() except Exception as e: diff --git a/apps/scheduler/mcp_agent/base.py b/apps/scheduler/mcp_agent/base.py index 6cd60a9341f8356feb2321507471a1dce45e6a48..f69d71ab16d29e23dd047d9f8e7847ec9043ca86 100644 --- a/apps/scheduler/mcp_agent/base.py +++ b/apps/scheduler/mcp_agent/base.py @@ -16,8 +16,8 @@ class MCPBase: """获取推理结果""" # 调用推理大模型 message = [ - {"role": "system", "content": "You are a helpful assistant."}, - {"role": "user", "content": prompt}, + {"role": "system", "content": prompt}, + {"role": "user", "content": "Please provide a JSON response based on the above information and schema."}, ] result = "" async for chunk in resoning_llm.call( @@ -50,10 +50,10 @@ class MCPBase: flag = False if not flag: json_generator = JsonGenerator( - "请提取下面内容中的json\n\n", + "Please provide a JSON response based on the above information and schema.\n\n", [ {"role": "system", "content": "You are a helpful assistant."}, - {"role": "user", "content": "请提取下面内容中的json\n\n"+result}, + {"role": "user", "content": result}, ], schema, ) diff --git a/apps/scheduler/mcp_agent/plan.py b/apps/scheduler/mcp_agent/plan.py index 31b3c7c059118be101c617cd7e92ee76733dbb78..faa9720db5630a88f7aaad00c60844dff8515218 100644 --- a/apps/scheduler/mcp_agent/plan.py +++ b/apps/scheduler/mcp_agent/plan.py @@ -29,6 +29,7 @@ from apps.schemas.enum_var import LanguageType from apps.scheduler.slot.slot import Slot from apps.schemas.mcp import ( GoalEvaluationResult, + FlowName, IsParamError, MCPPlan, MCPTool, @@ -54,8 +55,9 @@ class MCPPlanner(MCPBase): @staticmethod async def evaluate_goal( - goal: str, tool_list: list[MCPTool], resoning_llm: ReasoningLLM = ReasoningLLM(), language: LanguageType = LanguageType.CHINESE, - ) -> GoalEvaluationResult: + goal: str, tool_list: list[MCPTool], + resoning_llm: ReasoningLLM = ReasoningLLM(), + language: LanguageType = LanguageType.CHINESE,) -> GoalEvaluationResult: """评估用户目标的可行性""" # 获取推理结果 result = await MCPPlanner._get_reasoning_evaluation(goal, tool_list, resoning_llm, language) @@ -65,8 +67,9 @@ class MCPPlanner(MCPBase): @staticmethod async def _get_reasoning_evaluation( - goal, tool_list: list[MCPTool], resoning_llm: ReasoningLLM = ReasoningLLM(), language: LanguageType = LanguageType.CHINESE, - ) -> str: + goal, tool_list: list[MCPTool], + resoning_llm: ReasoningLLM = ReasoningLLM(), + language: LanguageType = LanguageType.CHINESE,) -> str: """获取推理大模型的评估结果""" template = _env.from_string(EVALUATE_GOAL[language]) prompt = template.render( @@ -87,10 +90,13 @@ class MCPPlanner(MCPBase): user_goal: str, resoning_llm: ReasoningLLM = ReasoningLLM(), language: LanguageType = LanguageType.CHINESE, - ) -> str: + ) -> FlowName: """获取当前流程的名称""" - return await MCPPlanner._get_reasoning_flow_name(user_goal, resoning_llm, language) + result = await MCPPlanner._get_reasoning_flow_name(user_goal, resoning_llm, language) + result = await MCPPlanner._parse_result(result, FlowName.model_json_schema()) + # 使用FlowName模型解析结果 + return FlowName.model_validate(result) @staticmethod async def _get_reasoning_flow_name( @@ -217,9 +223,6 @@ class MCPPlanner(MCPBase): # 使用Step模型解析结果 step = Step.model_validate(step) - if len(step.description) > 15: - # 限制步骤描述长度为15个字符 - step.description = step.description[:15]+'...' return step @staticmethod diff --git a/apps/scheduler/mcp_agent/prompt.py b/apps/scheduler/mcp_agent/prompt.py index 59abd383858e386f32fe7d0ec214cfbbf39b0996..086bf4dc0c8690d227eefc2ecb670907c785c8a3 100644 --- a/apps/scheduler/mcp_agent/prompt.py +++ b/apps/scheduler/mcp_agent/prompt.py @@ -365,7 +365,9 @@ GENERATE_FLOW_NAME: dict[LanguageType, str] = { # 目标 我需要扫描当前mysql数据库,分析性能瓶颈, 并调优 # 输出 - 扫描MySQL数据库并分析性能瓶颈,进行调优 + { + "flow_name": "扫描MySQL数据库并分析性能瓶颈,进行调优" + } # 现在开始生成流程名称: # 目标 {{goal}} @@ -386,7 +388,9 @@ GENERATE_FLOW_NAME: dict[LanguageType, str] = { # Goal I need to scan the current MySQL database, analyze performance bottlenecks, and optimize it. # Output - Scan MySQL database and analyze performance bottlenecks, and optimize it. + { + "flow_name": "Scan MySQL database and analyze performance bottlenecks, and optimize it." + } # Now start generating the flow name: # Goal {{goal}} diff --git a/apps/schemas/mcp.py b/apps/schemas/mcp.py index 676e9c7d101bd0e942b1ef5edf793bc537550db7..7506e3326851063d45e7aa89eafa7e01e3c597b6 100644 --- a/apps/schemas/mcp.py +++ b/apps/schemas/mcp.py @@ -116,6 +116,12 @@ class GoalEvaluationResult(BaseModel): reason: str = Field(description="评估原因") +class FlowName(BaseModel): + """MCP 流程名称""" + + flow_name: str = Field(description="MCP 流程名称", default="") + + class RestartStepIndex(BaseModel): """MCP重新规划的步骤索引""" diff --git a/perf.data b/perf.data deleted file mode 100644 index f319e212c75a402bbf0ff2e488552bad93ff5b8b..0000000000000000000000000000000000000000 Binary files a/perf.data and /dev/null differ