WHG2023
commited on
Commit
·
e81b381
1
Parent(s):
54dd993
refactor: Replace Google Search with Serper.dev for prior art
Browse files- real_ai_agents_implementation.py +22 -20
real_ai_agents_implementation.py
CHANGED
|
@@ -17,30 +17,32 @@ from dotenv import load_dotenv
|
|
| 17 |
|
| 18 |
# --- Tool Imports ---
|
| 19 |
def web_search(query: str, num_results: int = 5) -> List[Dict]:
|
| 20 |
-
"""Performs a real web search using
|
| 21 |
|
| 22 |
-
api_key = os.getenv("
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
'
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
try:
|
| 38 |
-
print(f"Executing REAL web search for: {query}")
|
| 39 |
-
response = requests.
|
| 40 |
-
response.raise_for_status()
|
| 41 |
search_results = response.json()
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
return []
|
| 45 |
|
| 46 |
# Format the results to match the expected structure
|
|
@@ -50,12 +52,12 @@ def web_search(query: str, num_results: int = 5) -> List[Dict]:
|
|
| 50 |
"link": item.get('link'),
|
| 51 |
"snippet": item.get('snippet')
|
| 52 |
}
|
| 53 |
-
for item in search_results.get('
|
| 54 |
]
|
| 55 |
return formatted_results
|
| 56 |
|
| 57 |
except requests.exceptions.RequestException as e:
|
| 58 |
-
print(f"Error during web search: {e}")
|
| 59 |
return [{"title": "Web Search Error", "link": "#", "snippet": f"An error occurred during the search: {e}"}]
|
| 60 |
|
| 61 |
|
|
|
|
| 17 |
|
| 18 |
# --- Tool Imports ---
|
| 19 |
def web_search(query: str, num_results: int = 5) -> List[Dict]:
|
| 20 |
+
"""Performs a real web search using the free Serper.dev API."""
|
| 21 |
|
| 22 |
+
api_key = os.getenv("SERPER_API_KEY")
|
| 23 |
+
|
| 24 |
+
if not api_key:
|
| 25 |
+
print("⚠️ WARNING: SERPER_API_KEY not set. Web search is disabled.")
|
| 26 |
+
return [{"title": "Web Search Disabled", "link": "#", "snippet": "Please provide a Serper.dev API key to enable live web search."}]
|
| 27 |
+
|
| 28 |
+
url = "https://google.serper.dev/search"
|
| 29 |
+
payload = json.dumps({
|
| 30 |
+
"q": query,
|
| 31 |
+
"num": num_results
|
| 32 |
+
})
|
| 33 |
+
headers = {
|
| 34 |
+
'X-API-KEY': api_key,
|
| 35 |
+
'Content-Type': 'application/json'
|
| 36 |
}
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
print(f"Executing REAL web search for: {query} via Serper.dev")
|
| 40 |
+
response = requests.post(url, headers=headers, data=payload, timeout=10)
|
| 41 |
+
response.raise_for_status()
|
| 42 |
search_results = response.json()
|
| 43 |
|
| 44 |
+
# The key for organic results is 'organic'
|
| 45 |
+
if 'organic' not in search_results:
|
| 46 |
return []
|
| 47 |
|
| 48 |
# Format the results to match the expected structure
|
|
|
|
| 52 |
"link": item.get('link'),
|
| 53 |
"snippet": item.get('snippet')
|
| 54 |
}
|
| 55 |
+
for item in search_results.get('organic', [])
|
| 56 |
]
|
| 57 |
return formatted_results
|
| 58 |
|
| 59 |
except requests.exceptions.RequestException as e:
|
| 60 |
+
print(f"Error during web search with Serper.dev: {e}")
|
| 61 |
return [{"title": "Web Search Error", "link": "#", "snippet": f"An error occurred during the search: {e}"}]
|
| 62 |
|
| 63 |
|