Sborole commited on
Commit
e5e6f55
·
verified ·
1 Parent(s): adbb1dd

Update tools/WebSearchTool.py

Browse files
Files changed (1) hide show
  1. tools/WebSearchTool.py +15 -15
tools/WebSearchTool.py CHANGED
@@ -3,14 +3,14 @@ from smolagents import Tool
3
  import os
4
  import json
5
 
6
- class ScaleSerpSearchTool(Tool):
7
  """
8
- A reliable web search tool using the Scale SERP API. This tool is chosen
9
- for its high reliability and accessible free tier when other search providers
10
- fail due to quota or instability.
11
  """
12
- name = "scaleserp_search"
13
- description = "Use the reliable Scale SERP API to fetch current and general knowledge from Google Search results. Requires an API key."
14
 
15
  inputs = {
16
  "query": {"type": "string", "description": "The search term to look up."}
@@ -20,15 +20,15 @@ class ScaleSerpSearchTool(Tool):
20
  def __init__(self, **kwargs):
21
  super().__init__(**kwargs)
22
  # Retrieve API key from environment variables
23
- self.api_key = os.getenv("SCALESERP_API_KEY")
24
- self.endpoint = "https://api.scaleserp.com/search"
25
 
26
  if not self.api_key:
27
- raise ValueError("SCALESERP_API_KEY secret not found. You need a key from the Scale SERP free tier.")
28
 
29
  def forward(self, query: str) -> str:
30
  """
31
- Executes a Scale SERP search query and formats the top results (up to 3).
32
 
33
  Args:
34
  query: The search term provided by the agent.
@@ -36,13 +36,13 @@ class ScaleSerpSearchTool(Tool):
36
  Returns:
37
  A formatted string of search results, or an error message.
38
  """
39
- print(f"Executing Scale SERP Search for: '{query}'")
40
 
41
  params = {
42
  "api_key": self.api_key,
43
  "q": query,
44
  "num": 3, # Request up to 3 results
45
- "gl": "us" # Limit results to US region for consistency
46
  }
47
 
48
  try:
@@ -69,13 +69,13 @@ class ScaleSerpSearchTool(Tool):
69
  search_results.append(
70
  f"RESULT {i+1}: '{item.get('title', 'N/A')}'\n"
71
  f"CONTENT: {item.get('snippet', 'No snippet available.')}\n"
72
- f"SOURCE: {item.get('url', 'N/A')}"
73
  )
74
 
75
  # Join the results with a clear separator
76
  return "\n\n---SEPARATOR---\n\n".join(search_results)
77
 
78
  except requests.exceptions.RequestException as e:
79
- return f"Error during Scale SERP API Request: {e}"
80
  except Exception as e:
81
- return f"Error processing Scale SERP results: {e}"
 
3
  import os
4
  import json
5
 
6
+ class SerpApiSearchTool(Tool):
7
  """
8
+ A reliable web search tool using the SerpApi service, which wraps major
9
+ search engines (like Google) to provide structured results. This is ideal
10
+ for high reliability when keyless options are unstable.
11
  """
12
+ name = "serpapi_search"
13
+ description = "Use the reliable SerpApi to fetch current information from Google Search results. Requires an API key."
14
 
15
  inputs = {
16
  "query": {"type": "string", "description": "The search term to look up."}
 
20
  def __init__(self, **kwargs):
21
  super().__init__(**kwargs)
22
  # Retrieve API key from environment variables
23
+ self.api_key = os.getenv("SERPAPI_API_KEY")
24
+ self.endpoint = "https://serpapi.com/search"
25
 
26
  if not self.api_key:
27
+ raise ValueError("SERPAPI_API_KEY secret not found. You need a key from the SerpApi free tier.")
28
 
29
  def forward(self, query: str) -> str:
30
  """
31
+ Executes a SerpApi search query and formats the top results (up to 3).
32
 
33
  Args:
34
  query: The search term provided by the agent.
 
36
  Returns:
37
  A formatted string of search results, or an error message.
38
  """
39
+ print(f"Executing SerpApi Search for: '{query}'")
40
 
41
  params = {
42
  "api_key": self.api_key,
43
  "q": query,
44
  "num": 3, # Request up to 3 results
45
+ "engine": "google", # Use Google search engine
46
  }
47
 
48
  try:
 
69
  search_results.append(
70
  f"RESULT {i+1}: '{item.get('title', 'N/A')}'\n"
71
  f"CONTENT: {item.get('snippet', 'No snippet available.')}\n"
72
+ f"SOURCE: {item.get('link', 'N/A')}"
73
  )
74
 
75
  # Join the results with a clear separator
76
  return "\n\n---SEPARATOR---\n\n".join(search_results)
77
 
78
  except requests.exceptions.RequestException as e:
79
+ return f"Error during SerpApi Search API Request: {e}"
80
  except Exception as e:
81
+ return f"Error processing SerpApi results: {e}"