Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/agents/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
"evaluate_expression": EvaluateExpressionTool,
"calculate_polynomial_roots": CalculatePolynomialRootsTool,
"solve_algebraic_equation": SolveAlgebraicEquationTool,
"web_search": WebSearchTool,
}
43 changes: 41 additions & 2 deletions src/agents/tools/web_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
from googleapiclient.discovery import build
from .tool import Tool


class WebSearchTool(Tool):
"""web search engines"""

__ENGINE__: List = ["google", "bing", "serpapi"]
__ENGINE__: List = ["google", "bing", "serpapi", "bocha"]

def __init__(self, engine: str, api: Dict):
"""
Expand Down Expand Up @@ -37,6 +36,7 @@ def __init__(self, engine: str, api: Dict):
"bing": self._bing_search,
"google": self._google_search,
"serpapi": self._serpapi_request,
"bocha": self.bocha_websearch,
}

def _bing_search(self, query: str, **kwargs):
Expand Down Expand Up @@ -65,6 +65,45 @@ def _bing_search(self, query: str, **kwargs):
metadata_results.append(metadata_result)
return {"meta data": metadata_results}

def bocha_websearch(self, query: str, count: int = 10):
"""
Perform web search using the Bocha Web Search API.

Parameters:
- query: Search query keywords
- freshness: Time range filter for results (fixed to "noLimit" internally)
- summary: Whether to include text summaries (fixed to True internally)
- count: Number of search results to return

Returns:
- Dictionary containing search result metadata including title, URL, snippet, site name, site icon, and publication time.
"""

url = 'https://api.bochaai.com/v1/web-search' # Removed trailing spaces for correctness
headers = {
'Authorization': f'Bearer {self.api["bocha"]}',
'Content-Type': 'application/json'
}
data = {
"query": query,
"freshness": "noLimit", # Time range filter for search results
"summary": True, # Whether to return detailed text summaries
"count": count
}

response = requests.post(url, headers=headers, json=data).json()
results = response["data"]["webPages"]["value"]
metadata_results = []
for result in results:
metadata_result = {
"snippet": result["snippet"],
"title": result["name"],
"link": result["url"],
}
metadata_results.append(metadata_result)
print(metadata_result)
return {"meta data": metadata_results}

def _google_search(self, query: str, **kwargs):
"""Initialize search hyperparameters"""
api_key = self.api[self.engine]["api_key"]
Expand Down