-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch_tool.py
More file actions
58 lines (46 loc) · 1.82 KB
/
search_tool.py
File metadata and controls
58 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import requests
from typing import Dict, Any
from dotenv import load_dotenv
def web_search(query: str) -> Dict[Any, Any]:
load_dotenv()
api_key = os.getenv('SEARCH_API_KEY')
if not api_key:
return {"error": "API key not found in environment variables"}
url = "https://www.searchapi.io/api/v1/search"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
params = {
"engine": "google_news",
"q": query,
"num": 5
}
try:
response = requests.get(url, headers=headers, params=params)
if response.status_code == 401:
return {"error": "Invalid API key or authentication failed"}
elif response.status_code == 429:
return {"error": "Rate limit exceeded"}
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
error_msg = f"Error fetching search results: {e}"
if hasattr(e, 'response') and e.response:
try:
error_details = e.response.json()
error_msg = f"{error_msg} - {error_details.get('message', '')}"
except:
pass
return {"error": error_msg}
def extract_content(search_results: dict) -> str:
content = []
if "organic_results" in search_results:
for result in search_results["organic_results"][:4]: # Top 4 results
if "snippet" in result:
content.append(result["snippet"])
if "answer_box" in search_results and search_results["answer_box"]:
if "answer" in search_results["answer_box"]:
content.insert(0, search_results["answer_box"]["answer"])
return "\n\n".join(content)