Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
shenchucheng committed Jun 6, 2024

Unverified

The email in this signature doesn’t match the committer email.
1 parent 5e8bd10 commit ff7c6b8
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions metagpt/roles/researcher.py
Original file line number Diff line number Diff line change
@@ -31,27 +31,15 @@ class Researcher(Role):
goal: str = "Gather information and conduct research"
constraints: str = "Ensure accuracy and relevance of information"
language: str = "en-us"
enable_concurrency: bool = True

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_actions(
[CollectLinks(name=self.name), WebBrowseAndSummarize(name=self.name), ConductResearch(name=self.name)]
)
self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value)
self.set_actions([CollectLinks, WebBrowseAndSummarize, ConductResearch])
self._set_react_mode(RoleReactMode.BY_ORDER.value, len(self.actions))
if self.language not in ("en-us", "zh-cn"):
logger.warning(f"The language `{self.language}` has not been tested, it may not work.")

async def _think(self) -> bool:
if self.rc.todo is None:
self._set_state(0)
return True

if self.rc.state + 1 < len(self.states):
self._set_state(self.rc.state + 1)
else:
self.set_todo(None)
return False

async def _act(self) -> Message:
logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})")
todo = self.rc.todo
@@ -71,7 +59,10 @@ async def _act(self) -> Message:
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)
if self.enable_concurrency:
summaries = await asyncio.gather(*todos)
else:
summaries = [await i for i in todos]
summaries = list((url, summary) for i in summaries for (url, summary) in i.items() if summary)
ret = Message(
content="", instruct_content=Report(topic=topic, summaries=summaries), role=self.profile, cause_by=todo
@@ -119,8 +110,8 @@ def write_report(self, topic: str, content: str):
if __name__ == "__main__":
import fire

async def main(topic: str, language="en-us"):
role = Researcher(language=language)
async def main(topic: str, language: str = "en-us", enable_concurrency: bool = True):
role = Researcher(language=language, enable_concurrency=enable_concurrency)
await role.run(topic)

fire.Fire(main)

0 comments on commit ff7c6b8

Please sign in to comment.