fix batch query:

This commit is contained in:
leonzh0u 2023-07-23 18:25:08 -04:00
parent 242bbbc13a
commit 3e13a00003
4 changed files with 18 additions and 22 deletions

View file

@ -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?","What are some of the best beaches?"])
# 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())

View file

@ -226,7 +226,7 @@ class Role:
if isinstance(message, Message):
self.recv(message)
if isinstance(message, list):
self.recv(Message("|".join(message)))
self.recv(Message("\n".join(message)))
elif not await self._observe():
# 如果没有任何新信息,挂起等待
logger.debug(f"{self._setting}: no news. waiting.")

View file

@ -53,11 +53,11 @@ class SearchEngine:
return rsp
def google_official_search(queries: list[str], num_results: int = 8, focus=['snippet', 'link', 'title']) -> dict | list[dict]:
"""Return the results of a batch of Google search using the official Google API
def google_official_search(query: str, num_results: int = 8, focus=['snippet', 'link', 'title']) -> dict | list[dict]:
"""Return the results of a Google search using the official Google API
Args:
queries (list[str]): A batch of search queries.
query (str): The search query.
num_results (int): The number of results to return.
Returns:
@ -71,19 +71,15 @@ def google_official_search(queries: list[str], num_results: int = 8, focus=['sni
api_key = config.google_api_key
custom_search_engine_id = config.google_cse_id
service = build("customsearch", "v2", developerKey=api_key)
batch = service.new_batch_http_request()
for query in queries:
batch.add(service.cse()
.list(q=query, cx=custom_search_engine_id, num=num_results))
batch.execute()
result = (
service.cse()
.list(q=query, cx=custom_search_engine_id, num=num_results)
.execute()
)
with build("customsearch", "v1", developerKey=api_key) as service:
# 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

View file

@ -38,8 +38,8 @@ class SerperWrapper(BaseModel):
async def run(self, query: str, **kwargs: Any) -> str:
"""Run query through Serper and parse result async."""
queries = query.split("|")
return "|".join([self._process_response(res) for res in await self.results(queries)])
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."""