| 12 | |
| 13 | @register_tool("search", allow_overwrite=True) |
| 14 | class Search(BaseTool): |
| 15 | name = "search" |
| 16 | description = "Performs batched web searches: supply an array 'query'; the tool retrieves the top 10 results for each query in one call." |
| 17 | parameters = { |
| 18 | "type": "object", |
| 19 | "properties": { |
| 20 | "query": { |
| 21 | "type": "array", |
| 22 | "items": { |
| 23 | "type": "string" |
| 24 | }, |
| 25 | "description": "Array of query strings. Include multiple complementary search queries in a single call." |
| 26 | }, |
| 27 | }, |
| 28 | "required": ["query"], |
| 29 | } |
| 30 | |
| 31 | def google_search(self, query: str): |
| 32 | url = 'https://google.serper.dev/search' |
| 33 | headers = { |
| 34 | 'X-API-KEY': GOOGLE_SEARCH_KEY, |
| 35 | 'Content-Type': 'application/json', |
| 36 | } |
| 37 | data = { |
| 38 | "q": query, |
| 39 | "num": 10, |
| 40 | "extendParams": { |
| 41 | "country": "en", |
| 42 | "page": 1, |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | for i in range(5): |
| 47 | try: |
| 48 | response = requests.post(url, headers=headers, data=json.dumps(data)) |
| 49 | results = response.json() |
| 50 | except Exception as e: |
| 51 | print(e) |
| 52 | if i == 4: |
| 53 | return f"Google search Timeout, return None, Please try again later." |
| 54 | if response.status_code != 200: |
| 55 | raise Exception(f"Error: {response.status_code} - {response.text}") |
| 56 | |
| 57 | try: |
| 58 | if "organic" not in results: |
| 59 | raise Exception(f"No results found for query: '{query}'. Use a less specific query.") |
| 60 | |
| 61 | web_snippets = list() |
| 62 | idx = 0 |
| 63 | if "organic" in results: |
| 64 | for page in results["organic"]: |
| 65 | idx += 1 |
| 66 | date_published = "" |
| 67 | if "date" in page: |
| 68 | date_published = "\nDate published: " + page["date"] |
| 69 | |
| 70 | source = "" |
| 71 | if "source" in page: |