1
+ import functools
1
2
from dataclasses import dataclass
2
3
from typing import TypedDict
3
4
@@ -39,6 +40,9 @@ class DuckDuckGoSearchTool:
39
40
client : DDGS
40
41
"""The DuckDuckGo search client."""
41
42
43
+ max_results : int | None = None
44
+ """The maximum number of results. If None, returns results only from the first response."""
45
+
42
46
async def __call__ (self , query : str ) -> list [DuckDuckGoResult ]:
43
47
"""Searches DuckDuckGo for the given query and returns the results.
44
48
@@ -48,16 +52,22 @@ async def __call__(self, query: str) -> list[DuckDuckGoResult]:
48
52
Returns:
49
53
The search results.
50
54
"""
51
- results = await anyio .to_thread .run_sync (self .client .text , query )
55
+ search = functools .partial (self .client .text , max_results = self .max_results )
56
+ results = await anyio .to_thread .run_sync (search , query )
52
57
if len (results ) == 0 :
53
58
raise RuntimeError ('No search results found.' )
54
59
return duckduckgo_ta .validate_python (results )
55
60
56
61
57
- def duckduckgo_search_tool (duckduckgo_client : DDGS | None = None ):
58
- """Creates a DuckDuckGo search tool."""
62
+ def duckduckgo_search_tool (duckduckgo_client : DDGS | None = None , max_results : int | None = None ):
63
+ """Creates a DuckDuckGo search tool.
64
+
65
+ Args:
66
+ duckduckgo_client: The DuckDuckGo search client.
67
+ max_results: The maximum number of results. If None, returns results only from the first response.
68
+ """
59
69
return Tool (
60
- DuckDuckGoSearchTool (client = duckduckgo_client or DDGS ()).__call__ ,
70
+ DuckDuckGoSearchTool (client = duckduckgo_client or DDGS (), max_results = max_results ).__call__ ,
61
71
name = 'duckduckgo_search' ,
62
72
description = 'Searches DuckDuckGo for the given query and returns the results.' ,
63
73
)
0 commit comments