fix serper integration bug to support batch queries

This commit is contained in:
leonzh0u 2023-07-21 09:42:01 -04:00
parent 4812a50cbd
commit b4536807d7
4 changed files with 7 additions and 7 deletions

View file

@ -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

View file

@ -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.")

View file

@ -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:

View file

@ -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."""