Merge branch 'master' into dev

This commit is contained in:
Andres Caicedo
2023-04-04 10:37:42 +02:00
7 changed files with 135 additions and 56 deletions

View File

@@ -10,6 +10,9 @@ from file_operations import read_file, write_to_file, append_to_file, delete_fil
from execute_code import execute_python_file
from json_parser import fix_and_parse_json
from googlesearch import search
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
cfg = Config()
@@ -46,7 +49,13 @@ def execute_command(command_name, arguments):
"""Execute the command and return the response"""
try:
if command_name == "google":
return google_search(arguments["input"])
# Check if the Google API key is set and use the official search method
# If the API key is not set or has only whitespaces, use the unofficial search method
if cfg.google_api_key and (cfg.google_api_key.strip() if cfg.google_api_key else None):
return google_official_search(arguments["input"])
else:
return google_search(arguments["input"])
elif command_name == "memory_add":
return commit_memory(arguments["string"])
elif command_name == "memory_del":
@@ -112,6 +121,40 @@ def google_search(query, num_results=8):
return json.dumps(search_results, ensure_ascii=False, indent=4)
def google_official_search(query, num_results=8):
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import json
try:
# Get the Google API key and Custom Search Engine ID from the config file
api_key = cfg.google_api_key
custom_search_engine_id = cfg.custom_search_engine_id
# Initialize the Custom Search API service
service = build("customsearch", "v1", developerKey=api_key)
# Send the search query and retrieve the results
result = service.cse().list(q=query, cx=custom_search_engine_id, num=num_results).execute()
# Extract the search result items from the response
search_results = result.get("items", [])
# Create a list of only the URLs from the search results
search_results_links = [item["link"] for item in search_results]
except HttpError as e:
# Handle errors in the API call
error_details = json.loads(e.content.decode())
# Check if the error is related to an invalid or missing API key
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 the list of search result URLs
return search_results_links
def browse_website(url):
"""Return the results of a google search"""