rnrahate007 commited on
Commit
1e1685f
·
verified ·
1 Parent(s): ebf826c

Update tools/web_search.py

Browse files
Files changed (1) hide show
  1. tools/web_search.py +82 -13
tools/web_search.py CHANGED
@@ -1,27 +1,96 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
  from smolagents.tools import Tool
3
+
4
 
5
  class DuckDuckGoSearchTool(Tool):
6
+ """
7
+ Performs a DuckDuckGo web search and returns formatted search results.
8
+ """
9
+
10
  name = "web_search"
11
+
12
+ description = (
13
+ "Searches the web using DuckDuckGo and returns the most relevant "
14
+ "search results. Use this whenever factual or current information "
15
+ "is required."
16
+ )
17
+
18
+ inputs = {
19
+ "query": {
20
+ "type": "string",
21
+ "description": "Search query."
22
+ }
23
+ }
24
+
25
  output_type = "string"
26
 
27
+ def __init__(self, max_results: int = 5):
28
+
29
  super().__init__()
30
+
31
  self.max_results = max_results
32
+
33
  try:
34
+
35
+ from ddgs import DDGS
36
+
37
+ self.ddgs = DDGS()
38
+
39
  except ImportError as e:
40
+
41
  raise ImportError(
42
+ "Install ddgs using:\n\npip install ddgs"
43
  ) from e
 
44
 
45
  def forward(self, query: str) -> str:
46
+
47
+ try:
48
+
49
+ results = list(
50
+ self.ddgs.text(
51
+ query,
52
+ max_results=self.max_results
53
+ )
54
+ )
55
+
56
+ except Exception as e:
57
+
58
+ return f"Search failed.\nReason: {e}"
59
+
60
+ if not results:
61
+
62
+ return "No search results found."
63
+
64
+ formatted = []
65
+
66
+ seen_urls = set()
67
+
68
+ for i, result in enumerate(results, start=1):
69
+
70
+ title = result.get("title", "No title")
71
+
72
+ url = result.get("href", "")
73
+
74
+ snippet = result.get("body", "")
75
+
76
+ if url in seen_urls:
77
+ continue
78
+
79
+ seen_urls.add(url)
80
+
81
+ formatted.append(
82
+ f"""
83
+ Result {i}
84
+
85
+ Title:
86
+ {title}
87
+
88
+ URL:
89
+ {url}
90
+
91
+ Snippet:
92
+ {snippet}
93
+ """
94
+ )
95
+
96
+ return "\n".join(formatted)