refactor: cause_by

This commit is contained in:
莘权 马 2023-11-10 16:48:34 +08:00
parent efe6ead27c
commit 44aa1dd563
6 changed files with 22 additions and 25 deletions

View file

@ -15,6 +15,7 @@ from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.software_company import SoftwareCompany
from metagpt.utils.common import any_to_str
class ShoutOut(Action):
@ -101,7 +102,9 @@ class Biden(Role):
await super()._observe()
# accept the very first human instruction (the debate topic) or messages sent (from opponent) to self,
# disregard own messages from the last round
self._rc.news = [msg for msg in self._rc.news if msg.cause_by == BossRequirement or msg.send_to == {self.name}]
self._rc.news = [
msg for msg in self._rc.news if msg.cause_by == any_to_str(BossRequirement) or msg.send_to == {self.name}
]
return len(self._rc.news)
async def _act(self) -> Message:

View file

@ -141,13 +141,13 @@ class Engineer(Role):
logger.info(todo)
logger.info(code_rsp)
# self.write_file(todo, code)
msg = Message(content=code_rsp, role=self.profile, cause_by=any_to_str(self._rc.todo))
msg = Message(content=code_rsp, role=self.profile, cause_by=self._rc.todo)
self._rc.memory.add(msg)
self.publish_message(msg)
del self.todos[0]
logger.info(f"Done {self.get_workspace()} generating.")
msg = Message(content="all done.", role=self.profile, cause_by=any_to_str(self._rc.todo))
msg = Message(content="all done.", role=self.profile, cause_by=self._rc.todo)
return msg
async def _act_sp(self) -> Message:
@ -158,7 +158,7 @@ class Engineer(Role):
# logger.info(code_rsp)
# code = self.parse_code(code_rsp)
file_path = self.write_file(todo, code)
msg = Message(content=code, role=self.profile, cause_by=any_to_str(self._rc.todo))
msg = Message(content=code, role=self.profile, cause_by=self._rc.todo)
self._rc.memory.add(msg)
self.publish_message(msg)
@ -169,7 +169,7 @@ class Engineer(Role):
msg = Message(
content=MSG_SEP.join(code_msg_all),
role=self.profile,
cause_by=any_to_str(self._rc.todo),
cause_by=self._rc.todo,
send_to="Edward",
)
return msg
@ -211,7 +211,7 @@ class Engineer(Role):
msg = Message(
content=MSG_SEP.join(code_msg_all),
role=self.profile,
cause_by=any_to_str(self._rc.todo),
cause_by=self._rc.todo,
send_to="Edward",
)
return msg

View file

@ -22,7 +22,7 @@ from metagpt.const import WORKSPACE_ROOT
from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.utils.common import CodeParser, any_to_str, any_to_str_set, parse_recipient
from metagpt.utils.common import CodeParser, any_to_str_set, parse_recipient
from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP
@ -99,7 +99,7 @@ class QaEngineer(Role):
msg = Message(
content=str(file_info),
role=self.profile,
cause_by=any_to_str(WriteTest),
cause_by=WriteTest,
sent_from=self.profile,
send_to=self.profile,
)
@ -133,9 +133,7 @@ class QaEngineer(Role):
recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself
content = str(file_info) + FILENAME_CODE_SEP + result_msg
msg = Message(
content=content, role=self.profile, cause_by=any_to_str(RunCode), sent_from=self.profile, send_to=recipient
)
msg = Message(content=content, role=self.profile, cause_by=RunCode, sent_from=self.profile, send_to=recipient)
self.publish_message(msg)
async def _debug_error(self, msg):
@ -147,7 +145,7 @@ class QaEngineer(Role):
msg = Message(
content=file_info,
role=self.profile,
cause_by=any_to_str(DebugError),
cause_by=DebugError,
sent_from=self.profile,
send_to=recipient,
)
@ -165,7 +163,7 @@ class QaEngineer(Role):
result_msg = Message(
content=f"Exceeding {self.test_round_allowed} rounds of tests, skip (writing code counts as a round, too)",
role=self.profile,
cause_by=any_to_str(WriteTest),
cause_by=WriteTest,
sent_from=self.profile,
)
return result_msg
@ -189,7 +187,7 @@ class QaEngineer(Role):
result_msg = Message(
content=f"Round {self.test_round} of tests done",
role=self.profile,
cause_by=any_to_str(WriteTest),
cause_by=WriteTest,
sent_from=self.profile,
)
return result_msg

View file

@ -15,7 +15,6 @@ from metagpt.const import RESEARCH_PATH
from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.utils.common import any_to_str
class Report(BaseModel):
@ -64,20 +63,18 @@ class Researcher(Role):
research_system_text = get_research_system_text(topic, self.language)
if isinstance(todo, CollectLinks):
links = await todo.run(topic, 4, 4)
ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=any_to_str(todo))
ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=todo)
elif isinstance(todo, WebBrowseAndSummarize):
links = instruct_content.links
todos = (todo.run(*url, query=query, system_text=research_system_text) for (query, url) in links.items())
summaries = await asyncio.gather(*todos)
summaries = list((url, summary) for i in summaries for (url, summary) in i.items() if summary)
ret = Message("", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=any_to_str(todo))
ret = Message("", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=todo)
else:
summaries = instruct_content.summaries
summary_text = "\n---\n".join(f"url: {url}\nsummary: {summary}" for (url, summary) in summaries)
content = await self._rc.todo.run(topic, summary_text, system_text=research_system_text)
ret = Message(
"", Report(topic=topic, content=content), role=self.profile, cause_by=any_to_str(self._rc.todo)
)
ret = Message("", Report(topic=topic, content=content), role=self.profile, cause_by=self._rc.todo)
self._rc.memory.add(ret)
return ret

View file

@ -210,10 +210,10 @@ class Role:
content=response.content,
instruct_content=response.instruct_content,
role=self.profile,
cause_by=any_to_str(self._rc.todo),
cause_by=self._rc.todo,
)
else:
msg = Message(content=response, role=self.profile, cause_by=any_to_str(self._rc.todo))
msg = Message(content=response, role=self.profile, cause_by=self._rc.todo)
return msg

View file

@ -12,7 +12,6 @@ from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.tools import SearchEngineType
from metagpt.utils.common import any_to_str
class Searcher(Role):
@ -64,10 +63,10 @@ class Searcher(Role):
content=response.content,
instruct_content=response.instruct_content,
role=self.profile,
cause_by=any_to_str(self._rc.todo),
cause_by=self._rc.todo,
)
else:
msg = Message(content=response, role=self.profile, cause_by=any_to_str(self._rc.todo))
msg = Message(content=response, role=self.profile, cause_by=self._rc.todo)
self._rc.memory.add(msg)
return msg