Merge branch 'mgx_ops' into 'data_analyst_ldy'

# Conflicts:
#   metagpt/roles/di/data_analyst.py
This commit is contained in:
李丹阳 2024-07-26 06:12:20 +00:00
commit c1fd2ae109
22 changed files with 277 additions and 99 deletions

View file

@ -146,6 +146,7 @@ class RoleZero(Role):
tool_info = json.dumps({tool.name: tool.schemas for tool in tools})
### Make Decision Dynamically ###
memory = self.rc.memory.get(self.memory_k)
instruction = self.instruction.strip()
prompt = self.cmd_prompt.format(
example=example,
@ -154,10 +155,9 @@ class RoleZero(Role):
plan_status=plan_status,
current_task=current_task,
instruction=instruction,
latest_observation=memory[-1].content,
)
memory = self.rc.memory.get(self.memory_k)
memory = await self.parse_browser_actions(memory)
req = self.llm.format_msg(memory + [UserMessage(content=prompt)])
async with ThoughtReporter(enable_llm_stream=True) as reporter:
await reporter.async_report({"type": "react"})
@ -169,7 +169,6 @@ class RoleZero(Role):
self.command_rsp = await self.llm_cached_aask(req=req, system_msgs=self.system_msg, state_data=state_data)
self.rc.memory.add(AIMessage(content=self.command_rsp))
return True
@exp_cache(context_builder=RoleZeroContextBuilder(), serializer=RoleZeroSerializer())
@ -236,20 +235,21 @@ class RoleZero(Role):
return rsp # return output from the last action
async def _quick_think(self) -> Message:
msg = self.rc.news[-1]
rsp_msg = None
if msg.cause_by != any_to_str(UserRequirement):
if self.rc.news[-1].cause_by != any_to_str(UserRequirement):
# Agents themselves won't generate quick questions, use this rule to reduce extra llm calls
return rsp_msg
context = self.llm.format_msg(self.get_memories(k=4) + [UserMessage(content=QUICK_THINK_PROMPT)])
async with ThoughtReporter(enable_llm_stream=True) as reporter:
await reporter.async_report({"type": "quick"})
rsp = await self.llm.aask(context)
# routing
memory = self.get_memories(k=4)
context = self.llm.format_msg(memory + [UserMessage(content=QUICK_THINK_PROMPT)])
rsp = await self.llm.aask(context)
pattern = r"#YES#,? ?"
if re.search(pattern, rsp):
answer = re.sub(pattern, "", rsp).strip()
if "yes" in rsp.lower():
# llm call with the original context
async with ThoughtReporter(enable_llm_stream=True) as reporter:
await reporter.async_report({"type": "quick"})
answer = await self.llm.aask(self.llm.format_msg(memory))
self.rc.memory.add(AIMessage(content=answer, cause_by=RunCommand))
await self.reply_to_human(content=answer)
rsp_msg = AIMessage(
@ -339,7 +339,7 @@ class RoleZero(Role):
elif cmd["command_name"] == "end":
self._set_state(-1)
command_output = "Everything Done"
command_output = ""
return command_output

View file

@ -9,7 +9,7 @@ from metagpt.prompts.di.swe_agent import (
SWE_AGENT_SYSTEM_TEMPLATE,
)
from metagpt.roles.di.role_zero import RoleZero
from metagpt.tools.libs.git import git_create_pull, git_push
from metagpt.tools.libs.git import git_create_pull
from metagpt.tools.libs.terminal import Bash
@ -23,7 +23,6 @@ class SWEAgent(RoleZero):
"Bash",
"Browser:goto,scroll",
"RoleZero",
"git_push",
"git_create_pull",
]
terminal: Bash = Field(default_factory=Bash, exclude=True)
@ -42,7 +41,6 @@ class SWEAgent(RoleZero):
self.tool_execution_map.update(
{
"Bash.run": self.terminal.run,
"git_push": git_push,
"git_create_pull": git_create_pull,
}
)

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from metagpt.actions.di.run_command import RunCommand
from metagpt.prompts.di.team_leader import (
FINISH_CURRENT_TASK_CMD,
QUICK_THINK_SYSTEM_PROMPT,
SYSTEM_PROMPT,
TL_INSTRUCTION,
)
@ -16,6 +17,7 @@ from metagpt.tools.tool_registry import register_tool
class TeamLeader(RoleZero):
name: str = "Mike"
profile: str = "Team Leader"
goal: str = "Manage a team to assist users"
system_msg: list[str] = [SYSTEM_PROMPT]
# TeamLeader only reacts once each time, but may encounter errors or need to ask human, thus allowing 2 more turns
@ -33,16 +35,26 @@ class TeamLeader(RoleZero):
}
)
def set_instruction(self):
def _get_team_info(self) -> str:
if not self.rc.env:
return ""
team_info = ""
for role in self.rc.env.roles.values():
# if role.profile == "Team Leader":
# continue
team_info += f"{role.name}: {role.profile}, {role.goal}\n"
self.instruction = TL_INSTRUCTION.format(team_info=team_info)
return team_info
async def _quick_think(self) -> Message:
# insert team info for quick question
self.llm.system_prompt = QUICK_THINK_SYSTEM_PROMPT.format(
role_info=super()._get_prefix(),
team_info=self._get_team_info(),
)
return await super()._quick_think()
async def _think(self) -> bool:
self.set_instruction()
self.instruction = TL_INSTRUCTION.format(team_info=self._get_team_info())
return await super()._think()
def publish_message(self, msg: Message, send_to="no one"):

View file

@ -33,9 +33,9 @@ class ProductManager(RoleZero):
constraints: str = "utilize the same language as the user requirements for seamless communication"
todo_action: str = any_to_name(WritePRD)
instruction: str = """Use WritePRD tool to write PRD if a PRD is required; Use `Pic2Txt` tool to write out an intact textual user requirements if an intact textual user requiremnt is required given some images alongside the contextual textual descriptions;"""
instruction: str = """Use WritePRD tool to write PRD if a PRD is required, users may asks for a software without mentioning PRD, but you should output the PRD of that software; Use `Pic2Txt` tool to write out an intact textual user requirements if an intact textual user requiremnt is required given some images alongside the contextual textual descriptions"""
max_react_loop: int = 1 # FIXME: Read and edit files requires more steps, consider later
tools: list[str] = ["Editor:write,read,write_content", "RoleZero", "WritePRD", Pic2Txt.__name__]
tools: list[str] = ["RoleZero", "WritePRD", Pic2Txt.__name__]
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)