Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

fix conflicts

seehi0312f4f7281e
diff --cc metagpt/prompts/di/role_zero.py
index 1920d0345,243f33604..f1de5bf1b
--- a/metagpt/prompts/di/role_zero.py
+++ b/metagpt/prompts/di/role_zero.py
@@@ -5,19 -5,10 +5,20 @@@ When presented a current task, tackle t
Pay close attention to new user message, review the conversation history, use RoleZero.reply_to_human to respond to new user requirement.
Note:
1. If you keeping encountering errors, unexpected situation, or you are not sure of proceeding, use RoleZero.ask_human to ask for help.
- 2. Carefully review your progress at the current task, if your actions so far has not fulfilled the task instruction, you should continue with current task. Otherwise, finish current task.
+ 2. Carefully review your progress at the current task, if your actions so far has not fulfilled the task instruction, you should continue with current task. Otherwise, finish current task by Plan.finish_current_task explicitly.
3. Each time you finish a task, use RoleZero.reply_to_human to report your progress.
"""
+CMD_PROMPT_EXP_PART = """
+# Current Plan
+{plan_status}
+
+# Current Task
+{current_task}
+# Instruction
+{instruction}
+"""
++# To ensure compatibility with hard-coded experience, do not add any other content between "# Example" and "# Available Commands".
CMD_PROMPT = """
# Data Structure
class Task(BaseModel):
@@@ -34,7 -25,17 +35,10 @@@
{available_commands}
Special Command: Use {{"command_name": "end"}} to do nothing or indicate completion of all requirements and the end of actions.
-# Current Plan
-{plan_status}
-
-# Current Task
-{current_task}
-
-# Example
-{example}
++# Available Task Types
++{task_type_desc}
+
-# Instruction
-{instruction}
+{cmd_prompt_exp_part}
Pay close attention to the Example provided, you can reuse the example for your current situation if it fits.
You may use any of the available commands to create a plan or update the plan. You may output mutiple commands, they will be executed sequentially.
diff --cc metagpt/roles/di/role_zero.py
index f542e098a,5f27c79ef..8411cf24b
--- a/metagpt/roles/di/role_zero.py
+++ b/metagpt/roles/di/role_zero.py
@@@ -146,42 -143,34 +147,51 @@@ class RoleZero(Role)
tool_info = json.dumps({tool.name: tool.schemas for tool in tools})
### Make Decision Dynamically ###
- prompt = self.cmd_prompt.format(
+ cmd_prompt_exp_part = CMD_PROMPT_EXP_PART.format(
plan_status=plan_status,
current_task=current_task,
+ instruction=self.instruction.strip(),
+ )
+ prompt = self.cmd_prompt.format(
- example=example, available_commands=tool_info, cmd_prompt_exp_part=cmd_prompt_exp_part
+ example=example,
+ available_commands=tool_info,
- instruction=self.instruction.strip(),
+ task_type_desc=self.task_type_desc,
++ cmd_prompt_exp_part=cmd_prompt_exp_part,
)
memory = self.rc.memory.get(self.memory_k)
- if not self.browser.is_empty_page:
- pattern = re.compile(r"Command Browser\.(\w+) executed")
- for index, msg in zip(range(len(memory), 0, -1), memory[::-1]):
- if pattern.match(msg.content):
- memory.insert(index, UserMessage(cause_by="browser", content=await self.browser.view()))
- break
+ memory = await self.parse_browser_actions(memory)
- context = self.llm.format_msg(memory + [UserMessage(content=prompt)])
- # print(*context, sep="\n" + "*" * 5 + "\n")
++
+ req = self.llm.format_msg(memory + [UserMessage(content=prompt), UserMessage(content=cmd_prompt_exp_part)])
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)
+ self.command_rsp = await self.llm_cached_aask(req=req, system_msgs=self.system_msg)
++
self.rc.memory.add(AIMessage(content=self.command_rsp))
return True
+ @exp_cache(context_builder=RoleZeroContextBuilder(), serializer=RoleZeroSerializer())
+ async def llm_cached_aask(self, *, req: list[dict], system_msgs: list[str]) -> str:
+ """Use `exp_cache` to automatically manage experiences.
+
+ The `RoleZeroContextBuilder` attempts to add experiences to `req`.
+ The `RoleZeroSerializer` extracts essential parts of `req` for the experience pool, trimming lengthy entries to retain only necessary parts.
+ """
+ # Remove the "cmd_prompt_exp_part", it is only used within the exp_cache decorator.
+ if req:
+ req.pop()
+
+ return await self.llm.aask(req, system_msgs=system_msgs)
+
+ 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")
+ for index, msg in zip(range(len(memory), 0, -1), memory[::-1]):
+ if pattern.match(msg.content):
+ memory.insert(index, UserMessage(cause_by="browser", content=await self.browser.view()))
+ break
+ return memory
+
async def _act(self) -> Message:
if self.use_fixed_sop:
return await super()._act()