| """Google search command for Autogpt.""" |
| from __future__ import annotations |
|
|
| import json |
|
|
| from duckduckgo_search import ddg |
|
|
| from autogpt.config import Config |
|
|
| CFG = Config() |
|
|
|
|
| def google_search(query: str, num_results: int = 8) -> str: |
| """Return the results of a Google search |
| |
| Args: |
| query (str): The search query. |
| num_results (int): The number of results to return. |
| |
| Returns: |
| str: The results of the search. |
| """ |
| search_results = [] |
| if not query: |
| return json.dumps(search_results) |
|
|
| results = ddg(query, max_results=num_results) |
| if not results: |
| return json.dumps(search_results) |
|
|
| for j in results: |
| search_results.append(j) |
|
|
| return json.dumps(search_results, ensure_ascii=False, indent=4) |
|
|
|
|
| def google_official_search(query: str, num_results: int = 8) -> str | list[str]: |
| """Return the results of a Google search using the official Google API |
| |
| Args: |
| query (str): The search query. |
| num_results (int): The number of results to return. |
| |
| Returns: |
| str: The results of the search. |
| """ |
|
|
| from googleapiclient.discovery import build |
| from googleapiclient.errors import HttpError |
|
|
| try: |
| |
| api_key = CFG.google_api_key |
| custom_search_engine_id = CFG.custom_search_engine_id |
|
|
| |
| service = build("customsearch", "v1", developerKey=api_key) |
|
|
| |
| result = ( |
| service.cse() |
| .list(q=query, cx=custom_search_engine_id, num=num_results) |
| .execute() |
| ) |
|
|
| |
| search_results = result.get("items", []) |
|
|
| |
| search_results_links = [item["link"] for item in search_results] |
|
|
| except HttpError as e: |
| |
| error_details = json.loads(e.content.decode()) |
|
|
| |
| if error_details.get("error", {}).get( |
| "code" |
| ) == 403 and "invalid API key" in error_details.get("error", {}).get( |
| "message", "" |
| ): |
| return "Error: The provided Google API key is invalid or missing." |
| else: |
| return f"Error: {e}" |
|
|
| |
| return search_results_links |
|
|