acrowth commited on
Commit
ef1399d
·
verified ·
1 Parent(s): 8bdb69f

Upload agent

Browse files
Files changed (4) hide show
  1. agent.json +2 -0
  2. app.py +3 -1
  3. requirements.txt +1 -1
  4. tools/web_search.py +27 -0
agent.json CHANGED
@@ -1,5 +1,6 @@
1
  {
2
  "tools": [
 
3
  "final_answer"
4
  ],
5
  "model": {
@@ -38,6 +39,7 @@
38
  "name": null,
39
  "description": null,
40
  "requirements": [
 
41
  "smolagents"
42
  ],
43
  "authorized_imports": [
 
1
  {
2
  "tools": [
3
+ "web_search",
4
  "final_answer"
5
  ],
6
  "model": {
 
39
  "name": null,
40
  "description": null,
41
  "requirements": [
42
+ "duckduckgo_search",
43
  "smolagents"
44
  ],
45
  "authorized_imports": [
app.py CHANGED
@@ -5,6 +5,7 @@ from smolagents import GradioUI, CodeAgent, LiteLLMModel
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
 
@@ -14,6 +15,7 @@ model_id='gemini/gemini-2.0-flash',
14
  api_base=None,
15
  )
16
 
 
17
  final_answer = FinalAnswer()
18
 
19
 
@@ -22,7 +24,7 @@ with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream:
22
 
23
  agent = CodeAgent(
24
  model=model,
25
- tools=[],
26
  managed_agents=[],
27
  max_steps=6,
28
  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
 
 
15
  api_base=None,
16
  )
17
 
18
+ web_search = WebSearch()
19
  final_answer = FinalAnswer()
20
 
21
 
 
24
 
25
  agent = CodeAgent(
26
  model=model,
27
+ tools=[web_search],
28
  managed_agents=[],
29
  max_steps=6,
30
  verbosity_level=1,
requirements.txt CHANGED
@@ -1,2 +1,2 @@
 
1
  smolagents
2
- litellm
 
1
+ duckduckgo_search
2
  smolagents
 
tools/web_search.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Optional
2
+ from smolagents.tools import Tool
3
+ import duckduckgo_search
4
+
5
+ class DuckDuckGoSearchTool(Tool):
6
+ name = "web_search"
7
+ description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
8
+ inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
9
+ output_type = "string"
10
+
11
+ def __init__(self, max_results=10, **kwargs):
12
+ super().__init__()
13
+ self.max_results = max_results
14
+ try:
15
+ from duckduckgo_search import DDGS
16
+ except ImportError as e:
17
+ raise ImportError(
18
+ "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
19
+ ) from e
20
+ self.ddgs = DDGS(**kwargs)
21
+
22
+ def forward(self, query: str) -> str:
23
+ results = self.ddgs.text(query, max_results=self.max_results)
24
+ if len(results) == 0:
25
+ raise Exception("No results found! Try a less restrictive/shorter query.")
26
+ postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
27
+ return "## Search Results\n\n" + "\n\n".join(postprocessed_results)