mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-08 15:05:17 +02:00
fix serper integration bug to support batch queries
This commit is contained in:
parent
4812a50cbd
commit
b4536807d7
4 changed files with 7 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.")
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue