Spaces:
Sleeping

vimj1915 commited on
Commit
396c58d
·
verified ·
1 Parent(s): ee30225

Update tools/serpapi_tools.py

Browse files
Files changed (1) hide show
  1. tools/serpapi_tools.py +18 -25
tools/serpapi_tools.py CHANGED
@@ -1,32 +1,25 @@
1
  # tools/serpapi_tools.py
2
 
3
  import os
4
- from serpapi import GoogleSearch
5
 
6
- def search_serpapi(query: str, num_results: int = 5):
7
- api_key = os.getenv("SERPAPI_API_KEY")
8
-
9
- if not api_key:
10
- return [{"title": "Error", "link": "Missing SERPAPI_API_KEY in .env"}]
11
-
12
- params = {
 
 
 
 
13
  "q": query,
14
- "num": num_results,
15
  "hl": "en",
16
  "gl": "us",
17
- "api_key": api_key
18
- }
19
-
20
- try:
21
- search = GoogleSearch(params)
22
- results = search.get_dict()
23
- organic_results = results.get("organic_results", [])
24
-
25
- parsed_results = [
26
- {"title": item.get("title"), "link": item.get("link")}
27
- for item in organic_results[:num_results]
28
- ]
29
- return parsed_results
30
-
31
- except Exception as e:
32
- return [{"title": "Error", "link": str(e)}]
 
1
  # tools/serpapi_tools.py
2
 
3
  import os
4
+ from serpapi.search import SerpApiSearch # ✅ Correct import path
5
 
6
+ def search_serpapi(query: str):
7
+ """
8
+ Use SerpAPI to search Google and return organic results.
9
+
10
+ Parameters:
11
+ - query (str): The search query string
12
+
13
+ Returns:
14
+ - list of dicts: A list of search result items (title, link, snippet, etc.)
15
+ """
16
+ search = SerpApiSearch({
17
  "q": query,
18
+ "location": "United States",
19
  "hl": "en",
20
  "gl": "us",
21
+ "api_key": os.getenv("SERPAPI_API_KEY")
22
+ })
23
+
24
+ results = search.get_dict()
25
+ return results.get("organic_results", [])