Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Merge branch 'master' into dev

Andres Caicedoe2892a92a1e3
diff --cc scripts/commands.py
index 7a3633df9c,8ad9533646..42fe1ebb57
--- a/scripts/commands.py
+++ b/scripts/commands.py
@@@ -43,10 -45,15 +46,16 @@@ def get_command(response)
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,9 -117,42 +121,43 @@@ 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"""
summary = get_text_summary(url)
links = get_hyperlinks(url)
diff --cc scripts/config.py
index d98cb69891,766cb94f41..c7d04bf822
--- a/scripts/config.py
+++ b/scripts/config.py
@@@ -41,9 -42,7 +44,8 @@@ class Config(metaclass=Singleton)
# Initialize the OpenAI API client
openai.api_key = self.openai_api_key
-
def set_continuous_mode(self, value: bool):
+ """Set the continuous mode value."""
self.continuous_mode = value
def set_speak_mode(self, value: bool):
@@@ -67,12 -61,13 +69,17 @@@
self.smart_token_limit = value
def set_openai_api_key(self, value: str):
+ """Set the OpenAI API key value."""
- self.apiopenai_api_key_key = value
+ self.openai_api_key = value
def set_elevenlabs_api_key(self, value: str):
+ """Set the ElevenLabs API key value."""
self.elevenlabs_api_key = value
-
-
+ def set_google_api_key(self, value: str):
++ """Set the Google API key value."""
+ self.google_api_key = value
+
+ def set_custom_search_engine_id(self, value: str):
++ """Set the custom search engine ID value."""
+ self.custom_search_engine_id = value
diff --cc scripts/file_operations.py
index f58d2cbcb5,81ad471577..65664e609d
--- a/scripts/file_operations.py
+++ b/scripts/file_operations.py
@@@ -31,9 -29,11 +31,12 @@@ def read_file(filename)
def write_to_file(filename, text):
+ """Write text to a file"""
try:
filepath = safe_join(working_directory, filename)
+ directory = os.path.dirname(filepath)
+ if not os.path.exists(directory):
+ os.makedirs(directory)
with open(filepath, "w") as f:
f.write(text)
return "File written to successfully."
diff --cc scripts/main.py
index c4c10f0839,97c05d7dbd..0ec8978fc4
--- a/scripts/main.py
+++ b/scripts/main.py
@@@ -113,8 -95,9 +97,9 @@@ def print_assistant_thoughts(assistant_
call_stack = traceback.format_exc()
print_to_console("Error: \n", Fore.RED, call_stack)
+
def load_variables(config_file="config.yaml"):
- # Load variables from yaml file if it exists
+ """Load variables from yaml file if it exists, otherwise prompt the user for input"""
try:
with open(config_file) as file:
config = yaml.load(file, Loader=yaml.FullLoader)