Skip to content

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

remove browser_actions from role

lidanyangdf340cb33084
diff --git a/metagpt/actions/di/write_analysis_code.py b/metagpt/actions/di/write_analysis_code.py
index d0c4c016e..06e0ba4e2 100644
--- a/metagpt/actions/di/write_analysis_code.py
+++ b/metagpt/actions/di/write_analysis_code.py
@@ -42,7 +42,6 @@ class WriteAnalysisCode(Action):
tool_info: str = "",
working_memory: list[Message] = None,
use_reflection: bool = False,
- browser_actions: list[dict] = None,
**kwargs,
) -> str:
structual_prompt = STRUCTUAL_PROMPT.format(
@@ -51,11 +50,12 @@ class WriteAnalysisCode(Action):
tool_info=tool_info,
)
message = [Message(content=structual_prompt, role="user")]
+ browser_actions = [msg for msg in working_memory if msg.cause_by == "browser"]
if browser_actions:
browser_prompt = BROWSER_INFO.format(browser_actions=browser_actions)
message = [Message(content=browser_prompt, role="user")] + message
- working_memory = working_memory or []
+ working_memory = [msg for msg in working_memory if msg.cause_by != "browser"] if use_reflection else []
context = self.llm.format_msg(message + working_memory)
# LLM call
diff --git a/metagpt/roles/di/data_analyst.py b/metagpt/roles/di/data_analyst.py
index e6b734417..066b82874 100644
--- a/metagpt/roles/di/data_analyst.py
+++ b/metagpt/roles/di/data_analyst.py
@@ -1,7 +1,7 @@
from __future__ import annotations
+import json
import re
-from typing import List
from pydantic import Field, model_validator
@@ -44,7 +44,8 @@ class DataAnalyst(RoleZero):
"DataAnalyst.write_and_exec_code": self.write_and_exec_code,
})
- def parse_browser_actions(self, memory: List[Message]):
+ async def parse_browser_actions(self):
+ memory = await super().parse_browser_actions()
for index, msg in enumerate(memory):
if msg.cause_by == "browser":
browser_url = re.search('URL: (.*?)\\n', msg.content).group(1)
@@ -53,7 +54,10 @@ class DataAnalyst(RoleZero):
'command': pattern.match(memory[index - 1].content).group(1),
'current url': browser_url
}
- self.browser_actions.append(browser_action)
+ self.rc.working_memory.add(
+ Message(content=json.dumps(browser_action), role="user", cause_by="browser")
+ )
+ return memory
async def write_and_exec_code(self):
"""Write a code block for current task and execute it in an interactive notebook environment."""
@@ -81,9 +85,8 @@ class DataAnalyst(RoleZero):
user_requirement=self.planner.plan.goal,
plan_status=plan_status,
tool_info=tool_info,
- working_memory=self.rc.working_memory.get() if use_reflection else None,
+ working_memory=self.rc.working_memory.get(),
use_reflection=use_reflection,
- browser_actions=self.browser_actions
)
self.rc.working_memory.add(Message(content=code, role="assistant", cause_by=WriteAnalysisCode))
diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.py
index fb89114c8..ed617eb81 100644
--- a/metagpt/roles/di/role_zero.py
+++ b/metagpt/roles/di/role_zero.py
@@ -54,7 +54,6 @@ class RoleZero(Role):
# Equipped with three basic tools by default for optional use
editor: Editor = Editor()
browser: Browser = Browser()
- browser_actions: list[dict] = [] # store the browser history actions
# terminal: Terminal = Terminal() # FIXME: TypeError: cannot pickle '_thread.lock' object
# Experience
@@ -151,14 +150,7 @@ class RoleZero(Role):
instruction=self.instruction.strip(),
task_type_desc=self.task_type_desc,
)
- 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
- self.parse_browser_actions(memory=memory)
+ memory = await self.parse_browser_actions()
context = self.llm.format_msg(memory + [UserMessage(content=prompt)])
# print(*context, sep="\n" + "*" * 5 + "\n")
async with ThoughtReporter(enable_llm_stream=True):
@@ -167,8 +159,15 @@ class RoleZero(Role):
return True
- def parse_browser_actions(self, memory: List[Message]):
- pass
+ async def parse_browser_actions(self):
+ 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
+ return memory
async def _act(self) -> Message:
if self.use_fixed_sop: