elliemci commited on
Commit
df00ae8
·
verified ·
1 Parent(s): 0ec6f26

Upload web_search.py

Browse files

adding a web search tool in order to be able to monitor the agent behavior with langfuse

Files changed (1) hide show
  1. tools/web_search.py +33 -0
tools/web_search.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Optional
2
+ from smolagents.tools import Tool
3
+ import duckduckgo_search
4
+
5
+
6
+ class DuckDuckGoSearchTool(Tool):
7
+ name = "web_search"
8
+ description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
9
+ inputs = {
10
+ "query": {"type": "string", "description": "The search query to perform."}
11
+ }
12
+ output_type = "string"
13
+
14
+ def __init__(self, max_results=10, **kwargs):
15
+ super().__init__()
16
+ self.max_results = max_results
17
+ try:
18
+ from duckduckgo_search import DDGS
19
+ except ImportError as e:
20
+ raise ImportError(
21
+ "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
22
+ ) from e
23
+ self.ddgs = DDGS(**kwargs)
24
+
25
+ def forward(self, query: str) -> str:
26
+ results = self.ddgs.text(query, max_results=self.max_results)
27
+ if len(results) == 0:
28
+ raise Exception("No results found! Try a less restrictive/shorter query.")
29
+ postprocessed_results = [
30
+ f"[{result['title']}]({result['href']})\n{result['body']}"
31
+ for result in results
32
+ ]
33
+ return "## Search Results\n\n" + "\n\n".join(postprocessed_results)