From 1c215d3370413755dc5325acfa6234817676100c Mon Sep 17 00:00:00 2001 From: yzlin Date: Sun, 28 Apr 2024 18:07:59 +0800 Subject: [PATCH] fix critical bug: add available commands --- metagpt/prompts/di/team_leader.py | 10 ---------- metagpt/roles/di/team_leader.py | 23 +++++++---------------- metagpt/strategy/thinking_command.py | 7 +++++++ 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/metagpt/prompts/di/team_leader.py b/metagpt/prompts/di/team_leader.py index 87e8a1336..dd9c46618 100644 --- a/metagpt/prompts/di/team_leader.py +++ b/metagpt/prompts/di/team_leader.py @@ -1,13 +1,3 @@ -from metagpt.strategy.thinking_command import Command - - -def prepare_command_prompt(commands: list[Command]) -> str: - command_prompt = "" - for i, command in enumerate(commands): - command_prompt += f"{i+1}. {command.value.signature}:\n{command.value.desc}\n\n" - return command_prompt - - CMD_PROMPT = """ # Data Structure class Task(BaseModel): diff --git a/metagpt/roles/di/team_leader.py b/metagpt/roles/di/team_leader.py index 1ea873b56..b3f0ae78a 100644 --- a/metagpt/roles/di/team_leader.py +++ b/metagpt/roles/di/team_leader.py @@ -6,16 +6,12 @@ from pydantic import model_validator from metagpt.actions.di.run_command import RunCommand from metagpt.environment.mgx.mgx_env import MGXEnv -from metagpt.prompts.di.team_leader import ( - CMD_PROMPT, - FINISH_CURRENT_TASK_CMD, - prepare_command_prompt, -) +from metagpt.prompts.di.team_leader import CMD_PROMPT, FINISH_CURRENT_TASK_CMD from metagpt.roles import Role from metagpt.schema import Message, Task, TaskResult from metagpt.strategy.experience_retriever import SimpleExpRetriever from metagpt.strategy.planner import Planner -from metagpt.strategy.thinking_command import Command +from metagpt.strategy.thinking_command import Command, prepare_command_prompt from metagpt.utils.common import CodeParser @@ -23,7 +19,7 @@ class TeamLeader(Role): name: str = "Tim" profile: str = "Team Leader" task_result: TaskResult = None - commands: list[Command] = [ + available_commands: list[Command] = [ Command.APPEND_TASK, Command.RESET_TASK, Command.REPLACE_TASK, @@ -33,6 +29,7 @@ class TeamLeader(Role): Command.REPLY_TO_HUMAN, Command.PASS, ] + commands: list[dict] = [] # issued commands to be executed @model_validator(mode="after") def set_plan(self) -> "TeamLeader": @@ -69,13 +66,8 @@ class TeamLeader(Role): if self.planner.plan.is_plan_finished(): self._set_state(-1) - def get_memory(self, k=10) -> list[Message]: - """A wrapper with default value""" - return self.rc.memory.get(k=k) - async def _think(self) -> bool: """Useful in 'react' mode. Use LLM to decide whether and what to do next.""" - self.commands = [] if not self.planner.plan.goal: user_requirement = self.get_memories()[-1].content @@ -96,13 +88,12 @@ class TeamLeader(Role): plan_status=plan_status, team_info=team_info, example=example, - available_commands=prepare_command_prompt(self.commands), + available_commands=prepare_command_prompt(self.available_commands), ) - context = self.llm.format_msg(self.get_memory() + [Message(content=prompt, role="user")]) + context = self.llm.format_msg(self.get_memories(k=10) + [Message(content=prompt, role="user")]) rsp = await self.llm.aask(context) - rsp_dict = json.loads(CodeParser.parse_code(block=None, text=rsp)) - self.commands.extend(rsp_dict) + self.commands = json.loads(CodeParser.parse_code(block=None, text=rsp)) self.rc.memory.add(Message(content=rsp, role="assistant")) return True diff --git a/metagpt/strategy/thinking_command.py b/metagpt/strategy/thinking_command.py index 7b48cae51..53b206da8 100644 --- a/metagpt/strategy/thinking_command.py +++ b/metagpt/strategy/thinking_command.py @@ -59,3 +59,10 @@ class Command(Enum): @property def cmd_name(self): return self.value.name + + +def prepare_command_prompt(commands: list[Command]) -> str: + command_prompt = "" + for i, command in enumerate(commands): + command_prompt += f"{i+1}. {command.value.signature}:\n{command.value.desc}\n\n" + return command_prompt