diff --git a/examples/search_with_specific_engine.py b/examples/search_with_specific_engine.py index d63981c88..7cc431cd4 100644 --- a/examples/search_with_specific_engine.py +++ b/examples/search_with_specific_engine.py @@ -6,11 +6,11 @@ from metagpt.tools import SearchEngineType async def main(): # Serper API - await Searcher(engine=SearchEngineType.SERPER_GOOGLE).run("What are some good sun protection products?") - # Serper API - # await Searcher(engine=SearchEngineType.SERPAPI_GOOGLE).run("What are the best ski brands for skiers?") + #await Searcher(engine = SearchEngineType.SERPER_GOOGLE).run(["What are some good sun protection products?","What are some of the best beaches?"]) + # SerpAPI + #await Searcher(engine=SearchEngineType.SERPAPI_GOOGLE).run("What are the best ski brands for skiers?") # Google API - # await Searcher(engine=SearchEngineType.DIRECT_GOOGLE).run("What are the most interesting human facts?") + await Searcher(engine=SearchEngineType.DIRECT_GOOGLE).run("What are the most interesting human facts?") if __name__ == '__main__': asyncio.run(main()) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 1681586cc..3e3fef7b9 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -229,6 +229,8 @@ class Role: message = Message(message) if isinstance(message, Message): self.recv(message) + if isinstance(message, list): + self.recv(Message("\n".join(message))) elif not await self._observe(): # 如果没有任何新信息,挂起等待 logger.debug(f"{self._setting}: no news. waiting.") diff --git a/metagpt/tools/search_engine.py b/metagpt/tools/search_engine.py index 69670df6f..cfd4e8789 100644 --- a/metagpt/tools/search_engine.py +++ b/metagpt/tools/search_engine.py @@ -37,7 +37,7 @@ class SearchEngine: logger.info(results) return results - async def run(self, query, max_results=8): + async def run(self, query: str, max_results=8): if self.engine == SearchEngineType.SERPAPI_GOOGLE: api = SerpAPIWrapper() rsp = await api.run(query) @@ -45,10 +45,7 @@ class SearchEngine: rsp = SearchEngine.run_google(query, max_results) elif self.engine == SearchEngineType.SERPER_GOOGLE: api = SerperWrapper() - if isinstance(query, list): - rsp = await api.run(query) - elif isinstance(query, str): - rsp = await api.run([query]) + rsp = await api.run(query) elif self.engine == SearchEngineType.CUSTOM_ENGINE: rsp = self.run_func(query) else: @@ -74,15 +71,15 @@ def google_official_search(query: str, num_results: int = 8, focus=['snippet', ' api_key = config.google_api_key custom_search_engine_id = config.google_cse_id - service = build("customsearch", "v1", developerKey=api_key) + with build("customsearch", "v1", developerKey=api_key) as service: - result = ( - service.cse() - .list(q=query, cx=custom_search_engine_id, num=num_results) - .execute() - ) - - # Extract the search result items from the response + result = ( + service.cse() + .list(q=query, cx=custom_search_engine_id, num=num_results) + .execute() + ) + logger.info(result) + # Extract the search result items from the response search_results = result.get("items", []) # Create a list of only the URLs from the search results diff --git a/metagpt/tools/search_engine_serper.py b/metagpt/tools/search_engine_serper.py index 59e48840c..80c2f8001 100644 --- a/metagpt/tools/search_engine_serper.py +++ b/metagpt/tools/search_engine_serper.py @@ -38,7 +38,8 @@ class SerperWrapper(BaseModel): async def run(self, query: str, **kwargs: Any) -> str: """Run query through Serper and parse result async.""" - return ";".join([self._process_response(res) for res in await self.results(query)]) + queries = query.split("\n") + return "\n".join([self._process_response(res) for res in await self.results(queries)]) async def results(self, queries: list[str]) -> dict: """Use aiohttp to run query through Serper and return the results async."""