Skip to content

Commit 69185f4

Browse files
committed
test: Update web search tests for ddgs library and cleanup code
* In `etc/unittest/web_search.py`, updated imports from `duckduckgo_search` to `ddgs` and `DDGSError`. * Added an import for `MissingRequirementsError` in `etc/unittest/web_search.py`. * Modified exception handling in web search tests to catch both `DDGSError` and `MissingRequirementsError`. * Removed temporary modification comments in `g4f/cli/agent/agent.py` and `g4f/tools/web_search.py`.
1 parent e11f885 commit 69185f4

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

etc/unittest/web_search.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import unittest
55

66
try:
7-
from duckduckgo_search import DDGS
8-
from duckduckgo_search.exceptions import DuckDuckGoSearchException
7+
from ddgs import DDGS, DDGSError
98
from bs4 import BeautifulSoup
109
has_requirements = True
1110
except ImportError:
1211
has_requirements = False
1312

1413
from g4f.client import AsyncClient
14+
from g4f.errors import MissingRequirementsError
1515
from .mocks import YieldProviderMock
1616

1717
DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
@@ -45,8 +45,8 @@ async def test_search(self):
4545
try:
4646
response = await client.chat.completions.create([{"content": "", "role": "user"}], "", tool_calls=tool_calls)
4747
self.assertIn("Using the provided web search results", response.choices[0].message.content)
48-
except DuckDuckGoSearchException as e:
49-
self.skipTest(f'DuckDuckGoSearchException: {e}')
48+
except (DDGSError, MissingRequirementsError) as e:
49+
self.skipTest(f'Search error: {e}')
5050

5151
async def test_search2(self):
5252
client = AsyncClient(provider=YieldProviderMock)
@@ -64,8 +64,8 @@ async def test_search2(self):
6464
try:
6565
response = await client.chat.completions.create([{"content": "", "role": "user"}], "", tool_calls=tool_calls)
6666
self.assertIn("Using the provided web search results", response.choices[0].message.content)
67-
except DuckDuckGoSearchException as e:
68-
self.skipTest(f'DuckDuckGoSearchException: {e}')
67+
except (DDGSError, MissingRequirementsError) as e:
68+
self.skipTest(f'Search error: {e}')
6969

7070
async def test_search3(self):
7171
client = AsyncClient(provider=YieldProviderMock)
@@ -85,5 +85,5 @@ async def test_search3(self):
8585
try:
8686
response = await client.chat.completions.create([{"content": "", "role": "user"}], "", tool_calls=tool_calls)
8787
self.assertIn("Using the provided web search results", response.choices[0].message.content)
88-
except DuckDuckGoSearchException as e:
89-
self.skipTest(f'DuckDuckGoSearchException: {e}')
88+
except (DDGSError, MissingRequirementsError) as e:
89+
self.skipTest(f'Search error: {e}')

g4f/cli/agent/agent.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,10 @@ def _web_search(self, action: Action) -> Optional[str]:
362362
query = action.query or action.content
363363
self.console.print(f"[cyan]🌐 Performing web search for:[/cyan] '{query}'")
364364
try:
365-
# --- MODIFICATION START ---
366365
# Use the new 'ddgs' library
367366
from ddgs import DDGS
368367
with DDGS() as ddgs:
369368
results = [r for r in ddgs.text(query, max_results=3)]
370-
# --- MODIFICATION END ---
371369
if results:
372370
search_summary = ""
373371
for i, res in enumerate(results, 1):

g4f/tools/web_search.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ async def search(
211211
raise MissingRequirementsError('Install "ddgs" and "beautifulsoup4" | pip install -U g4f[search]')
212212

213213
results: List[SearchResultEntry] = []
214-
# --- MODIFICATION START ---
215214
# Use the new DDGS() context manager style
216215
async with DDGS() as ddgs:
217216
async for result in ddgs.text(
@@ -229,7 +228,6 @@ async def search(
229228
url=result["href"],
230229
snippet=result["body"]
231230
))
232-
# --- MODIFICATION END ---
233231

234232
if add_text:
235233
tasks = []

0 commit comments

Comments
 (0)