prevent DuckDuckGoSearchAPIWrapper from consuming top result (#6727)

remove the `next` call that checks for None on the results generator
This commit is contained in:
Gabriel Altay 2023-06-25 22:54:15 -04:00 committed by GitHub
parent 87802c86d9
commit f9771700e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,12 +49,13 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
safesearch=self.safesearch,
timelimit=self.time,
)
if results is None or next(results, None) is None:
if results is None:
return ["No good DuckDuckGo Search Result was found"]
snippets = []
for i, res in enumerate(results, 1):
if res is not None:
snippets.append(res["body"])
if i == self.max_results:
if len(snippets) == self.max_results:
break
return snippets
@ -84,7 +85,7 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
safesearch=self.safesearch,
timelimit=self.time,
)
if results is None or next(results, None) is None:
if results is None:
return [{"Result": "No good DuckDuckGo Search Result was found"}]
def to_metadata(result: Dict) -> Dict[str, str]:
@ -96,7 +97,8 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
formatted_results = []
for i, res in enumerate(results, 1):
if res is not None:
formatted_results.append(to_metadata(res))
if i == num_results:
if len(formatted_results) == num_results:
break
return formatted_results