WHG2023 commited on
Commit
e81b381
·
1 Parent(s): 54dd993

refactor: Replace Google Search with Serper.dev for prior art

Browse files
Files changed (1) hide show
  1. 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 Google Custom Search API."""
21
 
22
- api_key = os.getenv("GOOGLE_API_KEY")
23
- cse_id = os.getenv("GOOGLE_CSE_ID")
24
-
25
- if not api_key or not cse_id:
26
- print("⚠️ WARNING: GOOGLE_API_KEY or GOOGLE_CSE_ID not set. Web search is disabled.")
27
- return [{"title": "Web Search Disabled", "link": "#", "snippet": "Please provide Google API credentials to enable live web search."}]
28
-
29
- url = "https://www.googleapis.com/customsearch/v1"
30
- params = {
31
- 'key': api_key,
32
- 'cx': cse_id,
33
- 'q': query,
34
- 'num': num_results
 
35
  }
36
 
37
  try:
38
- print(f"Executing REAL web search for: {query}")
39
- response = requests.get(url, params=params, timeout=10)
40
- response.raise_for_status() # Raise an exception for bad status codes
41
  search_results = response.json()
42
 
43
- if 'items' not in search_results:
 
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('items', [])
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