cyberosa commited on
Commit
98e00e9
·
1 Parent(s): 51751a6

extending WebSearchTool

Browse files
Files changed (1) hide show
  1. tools.py +13 -10
tools.py CHANGED
@@ -1,10 +1,10 @@
1
- from smolagents import Tool
2
  import requests
3
  import time
4
  from typing import List, Dict, Any, Optional
5
 
6
 
7
- class MySearchTool(Tool):
8
  name = "Search tool to avoid rate limit"
9
  description = "It receives a question and returns a list of search results from various sources. It uses DuckDuckGo as the primary source"
10
  inputs = {
@@ -15,11 +15,11 @@ class MySearchTool(Tool):
15
  }
16
  output_type = "string"
17
 
18
- def __init__(self, query: str):
19
- self.query = query
20
- self.is_initialized = True
21
 
22
- def forward(self, max_results: int = 5) -> List[Dict[str, Any]]:
23
  """
24
  Free search using DuckDuckGo Instant Answer API
25
 
@@ -34,7 +34,7 @@ class MySearchTool(Tool):
34
  # DuckDuckGo Instant Answer API (free)
35
  url = "https://api.duckduckgo.com/"
36
  params = {
37
- "q": self.query,
38
  "format": "json",
39
  "no_html": "1",
40
  "skip_disambig": "1",
@@ -42,8 +42,9 @@ class MySearchTool(Tool):
42
 
43
  response = requests.get(url, params=params, timeout=10)
44
  response.raise_for_status()
45
- time.sleep(2) # 2-second delay
46
  data = response.json()
 
 
47
  results = []
48
 
49
  # Process abstract
@@ -58,7 +59,9 @@ class MySearchTool(Tool):
58
  )
59
 
60
  # Process related topics
61
- for topic in data.get("RelatedTopics", [])[: max_results - len(results)]:
 
 
62
  if isinstance(topic, dict) and "Text" in topic:
63
  results.append(
64
  {
@@ -69,7 +72,7 @@ class MySearchTool(Tool):
69
  }
70
  )
71
 
72
- return results[:max_results]
73
 
74
  except Exception as e:
75
  print(f"DuckDuckGo search failed: {str(e)}")
 
1
+ from smolagents import WebSearchTool
2
  import requests
3
  import time
4
  from typing import List, Dict, Any, Optional
5
 
6
 
7
+ class MySearchTool(WebSearchTool):
8
  name = "Search tool to avoid rate limit"
9
  description = "It receives a question and returns a list of search results from various sources. It uses DuckDuckGo as the primary source"
10
  inputs = {
 
15
  }
16
  output_type = "string"
17
 
18
+ def __init__(self, max_results=5):
19
+ super().__init__()
20
+ self.max_results = max_results
21
 
22
+ def search_duckduckgo(self, query: str) -> list:
23
  """
24
  Free search using DuckDuckGo Instant Answer API
25
 
 
34
  # DuckDuckGo Instant Answer API (free)
35
  url = "https://api.duckduckgo.com/"
36
  params = {
37
+ "q": query,
38
  "format": "json",
39
  "no_html": "1",
40
  "skip_disambig": "1",
 
42
 
43
  response = requests.get(url, params=params, timeout=10)
44
  response.raise_for_status()
 
45
  data = response.json()
46
+ time.sleep(2) # 2-second delay
47
+
48
  results = []
49
 
50
  # Process abstract
 
59
  )
60
 
61
  # Process related topics
62
+ for topic in data.get("RelatedTopics", [])[
63
+ : self.max_results - len(results)
64
+ ]:
65
  if isinstance(topic, dict) and "Text" in topic:
66
  results.append(
67
  {
 
72
  }
73
  )
74
 
75
+ return results[: self.max_results]
76
 
77
  except Exception as e:
78
  print(f"DuckDuckGo search failed: {str(e)}")