Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

fix serper integration bug to support batch queries

leonzh0ub4536807d713
diff --git a/examples/search_with_specific_engine.py b/examples/search_with_specific_engine.py
index 81333bf83..b4da84f47 100644
--- a/examples/search_with_specific_engine.py
+++ b/examples/search_with_specific_engine.py
@@ -5,7 +5,7 @@ from metagpt.tools import SearchEngineType
async def main():
# Serper API
- await Searcher(engine = SearchEngineType.SERPER_GOOGLE).run("What are some good sun protection products?")
+ await Searcher(engine = SearchEngineType.SERPER_GOOGLE).run(["What are some good sun protection products?","What are some of the best beaches?"])
# Serper API
#await Searcher(engine = SearchEngineType.SERPAPI_GOOGLE).run("What are the best ski brands for skiers?")
# Google API
diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py
index 36269aed2..e8870b4d1 100644
--- a/metagpt/roles/role.py
+++ b/metagpt/roles/role.py
@@ -221,6 +221,8 @@ class Role:
message = Message(message)
if isinstance(message, Message):
self.recv(message)
+ if isinstance(message, list):
+ self.recv(Message("|".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 5b9e1cd23..fb666f952 100644
--- a/metagpt/tools/search_engine.py
+++ b/metagpt/tools/search_engine.py
@@ -39,7 +39,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)
@@ -47,10 +47,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:
diff --git a/metagpt/tools/search_engine_serper.py b/metagpt/tools/search_engine_serper.py
index 91a8afce9..2d105326d 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("|")
+ return "|".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."""