Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Fix duckduckgo rate limiting (#4592)

merwanehamadi1b04e5cafc1b
diff --git a/autogpt/commands/google_search.py b/autogpt/commands/google_search.py
index c01ec0a12a..aa34861d30 100644
--- a/autogpt/commands/google_search.py
+++ b/autogpt/commands/google_search.py
@@ -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 = []
- if not query:
- return json.dumps(search_results)
+ attempts = 0
+
+ while attempts < DUCKDUCKGO_MAX_ATTEMPTS:
+ if not query:
+ return json.dumps(search_results)
+
+ results = DDGS().text(query)
+ search_results = list(islice(results, num_results))
- results = DDGS().text(query)
- if not results:
- return json.dumps(search_results)
+ if search_results:
+ break
- for item in islice(results, num_results):
- search_results.append(item)
+ time.sleep(1)
+ attempts += 1
results = json.dumps(search_results, ensure_ascii=False, indent=4)
return safe_google_results(results)