Merge branch 'fix_duplicate' into 'mgx_ops'

Fix duplicate

See merge request pub/MetaGPT!259
This commit is contained in:
林义章 2024-07-29 06:04:42 +00:00
commit ff205a3d12
7 changed files with 75 additions and 12 deletions

View file

@ -31,7 +31,7 @@ class DataAnalyst(RoleZero):
task_type_desc: str = TASK_TYPE_DESC
tools: list[str] = ["Plan", "DataAnalyst", "RoleZero", "Browser"]
custom_tools: list[str] = ["machine learning", "web scraping", "Terminal"]
custom_tools: list[str] = ["web scraping", "Terminal"]
custom_tool_recommender: ToolRecommender = None
experience_retriever: ExpRetriever = KeywordExpRetriever()
@ -72,6 +72,9 @@ class DataAnalyst(RoleZero):
Args:
instruction: The specific task description for which the code needs to be written.
"""
if self.planner.plan:
logger.info(f"Current task {self.planner.plan.current_task}")
counter = 0
success = False
await self.execute_code.init_code()

View file

@ -18,7 +18,9 @@ from metagpt.prompts.di.role_zero import (
CMD_PROMPT,
JSON_REPAIR_PROMPT,
QUICK_THINK_PROMPT,
REGENERATE_PROMPT,
ROLE_INSTRUCTION,
THOUGHT_GUIDANCE,
)
from metagpt.roles import Role
from metagpt.schema import AIMessage, Message, UserMessage
@ -155,6 +157,7 @@ class RoleZero(Role):
plan_status=plan_status,
current_task=current_task,
instruction=instruction,
thought_guidance=THOUGHT_GUIDANCE,
latest_observation=memory[-1].content,
)
memory = await self.parse_browser_actions(memory)
@ -168,6 +171,8 @@ class RoleZero(Role):
)
self.command_rsp = await self.llm_cached_aask(req=req, system_msgs=self.system_msg, state_data=state_data)
self.command_rsp = await self._check_duplicates(req, self.command_rsp)
self.rc.memory.add(AIMessage(content=self.command_rsp))
return True
@ -260,6 +265,19 @@ class RoleZero(Role):
return rsp_msg
async def _check_duplicates(self, req: list[dict], command_rsp: str):
past_rsp = [mem.content for mem in self.rc.memory.get(self.memory_k)]
if command_rsp in past_rsp:
# Normal response with thought contents are highly unlikely to reproduce
# If an identical response is detected, it is a bad response, mostly due to LLM repeating generated content
# In this case, ask human for help and regenerate
# TODO: switch to llm_cached_aask
logger.warning(f"Duplicate response detected: {command_rsp}")
regenerate_req = req + [UserMessage(content=REGENERATE_PROMPT)]
regenerate_req = self.llm.format_msg(regenerate_req)
command_rsp = await self.llm.aask(regenerate_req)
return command_rsp
async def _parse_commands(self) -> Tuple[List[Dict], bool]:
"""Retrieves commands from the Large Language Model (LLM).

View file

@ -72,6 +72,9 @@ class TeamLeader(RoleZero):
Publish a message to a team member, use member name to fill send_to args. You may copy the full original content or add additional information from upstream. This will make team members start their work.
DONT omit any necessary info such as path, link, environment, programming language, framework, requirement, constraint from original content to team members because you are their sole info source.
"""
self._set_state(-1) # each time publishing a message, pause to wait for the response
if send_to == self.name:
return # Avoid sending message to self
# Specify the outer send_to to overwrite the default "no one" value. Use UserMessage because message from self is like a user request for others.
self.publish_message(
UserMessage(content=content, sent_from=self.name, send_to=send_to, cause_by=RunCommand), send_to=send_to