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.

Migrate google search to use DDGS.text function (#4383)

Kinance5168cb52193c
diff --git a/autogpt/commands/google_search.py b/autogpt/commands/google_search.py
index 0290944867..23abef7d6a 100644
--- a/autogpt/commands/google_search.py
+++ b/autogpt/commands/google_search.py
@@ -2,8 +2,9 @@
from __future__ import annotations
import json
+from itertools import islice
-from duckduckgo_search import ddg
+from duckduckgo_search import DDGS
from autogpt.commands.command import command
from autogpt.config import Config
@@ -26,12 +27,12 @@ def google_search(query: str, num_results: int = 8) -> str:
if not query:
return json.dumps(search_results)
- results = ddg(query, max_results=num_results)
+ results = DDGS().text(query)
if not results:
return json.dumps(search_results)
- for j in results:
- search_results.append(j)
+ for item in islice(results, num_results):
+ search_results.append(item)
results = json.dumps(search_results, ensure_ascii=False, indent=4)
return safe_google_results(results)
diff --git a/tests/integration/test_google_search.py b/tests/integration/test_google_search.py
index 798765e0ee..50a2ce35de 100644
--- a/tests/integration/test_google_search.py
+++ b/tests/integration/test_google_search.py
@@ -42,7 +42,7 @@ def test_google_search(query, num_results, expected_output, return_value, mocker
mock_ddg = mocker.Mock()
mock_ddg.return_value = return_value
- mocker.patch("autogpt.commands.google_search.ddg", mock_ddg)
+ mocker.patch("autogpt.commands.google_search.DDGS.text", mock_ddg)
actual_output = google_search(query, num_results=num_results)
expected_output = safe_google_results(expected_output)
assert actual_output == expected_output