remove browser_actions from role

This commit is contained in:
lidanyang 2024-07-12 15:55:52 +08:00
parent 7508ff66cd
commit df340cb330
3 changed files with 20 additions and 18 deletions

View file

@ -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

View file

@ -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))

View file

@ -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: