From 56efc6d155a89b8407cdbd59181451bf2fa6abc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BC=9F=E9=9F=AC?= Date: Thu, 18 Jul 2024 14:43:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9role=5Fzore=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/roles/di/role_zero.py | 36 ++++++++++++------------ metagpt/strategy/experience_retriever.py | 4 +-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.py index d4bba89de..23755d3e8 100644 --- a/metagpt/roles/di/role_zero.py +++ b/metagpt/roles/di/role_zero.py @@ -10,9 +10,7 @@ from pydantic import model_validator from metagpt.actions import Action, UserRequirement from metagpt.actions.di.run_command import RunCommand -from metagpt.exp_pool import exp_cache -from metagpt.exp_pool.context_builders import RoleZeroContextBuilder -from metagpt.exp_pool.serializers import RoleZeroSerializer +from metagpt.const import DEFAULT_WORKSPACE_ROOT from metagpt.logs import logger from metagpt.prompts.di.role_zero import ( CMD_PROMPT, @@ -31,7 +29,6 @@ from metagpt.tools.tool_registry import register_tool from metagpt.utils.common import CodeParser, any_to_str from metagpt.utils.repair_llm_raw_output import RepairType, repair_llm_raw_output from metagpt.utils.report import ThoughtReporter -from metagpt.const import DEFAULT_WORKSPACE_ROOT @register_tool(include_functions=["ask_human", "reply_to_human"]) @@ -164,22 +161,24 @@ class RoleZero(Role): async with ThoughtReporter(enable_llm_stream=True) as reporter: await reporter.async_report({"type": "react"}) self.command_rsp = await self.llm.aask(context, system_msgs=self.system_msg) - try : + # check and repair + try: commands = CodeParser.parse_code(block=None, lang="json", text=self.command_rsp) commands = json.loads(repair_llm_raw_output(output=commands, req_keys=[None], repair_type=RepairType.JSON)) - except : - logger.warning('Trying to repair json string with repair tool...') + except: + logger.warning("Trying to repair json string with repair tool...") commands = CodeParser.parse_code(block=None, lang="json", text=self.command_rsp).strip() - if commands.endswith(']') and not commands.startswith('['): - commands = '['+ commands - self.command_rsp = f"```json\n{self.commands}\n```" + if commands.endswith("]") and not commands.startswith("["): + commands = "[" + commands + self.command_rsp = f"```json\n{self.command_rsp}\n```" print(self.command_rsp) - self.rc.memory.add(AIMessage(content=self.command_rsp)) + self.rc.memory.add(AIMessage(content=self.command_rsp)) return True - - async def add_editor_root_directory(self,memory) -> List[Message]: - memory.append(UserMessage(cause_by="editory", content=f'Root directory is {DEFAULT_WORKSPACE_ROOT}')) + + async def add_editor_root_directory(self, memory) -> List[Message]: + memory.append(UserMessage(cause_by="editory", content=f"Root directory is {DEFAULT_WORKSPACE_ROOT}")) return memory + async def parse_browser_actions(self, memory: List[Message]) -> List[Message]: if not self.browser.is_empty_page: pattern = re.compile(r"Command Browser\.(\w+) executed") @@ -257,6 +256,7 @@ class RoleZero(Role): ) return rsp_msg + async def _parse_commands(self) -> Tuple[List[Dict], bool]: """Retrieves commands from the Large Language Model (LLM). @@ -294,7 +294,7 @@ class RoleZero(Role): # handle special command first if self._is_special_command(cmd): special_command_output = await self._run_special_command(cmd) - outputs.append(output+':'+special_command_output) + outputs.append(output + ":" + special_command_output) continue # run command as specified by tool_execute_map if cmd["command_name"] in self.tool_execution_map: @@ -318,10 +318,10 @@ class RoleZero(Role): outputs = "\n\n".join(outputs) return outputs - def _is_special_command(self,cmd) -> bool: - + + def _is_special_command(self, cmd) -> bool: return cmd["command_name"] in self.special_tool_commands - + async def _run_special_command(self, cmd) -> str: """command requiring special check or parsing""" command_output = "" diff --git a/metagpt/strategy/experience_retriever.py b/metagpt/strategy/experience_retriever.py index ee034cb16..bb1565f66 100644 --- a/metagpt/strategy/experience_retriever.py +++ b/metagpt/strategy/experience_retriever.py @@ -520,7 +520,7 @@ Explanation: The requirement is about software development. Assign each tasks to { "command_name": "RoleZero.reply_to_human", "args": { - "content": "I have assigned the tasks to the team members. Alice will create the PRD, Bob will design the software architecture, Eve will break down the architecture into tasks, Alex will implement the core game logic, and Edward will write comprehensive tests. The team will work on the project accordingly", + "content": "I have assigned the tasks to the team members. Alice will create the PRD, Bob will design the software architecture, Eve will break down the architecture into tasks, Alex will implement the core game logic, and Edward will write comprehensive tests. The team will work on the project accordingly" } }, { @@ -602,7 +602,7 @@ Explanation: The user is asking for a general update on the project status. Give { "command_name": "RoleZero.reply_to_human", "args": { - "content": "The team is currently working on ... We have completed ...", + "content": "The team is currently working on ... We have completed ..." } }, {