Spaces:
Sleeping
Sleeping
Update tools/WebSearchTool.py
Browse files- 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
|
| 7 |
"""
|
| 8 |
-
A reliable web search tool using the
|
| 9 |
-
|
| 10 |
-
|
| 11 |
"""
|
| 12 |
-
name = "
|
| 13 |
-
description = "Use the reliable
|
| 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("
|
| 24 |
-
self.endpoint = "https://
|
| 25 |
|
| 26 |
if not self.api_key:
|
| 27 |
-
raise ValueError("
|
| 28 |
|
| 29 |
def forward(self, query: str) -> str:
|
| 30 |
"""
|
| 31 |
-
Executes a
|
| 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
|
| 40 |
|
| 41 |
params = {
|
| 42 |
"api_key": self.api_key,
|
| 43 |
"q": query,
|
| 44 |
"num": 3, # Request up to 3 results
|
| 45 |
-
"
|
| 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('
|
| 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
|
| 80 |
except Exception as e:
|
| 81 |
-
return f"Error processing
|
|
|
|
| 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}"
|