Migrate google search to use DDGS.text function (#4383)
parent
438d3e489b
commit
5168cb5219
|
@ -2,8 +2,9 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
from itertools import islice
|
||||||
|
|
||||||
from duckduckgo_search import ddg
|
from duckduckgo_search import DDGS
|
||||||
|
|
||||||
from autogpt.commands.command import command
|
from autogpt.commands.command import command
|
||||||
from autogpt.config import Config
|
from autogpt.config import Config
|
||||||
|
@ -26,12 +27,12 @@ def google_search(query: str, num_results: int = 8) -> str:
|
||||||
if not query:
|
if not query:
|
||||||
return json.dumps(search_results)
|
return json.dumps(search_results)
|
||||||
|
|
||||||
results = ddg(query, max_results=num_results)
|
results = DDGS().text(query)
|
||||||
if not results:
|
if not results:
|
||||||
return json.dumps(search_results)
|
return json.dumps(search_results)
|
||||||
|
|
||||||
for j in results:
|
for item in islice(results, num_results):
|
||||||
search_results.append(j)
|
search_results.append(item)
|
||||||
|
|
||||||
results = json.dumps(search_results, ensure_ascii=False, indent=4)
|
results = json.dumps(search_results, ensure_ascii=False, indent=4)
|
||||||
return safe_google_results(results)
|
return safe_google_results(results)
|
||||||
|
|
|
@ -42,7 +42,7 @@ def test_google_search(query, num_results, expected_output, return_value, mocker
|
||||||
mock_ddg = mocker.Mock()
|
mock_ddg = mocker.Mock()
|
||||||
mock_ddg.return_value = return_value
|
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)
|
actual_output = google_search(query, num_results=num_results)
|
||||||
expected_output = safe_google_results(expected_output)
|
expected_output = safe_google_results(expected_output)
|
||||||
assert actual_output == expected_output
|
assert actual_output == expected_output
|
||||||
|
|
Loading…
Reference in New Issue