Update tools/WebSearchTool.py
Browse files- tools/WebSearchTool.py +20 -16
tools/WebSearchTool.py
CHANGED
|
@@ -5,13 +5,13 @@ from googleapiclient.discovery import build
|
|
| 5 |
class GoogleSearchTool(Tool):
|
| 6 |
"""
|
| 7 |
A tool to perform web search Google searches using the Custom Search Engine (CSE) API.
|
| 8 |
-
Use this tool first to get the necessary information for calculation or further tools.
|
| 9 |
"""
|
| 10 |
# Use a descriptive name to guide the agent
|
| 11 |
name = "google_search"
|
| 12 |
# Update the description to reflect the new functionality
|
| 13 |
description = "Use Google to find current information and general knowledge. Returns a snippet and URL."
|
| 14 |
-
|
| 15 |
# Define the required input structure for the agent framework
|
| 16 |
inputs = {
|
| 17 |
"query": {"type": "string", "description": "The search term to look up."}
|
|
@@ -20,15 +20,15 @@ class GoogleSearchTool(Tool):
|
|
| 20 |
|
| 21 |
def __init__(self, **kwargs):
|
| 22 |
super().__init__(**kwargs)
|
| 23 |
-
|
| 24 |
# Retrieve credentials from environment variables
|
| 25 |
self.api_key = os.getenv("GOOGLE_API_KEY")
|
| 26 |
self.cse_id = os.getenv("GOOGLE_CSE_ID")
|
| 27 |
-
|
| 28 |
# Check for mandatory credentials
|
| 29 |
if not self.api_key or not self.cse_id:
|
| 30 |
raise ValueError("GOOGLE_API_KEY or GOOGLE_CSE_ID secret not found. Check environment variables.")
|
| 31 |
-
|
| 32 |
# Initialize the Google Custom Search service
|
| 33 |
# 'customsearch' is the API name, 'v1' is the version
|
| 34 |
self.service = build(
|
|
@@ -38,10 +38,13 @@ class GoogleSearchTool(Tool):
|
|
| 38 |
def forward(self, query: str) -> str:
|
| 39 |
"""
|
| 40 |
Executes a Google search query and formats the top results (up to 3).
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
Args:
|
| 43 |
query: The search term provided by the agent.
|
| 44 |
-
|
| 45 |
Returns:
|
| 46 |
A formatted string of search results, or an error message.
|
| 47 |
"""
|
|
@@ -53,24 +56,25 @@ class GoogleSearchTool(Tool):
|
|
| 53 |
cx=self.cse_id,
|
| 54 |
num=3
|
| 55 |
).execute()
|
| 56 |
-
|
| 57 |
items = res.get('items', [])
|
| 58 |
-
|
| 59 |
if not items:
|
| 60 |
# Return the specific failure message expected by the agent
|
| 61 |
return "XX record info: No results found."
|
| 62 |
|
| 63 |
search_results = []
|
| 64 |
-
for item in items:
|
| 65 |
-
#
|
|
|
|
| 66 |
search_results.append(
|
| 67 |
-
f"
|
| 68 |
-
f"
|
| 69 |
-
f"
|
| 70 |
)
|
| 71 |
-
|
| 72 |
# Join the results with a clear separator
|
| 73 |
-
return "\n---\n".join(search_results)
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
# Provide an informative error message upon API failure
|
|
|
|
| 5 |
class GoogleSearchTool(Tool):
|
| 6 |
"""
|
| 7 |
A tool to perform web search Google searches using the Custom Search Engine (CSE) API.
|
| 8 |
+
Use this tool first to get the necessary information for calculation or further tools.
|
| 9 |
"""
|
| 10 |
# Use a descriptive name to guide the agent
|
| 11 |
name = "google_search"
|
| 12 |
# Update the description to reflect the new functionality
|
| 13 |
description = "Use Google to find current information and general knowledge. Returns a snippet and URL."
|
| 14 |
+
|
| 15 |
# Define the required input structure for the agent framework
|
| 16 |
inputs = {
|
| 17 |
"query": {"type": "string", "description": "The search term to look up."}
|
|
|
|
| 20 |
|
| 21 |
def __init__(self, **kwargs):
|
| 22 |
super().__init__(**kwargs)
|
| 23 |
+
|
| 24 |
# Retrieve credentials from environment variables
|
| 25 |
self.api_key = os.getenv("GOOGLE_API_KEY")
|
| 26 |
self.cse_id = os.getenv("GOOGLE_CSE_ID")
|
| 27 |
+
|
| 28 |
# Check for mandatory credentials
|
| 29 |
if not self.api_key or not self.cse_id:
|
| 30 |
raise ValueError("GOOGLE_API_KEY or GOOGLE_CSE_ID secret not found. Check environment variables.")
|
| 31 |
+
|
| 32 |
# Initialize the Google Custom Search service
|
| 33 |
# 'customsearch' is the API name, 'v1' is the version
|
| 34 |
self.service = build(
|
|
|
|
| 38 |
def forward(self, query: str) -> str:
|
| 39 |
"""
|
| 40 |
Executes a Google search query and formats the top results (up to 3).
|
| 41 |
+
|
| 42 |
+
The output is formatted to prioritize the Title and Snippet (informational content)
|
| 43 |
+
over the URL to address the confusion that it was only returning links.
|
| 44 |
+
|
| 45 |
Args:
|
| 46 |
query: The search term provided by the agent.
|
| 47 |
+
|
| 48 |
Returns:
|
| 49 |
A formatted string of search results, or an error message.
|
| 50 |
"""
|
|
|
|
| 56 |
cx=self.cse_id,
|
| 57 |
num=3
|
| 58 |
).execute()
|
| 59 |
+
|
| 60 |
items = res.get('items', [])
|
| 61 |
+
|
| 62 |
if not items:
|
| 63 |
# Return the specific failure message expected by the agent
|
| 64 |
return "XX record info: No results found."
|
| 65 |
|
| 66 |
search_results = []
|
| 67 |
+
for i, item in enumerate(items):
|
| 68 |
+
# We've adjusted the format here to put the snippet first,
|
| 69 |
+
# which is usually the most important information for the agent.
|
| 70 |
search_results.append(
|
| 71 |
+
f"RESULT {i+1}: '{item.get('title')}'\n"
|
| 72 |
+
f"CONTENT: {item.get('snippet')}\n"
|
| 73 |
+
f"SOURCE: {item.get('link')}" # The URL is now clearly labeled as the SOURCE
|
| 74 |
)
|
| 75 |
+
|
| 76 |
# Join the results with a clear separator
|
| 77 |
+
return "\n\n---SEPARATOR---\n\n".join(search_results)
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
# Provide an informative error message upon API failure
|