Fix duckduckgo rate limiting (#4592)
* Fix duckduckgo rate limiting * use list instead of loop --------- Co-authored-by: Reinier van der Leer <github@pwuts.nl>pull/4602/head
parent
835decc6c1
commit
1b04e5cafc
|
@ -2,6 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import time
|
||||
from itertools import islice
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
|
@ -12,6 +13,8 @@ from autogpt.commands.command import command
|
|||
if TYPE_CHECKING:
|
||||
from autogpt.config import Config
|
||||
|
||||
DUCKDUCKGO_MAX_ATTEMPTS = 3
|
||||
|
||||
|
||||
@command(
|
||||
"google",
|
||||
|
@ -30,15 +33,20 @@ def google_search(query: str, config: Config, num_results: int = 8) -> str:
|
|||
str: The results of the search.
|
||||
"""
|
||||
search_results = []
|
||||
attempts = 0
|
||||
|
||||
while attempts < DUCKDUCKGO_MAX_ATTEMPTS:
|
||||
if not query:
|
||||
return json.dumps(search_results)
|
||||
|
||||
results = DDGS().text(query)
|
||||
if not results:
|
||||
return json.dumps(search_results)
|
||||
search_results = list(islice(results, num_results))
|
||||
|
||||
for item in islice(results, num_results):
|
||||
search_results.append(item)
|
||||
if search_results:
|
||||
break
|
||||
|
||||
time.sleep(1)
|
||||
attempts += 1
|
||||
|
||||
results = json.dumps(search_results, ensure_ascii=False, indent=4)
|
||||
return safe_google_results(results)
|
||||
|
|
Loading…
Reference in New Issue