feat: replace get_class_name and get_object_name

This commit is contained in:
莘权 马 2023-11-10 15:27:27 +08:00
parent 1be1bb56e3
commit a3cb2b4fdc
5 changed files with 23 additions and 30 deletions

View file

@ -21,12 +21,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_set,
get_class_name,
get_object_name,
)
from metagpt.utils.common import CodeParser, any_to_str, any_to_str_set
from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP
@ -107,7 +102,7 @@ class Engineer(Role):
return CodeParser.parse_str(block="Python package name", text=system_design_msg.content)
def get_workspace(self) -> Path:
msg = self._rc.memory.get_by_action(get_class_name(WriteDesign))[-1]
msg = self._rc.memory.get_by_action(any_to_str(WriteDesign))[-1]
if not msg:
return WORKSPACE_ROOT / "src"
workspace = self.parse_workspace(msg)
@ -146,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=get_object_name(self._rc.todo))
msg = Message(content=code_rsp, role=self.profile, cause_by=any_to_str(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=get_object_name(self._rc.todo))
msg = Message(content="all done.", role=self.profile, cause_by=any_to_str(self._rc.todo))
return msg
async def _act_sp(self) -> Message:
@ -163,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=get_object_name(self._rc.todo))
msg = Message(content=code, role=self.profile, cause_by=any_to_str(self._rc.todo))
self._rc.memory.add(msg)
self.publish_message(msg)
@ -174,7 +169,7 @@ class Engineer(Role):
msg = Message(
content=MSG_SEP.join(code_msg_all),
role=self.profile,
cause_by=get_object_name(self._rc.todo),
cause_by=any_to_str(self._rc.todo),
send_to="Edward",
)
return msg
@ -216,7 +211,7 @@ class Engineer(Role):
msg = Message(
content=MSG_SEP.join(code_msg_all),
role=self.profile,
cause_by=get_object_name(self._rc.todo),
cause_by=any_to_str(self._rc.todo),
send_to="Edward",
)
return msg
@ -236,7 +231,7 @@ class Engineer(Role):
# Parse task lists
for message in self._rc.news:
if not message.cause_by == get_class_name(WriteTasks):
if not message.cause_by == any_to_str(WriteTasks):
continue
self.todos = self.parse_tasks(message)
return 1
@ -245,7 +240,7 @@ class Engineer(Role):
async def _think(self) -> None:
# In asynchronous scenarios, first check if the required messages are ready.
filters = {get_class_name(WriteTasks)}
filters = {any_to_str(WriteTasks)}
msgs = self._rc.memory.get_by_actions(filters)
if not msgs:
self._rc.todo = None

View file

@ -15,7 +15,7 @@ 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 get_object_name
from metagpt.utils.common import any_to_str
class Report(BaseModel):
@ -64,21 +64,19 @@ 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=get_object_name(todo))
ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=any_to_str(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=get_object_name(todo)
)
ret = Message("", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=any_to_str(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=get_object_name(self._rc.todo)
"", Report(topic=topic, content=content), role=self.profile, cause_by=any_to_str(self._rc.todo)
)
self._rc.memory.add(ret)
return ret

View file

@ -30,7 +30,7 @@ from metagpt.llm import LLM
from metagpt.logs import logger
from metagpt.memory import LongTermMemory, Memory
from metagpt.schema import Message, MessageQueue
from metagpt.utils.common import get_class_name, get_object_name
from metagpt.utils.common import any_to_str
PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """
@ -116,7 +116,7 @@ class Role:
self._actions = []
self._role_id = str(self._setting)
self._rc = RoleContext()
self._subscription = {get_object_name(self)}
self._subscription = {any_to_str(self)}
if name:
self._subscription.add(name)
@ -137,7 +137,7 @@ class Role:
def _watch(self, actions: Iterable[Type[Action]]):
"""Listen to the corresponding behaviors in private message buffer"""
tags = {get_class_name(t) for t in actions}
tags = {any_to_str(t) for t in actions}
self._rc.watch.update(tags)
# check RoleContext after adding watch actions
self._rc.check(self._role_id)
@ -207,10 +207,10 @@ class Role:
content=response.content,
instruct_content=response.instruct_content,
role=self.profile,
cause_by=get_object_name(self._rc.todo),
cause_by=any_to_str(self._rc.todo),
)
else:
msg = Message(content=response, role=self.profile, cause_by=get_object_name(self._rc.todo))
msg = Message(content=response, role=self.profile, cause_by=any_to_str(self._rc.todo))
return msg

View file

@ -12,7 +12,7 @@ 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 get_object_name
from metagpt.utils.common import any_to_str
class Searcher(Role):
@ -64,10 +64,10 @@ class Searcher(Role):
content=response.content,
instruct_content=response.instruct_content,
role=self.profile,
cause_by=get_object_name(self._rc.todo),
cause_by=any_to_str(self._rc.todo),
)
else:
msg = Message(content=response, role=self.profile, cause_by=get_object_name(self._rc.todo))
msg = Message(content=response, role=self.profile, cause_by=any_to_str(self._rc.todo))
self._rc.memory.add(msg)
return msg

View file

@ -17,7 +17,7 @@ from metagpt.actions.execute_task import ExecuteTask
from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.utils.common import get_object_name
from metagpt.utils.common import any_to_str
from metagpt.utils.make_sk_kernel import make_sk_kernel
@ -74,7 +74,7 @@ class SkAgent(Role):
result = (await self.plan.invoke_async()).result
logger.info(result)
msg = Message(content=result, role=self.profile, cause_by=get_object_name(self._rc.todo))
msg = Message(content=result, role=self.profile, cause_by=any_to_str(self._rc.todo))
self._rc.memory.add(msg)
self.publish_message(msg)
return msg