Spaces:
Sleeping
Sleeping
Update tools/WebSearchTool.py
Browse files- tools/WebSearchTool.py +53 -14
tools/WebSearchTool.py
CHANGED
|
@@ -1,24 +1,63 @@
|
|
|
|
|
| 1 |
from smolagents import Tool
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
inputs = {
|
| 7 |
-
"query": {"type": "string", "description": "
|
| 8 |
}
|
| 9 |
output_type = "string"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def forward(self, query: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
return "No results found."
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
from smolagents import Tool
|
| 3 |
+
from googleapiclient.discovery import build
|
| 4 |
+
|
| 5 |
+
class GoogleSearchTool(Tool):
|
| 6 |
+
# Use a descriptive name to guide the agent
|
| 7 |
+
name = "google_search"
|
| 8 |
+
# Update the description to reflect the new functionality
|
| 9 |
+
description = "Use Google to find current information and general knowledge. Returns a snippet and URL."
|
| 10 |
+
|
| 11 |
inputs = {
|
| 12 |
+
"query": {"type": "string", "description": "The search term to look up."}
|
| 13 |
}
|
| 14 |
output_type = "string"
|
| 15 |
|
| 16 |
+
def __init__(self, **kwargs):
|
| 17 |
+
super().__init__(**kwargs)
|
| 18 |
+
# Retrieve credentials from environment variables
|
| 19 |
+
self.api_key = os.getenv("GOOGLE_API_KEY")
|
| 20 |
+
self.cse_id = os.getenv("GOOGLE_CSE_ID")
|
| 21 |
+
|
| 22 |
+
if not self.api_key or not self.cse_id:
|
| 23 |
+
# Raise an error if credentials are missing
|
| 24 |
+
raise ValueError("GOOGLE_API_KEY or GOOGLE_CSE_ID secret not found.")
|
| 25 |
+
|
| 26 |
+
# Initialize the Google Custom Search service
|
| 27 |
+
# 'customsearch' is the API name, 'v1' is the version
|
| 28 |
+
self.service = build(
|
| 29 |
+
"customsearch", "v1", developerKey=self.api_key
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
def forward(self, query: str) -> str:
|
| 33 |
+
"""
|
| 34 |
+
Executes a Google search query and formats the top results.
|
| 35 |
+
"""
|
| 36 |
try:
|
| 37 |
+
# Execute the search request for up to 3 results
|
| 38 |
+
res = self.service.cse().list(
|
| 39 |
+
q=query,
|
| 40 |
+
cx=self.cse_id,
|
| 41 |
+
num=3
|
| 42 |
+
).execute()
|
| 43 |
|
| 44 |
+
items = res.get('items', [])
|
|
|
|
| 45 |
|
| 46 |
+
if not items:
|
| 47 |
+
# Return the specific failure message expected by the agent
|
| 48 |
+
return "XX record info: No results found."
|
| 49 |
+
|
| 50 |
+
search_results = []
|
| 51 |
+
for item in items:
|
| 52 |
+
# Format the output clearly for the LLM
|
| 53 |
+
search_results.append(
|
| 54 |
+
f"Title: {item.get('title')}\n"
|
| 55 |
+
f"Snippet: {item.get('snippet')}\n"
|
| 56 |
+
f"URL: {item.get('link')}"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
return "\n---\n".join(search_results)
|
| 60 |
+
|
| 61 |
except Exception as e:
|
| 62 |
+
# Provide an informative error message
|
| 63 |
+
return f"Error during Google Search API call: {e}"
|