Merge pull request #599 from better629/feat_other_basemodel

support Message() without content param
This commit is contained in:
geekan 2023-12-21 17:40:52 +08:00 committed by GitHub
commit eac2ba1f54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 14 deletions

View file

@ -31,8 +31,8 @@ async def search():
role = Sales(profile="Sales", store=store)
role._watch({Action})
queries = [
Message("Which facial cleanser is good for oily skin?", cause_by=Action),
Message("Is L'Oreal good to use?", cause_by=Action),
Message(content="Which facial cleanser is good for oily skin?", cause_by=Action),
Message(content="Is L'Oreal good to use?", cause_by=Action),
]
for query in queries:
logger.info(f"User: {query}")

View file

@ -54,18 +54,27 @@ class Researcher(Role):
research_system_text = self.research_system_text(topic, todo)
if isinstance(todo, CollectLinks):
links = await todo.run(topic, 4, 4)
ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=todo)
ret = Message(
content="", instruct_content=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=todo)
ret = Message(
content="", instruct_content=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=self._rc.todo)
ret = Message(
content="",
instruct_content=Report(topic=topic, content=content),
role=self.profile,
cause_by=self._rc.todo,
)
self._rc.memory.add(ret)
return ret

View file

@ -110,7 +110,7 @@ class Message(BaseModel):
sent_from: str = ""
send_to: Set = Field(default_factory={MESSAGE_ROUTE_TO_ALL})
def __init__(self, **kwargs):
def __init__(self, content: str = "", **kwargs):
ic = kwargs.get("instruct_content", None)
if ic and not isinstance(ic, BaseModel) and "class" in ic:
# compatible with custom-defined ActionOutput
@ -122,6 +122,7 @@ class Message(BaseModel):
kwargs["instruct_content"] = ic_new
kwargs["id"] = kwargs.get("id", uuid.uuid4().hex)
kwargs["content"] = kwargs.get("content", content)
kwargs["cause_by"] = any_to_str(
kwargs.get("cause_by", import_class("UserRequirement", "metagpt.actions.add_requirement"))
)

View file

@ -19,7 +19,7 @@ class SubscriptionRunner(BaseModel):
>>> async def trigger():
... while True:
... yield Message("the latest news about OpenAI")
... yield Message(content="the latest news about OpenAI")
... await asyncio.sleep(3600 * 24)
>>> async def callback(msg: Message):

View file

@ -23,7 +23,7 @@ def test_all_messages():
UserMessage(test_content),
SystemMessage(test_content),
AIMessage(test_content),
Message(test_content, role="QA"),
Message(content=test_content, role="QA"),
]
for msg in msgs:
assert msg.content == test_content

View file

@ -13,12 +13,12 @@ async def test_subscription_run():
async def trigger():
while True:
yield Message("the latest news about OpenAI")
yield Message(content="the latest news about OpenAI")
await asyncio.sleep(3600 * 24)
class MockRole(Role):
async def run(self, message=None):
return Message("")
return Message(content="")
async def callback(message):
nonlocal callback_done
@ -61,11 +61,11 @@ async def test_subscription_run():
async def test_subscription_run_error(loguru_caplog):
async def trigger1():
while True:
yield Message("the latest news about OpenAI")
yield Message(content="the latest news about OpenAI")
await asyncio.sleep(3600 * 24)
async def trigger2():
yield Message("the latest news about OpenAI")
yield Message(content="the latest news about OpenAI")
class MockRole1(Role):
async def run(self, message=None):
@ -73,7 +73,7 @@ async def test_subscription_run_error(loguru_caplog):
class MockRole2(Role):
async def run(self, message=None):
return Message("")
return Message(content="")
async def callback(msg: Message):
print(msg)

View file

@ -47,7 +47,7 @@ class TestGetProjectRoot:
Input(x=RunCode, want="metagpt.actions.run_code.RunCode"),
Input(x=RunCode(), want="metagpt.actions.run_code.RunCode"),
Input(x=Message, want="metagpt.schema.Message"),
Input(x=Message(""), want="metagpt.schema.Message"),
Input(x=Message(content=""), want="metagpt.schema.Message"),
Input(x="A", want="A"),
]
for i in inputs: