yzchen563 commited on
Commit
76860ad
·
verified ·
1 Parent(s): 935d623

Upload agent

Browse files
Files changed (4) hide show
  1. agent.json +2 -0
  2. app.py +3 -1
  3. requirements.txt +1 -0
  4. tools/web_search.py +60 -0
agent.json CHANGED
@@ -1,6 +1,7 @@
1
  {
2
  "class": "CodeAgent",
3
  "tools": [
 
4
  "final_answer"
5
  ],
6
  "model": {
@@ -32,6 +33,7 @@
32
  "name": null,
33
  "description": null,
34
  "requirements": [
 
35
  "smolagents"
36
  ],
37
  "authorized_imports": [
 
1
  {
2
  "class": "CodeAgent",
3
  "tools": [
4
+ "web_search",
5
  "final_answer"
6
  ],
7
  "model": {
 
33
  "name": null,
34
  "description": null,
35
  "requirements": [
36
+ "ddgs",
37
  "smolagents"
38
  ],
39
  "authorized_imports": [
app.py CHANGED
@@ -5,6 +5,7 @@ from smolagents import GradioUI, CodeAgent, InferenceClientModel
5
  # Get current directory path
6
  CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
7
 
 
8
  from tools.final_answer import FinalAnswerTool as FinalAnswer
9
 
10
 
@@ -13,6 +14,7 @@ model = InferenceClientModel(
13
  model_id='Qwen/Qwen3-Next-80B-A3B-Thinking',
14
  )
15
 
 
16
  final_answer = FinalAnswer()
17
 
18
 
@@ -21,7 +23,7 @@ with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream:
21
 
22
  agent = CodeAgent(
23
  model=model,
24
- tools=[],
25
  managed_agents=[],
26
  max_steps=20,
27
  verbosity_level=1,
 
5
  # Get current directory path
6
  CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
7
 
8
+ from tools.web_search import DuckDuckGoSearchTool as WebSearch
9
  from tools.final_answer import FinalAnswerTool as FinalAnswer
10
 
11
 
 
14
  model_id='Qwen/Qwen3-Next-80B-A3B-Thinking',
15
  )
16
 
17
+ web_search = WebSearch()
18
  final_answer = FinalAnswer()
19
 
20
 
 
23
 
24
  agent = CodeAgent(
25
  model=model,
26
+ tools=[web_search],
27
  managed_agents=[],
28
  max_steps=20,
29
  verbosity_level=1,
requirements.txt CHANGED
@@ -1 +1,2 @@
 
1
  smolagents
 
1
+ ddgs
2
  smolagents
tools/web_search.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Optional
2
+ from smolagents.tools import Tool
3
+ import ddgs
4
+ import time
5
+
6
+ class DuckDuckGoSearchTool(Tool):
7
+ """Web search tool that performs searches using the DuckDuckGo search engine.
8
+
9
+ Args:
10
+ max_results (`int`, default `10`): Maximum number of search results to return.
11
+ rate_limit (`float`, default `1.0`): Maximum queries per second. Set to `None` to disable rate limiting.
12
+ **kwargs: Additional keyword arguments for the `DDGS` client.
13
+
14
+ Examples:
15
+ ```python
16
+ >>> from smolagents import DuckDuckGoSearchTool
17
+ >>> web_search_tool = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0)
18
+ >>> results = web_search_tool("Hugging Face")
19
+ >>> print(results)
20
+ ```
21
+ """
22
+ name = "web_search"
23
+ description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
24
+ inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
25
+ output_type = "string"
26
+
27
+ def __init__(self, max_results: int = 10, rate_limit: float | None = 1.0, **kwargs):
28
+ super().__init__()
29
+ self.max_results = max_results
30
+ self.rate_limit = rate_limit
31
+ self._min_interval = 1.0 / rate_limit if rate_limit else 0.0
32
+ self._last_request_time = 0.0
33
+ try:
34
+ from ddgs import DDGS
35
+ except ImportError as e:
36
+ raise ImportError(
37
+ "You must install package `ddgs` to run this tool: for instance run `pip install ddgs`."
38
+ ) from e
39
+ self.ddgs = DDGS(**kwargs)
40
+
41
+ def forward(self, query: str) -> str:
42
+ self._enforce_rate_limit()
43
+ results = self.ddgs.text(query, max_results=self.max_results)
44
+ if len(results) == 0:
45
+ raise Exception("No results found! Try a less restrictive/shorter query.")
46
+ postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
47
+ return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
48
+
49
+ def _enforce_rate_limit(self) -> None:
50
+ import time
51
+
52
+ # No rate limit enforced
53
+ if not self.rate_limit:
54
+ return
55
+
56
+ now = time.time()
57
+ elapsed = now - self._last_request_time
58
+ if elapsed < self._min_interval:
59
+ time.sleep(self._min_interval - elapsed)
60
+ self._last_request_time = time.time()