feat: Add max_results kwarg to DDGS tool

This commit is contained in:
Jason Stillerman 2025-01-02 11:20:17 -05:00
parent 5991206ae5
commit 8ec6674592
2 changed files with 9 additions and 3 deletions

5
examples/ddg_leopard.py Normal file
View File

@ -0,0 +1,5 @@
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
agent = CodeAgent(tools=[DuckDuckGoSearchTool(max_results=2)], model=HfApiModel())
agent.run("How many seconds would it take for a leopard at full speed to run through Pont des Arts?")

View File

@ -153,8 +153,9 @@ class DuckDuckGoSearchTool(Tool):
}
output_type = "any"
def __init__(self, **kwargs):
super().__init__(self, **kwargs)
def __init__(self, *args, max_results=10, **kwargs):
super().__init__(*args, **kwargs)
self.max_results = max_results
try:
from duckduckgo_search import DDGS
except ImportError:
@ -164,7 +165,7 @@ class DuckDuckGoSearchTool(Tool):
self.ddgs = DDGS()
def forward(self, query: str) -> str:
results = self.ddgs.text(query, max_results=10)
results = self.ddgs.text(query, max_results=self.max_results)
postprocessed_results = [
f"[{result['title']}]({result['href']})\n{result['body']}"
for result in results