From 0cf790b633b417c092f84b52715f5ec057c045aa Mon Sep 17 00:00:00 2001 From: meta-fx Date: Mon, 10 Apr 2023 20:00:43 -0500 Subject: [PATCH 01/74] Added new env variable and speech function for alternative TTS voice --- .env.template | 1 + scripts/config.py | 11 ++++++++--- scripts/speak.py | 28 ++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/.env.template b/.env.template index 01735615..98d2ca91 100644 --- a/.env.template +++ b/.env.template @@ -13,3 +13,4 @@ OPENAI_AZURE_DEPLOYMENT_ID=deployment-id-for-azure IMAGE_PROVIDER=dalle HUGGINGFACE_API_TOKEN= USE_MAC_OS_TTS=False +USE_BRIAN_TTS=False \ No newline at end of file diff --git a/scripts/config.py b/scripts/config.py index 27cc946c..3bf5cd9a 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -57,7 +57,10 @@ class Config(metaclass=Singleton): self.use_mac_os_tts = False self.use_mac_os_tts = os.getenv("USE_MAC_OS_TTS") - + + self.use_brian_tts = False + self.use_brian_tts = os.getenv("USE_BRIAN_TTS") + self.google_api_key = os.getenv("GOOGLE_API_KEY") self.custom_search_engine_id = os.getenv("CUSTOM_SEARCH_ENGINE_ID") @@ -69,11 +72,13 @@ class Config(metaclass=Singleton): # User agent headers to use when browsing web # Some websites might just completely deny request with an error code if no user agent was found. - self.user_agent_header = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} + self.user_agent_header = { + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} self.redis_host = os.getenv("REDIS_HOST", "localhost") self.redis_port = os.getenv("REDIS_PORT", "6379") self.redis_password = os.getenv("REDIS_PASSWORD", "") - self.wipe_redis_on_start = os.getenv("WIPE_REDIS_ON_START", "True") == 'True' + self.wipe_redis_on_start = os.getenv( + "WIPE_REDIS_ON_START", "True") == 'True' self.memory_index = os.getenv("MEMORY_INDEX", 'auto-gpt') # Note that indexes must be created on db 0 in redis, this is not configureable. diff --git a/scripts/speak.py b/scripts/speak.py index 5d1e153c..ebaae8d1 100644 --- a/scripts/speak.py +++ b/scripts/speak.py @@ -1,9 +1,9 @@ +import gtts import os from playsound import playsound import requests from config import Config cfg = Config() -import gtts # TODO: Nicer names for these ids @@ -14,6 +14,7 @@ tts_headers = { "xi-api-key": cfg.elevenlabs_api_key } + def eleven_labs_speech(text, voice_index=0): """Speak text using elevenlabs.io's API""" tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format( @@ -33,23 +34,46 @@ def eleven_labs_speech(text, voice_index=0): print("Response content:", response.content) return False + +def brian_speech(text): + """Speak text using Brian with the streamelements API""" + tts_url = f"https://api.streamelements.com/kappa/v2/speech?voice=Brian&text={text}" + response = requests.get(tts_url) + + if response.status_code == 200: + with open("speech.mp3", "wb") as f: + f.write(response.content) + playsound("speech.mp3") + os.remove("speech.mp3") + return True + else: + print("Request failed with status code:", response.status_code) + print("Response content:", response.content) + return False + + def gtts_speech(text): tts = gtts.gTTS(text) tts.save("speech.mp3") playsound("speech.mp3") os.remove("speech.mp3") + def macos_tts_speech(text): os.system(f'say "{text}"') + def say_text(text, voice_index=0): if not cfg.elevenlabs_api_key: if cfg.use_mac_os_tts == 'True': macos_tts_speech(text) + elif cfg.use_brian_tts == 'True': + success = brian_speech(text) + if not success: + gtts_speech(text) else: gtts_speech(text) else: success = eleven_labs_speech(text, voice_index) if not success: gtts_speech(text) - From 3ee62211db3003312e09ff02517b0f250d7717a6 Mon Sep 17 00:00:00 2001 From: meta-fx Date: Mon, 10 Apr 2023 20:56:27 -0500 Subject: [PATCH 02/74] Fixed formatting issues --- scripts/config.py | 8 +++----- scripts/speak.py | 9 ++------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index 3bf5cd9a..f636da7d 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -72,13 +72,11 @@ class Config(metaclass=Singleton): # User agent headers to use when browsing web # Some websites might just completely deny request with an error code if no user agent was found. - self.user_agent_header = { - "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} + self.user_agent_header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} self.redis_host = os.getenv("REDIS_HOST", "localhost") self.redis_port = os.getenv("REDIS_PORT", "6379") self.redis_password = os.getenv("REDIS_PASSWORD", "") - self.wipe_redis_on_start = os.getenv( - "WIPE_REDIS_ON_START", "True") == 'True' + self.wipe_redis_on_start = os.getenv("WIPE_REDIS_ON_START", "True") == 'True' self.memory_index = os.getenv("MEMORY_INDEX", 'auto-gpt') # Note that indexes must be created on db 0 in redis, this is not configureable. @@ -139,4 +137,4 @@ class Config(metaclass=Singleton): def set_debug_mode(self, value: bool): """Set the debug mode value.""" - self.debug = value + self.debug = value \ No newline at end of file diff --git a/scripts/speak.py b/scripts/speak.py index ebaae8d1..2464c625 100644 --- a/scripts/speak.py +++ b/scripts/speak.py @@ -1,9 +1,9 @@ -import gtts import os from playsound import playsound import requests from config import Config cfg = Config() +import gtts # TODO: Nicer names for these ids @@ -14,7 +14,6 @@ tts_headers = { "xi-api-key": cfg.elevenlabs_api_key } - def eleven_labs_speech(text, voice_index=0): """Speak text using elevenlabs.io's API""" tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format( @@ -34,7 +33,6 @@ def eleven_labs_speech(text, voice_index=0): print("Response content:", response.content) return False - def brian_speech(text): """Speak text using Brian with the streamelements API""" tts_url = f"https://api.streamelements.com/kappa/v2/speech?voice=Brian&text={text}" @@ -51,18 +49,15 @@ def brian_speech(text): print("Response content:", response.content) return False - def gtts_speech(text): tts = gtts.gTTS(text) tts.save("speech.mp3") playsound("speech.mp3") os.remove("speech.mp3") - def macos_tts_speech(text): os.system(f'say "{text}"') - def say_text(text, voice_index=0): if not cfg.elevenlabs_api_key: if cfg.use_mac_os_tts == 'True': @@ -76,4 +71,4 @@ def say_text(text, voice_index=0): else: success = eleven_labs_speech(text, voice_index) if not success: - gtts_speech(text) + gtts_speech(text) \ No newline at end of file From 3cdde2d49cba4b43045ebef7f16236fed3a4acc9 Mon Sep 17 00:00:00 2001 From: meta-fx Date: Tue, 11 Apr 2023 08:15:58 -0500 Subject: [PATCH 03/74] Resolved conflicts in config.py and speak.py --- scripts/config.py | 7 ++---- scripts/speak.py | 60 ++++++++++++++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index f636da7d..cdf0287c 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -33,7 +33,7 @@ class Config(metaclass=Singleton): def __init__(self): """Initialize the Config class""" - self.debug = False + self.debug_mode = False self.continuous_mode = False self.speak_mode = False @@ -92,9 +92,6 @@ class Config(metaclass=Singleton): """Set the speak mode value.""" self.speak_mode = value - def set_debug_mode(self, value: bool): - self.debug_mode = value - def set_fast_llm_model(self, value: str): """Set the fast LLM model value.""" self.fast_llm_model = value @@ -137,4 +134,4 @@ class Config(metaclass=Singleton): def set_debug_mode(self, value: bool): """Set the debug mode value.""" - self.debug = value \ No newline at end of file + self.debug_mode = value \ No newline at end of file diff --git a/scripts/speak.py b/scripts/speak.py index 2464c625..bf5c6034 100644 --- a/scripts/speak.py +++ b/scripts/speak.py @@ -4,6 +4,8 @@ import requests from config import Config cfg = Config() import gtts +import threading +from threading import Lock, Semaphore # TODO: Nicer names for these ids @@ -14,6 +16,9 @@ tts_headers = { "xi-api-key": cfg.elevenlabs_api_key } +mutex_lock = Lock() # Ensure only one sound is played at a time +queue_semaphore = Semaphore(1) # The amount of sounds to queue before blocking the main thread + def eleven_labs_speech(text, voice_index=0): """Speak text using elevenlabs.io's API""" tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format( @@ -23,10 +28,11 @@ def eleven_labs_speech(text, voice_index=0): tts_url, headers=tts_headers, json=formatted_message) if response.status_code == 200: - with open("speech.mpeg", "wb") as f: - f.write(response.content) - playsound("speech.mpeg") - os.remove("speech.mpeg") + with mutex_lock: + with open("speech.mpeg", "wb") as f: + f.write(response.content) + playsound("speech.mpeg", True) + os.remove("speech.mpeg") return True else: print("Request failed with status code:", response.status_code) @@ -39,10 +45,11 @@ def brian_speech(text): response = requests.get(tts_url) if response.status_code == 200: - with open("speech.mp3", "wb") as f: - f.write(response.content) - playsound("speech.mp3") - os.remove("speech.mp3") + with mutex_lock: + with open("speech.mp3", "wb") as f: + f.write(response.content) + playsound("speech.mp3") + os.remove("speech.mp3") return True else: print("Request failed with status code:", response.status_code) @@ -51,24 +58,33 @@ def brian_speech(text): def gtts_speech(text): tts = gtts.gTTS(text) - tts.save("speech.mp3") - playsound("speech.mp3") - os.remove("speech.mp3") + with mutex_lock: + tts.save("speech.mp3") + playsound("speech.mp3", True) + os.remove("speech.mp3") def macos_tts_speech(text): os.system(f'say "{text}"') def say_text(text, voice_index=0): - if not cfg.elevenlabs_api_key: - if cfg.use_mac_os_tts == 'True': - macos_tts_speech(text) - elif cfg.use_brian_tts == 'True': - success = brian_speech(text) - if not success: + + def speak(): + if not cfg.elevenlabs_api_key: + if cfg.use_mac_os_tts == 'True': + macos_tts_speech(text) + elif cfg.use_brian_tts == 'True': + success = brian_speech(text) + if not success: + gtts_speech(text) + else: gtts_speech(text) else: - gtts_speech(text) - else: - success = eleven_labs_speech(text, voice_index) - if not success: - gtts_speech(text) \ No newline at end of file + success = eleven_labs_speech(text, voice_index) + if not success: + gtts_speech(text) + + queue_semaphore.release() + + queue_semaphore.acquire(True) + thread = threading.Thread(target=speak) + thread.start() \ No newline at end of file From b19eb74874a91b0c8e372ffb37d34e833de07cad Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:09:59 -0700 Subject: [PATCH 04/74] Refactor the seed prompt to be generated programmatically This removes the tedium of having to re-number every numbered item in the prompt.txt if you want to add/remove commands. --- scripts/ai_config.py | 3 +- scripts/data.py | 18 ---------- scripts/data/prompt.txt | 63 --------------------------------- scripts/main.py | 3 +- scripts/prompt.py | 51 +++++++++++++++++++++++++++ scripts/promptgenerator.py | 71 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 83 deletions(-) delete mode 100644 scripts/data.py delete mode 100644 scripts/data/prompt.txt create mode 100644 scripts/prompt.py create mode 100644 scripts/promptgenerator.py diff --git a/scripts/ai_config.py b/scripts/ai_config.py index 1d5832c1..332c7f06 100644 --- a/scripts/ai_config.py +++ b/scripts/ai_config.py @@ -1,6 +1,7 @@ import yaml import data import os +from prompt import get_prompt class AIConfig: """ @@ -90,6 +91,6 @@ class AIConfig: for i, goal in enumerate(self.ai_goals): full_prompt += f"{i+1}. {goal}\n" - full_prompt += f"\n\n{data.load_prompt()}" + full_prompt += f"\n\n{get_prompt()}" return full_prompt diff --git a/scripts/data.py b/scripts/data.py deleted file mode 100644 index f80c2875..00000000 --- a/scripts/data.py +++ /dev/null @@ -1,18 +0,0 @@ -import os -from pathlib import Path - -def load_prompt(): - """Load the prompt from data/prompt.txt""" - try: - # get directory of this file: - file_dir = Path(__file__).parent - prompt_file_path = file_dir / "data" / "prompt.txt" - - # Load the prompt from data/prompt.txt - with open(prompt_file_path, "r") as prompt_file: - prompt = prompt_file.read() - - return prompt - except FileNotFoundError: - print("Error: Prompt file not found", flush=True) - return "" diff --git a/scripts/data/prompt.txt b/scripts/data/prompt.txt deleted file mode 100644 index fc68f3ae..00000000 --- a/scripts/data/prompt.txt +++ /dev/null @@ -1,63 +0,0 @@ -CONSTRAINTS: - -1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files. -2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember. -3. No user assistance -4. Exclusively use the commands listed in double quotes e.g. "command name" - -COMMANDS: - -1. Google Search: "google", args: "input": "" -5. Browse Website: "browse_website", args: "url": "", "question": "" -6. Start GPT Agent: "start_agent", args: "name": "", "task": "", "prompt": "" -7. Message GPT Agent: "message_agent", args: "key": "", "message": "" -8. List GPT Agents: "list_agents", args: "" -9. Delete GPT Agent: "delete_agent", args: "key": "" -10. Write to file: "write_to_file", args: "file": "", "text": "" -11. Read file: "read_file", args: "file": "" -12. Append to file: "append_to_file", args: "file": "", "text": "" -13. Delete file: "delete_file", args: "file": "" -14. Search Files: "search_files", args: "directory": "" -15. Evaluate Code: "evaluate_code", args: "code": "" -16. Get Improved Code: "improve_code", args: "suggestions": "", "code": "" -17. Write Tests: "write_tests", args: "code": "", "focus": "" -18. Execute Python File: "execute_python_file", args: "file": "" -19. Task Complete (Shutdown): "task_complete", args: "reason": "" -20. Generate Image: "generate_image", args: "prompt": "" -21. Do Nothing: "do_nothing", args: "" - -RESOURCES: - -1. Internet access for searches and information gathering. -2. Long Term memory management. -3. GPT-3.5 powered Agents for delegation of simple tasks. -4. File output. - -PERFORMANCE EVALUATION: - -1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities. -2. Constructively self-criticize your big-picture behavior constantly. -3. Reflect on past decisions and strategies to refine your approach. -4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps. - -You should only respond in JSON format as described below - -RESPONSE FORMAT: -{ - "thoughts": - { - "text": "thought", - "reasoning": "reasoning", - "plan": "- short bulleted\n- list that conveys\n- long-term plan", - "criticism": "constructive self-criticism", - "speak": "thoughts summary to say to user" - }, - "command": { - "name": "command name", - "args":{ - "arg name": "value" - } - } -} - -Ensure the response can be parsed by Python json.loads diff --git a/scripts/main.py b/scripts/main.py index 4be0b2aa..a3d4f9df 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -18,6 +18,7 @@ import traceback import yaml import argparse import logging +from prompt import get_prompt cfg = Config() @@ -171,7 +172,7 @@ def load_variables(config_file="config.yaml"): with open(config_file, "w") as file: documents = yaml.dump(config, file) - prompt = data.load_prompt() + prompt = get_prompt() prompt_start = """Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.""" # Construct full prompt diff --git a/scripts/prompt.py b/scripts/prompt.py new file mode 100644 index 00000000..fd2a84a0 --- /dev/null +++ b/scripts/prompt.py @@ -0,0 +1,51 @@ +from promptgenerator import PromptGenerator + +def get_prompt(): + prompt_generator = PromptGenerator() + + # Add constraints + prompt_generator.add_constraint("~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") + prompt_generator.add_constraint("If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") + prompt_generator.add_constraint("No user assistance") + prompt_generator.add_constraint('Exclusively use the commands listed in double quotes e.g. "command name"') + + # Add commands + commands = [ + ("Google Search", "google", {"input": ""}), + ("Browse Website", "browse_website", {"url": "", "question": ""}), + ("Start GPT Agent", "start_agent", {"name": "", "task": "", "prompt": ""}), + ("Message GPT Agent", "message_agent", {"key": "", "message": ""}), + ("List GPT Agents", "list_agents", {}), + ("Delete GPT Agent", "delete_agent", {"key": ""}), + ("Write to file", "write_to_file", {"file": "", "text": ""}), + ("Read file", "read_file", {"file": ""}), + ("Append to file", "append_to_file", {"file": "", "text": ""}), + ("Delete file", "delete_file", {"file": ""}), + ("Search Files", "search_files", {"directory": ""}), + ("Evaluate Code", "evaluate_code", {"code": ""}), + ("Get Improved Code", "improve_code", {"suggestions": "", "code": ""}), + ("Write Tests", "write_tests", {"code": "", "focus": ""}), + ("Execute Python File", "execute_python_file", {"file": ""}), + ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), + ("Generate Image", "generate_image", {"prompt": ""}), + ("Do Nothing", "do_nothing", {}), + ] + + for command_label, command_name, args in commands: + prompt_generator.add_command(command_label, command_name, args) + + # Add resources + prompt_generator.add_resource("Internet access for searches and information gathering.") + prompt_generator.add_resource("Long Term memory management.") + prompt_generator.add_resource("GPT-3.5 powered Agents for delegation of simple tasks.") + prompt_generator.add_resource("File output.") + + # Add performance evaluation + prompt_generator.add_performance_evaluation("Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") + prompt_generator.add_performance_evaluation("Constructively self-criticize your big-picture behavior constantly.") + prompt_generator.add_performance_evaluation("Reflect on past decisions and strategies to refine your approach.") + prompt_generator.add_performance_evaluation("Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") + + # Generate prompt string + prompt_string = prompt_generator.generate_prompt_string() + return prompt_string diff --git a/scripts/promptgenerator.py b/scripts/promptgenerator.py new file mode 100644 index 00000000..1ae43aa7 --- /dev/null +++ b/scripts/promptgenerator.py @@ -0,0 +1,71 @@ +import json + + +class PromptGenerator: + def __init__(self): + self.constraints = [] + self.commands = [] + self.resources = [] + self.performance_evaluation = [] + self.response_format = { + "thoughts": { + "text": "thought", + "reasoning": "reasoning", + "plan": "- short bulleted\n- list that conveys\n- long-term plan", + "criticism": "constructive self-criticism", + "speak": "thoughts summary to say to user" + }, + "command": { + "name": "command name", + "args": { + "arg name": "value" + } + } + } + + def add_constraint(self, constraint): + self.constraints.append(constraint) + + # {CommandLabel}: "{CommandName}", args: "{arg#Name}": "{arg#Prompt}" + def add_command(self, command_label, command_name, args=None): + if args is None: + args = {} + + command_args = {arg_key: arg_value for arg_key, arg_value in args.items()} + + command = { + "label": command_label, + "name": command_name, + "args": command_args, + } + + self.commands.append(command) + + def _generate_command_string(self, command): + args_string = ', '.join(f'"{key}": "{value}"' for key, value in command['args'].items()) + return f'{command["label"]}: "{command["name"]}", args: {args_string}' + + def add_resource(self, resource): + self.resources.append(resource) + + def add_performance_evaluation(self, evaluation): + self.performance_evaluation.append(evaluation) + + + def _generate_numbered_list(self, items, item_type='list'): + if item_type == 'command': + return "\n".join(f"{i+1}. {self._generate_command_string(item)}" for i, item in enumerate(items)) + else: + return "\n".join(f"{i+1}. {item}" for i, item in enumerate(items)) + + def generate_prompt_string(self): + formatted_response_format = json.dumps(self.response_format, indent=4) + prompt_string = ( + f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n" + f"Commands:\n{self._generate_numbered_list(self.commands, item_type='command')}\n\n" + f"Resources:\n{self._generate_numbered_list(self.resources)}\n\n" + f"Performance Evaluation:\n{self._generate_numbered_list(self.performance_evaluation)}\n\n" + f"You should only respond in JSON format as described below \nResponse Format: \n{formatted_response_format} \nEnsure the response can be parsed by Python json.loads" + ) + + return prompt_string From fd1cfd2eff6bcb46af6e7728fa57dc980dde0e61 Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:15:45 -0700 Subject: [PATCH 05/74] Add docs and format code --- scripts/prompt.py | 69 +++++++++++++++++++++++++------------ scripts/promptgenerator.py | 70 ++++++++++++++++++++++++++++++++++---- 2 files changed, 112 insertions(+), 27 deletions(-) diff --git a/scripts/prompt.py b/scripts/prompt.py index fd2a84a0..e499a5f6 100644 --- a/scripts/prompt.py +++ b/scripts/prompt.py @@ -1,51 +1,78 @@ from promptgenerator import PromptGenerator + def get_prompt(): + """ + This function generates a prompt string that includes various constraints, commands, resources, and performance evaluations. + + Returns: + str: The generated prompt string. + """ + + # Initialize the PromptGenerator object prompt_generator = PromptGenerator() - # Add constraints - prompt_generator.add_constraint("~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") - prompt_generator.add_constraint("If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") + # Add constraints to the PromptGenerator object + prompt_generator.add_constraint( + "~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") + prompt_generator.add_constraint( + "If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") prompt_generator.add_constraint("No user assistance") - prompt_generator.add_constraint('Exclusively use the commands listed in double quotes e.g. "command name"') + prompt_generator.add_constraint( + 'Exclusively use the commands listed in double quotes e.g. "command name"') - # Add commands + # Define the command list commands = [ ("Google Search", "google", {"input": ""}), - ("Browse Website", "browse_website", {"url": "", "question": ""}), - ("Start GPT Agent", "start_agent", {"name": "", "task": "", "prompt": ""}), - ("Message GPT Agent", "message_agent", {"key": "", "message": ""}), + ("Browse Website", "browse_website", { + "url": "", "question": ""}), + ("Start GPT Agent", "start_agent", { + "name": "", "task": "", "prompt": ""}), + ("Message GPT Agent", "message_agent", { + "key": "", "message": ""}), ("List GPT Agents", "list_agents", {}), ("Delete GPT Agent", "delete_agent", {"key": ""}), - ("Write to file", "write_to_file", {"file": "", "text": ""}), + ("Write to file", "write_to_file", { + "file": "", "text": ""}), ("Read file", "read_file", {"file": ""}), - ("Append to file", "append_to_file", {"file": "", "text": ""}), + ("Append to file", "append_to_file", { + "file": "", "text": ""}), ("Delete file", "delete_file", {"file": ""}), ("Search Files", "search_files", {"directory": ""}), ("Evaluate Code", "evaluate_code", {"code": ""}), - ("Get Improved Code", "improve_code", {"suggestions": "", "code": ""}), - ("Write Tests", "write_tests", {"code": "", "focus": ""}), + ("Get Improved Code", "improve_code", { + "suggestions": "", "code": ""}), + ("Write Tests", "write_tests", { + "code": "", "focus": ""}), ("Execute Python File", "execute_python_file", {"file": ""}), ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), ("Generate Image", "generate_image", {"prompt": ""}), ("Do Nothing", "do_nothing", {}), ] + # Add commands to the PromptGenerator object for command_label, command_name, args in commands: prompt_generator.add_command(command_label, command_name, args) - # Add resources - prompt_generator.add_resource("Internet access for searches and information gathering.") + # Add resources to the PromptGenerator object + prompt_generator.add_resource( + "Internet access for searches and information gathering.") prompt_generator.add_resource("Long Term memory management.") - prompt_generator.add_resource("GPT-3.5 powered Agents for delegation of simple tasks.") + prompt_generator.add_resource( + "GPT-3.5 powered Agents for delegation of simple tasks.") prompt_generator.add_resource("File output.") - # Add performance evaluation - prompt_generator.add_performance_evaluation("Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") - prompt_generator.add_performance_evaluation("Constructively self-criticize your big-picture behavior constantly.") - prompt_generator.add_performance_evaluation("Reflect on past decisions and strategies to refine your approach.") - prompt_generator.add_performance_evaluation("Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") + # Add performance evaluations to the PromptGenerator object + prompt_generator.add_performance_evaluation( + "Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") + prompt_generator.add_performance_evaluation( + "Constructively self-criticize your big-picture behavior constantly.") + prompt_generator.add_performance_evaluation( + "Reflect on past decisions and strategies to refine your approach.") + prompt_generator.add_performance_evaluation( + "Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") - # Generate prompt string + # Generate the prompt string prompt_string = prompt_generator.generate_prompt_string() + return prompt_string diff --git a/scripts/promptgenerator.py b/scripts/promptgenerator.py index 1ae43aa7..6cfd9bcd 100644 --- a/scripts/promptgenerator.py +++ b/scripts/promptgenerator.py @@ -2,7 +2,14 @@ import json class PromptGenerator: + """ + A class for generating custom prompt strings based on constraints, commands, resources, and performance evaluations. + """ + def __init__(self): + """ + Initialize the PromptGenerator object with empty lists of constraints, commands, resources, and performance evaluations. + """ self.constraints = [] self.commands = [] self.resources = [] @@ -24,14 +31,28 @@ class PromptGenerator: } def add_constraint(self, constraint): + """ + Add a constraint to the constraints list. + + Args: + constraint (str): The constraint to be added. + """ self.constraints.append(constraint) - # {CommandLabel}: "{CommandName}", args: "{arg#Name}": "{arg#Prompt}" def add_command(self, command_label, command_name, args=None): + """ + Add a command to the commands list with a label, name, and optional arguments. + + Args: + command_label (str): The label of the command. + command_name (str): The name of the command. + args (dict, optional): A dictionary containing argument names and their values. Defaults to None. + """ if args is None: args = {} - - command_args = {arg_key: arg_value for arg_key, arg_value in args.items()} + + command_args = {arg_key: arg_value for arg_key, + arg_value in args.items()} command = { "label": command_label, @@ -42,23 +63,60 @@ class PromptGenerator: self.commands.append(command) def _generate_command_string(self, command): - args_string = ', '.join(f'"{key}": "{value}"' for key, value in command['args'].items()) + """ + Generate a formatted string representation of a command. + + Args: + command (dict): A dictionary containing command information. + + Returns: + str: The formatted command string. + """ + args_string = ', '.join( + f'"{key}": "{value}"' for key, value in command['args'].items()) return f'{command["label"]}: "{command["name"]}", args: {args_string}' - + def add_resource(self, resource): + """ + Add a resource to the resources list. + + Args: + resource (str): The resource to be added. + """ self.resources.append(resource) def add_performance_evaluation(self, evaluation): + """ + Add a performance evaluation item to the performance_evaluation list. + + Args: + evaluation (str): The evaluation item to be added. + """ self.performance_evaluation.append(evaluation) - def _generate_numbered_list(self, items, item_type='list'): + """ + Generate a numbered list from given items based on the item_type. + + Args: + items (list): A list of items to be numbered. + item_type (str, optional): The type of items in the list. Defaults to 'list'. + + Returns: + str: The formatted numbered list. + """ if item_type == 'command': return "\n".join(f"{i+1}. {self._generate_command_string(item)}" for i, item in enumerate(items)) else: return "\n".join(f"{i+1}. {item}" for i, item in enumerate(items)) def generate_prompt_string(self): + """ + Generate a prompt string based on the constraints, commands, resources, and performance evaluations. + + Returns: + str: The generated prompt string. + """ formatted_response_format = json.dumps(self.response_format, indent=4) prompt_string = ( f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n" From 72d4783a1d0e399972e16bdbcc2ac93e2c8b0f1d Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:21:20 -0700 Subject: [PATCH 06/74] formatting --- scripts/prompt.py | 53 ++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/scripts/prompt.py b/scripts/prompt.py index e499a5f6..bbdfa5ec 100644 --- a/scripts/prompt.py +++ b/scripts/prompt.py @@ -1,10 +1,9 @@ from promptgenerator import PromptGenerator - def get_prompt(): """ This function generates a prompt string that includes various constraints, commands, resources, and performance evaluations. - + Returns: str: The generated prompt string. """ @@ -13,37 +12,27 @@ def get_prompt(): prompt_generator = PromptGenerator() # Add constraints to the PromptGenerator object - prompt_generator.add_constraint( - "~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") - prompt_generator.add_constraint( - "If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") + prompt_generator.add_constraint("~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") + prompt_generator.add_constraint("If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") prompt_generator.add_constraint("No user assistance") - prompt_generator.add_constraint( - 'Exclusively use the commands listed in double quotes e.g. "command name"') + prompt_generator.add_constraint('Exclusively use the commands listed in double quotes e.g. "command name"') # Define the command list commands = [ ("Google Search", "google", {"input": ""}), - ("Browse Website", "browse_website", { - "url": "", "question": ""}), - ("Start GPT Agent", "start_agent", { - "name": "", "task": "", "prompt": ""}), - ("Message GPT Agent", "message_agent", { - "key": "", "message": ""}), + ("Browse Website", "browse_website", {"url": "", "question": ""}), + ("Start GPT Agent", "start_agent", {"name": "", "task": "", "prompt": ""}), + ("Message GPT Agent", "message_agent", {"key": "", "message": ""}), ("List GPT Agents", "list_agents", {}), ("Delete GPT Agent", "delete_agent", {"key": ""}), - ("Write to file", "write_to_file", { - "file": "", "text": ""}), + ("Write to file", "write_to_file", {"file": "", "text": ""}), ("Read file", "read_file", {"file": ""}), - ("Append to file", "append_to_file", { - "file": "", "text": ""}), + ("Append to file", "append_to_file", {"file": "", "text": ""}), ("Delete file", "delete_file", {"file": ""}), ("Search Files", "search_files", {"directory": ""}), ("Evaluate Code", "evaluate_code", {"code": ""}), - ("Get Improved Code", "improve_code", { - "suggestions": "", "code": ""}), - ("Write Tests", "write_tests", { - "code": "", "focus": ""}), + ("Get Improved Code", "improve_code", {"suggestions": "", "code": ""}), + ("Write Tests", "write_tests", {"code": "", "focus": ""}), ("Execute Python File", "execute_python_file", {"file": ""}), ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), ("Generate Image", "generate_image", {"prompt": ""}), @@ -55,24 +44,18 @@ def get_prompt(): prompt_generator.add_command(command_label, command_name, args) # Add resources to the PromptGenerator object - prompt_generator.add_resource( - "Internet access for searches and information gathering.") + prompt_generator.add_resource("Internet access for searches and information gathering.") prompt_generator.add_resource("Long Term memory management.") - prompt_generator.add_resource( - "GPT-3.5 powered Agents for delegation of simple tasks.") + prompt_generator.add_resource("GPT-3.5 powered Agents for delegation of simple tasks.") prompt_generator.add_resource("File output.") # Add performance evaluations to the PromptGenerator object - prompt_generator.add_performance_evaluation( - "Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") - prompt_generator.add_performance_evaluation( - "Constructively self-criticize your big-picture behavior constantly.") - prompt_generator.add_performance_evaluation( - "Reflect on past decisions and strategies to refine your approach.") - prompt_generator.add_performance_evaluation( - "Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") + prompt_generator.add_performance_evaluation("Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") + prompt_generator.add_performance_evaluation("Constructively self-criticize your big-picture behavior constantly.") + prompt_generator.add_performance_evaluation("Reflect on past decisions and strategies to refine your approach.") + prompt_generator.add_performance_evaluation("Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") # Generate the prompt string prompt_string = prompt_generator.generate_prompt_string() - + return prompt_string From 8bbfdeb04a5b98070bf0b44c9dc819c66159f05f Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:43:37 -0700 Subject: [PATCH 07/74] Add unit tests for prompt generator class --- tests/promptgenerator_tests.py | 99 ++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/promptgenerator_tests.py diff --git a/tests/promptgenerator_tests.py b/tests/promptgenerator_tests.py new file mode 100644 index 00000000..ac5c3a79 --- /dev/null +++ b/tests/promptgenerator_tests.py @@ -0,0 +1,99 @@ +# Import the required libraries for unit testing +import unittest +import sys +import os + +# Add the path to the "scripts" directory to import the PromptGenerator module +sys.path.append(os.path.abspath("../scripts")) +from promptgenerator import PromptGenerator + +# Create a test class for the PromptGenerator, subclassed from unittest.TestCase +class promptgenerator_tests(unittest.TestCase): + + # Set up the initial state for each test method by creating an instance of PromptGenerator + def setUp(self): + self.generator = PromptGenerator() + + # Test whether the add_constraint() method adds a constraint to the generator's constraints list + def test_add_constraint(self): + constraint = "Constraint1" + self.generator.add_constraint(constraint) + self.assertIn(constraint, self.generator.constraints) + + # Test whether the add_command() method adds a command to the generator's commands list + def test_add_command(self): + command_label = "Command Label" + command_name = "command_name" + args = {"arg1": "value1", "arg2": "value2"} + self.generator.add_command(command_label, command_name, args) + command = { + "label": command_label, + "name": command_name, + "args": args, + } + self.assertIn(command, self.generator.commands) + + # Test whether the add_resource() method adds a resource to the generator's resources list + def test_add_resource(self): + resource = "Resource1" + self.generator.add_resource(resource) + self.assertIn(resource, self.generator.resources) + + # Test whether the add_performance_evaluation() method adds an evaluation to the generator's performance_evaluation list + def test_add_performance_evaluation(self): + evaluation = "Evaluation1" + self.generator.add_performance_evaluation(evaluation) + self.assertIn(evaluation, self.generator.performance_evaluation) + + # Test whether the generate_prompt_string() method generates a prompt string with all the added constraints, commands, resources and evaluations + def test_generate_prompt_string(self): + constraints = ["Constraint1", "Constraint2"] + commands = [ + { + "label": "Command1", + "name": "command_name1", + "args": {"arg1": "value1"}, + }, + { + "label": "Command2", + "name": "command_name2", + "args": {}, + }, + ] + resources = ["Resource1", "Resource2"] + evaluations = ["Evaluation1", "Evaluation2"] + + # Add all the constraints, commands, resources, and evaluations to the generator + for constraint in constraints: + self.generator.add_constraint(constraint) + for command in commands: + self.generator.add_command( + command["label"], command["name"], command["args"]) + for resource in resources: + self.generator.add_resource(resource) + for evaluation in evaluations: + self.generator.add_performance_evaluation(evaluation) + + # Generate the prompt string and verify its correctness + prompt_string = self.generator.generate_prompt_string() + self.assertIsNotNone(prompt_string) + for constraint in constraints: + self.assertIn(constraint, prompt_string) + for command in commands: + self.assertIn(command["name"], prompt_string) + + # Check for each key-value pair in the command args dictionary + for key, value in command["args"].items(): + self.assertIn(f'"{key}": "{value}"', prompt_string) + for resource in resources: + self.assertIn(resource, prompt_string) + for evaluation in evaluations: + self.assertIn(evaluation, prompt_string) + self.assertIn("constraints", prompt_string.lower()) + self.assertIn("commands", prompt_string.lower()) + self.assertIn("resources", prompt_string.lower()) + self.assertIn("performance evaluation", prompt_string.lower()) + +# Run the tests when this script is executed +if __name__ == '__main__': + unittest.main() From de2281d824c13c1eadc3807d2437c58209ab2184 Mon Sep 17 00:00:00 2001 From: Mike Kelly Date: Tue, 11 Apr 2023 17:30:15 +0100 Subject: [PATCH 08/74] add docker compose scheduling --- docker-compose.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..af086f05 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +# To boot the app run the following: +# docker-compose run auto-gpt +version: "3.9" + +services: + auto-gpt: + depends_on: + - redis + build: ./ + volumes: + - "./scripts:/app" + - ".env:/app/.env" + profiles: ["exclude-from-up"] + + redis: + image: "redis/redis-stack-server:latest" From 7a0c9e8a9d13ef5930b56ee13e885c3b69deb293 Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 10:30:53 -0700 Subject: [PATCH 09/74] fix attempts to import a non-existent module --- scripts/ai_config.py | 1 - scripts/main.py | 1 - 2 files changed, 2 deletions(-) diff --git a/scripts/ai_config.py b/scripts/ai_config.py index 9aa01332..36e8be3c 100644 --- a/scripts/ai_config.py +++ b/scripts/ai_config.py @@ -1,5 +1,4 @@ import yaml -import data import os from prompt import get_prompt diff --git a/scripts/main.py b/scripts/main.py index 0946f21f..b51d486a 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -3,7 +3,6 @@ import random import commands as cmd import utils from memory import get_memory -import data import chat from colorama import Fore, Style from spinner import Spinner From 570f76bd51a2f01c78788938eadc17348bb92fe3 Mon Sep 17 00:00:00 2001 From: meta-fx Date: Tue, 11 Apr 2023 14:40:05 -0500 Subject: [PATCH 10/74] Removed trailing spaces and fixed CRLF being removed --- scripts/config.py | 4 ++-- scripts/speak.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index cdf0287c..c4ad3bf4 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -72,7 +72,7 @@ class Config(metaclass=Singleton): # User agent headers to use when browsing web # Some websites might just completely deny request with an error code if no user agent was found. - self.user_agent_header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} + self.user_agent_header = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} self.redis_host = os.getenv("REDIS_HOST", "localhost") self.redis_port = os.getenv("REDIS_PORT", "6379") self.redis_password = os.getenv("REDIS_PASSWORD", "") @@ -134,4 +134,4 @@ class Config(metaclass=Singleton): def set_debug_mode(self, value: bool): """Set the debug mode value.""" - self.debug_mode = value \ No newline at end of file + self.debug_mode = value diff --git a/scripts/speak.py b/scripts/speak.py index bf5c6034..4934ecef 100644 --- a/scripts/speak.py +++ b/scripts/speak.py @@ -67,7 +67,7 @@ def macos_tts_speech(text): os.system(f'say "{text}"') def say_text(text, voice_index=0): - + def speak(): if not cfg.elevenlabs_api_key: if cfg.use_mac_os_tts == 'True': @@ -87,4 +87,4 @@ def say_text(text, voice_index=0): queue_semaphore.acquire(True) thread = threading.Thread(target=speak) - thread.start() \ No newline at end of file + thread.start() From afc7fa6e26efbb781644ceea99004b5c3de55f40 Mon Sep 17 00:00:00 2001 From: Robin Richtsfeld Date: Wed, 12 Apr 2023 03:09:08 +0200 Subject: [PATCH 11/74] Fix JSON formatting in prompt.txt --- scripts/data/prompt.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/data/prompt.txt b/scripts/data/prompt.txt index fc68f3ae..50e9d3fe 100644 --- a/scripts/data/prompt.txt +++ b/scripts/data/prompt.txt @@ -44,8 +44,7 @@ You should only respond in JSON format as described below RESPONSE FORMAT: { - "thoughts": - { + "thoughts": { "text": "thought", "reasoning": "reasoning", "plan": "- short bulleted\n- list that conveys\n- long-term plan", @@ -54,7 +53,7 @@ RESPONSE FORMAT: }, "command": { "name": "command name", - "args":{ + "args": { "arg name": "value" } } From c785352ed2c6f1744bdeac5410ea93199bade937 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 16:23:09 +0200 Subject: [PATCH 12/74] Update main.py clean trailing whitespace --- scripts/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index 15af0c38..3dcedb5f 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -293,7 +293,7 @@ def parse_arguments(): if args.gpt3only: print_to_console("GPT3.5 Only Mode: ", Fore.GREEN, "ENABLED") cfg.set_smart_llm_model(cfg.fast_llm_model) - + if args.gpt4only: print_to_console("GPT4 Only Mode: ", Fore.GREEN, "ENABLED") cfg.set_fast_llm_model(cfg.smart_llm_model) From c986e8713512aad9f06c074b5e7fdfa31ade2df7 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 16:36:27 +0200 Subject: [PATCH 13/74] Edit config Class to manage browse_website command chunk size and summary size I added two new config parameters: - browse_chunk_max_length: define the max_length of a chunk being sent to the memory and to FAST_LLM_MODEL for summarizing - browse_summary_max_token: define the max_token passed to the model use for summary creation. Changing this can help with complex subject, allowing the agent to be more verbose in its attemps to summarize the chunk and the chunks summary. I've also edited the way the user_agent is handle. --- .env.template | 2 ++ scripts/config.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.env.template b/.env.template index 6fbc8424..0953fac9 100644 --- a/.env.template +++ b/.env.template @@ -15,3 +15,5 @@ OPENAI_AZURE_DEPLOYMENT_ID=deployment-id-for-azure IMAGE_PROVIDER=dalle HUGGINGFACE_API_TOKEN= USE_MAC_OS_TTS=False +BROWSE_CHUNK_MAX_LENGTH=4000 +BROWSE_SUMMARY_MAX_TOKEN=300 \ No newline at end of file diff --git a/scripts/config.py b/scripts/config.py index a280e6cc..1eeeb72f 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -41,6 +41,8 @@ class Config(metaclass=Singleton): self.smart_llm_model = os.getenv("SMART_LLM_MODEL", "gpt-4") self.fast_token_limit = int(os.getenv("FAST_TOKEN_LIMIT", 4000)) self.smart_token_limit = int(os.getenv("SMART_TOKEN_LIMIT", 8000)) + self.browse_chunk_max_length = int(os.getenv("BROWSE_CHUNK_MAX_LENGTH", 8000)) + self.browse_summary_max_token = int(os.getenv("BROWSE_SUMMARY_MAX_TOKEN", 300)) self.openai_api_key = os.getenv("OPENAI_API_KEY") self.use_azure = False @@ -71,7 +73,8 @@ class Config(metaclass=Singleton): # User agent headers to use when browsing web # Some websites might just completely deny request with an error code if no user agent was found. - self.user_agent_header = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} + self.user_agent = os.getenv("USER_AGENT", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36") + self.redis_host = os.getenv("REDIS_HOST", "localhost") self.redis_port = os.getenv("REDIS_PORT", "6379") self.redis_password = os.getenv("REDIS_PASSWORD", "") @@ -80,6 +83,7 @@ class Config(metaclass=Singleton): # Note that indexes must be created on db 0 in redis, this is not configurable. self.memory_backend = os.getenv("MEMORY_BACKEND", 'local') + # Initialize the OpenAI API client openai.api_key = self.openai_api_key @@ -107,6 +111,14 @@ class Config(metaclass=Singleton): """Set the smart token limit value.""" self.smart_token_limit = value + def set_browse_chunk_max_length(self, value: int): + """Set the browse_website command chunk max length value.""" + self.browse_chunk_max_length = value + + def set_browse_summary_max_token(self, value: int): + """Set the browse_website command summary max token value.""" + self.browse_summary_max_token = value + def set_openai_api_key(self, value: str): """Set the OpenAI API key value.""" self.openai_api_key = value From b20c0117c5732e73005ee9fc12380078d5ea442c Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 16:38:49 +0200 Subject: [PATCH 14/74] Add memory management to browse.py - Change the way User-Agent is handle when calling requests to browse website - Add chunk to memory before and after summary. We do not save the "summary of summaries" as this wasn't performing great and caused noise when the "question" couldn't be answered. - Use the newly added config parameters for max_length and max_token --- scripts/browse.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/scripts/browse.py b/scripts/browse.py index b0c745ef..5f4aafe6 100644 --- a/scripts/browse.py +++ b/scripts/browse.py @@ -1,9 +1,14 @@ import requests from bs4 import BeautifulSoup +from memory import get_memory from config import Config from llm_utils import create_chat_completion cfg = Config() +memory = get_memory(cfg) + +session = requests.Session() +session.headers.update({'User-Agent': cfg.user_agent}) # Define and check for local file address prefixes def check_local_file_access(url): @@ -21,7 +26,7 @@ def scrape_text(url): return "Error: Access to local files is restricted" try: - response = requests.get(url, headers=cfg.user_agent_header) + response = session.get(url) except requests.exceptions.RequestException as e: return "Error: " + str(e) @@ -60,7 +65,7 @@ def format_hyperlinks(hyperlinks): def scrape_links(url): """Scrape links from a webpage""" - response = requests.get(url, headers=cfg.user_agent_header) + response = session.get(url) # Check if the response contains an HTTP error if response.status_code >= 400: @@ -76,7 +81,7 @@ def scrape_links(url): return format_hyperlinks(hyperlinks) -def split_text(text, max_length=8192): +def split_text(text, max_length=cfg.browse_chunk_max_length): """Split text into chunks of a maximum length""" paragraphs = text.split("\n") current_length = 0 @@ -102,7 +107,7 @@ def create_message(chunk, question): "content": f"\"\"\"{chunk}\"\"\" Using the above text, please answer the following question: \"{question}\" -- if the question cannot be answered using the text, please summarize the text." } -def summarize_text(text, question): +def summarize_text(url, text, question): """Summarize text using the LLM model""" if not text: return "Error: No text to summarize" @@ -114,15 +119,28 @@ def summarize_text(text, question): chunks = list(split_text(text)) for i, chunk in enumerate(chunks): + print(f"Adding chunk {i + 1} / {len(chunks)} to memory") + + memory_to_add = f"Source: {url}\n" \ + f"Raw content part#{i + 1}: {chunk}" + + memory.add(memory_to_add) + print(f"Summarizing chunk {i + 1} / {len(chunks)}") messages = [create_message(chunk, question)] summary = create_chat_completion( model=cfg.fast_llm_model, messages=messages, - max_tokens=300, + max_tokens=cfg.browse_summary_max_token, ) summaries.append(summary) + print(f"Added chunk {i + 1} summary to memory") + + memory_to_add = f"Source: {url}\n" \ + f"Content summary part#{i + 1}: {summary}" + + memory.add(memory_to_add) print(f"Summarized {len(chunks)} chunks.") @@ -132,7 +150,7 @@ def summarize_text(text, question): final_summary = create_chat_completion( model=cfg.fast_llm_model, messages=messages, - max_tokens=300, + max_tokens=cfg.browse_summary_max_token, ) return final_summary From 5bb551db95fe1eb6765c61fa28bf384d8252cdad Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 16:42:14 +0200 Subject: [PATCH 15/74] add the url variable in the get_text_summary function to pass it to the memory By sending the url along when calling browse.summarize_text, we can then add it along the chunk in memory. --- scripts/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/commands.py b/scripts/commands.py index 92d46ae1..90d7a6f3 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -183,7 +183,7 @@ def browse_website(url, question): def get_text_summary(url, question): """Return the results of a google search""" text = browse.scrape_text(url) - summary = browse.summarize_text(text, question) + summary = browse.summarize_text(url, text, question) return """ "Result" : """ + summary From c5f0cb3d3faa06d0fa7335569247310d08e5f004 Mon Sep 17 00:00:00 2001 From: profound Date: Wed, 12 Apr 2023 23:38:30 +0800 Subject: [PATCH 16/74] fix read config file encoding that broke Chinese --- scripts/ai_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ai_config.py b/scripts/ai_config.py index bd373944..743c87e4 100644 --- a/scripts/ai_config.py +++ b/scripts/ai_config.py @@ -46,7 +46,7 @@ class AIConfig: """ try: - with open(config_file) as file: + with open(config_file, encoding='utf-8') as file: config_params = yaml.load(file, Loader=yaml.FullLoader) except FileNotFoundError: config_params = {} From a615e570616146ba51336b4160c2eff225479769 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 18:00:17 +0200 Subject: [PATCH 17/74] Revert "Update main.py" This reverts commit c785352ed2c6f1744bdeac5410ea93199bade937. --- scripts/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index 3dcedb5f..15af0c38 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -293,7 +293,7 @@ def parse_arguments(): if args.gpt3only: print_to_console("GPT3.5 Only Mode: ", Fore.GREEN, "ENABLED") cfg.set_smart_llm_model(cfg.fast_llm_model) - + if args.gpt4only: print_to_console("GPT4 Only Mode: ", Fore.GREEN, "ENABLED") cfg.set_fast_llm_model(cfg.smart_llm_model) From 8baa0769b154f3742cdc75e07404952de02e0669 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 18:03:59 +0200 Subject: [PATCH 18/74] Update config.py --- scripts/config.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/config.py b/scripts/config.py index 1eeeb72f..9c4e4572 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -83,7 +83,6 @@ class Config(metaclass=Singleton): # Note that indexes must be created on db 0 in redis, this is not configurable. self.memory_backend = os.getenv("MEMORY_BACKEND", 'local') - # Initialize the OpenAI API client openai.api_key = self.openai_api_key From d237cf3d87526e2aad6180de6a5ed531efca858d Mon Sep 17 00:00:00 2001 From: lekapsy <117356974+lekapsy@users.noreply.github.com> Date: Wed, 12 Apr 2023 18:54:10 +0200 Subject: [PATCH 19/74] Improve .env File Organization, Readability, and Documentation This pull request aims to enhance the organization, readability, and understanding of the .env.template file for users when they modify the settings. The changes include organizing the file in a tree-like structure with appropriate comments, providing clear guidance for users about the purpose of each variable, their possible values, and default settings when applicable. As a user with no prior knowledge of best practices of contributing to a project / .env.template file documentation, I took the liberty to make changes to the file based on what I would have liked to have seen when I first encountered it. My goal was to include every configurable option for ease of use and better understanding of how the code works. The key improvements made in this pull request are: 1. Grouping related variables under appropriate headers for better organization and ease of navigation. 2. Adding informative comments for each variable to help users understand their purpose and possible values. 3. Including default values in the comments to inform users of the consequences of not providing a specific value for a variable, allowing them to make informed decisions when configuring the application. 4. Formatting the file consistently for better readability. These changes will enhance user experience by simplifying the configuration process and reducing potential confusion. Users can quickly and easily configure the application without having to search through the code to determine default values or understand the relationship between various settings. Additionally, well-organized code and documentation can lead to fewer issues and misunderstandings, saving time for both users and maintainers of the project. Please review these changes and let me know if you have any questions or suggestions for further improvement so I can make any necessary adjustments. --- .env.template | 98 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 9 deletions(-) diff --git a/.env.template b/.env.template index 6fbc8424..9127659a 100644 --- a/.env.template +++ b/.env.template @@ -1,17 +1,97 @@ -PINECONE_API_KEY=your-pinecone-api-key -PINECONE_ENV=your-pinecone-region +################################################################################ +### LLM PROVIDER +################################################################################ + +### OPENAI +# OPENAI_API_KEY - OpenAI API Key (Example: my-openai-api-key) OPENAI_API_KEY=your-openai-api-key -ELEVENLABS_API_KEY=your-elevenlabs-api-key -ELEVENLABS_VOICE_1_ID=your-voice-id -ELEVENLABS_VOICE_2_ID=your-voice-id -SMART_LLM_MODEL=gpt-4 -FAST_LLM_MODEL=gpt-3.5-turbo -GOOGLE_API_KEY= -CUSTOM_SEARCH_ENGINE_ID= + +# Use Azure OpenAI +# USE_AZURE - Use Azure OpenAI or not (Default: False) USE_AZURE=False + +### AZURE OPENAI +# OPENAI_AZURE_API_BASE - OpenAI API base URL for Azure (Example: https://my-azure-openai-url.com) OPENAI_AZURE_API_BASE=your-base-url-for-azure +# OPENAI_AZURE_API_VERSION - OpenAI API version for Azure (Example: v1) OPENAI_AZURE_API_VERSION=api-version-for-azure +# OPENAI_AZURE_DEPLOYMENT_ID - OpenAI deployment ID for Azure (Example: my-deployment-id) OPENAI_AZURE_DEPLOYMENT_ID=deployment-id-for-azure + +################################################################################ +### LLM MODELS +################################################################################ + +# SMART_LLM_MODEL - Smart language model (Default: gpt-4) +SMART_LLM_MODEL=gpt-4 +# FAST_LLM_MODEL - Fast language model (Default: gpt-3.5-turbo) +FAST_LLM_MODEL=gpt-3.5-turbo + +### LLM MODEL SETTINGS +# FAST_TOKEN_LIMIT - Fast token limit for OpenAI (Default: 4000) +FAST_TOKEN_LIMIT=4000 +# SMART_TOKEN_LIMIT - Smart token limit for OpenAI (Default: 8000) +# When using --gpt3only this needs to be set to 4000. +SMART_TOKEN_LIMIT=8000 + +################################################################################ +### MEMORY +################################################################################ + +# MEMORY_BACKEND - Memory backend type (Default: local) +MEMORY_BACKEND=redis + +### PINECONE +# PINECONE_API_KEY - Pinecone API Key (Example: my-pinecone-api-key) +PINECONE_API_KEY=your-pinecone-api-key +# PINECONE_ENV - Pinecone environment (region) (Example: us-west-2) +PINECONE_ENV=your-pinecone-region + +### REDIS +# REDIS_HOST - Redis host (Default: localhost) +REDIS_HOST=localhost +# REDIS_PORT - Redis port (Default: 6379) +REDIS_PORT=6379 +# REDIS_PASSWORD - Redis password (Default: "") +REDIS_PASSWORD= + +################################################################################ +### IMAGE GENERATION PROVIDER +################################################################################ + +### OPEN AI +# IMAGE_PROVIDER - Image provider (Example: dalle) IMAGE_PROVIDER=dalle + +### HUGGINGFACE +# STABLE DIFFUSION( +# Default URL: https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4 +# Set in image_gen.py) +# HUGGINGFACE_API_TOKEN - HuggingFace API token (Example: my-huggingface-api-token) HUGGINGFACE_API_TOKEN= + +################################################################################ +### SEARCH PROVIDER +################################################################################ + +### GOOGLE +# GOOGLE_API_KEY - Google API key (Example: my-google-api-key) +GOOGLE_API_KEY= +# CUSTOM_SEARCH_ENGINE_ID - Custom search engine ID (Example: my-custom-search-engine-id) +CUSTOM_SEARCH_ENGINE_ID= + +################################################################################ +### TTS PROVIDER +################################################################################ + +### MAC OS +# USE_MAC_OS_TTS - Use Mac OS TTS or not (Default: False) USE_MAC_OS_TTS=False + +### ELEVENLABS +# ELEVENLABS_API_KEY - Eleven Labs API key (Example: my-elevenlabs-api-key) +ELEVENLABS_API_KEY=your-elevenlabs-api-key +# ELEVENLABS_VOICE_1_ID - Eleven Labs voice 1 ID (Example: my-voice-id-1) +ELEVENLABS_VOICE_1_ID=your-voice-id +# ELEVENLABS_VOICE_2_ID - Eleven Labs voice 2 ID (Example: my-voice-id-2) +ELEVENLABS_VOICE_2_ID=your-voice-id From 0dddc94bdac94707062b1863f3c5a72d113432ca Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 19:13:04 +0200 Subject: [PATCH 20/74] Add file ingestion methode in file_operations.py Add the following functions to ingest data into memory before Auto-GPT run. - split_file: given a content, split it in chunks of max_length with (or without) a specified overlap - ingest_file: read a file, use split_file to split it in chunks and load each chunk in memory - ingest_directory: ingest all files in a directory in memory --- scripts/file_operations.py | 75 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/scripts/file_operations.py b/scripts/file_operations.py index c6066ef9..3493c2bf 100644 --- a/scripts/file_operations.py +++ b/scripts/file_operations.py @@ -1,5 +1,10 @@ import os import os.path +from config import Config +from memory import get_memory + +cfg = Config() +memory = get_memory(cfg) # Set a dedicated folder for file I/O working_directory = "auto_gpt_workspace" @@ -20,6 +25,30 @@ def safe_join(base, *paths): return norm_new_path +def split_file(content, max_length=4000, overlap=0): + """ + Split text into chunks of a specified maximum length with a specified overlap + between chunks. + + :param text: The input text to be split into chunks + :param max_length: The maximum length of each chunk, default is 4000 (about 1k token) + :param overlap: The number of overlapping characters between chunks, default is no overlap + :return: A generator yielding chunks of text + """ + start = 0 + content_length = len(content) + + while start < content_length: + end = start + max_length + chunk = content[start:end] + yield chunk + start += max_length - overlap + if start + max_length - overlap >= content_length: + break + if end + overlap > content_length: + start = content_length - max_length + + def read_file(filename): """Read a file and return the contents""" try: @@ -31,6 +60,52 @@ def read_file(filename): return "Error: " + str(e) +def ingest_file(filename, memory, max_length=4000, overlap=200): + """ + Ingest a file by reading its content, splitting it into chunks with a specified + maximum length and overlap, and adding the chunks to the memory storage. + + :param filename: The name of the file to ingest + :param memory: An object with an add() method to store the chunks in memory + :param max_length: The maximum length of each chunk, default is 4000 + :param overlap: The number of overlapping characters between chunks, default is 200 + """ + try: + print(f"Working with file {filename}") + content = read_file(filename) + content_length = len(content) + print(f"File length: {content_length} characters") + + chunks = list(split_file(content, max_length=max_length, overlap=overlap)) + + num_chunks = len(chunks) + for i, chunk in enumerate(chunks): + print(f"Ingesting chunk {i + 1} / {num_chunks} into memory") + memory_to_add = f"Filename: {filename}\n" \ + f"Content part#{i + 1}/{num_chunks}: {chunk}" + + memory.add(memory_to_add) + + print(f"Done ingesting {num_chunks} chunks from {filename}.") + except Exception as e: + print(f"Error while ingesting file '{filename}': {str(e)}") + + +def ingest_directory(directory, memory): + """ + Ingest all files in a directory by calling the ingest_file function for each file. + + :param directory: The directory containing the files to ingest + :param memory: An object with an add() method to store the chunks in memory + """ + try: + files = search_files(directory) + for file in files: + ingest_file(file, memory) + except Exception as e: + print(f"Error while ingesting directory '{directory}': {str(e)}") + + def write_to_file(filename, text): """Write text to a file""" try: From 7975c184a55a477e884e1920ed87dc67ca4b4261 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 19:46:39 +0200 Subject: [PATCH 21/74] Update .gitignore add new log file to gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index aa0dceaa..fc496609 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,5 @@ log.txt # Coverage reports .coverage coverage.xml -htmlcov/ \ No newline at end of file +htmlcov/ +log-ingestion.txt From c91117616f7b5e16743208b8649ce4335077915b Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 19:46:58 +0200 Subject: [PATCH 22/74] Update file_operations.py revert change in import as we don't need them --- scripts/file_operations.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/scripts/file_operations.py b/scripts/file_operations.py index 3493c2bf..8e807bba 100644 --- a/scripts/file_operations.py +++ b/scripts/file_operations.py @@ -1,10 +1,5 @@ import os import os.path -from config import Config -from memory import get_memory - -cfg = Config() -memory = get_memory(cfg) # Set a dedicated folder for file I/O working_directory = "auto_gpt_workspace" From 8faa6ef949bf7fbbb8bd875a66bfd4fd231ecebc Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 19:47:51 +0200 Subject: [PATCH 23/74] Create data_ingestion.py This script is use when we want to seed Auto-GPT memory with one or multiple documents. The document are read, split into chunks and store in the memory. --- scripts/data_ingestion.py | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 scripts/data_ingestion.py diff --git a/scripts/data_ingestion.py b/scripts/data_ingestion.py new file mode 100644 index 00000000..3f6d1322 --- /dev/null +++ b/scripts/data_ingestion.py @@ -0,0 +1,52 @@ +import argparse +import logging +from config import Config +from memory import get_memory +from file_operations import ingest_file, ingest_directory + +cfg = Config() + +def configure_logging(): + logging.basicConfig(filename='log-ingestion.txt', + filemode='a', + format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', + datefmt='%H:%M:%S', + level=logging.DEBUG) + return logging.getLogger('AutoGPT-Ingestion') + + +def main(): + logger = configure_logging() + + parser = argparse.ArgumentParser(description="Ingest a file or a directory with multiple files into memory. Make sure to set your .env before running this script.") + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument("--file", type=str, help="The file to ingest.") + group.add_argument("--dir", type=str, help="The directory containing the files to ingest.") + parser.add_argument("--init", action='store_true', help="Init the memory and wipe its content", default=False) + args = parser.parse_args() + + + # Initialize memory + memory = get_memory(cfg, init=args.init) + print('Using memory of type: ' + memory.__class__.__name__) + + if args.file: + try: + ingest_file(args.file, memory) + print(f"File '{args.file}' ingested successfully.") + except Exception as e: + logger.error(f"Error while ingesting file '{args.file}': {str(e)}") + print(f"Error while ingesting file '{args.file}': {str(e)}") + elif args.dir: + try: + ingest_directory(args.dir, memory) + print(f"Directory '{args.dir}' ingested successfully.") + except Exception as e: + logger.error(f"Error while ingesting directory '{args.dir}': {str(e)}") + print(f"Error while ingesting directory '{args.dir}': {str(e)}") + else: + print("Please provide either a file path (--file) or a directory name (--dir) inside the auto_gpt_workspace directory as input.") + + +if __name__ == "__main__": + main() From 4465486ea39b0bc65715e48a1c7861a565b5126f Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 20:18:59 +0200 Subject: [PATCH 24/74] Update file_operations.py move the search_file function inside the data_ingestion script --- scripts/file_operations.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/scripts/file_operations.py b/scripts/file_operations.py index 8e807bba..e664fcc9 100644 --- a/scripts/file_operations.py +++ b/scripts/file_operations.py @@ -86,21 +86,6 @@ def ingest_file(filename, memory, max_length=4000, overlap=200): print(f"Error while ingesting file '{filename}': {str(e)}") -def ingest_directory(directory, memory): - """ - Ingest all files in a directory by calling the ingest_file function for each file. - - :param directory: The directory containing the files to ingest - :param memory: An object with an add() method to store the chunks in memory - """ - try: - files = search_files(directory) - for file in files: - ingest_file(file, memory) - except Exception as e: - print(f"Error while ingesting directory '{directory}': {str(e)}") - - def write_to_file(filename, text): """Write text to a file""" try: From 280647ff387bc29127b8403c7dd46f2c94d6a0b8 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 20:19:36 +0200 Subject: [PATCH 25/74] Update data_ingestion.py move the search_file function inside the data_ingestion script add memory initialisation argument add overlap argument add chunk max_length argument --- scripts/data_ingestion.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/scripts/data_ingestion.py b/scripts/data_ingestion.py index 3f6d1322..32811166 100644 --- a/scripts/data_ingestion.py +++ b/scripts/data_ingestion.py @@ -2,7 +2,7 @@ import argparse import logging from config import Config from memory import get_memory -from file_operations import ingest_file, ingest_directory +from file_operations import ingest_file, search_files cfg = Config() @@ -15,6 +15,21 @@ def configure_logging(): return logging.getLogger('AutoGPT-Ingestion') +def ingest_directory(directory, memory, args): + """ + Ingest all files in a directory by calling the ingest_file function for each file. + + :param directory: The directory containing the files to ingest + :param memory: An object with an add() method to store the chunks in memory + """ + try: + files = search_files(directory) + for file in files: + ingest_file(file, memory, args.max_length, args.overlap) + except Exception as e: + print(f"Error while ingesting directory '{directory}': {str(e)}") + + def main(): logger = configure_logging() @@ -22,7 +37,10 @@ def main(): group = parser.add_mutually_exclusive_group(required=True) group.add_argument("--file", type=str, help="The file to ingest.") group.add_argument("--dir", type=str, help="The directory containing the files to ingest.") - parser.add_argument("--init", action='store_true', help="Init the memory and wipe its content", default=False) + parser.add_argument("--init", action='store_true', help="Init the memory and wipe its content (default: False)", default=False) + parser.add_argument("--overlap", type=int, help="The overlap size between chunks when ingesting files (default: 200)", default=200) + parser.add_argument("--max_length", type=int, help="The max_length of each chunk when ingesting files (default: 4000)", default=4000) + args = parser.parse_args() @@ -32,14 +50,14 @@ def main(): if args.file: try: - ingest_file(args.file, memory) + ingest_file(args.file, memory, args.max_length, args.overlap) print(f"File '{args.file}' ingested successfully.") except Exception as e: logger.error(f"Error while ingesting file '{args.file}': {str(e)}") print(f"Error while ingesting file '{args.file}': {str(e)}") elif args.dir: try: - ingest_directory(args.dir, memory) + ingest_directory(args.dir, memory, args) print(f"Directory '{args.dir}' ingested successfully.") except Exception as e: logger.error(f"Error while ingesting directory '{args.dir}': {str(e)}") From 65cc4f833f56000ae3928cccc3c9821fece53958 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 20:47:46 +0200 Subject: [PATCH 26/74] Add Memory Pre-Seeding information to readme.md Add the documentation for memory pre-seeding --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/README.md b/README.md index 2900daa9..6262467d 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Your support is greatly appreciated - [🗣️ Speech Mode](#️-speech-mode) - [🔍 Google API Keys Configuration](#-google-api-keys-configuration) - [Setting up environment variables](#setting-up-environment-variables) + - [🧠 Memory pre-seeding](#memory-pre-seeding) - [💀 Continuous Mode ⚠️](#-continuous-mode-️) - [GPT3.5 ONLY Mode](#gpt35-only-mode) - [🖼 Image Generation](#image-generation) @@ -245,6 +246,52 @@ To switch to either, change the `MEMORY_BACKEND` env variable to the value that 1. View memory usage by using the `--debug` flag :) + +## 🧠 Memory pre-seeding + +``` +# python scripts/data_ingestion.py -h +usage: data_ingestion.py [-h] (--file FILE | --dir DIR) [--init] [--overlap OVERLAP] [--max_length MAX_LENGTH] + +Ingest a file or a directory with multiple files into memory. Make sure to set your .env before running this script. + +options: + -h, --help show this help message and exit + --file FILE The file to ingest. + --dir DIR The directory containing the files to ingest. + --init Init the memory and wipe its content (default: False) + --overlap OVERLAP The overlap size between chunks when ingesting files (default: 200) + --max_length MAX_LENGTH The max_length of each chunk when ingesting files (default: 4000 + +# python scripts/data_ingestion.py --dir seed_data --init --overlap 200 --max_length 1000 +``` + +This script located at scripts/data_ingestion.py, allows you to ingest files into memory and pre-seed it before running Auto-GPT. + +Memory pre-seeding is a technique that involves ingesting relevant documents or data into the AI's memory so that it can use this information to generate more informed and accurate responses. + +To pre-seed the memory, the content of each document is split into chunks of a specified maximum length with a specified overlap between chunks, and then each chunk is added to the memory backend set in the .env file. When the AI is prompted to recall information, it can then access those pre-seeded memories to generate more informed and accurate responses. + +This technique is particularly useful when working with large amounts of data or when there is specific information that the AI needs to be able to access quickly. +By pre-seeding the memory, the AI can retrieve and use this information more efficiently, saving time, API call and improving the accuracy of its responses. + +You could for example download the documentation of an API, a Github repository, etc. and ingest it into memory before running Auto-GPT. + +⚠️ If you use Redis as your memory, make sure to run Auto-GPT with the WIPE_REDIS_ON_START set to False in your .env file. + +⚠️For other memory backend, we currently forcefully wipe the memory when starting Auto-GPT. To ingest data with those memory backend, you can call the data_ingestion.py script anytime during an Auto-GPT run. + +Memories will be available to the AI immediately as they are ingested, even if ingested while Auto-GPT is running. + +In the example above, the script initializes the memory, ingests all files within the seed_data directory into memory with an overlap between chunks of 200 and a maximum length of each chunk of 4000. +Note that you can also use the --file argument to ingest a single file into memory and that the script will only ingest files within the auto_gpt_workspace directory. + +You can adjust the max_length and overlap parameters to fine-tune the way the docuents are presented to the AI when it "recall" that memory: + +- Adjusting the overlap value allows the AI to access more contextual information from each chunk when recalling information, but will result in more chunks being created and therefore increase memory backend usage and OpenAI API requests. +- Reducing the max_length value will create more chunks, which can save prompt tokens by allowing for more message history in the context, but will also increase the number of chunks. +- Increasing the max_length value will provide the AI with more contextual information from each chunk, reducing the number of chunks created and saving on OpenAI API requests. However, this may also use more prompt tokens and decrease the overall context available to the AI. + ## 💀 Continuous Mode ⚠️ Run the AI **without** user authorisation, 100% automated. From 2e0b44ae05fce7795f662a81c765eeeeae32a768 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 22:46:49 +0200 Subject: [PATCH 27/74] fix chunk creation the last chunk wasn't correctly created, this commit fix that issue. --- scripts/file_operations.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/file_operations.py b/scripts/file_operations.py index e664fcc9..f2a2b072 100644 --- a/scripts/file_operations.py +++ b/scripts/file_operations.py @@ -38,11 +38,12 @@ def split_file(content, max_length=4000, overlap=0): chunk = content[start:end] yield chunk start += max_length - overlap - if start + max_length - overlap >= content_length: - break - if end + overlap > content_length: + if start + max_length > content_length: start = content_length - max_length - + end = content_length + chunk = content[start:end] + yield chunk + break def read_file(filename): """Read a file and return the contents""" From 4e914e5ec1a4f7d39cba04cc2ebc0ba7f0055423 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 22:51:52 +0200 Subject: [PATCH 28/74] Revert "Update .gitignore" This reverts commit 7975c184a55a477e884e1920ed87dc67ca4b4261. --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index fc496609..aa0dceaa 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,4 @@ log.txt # Coverage reports .coverage coverage.xml -htmlcov/ -log-ingestion.txt +htmlcov/ \ No newline at end of file From 2f1181f9a12bbbbf55b8f2224ecc645d22c5d90d Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 22:52:37 +0200 Subject: [PATCH 29/74] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cfa3b08b..403417eb 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ ai_settings.yaml .idea/* auto-gpt.json log.txt +log-ingestion.txt # Coverage reports .coverage From 5badde2c2725d636fa415b53acf96a071512f22f Mon Sep 17 00:00:00 2001 From: "Joseph C. Miller, II" Date: Wed, 12 Apr 2023 15:30:34 -0600 Subject: [PATCH 30/74] Add message to explain exit. --- scripts/config.py | 5 +++++ scripts/main.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/scripts/config.py b/scripts/config.py index 6e448954..09f5276e 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -35,6 +35,7 @@ class Config(metaclass=Singleton): """Initialize the Config class""" self.debug_mode = False self.continuous_mode = False + self.continuous_limit = 0 self.speak_mode = False self.fast_llm_model = os.getenv("FAST_LLM_MODEL", "gpt-3.5-turbo") @@ -89,6 +90,10 @@ class Config(metaclass=Singleton): """Set the continuous mode value.""" self.continuous_mode = value + def set_continuous_limit(self, value: int): + """Set the continuous limit value.""" + self.continuous_limit = value + def set_speak_mode(self, value: bool): """Set the speak mode value.""" self.speak_mode = value diff --git a/scripts/main.py b/scripts/main.py index 0a4e97a2..27275cb0 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -271,6 +271,7 @@ def parse_arguments(): parser = argparse.ArgumentParser(description='Process arguments.') parser.add_argument('--continuous', action='store_true', help='Enable Continuous Mode') + parser.add_argument('--continuous-limit', '-l', type=int, default=0, dest="continuous_limit", help='Defines the number of times to run in continuous mode') parser.add_argument('--speak', action='store_true', help='Enable Speak Mode') parser.add_argument('--debug', action='store_true', help='Enable Debug Mode') parser.add_argument('--gpt3only', action='store_true', help='Enable GPT3.5 Only Mode') @@ -290,6 +291,16 @@ def parse_arguments(): "Continuous mode is not recommended. It is potentially dangerous and may cause your AI to run forever or carry out actions you would not usually authorise. Use at your own risk.") cfg.set_continuous_mode(True) + if args.continuous_limit and not args.continuous: + parser.error("--continuous-limit can only be used with --continuous") + + if args.continuous_limit > 0: + logger.typewriter_log( + "Continuous Limit: ", + Fore.GREEN, + f"{args.continuous_limit}") + cfg.set_continuous_limit(args.continuous_limit) + if args.speak: logger.typewriter_log("Speak Mode: ", Fore.GREEN, "ENABLED") cfg.set_speak_mode(True) @@ -337,7 +348,14 @@ memory = get_memory(cfg, init=True) print('Using memory of type: ' + memory.__class__.__name__) # Interaction Loop +loop_count = 0 while True: + # Discontinue if continuous limit is reached + loop_count += 1 + if cfg.continuous_mode and cfg.continuous_limit > 0 and loop_count > cfg.continuous_limit: + logger.typewriter_log("Continuous Limit Reached: ", Fore.RED, f"{cfg.continuous_limit}") + break + # Send message to AI, get response with Spinner("Thinking... "): assistant_reply = chat.chat_with_ai( From 36d455c20e52aa1e09766a01c880f7914c5c24b7 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 23:31:26 +0200 Subject: [PATCH 31/74] split_file() rework rework the split_file function to make it simple and only have one yield while providing an overlap at the start and end of each chunk --- scripts/file_operations.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/scripts/file_operations.py b/scripts/file_operations.py index db4702c5..c12774b9 100644 --- a/scripts/file_operations.py +++ b/scripts/file_operations.py @@ -35,15 +35,12 @@ def split_file(content, max_length=4000, overlap=0): while start < content_length: end = start + max_length - chunk = content[start:end] + if end + overlap < content_length: + chunk = content[start:end+overlap] + else: + chunk = content[start:content_length] yield chunk start += max_length - overlap - if start + max_length > content_length: - start = content_length - max_length - end = content_length - chunk = content[start:end] - yield chunk - break def read_file(filename): """Read a file and return the contents""" From 1c64a9d24508333d92cfdb26d38a90c4bd543dc6 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Wed, 12 Apr 2023 23:33:14 +0200 Subject: [PATCH 32/74] Update .env.template --- .env.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.template b/.env.template index 0aec7b10..f1e66ea5 100644 --- a/.env.template +++ b/.env.template @@ -19,4 +19,4 @@ HUGGINGFACE_API_TOKEN= USE_MAC_OS_TTS=False BROWSE_CHUNK_MAX_LENGTH=4000 BROWSE_SUMMARY_MAX_TOKEN=300 -MEMORY_BACKEND=local \ No newline at end of file +MEMORY_BACKEND=local From 12e1fcca92ed8ec57b1365f3a0f57eb29115c1ff Mon Sep 17 00:00:00 2001 From: "Joseph C. Miller, II" Date: Wed, 12 Apr 2023 15:36:35 -0600 Subject: [PATCH 33/74] Correct the checking for continuous limit without continuous mode --- scripts/main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 27275cb0..3d239009 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -291,9 +291,6 @@ def parse_arguments(): "Continuous mode is not recommended. It is potentially dangerous and may cause your AI to run forever or carry out actions you would not usually authorise. Use at your own risk.") cfg.set_continuous_mode(True) - if args.continuous_limit and not args.continuous: - parser.error("--continuous-limit can only be used with --continuous") - if args.continuous_limit > 0: logger.typewriter_log( "Continuous Limit: ", @@ -301,6 +298,10 @@ def parse_arguments(): f"{args.continuous_limit}") cfg.set_continuous_limit(args.continuous_limit) + # Check if continuous limit is used without continuous mode + if args.continuous_limit and not args.continuous: + parser.error("--continuous-limit can only be used with --continuous") + if args.speak: logger.typewriter_log("Speak Mode: ", Fore.GREEN, "ENABLED") cfg.set_speak_mode(True) From d706a3436d763ec78cc14e93533da26ac0ed40b0 Mon Sep 17 00:00:00 2001 From: "Joseph C. Miller, II" Date: Wed, 12 Apr 2023 15:39:25 -0600 Subject: [PATCH 34/74] Make configuration similar to other arguments. --- scripts/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 3d239009..90033afb 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -271,7 +271,7 @@ def parse_arguments(): parser = argparse.ArgumentParser(description='Process arguments.') parser.add_argument('--continuous', action='store_true', help='Enable Continuous Mode') - parser.add_argument('--continuous-limit', '-l', type=int, default=0, dest="continuous_limit", help='Defines the number of times to run in continuous mode') + parser.add_argument('--continuous-limit', '-l', type=int, dest="continuous_limit", help='Defines the number of times to run in continuous mode') parser.add_argument('--speak', action='store_true', help='Enable Speak Mode') parser.add_argument('--debug', action='store_true', help='Enable Debug Mode') parser.add_argument('--gpt3only', action='store_true', help='Enable GPT3.5 Only Mode') @@ -291,7 +291,7 @@ def parse_arguments(): "Continuous mode is not recommended. It is potentially dangerous and may cause your AI to run forever or carry out actions you would not usually authorise. Use at your own risk.") cfg.set_continuous_mode(True) - if args.continuous_limit > 0: + if args.continuous_limit: logger.typewriter_log( "Continuous Limit: ", Fore.GREEN, From 428caa9bef83e93a6f97a1341a03a0f41b71dec0 Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Thu, 13 Apr 2023 12:57:57 +0300 Subject: [PATCH 35/74] Added flags, and implemented skip-reprompt --- scripts/config.py | 1 + scripts/main.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index ebf1b08b..fd370a72 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -37,6 +37,7 @@ class Config(metaclass=Singleton): self.debug_mode = False self.continuous_mode = False self.speak_mode = False + self.skip_reprompt = False self.fast_llm_model = os.getenv("FAST_LLM_MODEL", "gpt-3.5-turbo") self.smart_llm_model = os.getenv("SMART_LLM_MODEL", "gpt-4") diff --git a/scripts/main.py b/scripts/main.py index 81f560b2..f81b09a7 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -183,7 +183,11 @@ def load_variables(config_file="config.yaml"): def construct_prompt(): """Construct the prompt for the AI to respond to""" config = AIConfig.load() - if config.ai_name: + if cfg.skip_reprompt and config.ai_name: + logger.typewriter_log("Name :", Fore.GREEN, config.ai_name) + logger.typewriter_log("Role :", Fore.GREEN, config.ai_role) + logger.typewriter_log("Goals:", Fore.GREEN, config.ai_goals) + elif config.ai_name: logger.typewriter_log( f"Welcome back! ", Fore.GREEN, @@ -270,12 +274,14 @@ def parse_arguments(): cfg.set_speak_mode(False) parser = argparse.ArgumentParser(description='Process arguments.') - parser.add_argument('--continuous', action='store_true', help='Enable Continuous Mode') + parser.add_argument('--continuous', '-c', action='store_true', help='Enable Continuous Mode') parser.add_argument('--speak', action='store_true', help='Enable Speak Mode') parser.add_argument('--debug', action='store_true', help='Enable Debug Mode') parser.add_argument('--gpt3only', action='store_true', help='Enable GPT3.5 Only Mode') parser.add_argument('--gpt4only', action='store_true', help='Enable GPT4 Only Mode') parser.add_argument('--use-memory', '-m', dest="memory_type", help='Defines which Memory backend to use') + parser.add_argument('--skip-reprompt', '-y', dest='skip_reprompt', action='store_true', help='Skips the re-prompting messages at the beginning of the script') + parser.add_argument('--ai-settings', '-C', dest='ai_settings_file', help="Specifies which ai_settings.yaml file to use, will also automatically skip the re-prompt.") args = parser.parse_args() if args.debug: @@ -315,6 +321,10 @@ def parse_arguments(): else: cfg.memory_backend = chosen + if args.skip_reprompt: + logger.typewriter_log("Skip Re-prompt: ", Fore.GREEN, "ENABLED") + cfg.skip_reprompt = True + # TODO: fill in llm values here check_openai_api_key() From 0f6fba7d65302591f2c77a41483953df43d12d2b Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Thu, 13 Apr 2023 14:02:42 +0300 Subject: [PATCH 36/74] Implemented the '--ai-settings' flag --- scripts/config.py | 1 + scripts/main.py | 16 +++++++++++++++- scripts/utils.py | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/scripts/config.py b/scripts/config.py index fd370a72..ad968fb2 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -39,6 +39,7 @@ class Config(metaclass=Singleton): self.speak_mode = False self.skip_reprompt = False + self.ai_settings_file = os.getenv("AI_SETTINGS_FILE", "ai_settings.yaml") self.fast_llm_model = os.getenv("FAST_LLM_MODEL", "gpt-3.5-turbo") self.smart_llm_model = os.getenv("SMART_LLM_MODEL", "gpt-4") self.fast_token_limit = int(os.getenv("FAST_TOKEN_LIMIT", 4000)) diff --git a/scripts/main.py b/scripts/main.py index f81b09a7..07d2bbd2 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -182,7 +182,7 @@ def load_variables(config_file="config.yaml"): def construct_prompt(): """Construct the prompt for the AI to respond to""" - config = AIConfig.load() + config = AIConfig.load(cfg.ai_settings_file) if cfg.skip_reprompt and config.ai_name: logger.typewriter_log("Name :", Fore.GREEN, config.ai_name) logger.typewriter_log("Role :", Fore.GREEN, config.ai_role) @@ -324,7 +324,21 @@ def parse_arguments(): if args.skip_reprompt: logger.typewriter_log("Skip Re-prompt: ", Fore.GREEN, "ENABLED") cfg.skip_reprompt = True + + if args.ai_settings_file: + file = args.ai_settings_file + # Validate file + (validated, message) = utils.validate_yaml_file(file) + if not validated: + logger.typewriter_log("FAILED FILE VALIDATION", Fore.RED, message) + exit(1) + + logger.typewriter_log("Using AI Settings File:", Fore.GREEN, file) + cfg.ai_settings_file = file + cfg.skip_reprompt = True + + # TODO: fill in llm values here check_openai_api_key() diff --git a/scripts/utils.py b/scripts/utils.py index 5039796f..bca8d4a8 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -1,3 +1,6 @@ +import yaml +from colorama import Fore + def clean_input(prompt: str=''): try: return input(prompt) @@ -6,3 +9,14 @@ def clean_input(prompt: str=''): print("Quitting...") exit(0) + +def validate_yaml_file(file: str): + try: + with open(file) as file: + yaml.load(file, Loader=yaml.FullLoader) + except FileNotFoundError: + return (False, f"The file {Fore.CYAN}`{file}`{Fore.RESET} wasn't found") + except yaml.YAMLError as e: + return (False, f"There was an issue while trying to read with your AI Settings file: {e}") + + return (True, f"Successfully validated {Fore.CYAN}`{file}`{Fore.RESET}!") \ No newline at end of file From a10ffc1dbed88ce74f7ebb1dae0c90fb18bae9f6 Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Thu, 13 Apr 2023 14:26:16 +0300 Subject: [PATCH 37/74] Fixed error logging when choosing non-supported memory backend with '--use-memory' --- scripts/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 07d2bbd2..59cb565e 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -316,8 +316,8 @@ def parse_arguments(): supported_memory = get_supported_memory_backends() chosen = args.memory_type if not chosen in supported_memory: - print_to_console("ONLY THE FOLLOWING MEMORY BACKENDS ARE SUPPORTED: ", Fore.RED, f'{supported_memory}') - print_to_console(f"Defaulting to: ", Fore.YELLOW, cfg.memory_backend) + logger.typewriter_log("ONLY THE FOLLOWING MEMORY BACKENDS ARE SUPPORTED: ", Fore.RED, f'{supported_memory}') + logger.typewriter_log(f"Defaulting to: ", Fore.YELLOW, cfg.memory_backend) else: cfg.memory_backend = chosen From ff094c7ecc58fad572dccbc8a376a75045d91733 Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Thu, 13 Apr 2023 15:09:24 +0300 Subject: [PATCH 38/74] Resolve Linter Issues --- scripts/main.py | 5 ++--- scripts/utils.py | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 59cb565e..0674db47 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -327,19 +327,18 @@ def parse_arguments(): if args.ai_settings_file: file = args.ai_settings_file - + # Validate file (validated, message) = utils.validate_yaml_file(file) if not validated: logger.typewriter_log("FAILED FILE VALIDATION", Fore.RED, message) exit(1) - + logger.typewriter_log("Using AI Settings File:", Fore.GREEN, file) cfg.ai_settings_file = file cfg.skip_reprompt = True - # TODO: fill in llm values here check_openai_api_key() parse_arguments() diff --git a/scripts/utils.py b/scripts/utils.py index bca8d4a8..2b51c1fc 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -18,5 +18,5 @@ def validate_yaml_file(file: str): return (False, f"The file {Fore.CYAN}`{file}`{Fore.RESET} wasn't found") except yaml.YAMLError as e: return (False, f"There was an issue while trying to read with your AI Settings file: {e}") - - return (True, f"Successfully validated {Fore.CYAN}`{file}`{Fore.RESET}!") \ No newline at end of file + + return (True, f"Successfully validated {Fore.CYAN}`{file}`{Fore.RESET}!") From 41f17f89043b19e7a5990894996c6dc407d734c0 Mon Sep 17 00:00:00 2001 From: Peter Edwards Date: Thu, 13 Apr 2023 16:02:15 +0200 Subject: [PATCH 39/74] Small README.md clarity update and usage fixup --- README.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0d12ae23..90706cbf 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,11 @@ Your support is greatly appreciated - [Python 3.8 or later](https://www.tutorialspoint.com/how-to-install-python-in-windows) - [OpenAI API key](https://platform.openai.com/account/api-keys) -- [PINECONE API key](https://www.pinecone.io/) + Optional: +- [PINECONE API key](https://www.pinecone.io/) (If you want Pinecone backed memory) - ElevenLabs Key (If you want the AI to speak) ## 💾 Installation @@ -114,8 +115,8 @@ pip install -r requirements.txt python scripts/main.py ``` -2. After each of AUTO-GPT's actions, type "NEXT COMMAND" to authorise them to continue. -3. To exit the program, type "exit" and press Enter. +2. After each of action, enter 'y' to authorise command, 'y -N' to run N continuous commands, 'n' to exit program, or enter additional feedback for the AI. + ### Logs @@ -217,7 +218,10 @@ Pinecone enables the storage of vast amounts of vector-based memory, allowing fo ### Setting up environment variables -Simply set them in the `.env` file. +In the `.env` file set: +- `PINECONE_API_KEY` +- `PINECONE_ENV` (something like: us-east4-gcp) +- `MEMORY_BACKEND=pinecone` Alternatively, you can set them from the command line (advanced): @@ -226,7 +230,7 @@ For Windows Users: ``` setx PINECONE_API_KEY "YOUR_PINECONE_API_KEY" setx PINECONE_ENV "Your pinecone region" # something like: us-east4-gcp - +setx MEMORY_BACKEND "pinecone" ``` For macOS and Linux users: @@ -234,7 +238,7 @@ For macOS and Linux users: ``` export PINECONE_API_KEY="YOUR_PINECONE_API_KEY" export PINECONE_ENV="Your pinecone region" # something like: us-east4-gcp - +export MEMORY_BACKEND="pinecone" ``` From ccfb568694ee6aa2bf2789ea8131a88bdb8ab0c2 Mon Sep 17 00:00:00 2001 From: sagarishere <5121817+sagarishere@users.noreply.github.com> Date: Thu, 13 Apr 2023 17:08:23 +0300 Subject: [PATCH 40/74] Fix twitter link: in README.md Fixed twitter link to go to: https://twitter.com/SigGravitas Previously it was going to the icon image. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 43d0ff72..f9e8d63d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Auto-GPT: An Autonomous GPT-4 Experiment ![GitHub Repo stars](https://img.shields.io/github/stars/Torantulino/auto-gpt?style=social) -![Twitter Follow](https://img.shields.io/twitter/follow/siggravitas?style=social) +[![Twitter Follow](https://img.shields.io/twitter/follow/siggravitas?style=social)](https://twitter.com/SigGravitas) [![Discord Follow](https://dcbadge.vercel.app/api/server/PQ7VX6TY4t?style=flat)](https://discord.gg/PQ7VX6TY4t) [![Unit Tests](https://github.com/Torantulino/Auto-GPT/actions/workflows/ci.yml/badge.svg)](https://github.com/Torantulino/Auto-GPT/actions/workflows/unit_tests.yml) From f7910e85ce8080602a02886712ebd8da0f1340a5 Mon Sep 17 00:00:00 2001 From: ShifraSec <48570596+MoElaSec@users.noreply.github.com> Date: Thu, 13 Apr 2023 18:33:20 +0400 Subject: [PATCH 41/74] Link to 11Labs website to obtain API_KEY --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 43d0ff72..64e29864 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Your support is greatly appreciated Optional: -- ElevenLabs Key (If you want the AI to speak) +- [ElevenLabs Key](https://elevenlabs.io/) (If you want the AI to speak) ## 💾 Installation From 2f7a40204088b5c5c9e9eee5e0e7b4445105c28d Mon Sep 17 00:00:00 2001 From: "Joseph C. Miller, II" Date: Thu, 13 Apr 2023 08:49:22 -0600 Subject: [PATCH 42/74] Use yellow instead of red for termination message --- scripts/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index e0ddc9fb..3c847eec 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -186,7 +186,7 @@ def construct_prompt(): if config.ai_name: logger.typewriter_log( f"Welcome back! ", - Fore.GREEN, + Fore.YELLOW, f"Would you like me to return to being {config.ai_name}?", speak_text=True) should_continue = utils.clean_input(f"""Continue with the last settings? From 2a623941127c03f900853ff201b75f5e40fc0adb Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Thu, 13 Apr 2023 07:56:56 -0700 Subject: [PATCH 43/74] add: execute shell command to prompt.py --- scripts/prompt.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/prompt.py b/scripts/prompt.py index bbdfa5ec..286002ee 100644 --- a/scripts/prompt.py +++ b/scripts/prompt.py @@ -34,6 +34,7 @@ def get_prompt(): ("Get Improved Code", "improve_code", {"suggestions": "", "code": ""}), ("Write Tests", "write_tests", {"code": "", "focus": ""}), ("Execute Python File", "execute_python_file", {"file": ""}), + ("Execute Shell Command, non-interactive commands only", "execute_shell", { "command_line": ""}), ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), ("Generate Image", "generate_image", {"prompt": ""}), ("Do Nothing", "do_nothing", {}), From c5188d561184db27e9ed2f71822d7c04cd9f6917 Mon Sep 17 00:00:00 2001 From: celthi Date: Thu, 13 Apr 2023 23:41:09 +0800 Subject: [PATCH 44/74] skip getting relevant memory if no message history --- scripts/chat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/chat.py b/scripts/chat.py index e16cee38..afec2219 100644 --- a/scripts/chat.py +++ b/scripts/chat.py @@ -69,7 +69,7 @@ def chat_with_ai( logger.debug(f"Token limit: {token_limit}") send_token_limit = token_limit - 1000 - relevant_memory = permanent_memory.get_relevant(str(full_message_history[-9:]), 10) + relevant_memory = '' if len(full_message_history) ==0 else permanent_memory.get_relevant(str(full_message_history[-9:]), 10) logger.debug(f'Memory Stats: {permanent_memory.get_stats()}') From d2f75e8659de28709ddb5839ee485e7918db4059 Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 13 Apr 2023 17:23:16 +0100 Subject: [PATCH 45/74] Added simple retry on Open AI chat if a Rate Limit or 502 Bad Gateway error received --- scripts/llm_utils.py | 56 ++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/scripts/llm_utils.py b/scripts/llm_utils.py index 35cc5ce0..731acae2 100644 --- a/scripts/llm_utils.py +++ b/scripts/llm_utils.py @@ -1,26 +1,52 @@ +import time import openai +from colorama import Fore from config import Config + cfg = Config() openai.api_key = cfg.openai_api_key + # Overly simple abstraction until we create something better +# simple retry mechanism when getting a rate error or a bad gateway def create_chat_completion(messages, model=None, temperature=cfg.temperature, max_tokens=None)->str: """Create a chat completion using the OpenAI API""" - if cfg.use_azure: - response = openai.ChatCompletion.create( - deployment_id=cfg.get_azure_deployment_id_for_model(model), - model=model, - messages=messages, - temperature=temperature, - max_tokens=max_tokens - ) - else: - response = openai.ChatCompletion.create( - model=model, - messages=messages, - temperature=temperature, - max_tokens=max_tokens - ) + response = None + num_retries = 5 + for attempt in range(num_retries): + try: + if cfg.use_azure: + response = openai.ChatCompletion.create( + deployment_id=cfg.get_azure_deployment_id_for_model(model), + model=model, + messages=messages, + temperature=temperature, + max_tokens=max_tokens + ) + else: + response = openai.ChatCompletion.create( + model=model, + messages=messages, + temperature=temperature, + max_tokens=max_tokens + ) + break + except openai.error.RateLimitError: + if cfg.debug_mode: + print(Fore.RED + "Error: ", "API Rate Limit Reached. Waiting 20 seconds..." + Fore.RESET) + time.sleep(20) + except openai.error.APIError as e: + if e.http_status == 502: + if cfg.debug_mode: + print(Fore.RED + "Error: ", "API Bad gateway. Waiting 20 seconds..." + Fore.RESET) + time.sleep(20) + else: + raise + if attempt == num_retries - 1: + raise + + if response is None: + raise RuntimeError("Failed to get response after 5 retries") return response.choices[0].message["content"] From f98fba66577b426630faab9cd01cee41e932bb73 Mon Sep 17 00:00:00 2001 From: Richard Beales Date: Thu, 13 Apr 2023 17:33:22 +0100 Subject: [PATCH 46/74] Update README - Discord link and unit test link Use new url for discord, correct link to ci.yaml workflow. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 68acfe75..5e3197d2 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ ![GitHub Repo stars](https://img.shields.io/github/stars/Torantulino/auto-gpt?style=social) ![Twitter Follow](https://img.shields.io/twitter/follow/siggravitas?style=social) -[![Discord Follow](https://dcbadge.vercel.app/api/server/PQ7VX6TY4t?style=flat)](https://discord.gg/PQ7VX6TY4t) -[![Unit Tests](https://github.com/Torantulino/Auto-GPT/actions/workflows/ci.yml/badge.svg)](https://github.com/Torantulino/Auto-GPT/actions/workflows/unit_tests.yml) +[![Discord Follow](https://dcbadge.vercel.app/api/server/autogpt?style=flat)](https://discord.gg/autogpt) +[![Unit Tests](https://github.com/Torantulino/Auto-GPT/actions/workflows/ci.yml/badge.svg)](https://github.com/Torantulino/Auto-GPT/actions/workflows/ci.yml) Auto-GPT is an experimental open-source application showcasing the capabilities of the GPT-4 language model. This program, driven by GPT-4, chains together LLM "thoughts", to autonomously achieve whatever goal you set. As one of the first examples of GPT-4 running fully autonomously, Auto-GPT pushes the boundaries of what is possible with AI. From 8186ccb56a52841bc859d1bcafffaba79c27d10a Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:36:48 -0700 Subject: [PATCH 47/74] formatting --- scripts/prompt.py | 5 +++-- tests/promptgenerator_tests.py | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/scripts/prompt.py b/scripts/prompt.py index 286002ee..188603a3 100644 --- a/scripts/prompt.py +++ b/scripts/prompt.py @@ -1,9 +1,10 @@ from promptgenerator import PromptGenerator + def get_prompt(): """ This function generates a prompt string that includes various constraints, commands, resources, and performance evaluations. - + Returns: str: The generated prompt string. """ @@ -58,5 +59,5 @@ def get_prompt(): # Generate the prompt string prompt_string = prompt_generator.generate_prompt_string() - + return prompt_string diff --git a/tests/promptgenerator_tests.py b/tests/promptgenerator_tests.py index ac5c3a79..181fdea6 100644 --- a/tests/promptgenerator_tests.py +++ b/tests/promptgenerator_tests.py @@ -7,6 +7,7 @@ import os sys.path.append(os.path.abspath("../scripts")) from promptgenerator import PromptGenerator + # Create a test class for the PromptGenerator, subclassed from unittest.TestCase class promptgenerator_tests(unittest.TestCase): @@ -81,7 +82,7 @@ class promptgenerator_tests(unittest.TestCase): self.assertIn(constraint, prompt_string) for command in commands: self.assertIn(command["name"], prompt_string) - + # Check for each key-value pair in the command args dictionary for key, value in command["args"].items(): self.assertIn(f'"{key}": "{value}"', prompt_string) @@ -94,6 +95,7 @@ class promptgenerator_tests(unittest.TestCase): self.assertIn("resources", prompt_string.lower()) self.assertIn("performance evaluation", prompt_string.lower()) + # Run the tests when this script is executed if __name__ == '__main__': unittest.main() From 95b93045be17bb7205f37bb6fa28ab9d6124093c Mon Sep 17 00:00:00 2001 From: "Joseph C. Miller, II" Date: Thu, 13 Apr 2023 13:16:14 -0600 Subject: [PATCH 48/74] Exit message should be yellow --- scripts/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index 59719130..0856648e 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -357,7 +357,7 @@ def main(): # Discontinue if continuous limit is reached loop_count += 1 if cfg.continuous_mode and cfg.continuous_limit > 0 and loop_count > cfg.continuous_limit: - logger.typewriter_log("Continuous Limit Reached: ", Fore.RED, f"{cfg.continuous_limit}") + logger.typewriter_log("Continuous Limit Reached: ", Fore.YELLOW, f"{cfg.continuous_limit}") break # Send message to AI, get response From f3e973950128f5a61d23c726b93f6d312a3ab123 Mon Sep 17 00:00:00 2001 From: "Joseph C. Miller, II" Date: Thu, 13 Apr 2023 13:35:46 -0600 Subject: [PATCH 49/74] Revert inadvertent change --- scripts/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index 0856648e..08b73d64 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -189,7 +189,7 @@ def construct_prompt(): if config.ai_name: logger.typewriter_log( f"Welcome back! ", - Fore.YELLOW, + Fore.GREEN, f"Would you like me to return to being {config.ai_name}?", speak_text=True) should_continue = utils.clean_input(f"""Continue with the last settings? From 439e736b8ba2c8e676b321695b032cac2561b50e Mon Sep 17 00:00:00 2001 From: Sma Das Date: Thu, 13 Apr 2023 17:00:03 -0400 Subject: [PATCH 50/74] Removed unneeded imports --- scripts/ai_functions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/ai_functions.py b/scripts/ai_functions.py index 8c95c0f2..f4ee79cd 100644 --- a/scripts/ai_functions.py +++ b/scripts/ai_functions.py @@ -1,8 +1,7 @@ -from typing import List, Optional +from typing import List import json from config import Config from call_ai_function import call_ai_function -from json_parser import fix_and_parse_json cfg = Config() From f9cbddc9f06baed21bc5bc0f3bd6848310240737 Mon Sep 17 00:00:00 2001 From: Ron Balter <44070810+Ronbalt@users.noreply.github.com> Date: Fri, 14 Apr 2023 00:58:51 +0300 Subject: [PATCH 51/74] Enable Custom Search API in gcp project While following this guide to enable google search, this step was missing for me and the API calls to https://customsearch.googleapis.com/customsearch/v1?q= failed with: """ Custom Search API has not been used in project before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/customsearch.googleapis.com/overview?project= then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. """ Also, checked that merely https://console.developers.google.com/apis/api/customsearch.googleapis.com redirects to the active project used in the last session in GCP, so no need to provide the projectId parameter. --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d1121976..3d8fd003 100644 --- a/README.md +++ b/README.md @@ -154,9 +154,10 @@ To use the `google_official_search` command, you need to set up your Google API 4. Go to the [APIs & Services Dashboard](https://console.cloud.google.com/apis/dashboard) and click "Enable APIs and Services". Search for "Custom Search API" and click on it, then click "Enable". 5. Go to the [Credentials](https://console.cloud.google.com/apis/credentials) page and click "Create Credentials". Choose "API Key". 6. Copy the API key and set it as an environment variable named `GOOGLE_API_KEY` on your machine. See setting up environment variables below. -7. Go to the [Custom Search Engine](https://cse.google.com/cse/all) page and click "Add". -8. Set up your search engine by following the prompts. You can choose to search the entire web or specific sites. -9. Once you've created your search engine, click on "Control Panel" and then "Basics". Copy the "Search engine ID" and set it as an environment variable named `CUSTOM_SEARCH_ENGINE_ID` on your machine. See setting up environment variables below. +7. [Enable](https://console.developers.google.com/apis/api/customsearch.googleapis.com) the Custom Search API on your project. (Might need to wait few minutes to propagate) +8. Go to the [Custom Search Engine](https://cse.google.com/cse/all) page and click "Add". +9. Set up your search engine by following the prompts. You can choose to search the entire web or specific sites. +10. Once you've created your search engine, click on "Control Panel" and then "Basics". Copy the "Search engine ID" and set it as an environment variable named `CUSTOM_SEARCH_ENGINE_ID` on your machine. See setting up environment variables below. _Remember that your free daily custom search quota allows only up to 100 searches. To increase this limit, you need to assign a billing account to the project to profit from up to 10K daily searches._ @@ -357,4 +358,4 @@ flake8 scripts/ tests/ # Or, if you want to run flake8 with the same configuration as the CI: flake8 scripts/ tests/ --select E303,W293,W291,W292,E305,E231,E302 -``` \ No newline at end of file +``` From c59b6b5543793a01d1231552f6c81f4f72ddc740 Mon Sep 17 00:00:00 2001 From: Merwane Hamadi Date: Thu, 13 Apr 2023 15:19:41 -0700 Subject: [PATCH 52/74] wrap infinite loop in class agent --- scripts/main.py | 226 ++++++++++++++++++++++++++++-------------------- 1 file changed, 132 insertions(+), 94 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 466f50dd..9c823ab1 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -351,110 +351,148 @@ def main(): # this is particularly important for indexing and referencing pinecone memory memory = get_memory(cfg, init=True) print('Using memory of type: ' + memory.__class__.__name__) - # Interaction Loop - loop_count = 0 - while True: - # Discontinue if continuous limit is reached - loop_count += 1 - if cfg.continuous_mode and cfg.continuous_limit > 0 and loop_count > cfg.continuous_limit: - logger.typewriter_log("Continuous Limit Reached: ", Fore.YELLOW, f"{cfg.continuous_limit}") - break + agent = Agent( + ai_name=ai_name, + memory=memory, + full_message_history=full_message_history, + next_action_count=next_action_count, + prompt=prompt, + user_input=user_input + ) + agent.start_interaction_loop() - # Send message to AI, get response - with Spinner("Thinking... "): - assistant_reply = chat.chat_with_ai( - prompt, - user_input, - full_message_history, - memory, - cfg.fast_token_limit) # TODO: This hardcodes the model to use GPT3.5. Make this an argument - # Print Assistant thoughts - print_assistant_thoughts(assistant_reply) +class Agent: + """Agent class for interacting with Auto-GPT. - # Get command name and arguments - try: - command_name, arguments = cmd.get_command( - attempt_to_fix_json_by_finding_outermost_brackets(assistant_reply)) - if cfg.speak_mode: - speak.say_text(f"I want to execute {command_name}") - except Exception as e: - logger.error("Error: \n", str(e)) + Attributes: + ai_name: The name of the agent. + memory: The memory object to use. + full_message_history: The full message history. + next_action_count: The number of actions to execute. + prompt: The prompt to use. + user_input: The user input. - if not cfg.continuous_mode and next_action_count == 0: - ### GET USER AUTHORIZATION TO EXECUTE COMMAND ### - # Get key press: Prompt the user to press enter to continue or escape - # to exit - user_input = "" - logger.typewriter_log( - "NEXT ACTION: ", - Fore.CYAN, - f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}") - print( - f"Enter 'y' to authorise command, 'y -N' to run N continuous commands, 'n' to exit program, or enter feedback for {ai_name}...", - flush=True) - while True: - console_input = utils.clean_input(Fore.MAGENTA + "Input:" + Style.RESET_ALL) - if console_input.lower().rstrip() == "y": - user_input = "GENERATE NEXT COMMAND JSON" - break - elif console_input.lower().startswith("y -"): - try: - next_action_count = abs(int(console_input.split(" ")[1])) - user_input = "GENERATE NEXT COMMAND JSON" - except ValueError: - print("Invalid input format. Please enter 'y -n' where n is the number of continuous tasks.") - continue - break - elif console_input.lower() == "n": - user_input = "EXIT" - break - else: - user_input = console_input - command_name = "human_feedback" - break + """ + def __init__(self, + ai_name, + memory, + full_message_history, + next_action_count, + prompt, + user_input): + self.ai_name = ai_name + self.memory = memory + self.full_message_history = full_message_history + self.next_action_count = next_action_count + self.prompt = prompt + self.user_input = user_input - if user_input == "GENERATE NEXT COMMAND JSON": - logger.typewriter_log( - "-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=", - Fore.MAGENTA, - "") - elif user_input == "EXIT": - print("Exiting...", flush=True) + def start_interaction_loop(self): + # Interaction Loop + loop_count = 0 + while True: + # Discontinue if continuous limit is reached + loop_count += 1 + if cfg.continuous_mode and cfg.continuous_limit > 0 and loop_count > cfg.continuous_limit: + logger.typewriter_log("Continuous Limit Reached: ", Fore.YELLOW, f"{cfg.continuous_limit}") break - else: - # Print command - logger.typewriter_log( - "NEXT ACTION: ", - Fore.CYAN, - f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}") - # Execute command - if command_name is not None and command_name.lower().startswith("error"): - result = f"Command {command_name} threw the following error: " + arguments - elif command_name == "human_feedback": - result = f"Human feedback: {user_input}" - else: - result = f"Command {command_name} returned: {cmd.execute_command(command_name, arguments)}" - if next_action_count > 0: - next_action_count -= 1 + # Send message to AI, get response + with Spinner("Thinking... "): + assistant_reply = chat.chat_with_ai( + self.prompt, + self.user_input, + self.full_message_history, + self.memory, + cfg.fast_token_limit) # TODO: This hardcodes the model to use GPT3.5. Make this an argument - memory_to_add = f"Assistant Reply: {assistant_reply} " \ - f"\nResult: {result} " \ - f"\nHuman Feedback: {user_input} " + # Print Assistant thoughts + print_assistant_thoughts(assistant_reply) - memory.add(memory_to_add) + # Get command name and arguments + try: + command_name, arguments = cmd.get_command( + attempt_to_fix_json_by_finding_outermost_brackets(assistant_reply)) + if cfg.speak_mode: + speak.say_text(f"I want to execute {command_name}") + except Exception as e: + logger.error("Error: \n", str(e)) - # Check if there's a result from the command append it to the message - # history - if result is not None: - full_message_history.append(chat.create_chat_message("system", result)) - logger.typewriter_log("SYSTEM: ", Fore.YELLOW, result) - else: - full_message_history.append( - chat.create_chat_message( - "system", "Unable to execute command")) - logger.typewriter_log("SYSTEM: ", Fore.YELLOW, "Unable to execute command") + if not cfg.continuous_mode and self.next_action_count == 0: + ### GET USER AUTHORIZATION TO EXECUTE COMMAND ### + # Get key press: Prompt the user to press enter to continue or escape + # to exit + self.user_input = "" + logger.typewriter_log( + "NEXT ACTION: ", + Fore.CYAN, + f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}") + print( + f"Enter 'y' to authorise command, 'y -N' to run N continuous commands, 'n' to exit program, or enter feedback for {self.ai_name}...", + flush=True) + while True: + console_input = utils.clean_input(Fore.MAGENTA + "Input:" + Style.RESET_ALL) + if console_input.lower().rstrip() == "y": + self.user_input = "GENERATE NEXT COMMAND JSON" + break + elif console_input.lower().startswith("y -"): + try: + self.next_action_count = abs(int(console_input.split(" ")[1])) + self.user_input = "GENERATE NEXT COMMAND JSON" + except ValueError: + print("Invalid input format. Please enter 'y -n' where n is the number of continuous tasks.") + continue + break + elif console_input.lower() == "n": + self.user_input = "EXIT" + break + else: + self.user_input = console_input + command_name = "human_feedback" + break + + if self.user_input == "GENERATE NEXT COMMAND JSON": + logger.typewriter_log( + "-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=", + Fore.MAGENTA, + "") + elif self.user_input == "EXIT": + print("Exiting...", flush=True) + break + else: + # Print command + logger.typewriter_log( + "NEXT ACTION: ", + Fore.CYAN, + f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}") + + # Execute command + if command_name is not None and command_name.lower().startswith("error"): + result = f"Command {command_name} threw the following error: " + arguments + elif command_name == "human_feedback": + result = f"Human feedback: {self.user_input}" + else: + result = f"Command {command_name} returned: {cmd.execute_command(command_name, arguments)}" + if self.next_action_count > 0: + self.next_action_count -= 1 + + memory_to_add = f"Assistant Reply: {assistant_reply} " \ + f"\nResult: {result} " \ + f"\nHuman Feedback: {self.user_input} " + + self.memory.add(memory_to_add) + + # Check if there's a result from the command append it to the message + # history + if result is not None: + self.full_message_history.append(chat.create_chat_message("system", result)) + logger.typewriter_log("SYSTEM: ", Fore.YELLOW, result) + else: + self.full_message_history.append( + chat.create_chat_message( + "system", "Unable to execute command")) + logger.typewriter_log("SYSTEM: ", Fore.YELLOW, "Unable to execute command") if __name__ == "__main__": From 47b72df262b894752b7fd0324f42f71ffc70e38c Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Fri, 14 Apr 2023 01:20:43 +0300 Subject: [PATCH 53/74] Added 'AI_SETTINGS_FILE' to .env --- .env.template | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.env.template b/.env.template index 474b2727..c5cb54fd 100644 --- a/.env.template +++ b/.env.template @@ -3,6 +3,8 @@ ################################################################################ # EXECUTE_LOCAL_COMMANDS - Allow local command execution (Example: False) EXECUTE_LOCAL_COMMANDS=False +# AI_SETTINGS_FILE - Specifies which AI Settings file to use (defaults to ai_settings.yaml) +AI_SETTINGS_FILE=ai_settings.yaml ################################################################################ ### LLM PROVIDER From 05f6e9673f285ac40cf982a544dfa14750cf6af1 Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Fri, 14 Apr 2023 01:23:23 +0300 Subject: [PATCH 54/74] Resolve Linter Issues --- scripts/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/utils.py b/scripts/utils.py index 2b51c1fc..7521df29 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -1,6 +1,7 @@ import yaml from colorama import Fore + def clean_input(prompt: str=''): try: return input(prompt) From 43efbff4b80edbad2fc35010773372e76c3eb34b Mon Sep 17 00:00:00 2001 From: Merwane Hamadi Date: Thu, 13 Apr 2023 15:22:43 -0700 Subject: [PATCH 55/74] remove useless load_variables_method --- scripts/main.py | 54 ------------------------------------------------- 1 file changed, 54 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 466f50dd..1425e420 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -129,60 +129,6 @@ def print_assistant_thoughts(assistant_reply): logger.error("Error: \n", call_stack) -def load_variables(config_file="config.yaml"): - """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) - ai_name = config.get("ai_name") - ai_role = config.get("ai_role") - ai_goals = config.get("ai_goals") - except FileNotFoundError: - ai_name = "" - ai_role = "" - ai_goals = [] - - # Prompt the user for input if config file is missing or empty values - if not ai_name: - ai_name = utils.clean_input("Name your AI: ") - if ai_name == "": - ai_name = "Entrepreneur-GPT" - - if not ai_role: - ai_role = utils.clean_input(f"{ai_name} is: ") - if ai_role == "": - ai_role = "an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth." - - if not ai_goals: - print("Enter up to 5 goals for your AI: ") - print("For example: \nIncrease net worth, Grow Twitter Account, Develop and manage multiple businesses autonomously'") - print("Enter nothing to load defaults, enter nothing when finished.") - ai_goals = [] - for i in range(5): - ai_goal = utils.clean_input(f"Goal {i+1}: ") - if ai_goal == "": - break - ai_goals.append(ai_goal) - if len(ai_goals) == 0: - ai_goals = ["Increase net worth", "Grow Twitter Account", "Develop and manage multiple businesses autonomously"] - - # Save variables to yaml file - config = {"ai_name": ai_name, "ai_role": ai_role, "ai_goals": ai_goals} - with open(config_file, "w") as file: - documents = yaml.dump(config, file) - - prompt = get_prompt() - prompt_start = """Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.""" - - # Construct full prompt - full_prompt = f"You are {ai_name}, {ai_role}\n{prompt_start}\n\nGOALS:\n\n" - for i, goal in enumerate(ai_goals): - full_prompt += f"{i+1}. {goal}\n" - - full_prompt += f"\n\n{prompt}" - return full_prompt - - def construct_prompt(): """Construct the prompt for the AI to respond to""" config = AIConfig.load() From 8472bbd4556999cdd62e4930ae3723f18b746ef4 Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Fri, 14 Apr 2023 01:34:30 +0300 Subject: [PATCH 56/74] Added 'Command Line Arguments' section to README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index d1121976..8d402e7d 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,14 @@ To output debug logs: ``` python scripts/main.py --debug ``` +### Command Line Arguments +Here are some common arguments you can use when running Auto-GPT: +> Replace anything in angled brackets (<>) to a value you want to specify +* `python scripts/main.py --help` to see a list of all available command line arguments +* `python scripts/main.py --ai-settings ` to run Auto-GPT with a different AI Settings file. +* `python scripts/main.py --use-memory ` to specify one of 3 memory backends: `local`, `redis` or `pinecone` + +> **NOTE**: There are shorthands for some of these flags, for example `-m` for `--use-memory`. Use `python scripts/main.py --help` for more information ## 🗣️ Speech Mode From 25509f9d2541a6c92949984da5548b11903fc98b Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Fri, 14 Apr 2023 00:48:07 +0200 Subject: [PATCH 57/74] Update config.py 8192 is the current default --- scripts/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/config.py b/scripts/config.py index 0150ca42..a856265a 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -43,7 +43,7 @@ class Config(metaclass=Singleton): self.smart_llm_model = os.getenv("SMART_LLM_MODEL", "gpt-4") self.fast_token_limit = int(os.getenv("FAST_TOKEN_LIMIT", 4000)) self.smart_token_limit = int(os.getenv("SMART_TOKEN_LIMIT", 8000)) - self.browse_chunk_max_length = int(os.getenv("BROWSE_CHUNK_MAX_LENGTH", 8000)) + self.browse_chunk_max_length = int(os.getenv("BROWSE_CHUNK_MAX_LENGTH", 8192)) self.browse_summary_max_token = int(os.getenv("BROWSE_SUMMARY_MAX_TOKEN", 300)) self.openai_api_key = os.getenv("OPENAI_API_KEY") From 6702a04f767702d1e57ddcec81f2481def19f8a7 Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Fri, 14 Apr 2023 01:50:13 +0300 Subject: [PATCH 58/74] Add 'no_memory' support for memory flag --- scripts/memory/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/memory/__init__.py b/scripts/memory/__init__.py index a0afc874..9b53d8d2 100644 --- a/scripts/memory/__init__.py +++ b/scripts/memory/__init__.py @@ -3,7 +3,7 @@ from memory.no_memory import NoMemory # List of supported memory backends # Add a backend to this list if the import attempt is successful -supported_memory = ['local'] +supported_memory = ['local', 'no_memory'] try: from memory.redismem import RedisMemory From 4f923ece60baee2c086c29610a05c4f130e43aa9 Mon Sep 17 00:00:00 2001 From: Eesa Hamza Date: Fri, 14 Apr 2023 01:56:45 +0300 Subject: [PATCH 59/74] Added double_check logging to AI Settings validator, and updated README for 'no_memory' --- README.md | 4 ++-- scripts/main.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8d402e7d..b55a80c5 100644 --- a/README.md +++ b/README.md @@ -137,9 +137,9 @@ python scripts/main.py --debug ### Command Line Arguments Here are some common arguments you can use when running Auto-GPT: > Replace anything in angled brackets (<>) to a value you want to specify -* `python scripts/main.py --help` to see a list of all available command line arguments +* `python scripts/main.py --help` to see a list of all available command line arguments. * `python scripts/main.py --ai-settings ` to run Auto-GPT with a different AI Settings file. -* `python scripts/main.py --use-memory ` to specify one of 3 memory backends: `local`, `redis` or `pinecone` +* `python scripts/main.py --use-memory ` to specify one of 3 memory backends: `local`, `redis`, `pinecone` or 'no_memory'. > **NOTE**: There are shorthands for some of these flags, for example `-m` for `--use-memory`. Use `python scripts/main.py --help` for more information diff --git a/scripts/main.py b/scripts/main.py index 400eb1f6..78ffe243 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -348,6 +348,7 @@ def parse_arguments(): (validated, message) = utils.validate_yaml_file(file) if not validated: logger.typewriter_log("FAILED FILE VALIDATION", Fore.RED, message) + logger.double_check() exit(1) logger.typewriter_log("Using AI Settings File:", Fore.GREEN, file) From 4666ea015074506196762cc8008489d06a3d02bb Mon Sep 17 00:00:00 2001 From: Jesse R Weigel Date: Thu, 13 Apr 2023 21:57:31 -0400 Subject: [PATCH 60/74] fix misspelling --- scripts/call_ai_function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/call_ai_function.py b/scripts/call_ai_function.py index 6f1d6cee..940eacfe 100644 --- a/scripts/call_ai_function.py +++ b/scripts/call_ai_function.py @@ -13,7 +13,7 @@ def call_ai_function(function, args, description, model=None): model = cfg.smart_llm_model # For each arg, if any are None, convert to "None": args = [str(arg) if arg is not None else "None" for arg in args] - # parse args to comma seperated string + # parse args to comma separated string args = ", ".join(args) messages = [ { From 1f21998f0c2115a76b1c53ae2b89ea581d2fd106 Mon Sep 17 00:00:00 2001 From: sunnypranay Date: Thu, 13 Apr 2023 21:47:28 -0500 Subject: [PATCH 61/74] Improve Dockerfile with best practices and optimizations --- Dockerfile | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4d264c88..e776664e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,23 @@ +# Use an official Python base image from the Docker Hub FROM python:3.11-slim -ENV PIP_NO_CACHE_DIR=yes -WORKDIR /app -COPY requirements.txt . -RUN pip install -r requirements.txt -COPY scripts/ . -ENTRYPOINT ["python", "main.py"] + +# Set environment variables +ENV PIP_NO_CACHE_DIR=yes \ + PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 + +# Create a non-root user and set permissions +RUN useradd --create-home appuser +WORKDIR /home/appuser +RUN chown appuser:appuser /home/appuser +USER appuser + +# Copy the requirements.txt file and install the requirements +COPY --chown=appuser:appuser requirements.txt . +RUN pip install --no-cache-dir --user -r requirements.txt + +# Copy the application files +COPY --chown=appuser:appuser scripts/ . + +# Set the entrypoint +ENTRYPOINT ["python", "main.py"] \ No newline at end of file From aeb81aa597a1eedcddcc42b060b98580a1870324 Mon Sep 17 00:00:00 2001 From: eng-cc <66scc66@gmail.com> Date: Fri, 14 Apr 2023 10:54:59 +0800 Subject: [PATCH 62/74] [environments] add devcontainer environment --- .devcontainer/Dockerfile | 23 +++++++++++++++++++ .devcontainer/devcontainer.json | 39 +++++++++++++++++++++++++++++++++ README.md | 4 +++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 00000000..f3b2e2db --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,23 @@ +# [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.10-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.10-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster +ARG VARIANT=3-bullseye +FROM python:3.8 + +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + # Remove imagemagick due to https://security-tracker.debian.org/tracker/CVE-2019-10131 + && apt-get purge -y imagemagick imagemagick-6-common + +# Temporary: Upgrade python packages due to https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-40897 +# They are installed by the base image (python) which does not have the patch. +RUN python3 -m pip install --upgrade setuptools + +# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image. +# COPY requirements.txt /tmp/pip-tmp/ +# RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \ +# && rm -rf /tmp/pip-tmp + +# [Optional] Uncomment this section to install additional OS packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends + +# [Optional] Uncomment this line to install global node packages. +# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..5fefd9c1 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,39 @@ +{ + "build": { + "dockerfile": "./Dockerfile", + "context": "." + }, + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "installZsh": "true", + "username": "vscode", + "userUid": "1000", + "userGid": "1000", + "upgradePackages": "true" + }, + "ghcr.io/devcontainers/features/python:1": "none", + "ghcr.io/devcontainers/features/node:1": "none", + "ghcr.io/devcontainers/features/git:1": { + "version": "latest", + "ppa": "false" + } + }, + // Configure tool-specific properties. + "customizations": { + // Configure properties specific to VS Code. + "vscode": { + // Set *default* container specific settings.json values on container create. + "settings": { + "python.defaultInterpreterPath": "/usr/local/bin/python" + } + } + }, + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "pip3 install --user -r requirements.txt", + + // Set `remoteUser` to `root` to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "vscode" +} diff --git a/README.md b/README.md index d1121976..84967997 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,9 @@ Your support is greatly appreciated ## 📋 Requirements -- [Python 3.8 or later](https://www.tutorialspoint.com/how-to-install-python-in-windows) +- environments(just choose one) + - [vscode + devcontainer](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers): It has been configured in the .devcontainer folder and can be used directly + - [Python 3.8 or later](https://www.tutorialspoint.com/how-to-install-python-in-windows) - [OpenAI API key](https://platform.openai.com/account/api-keys) - [PINECONE API key](https://www.pinecone.io/) From 3128397988de6dc5280eb45cca814e98cd8c0a2d Mon Sep 17 00:00:00 2001 From: GyDi Date: Fri, 14 Apr 2023 11:17:46 +0800 Subject: [PATCH 63/74] fix: remove duplicate debug mode logger --- scripts/main.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 466f50dd..0221af4c 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -318,10 +318,6 @@ def parse_arguments(): logger.typewriter_log("GPT4 Only Mode: ", Fore.GREEN, "ENABLED") cfg.set_fast_llm_model(cfg.smart_llm_model) - if args.debug: - logger.typewriter_log("Debug Mode: ", Fore.GREEN, "ENABLED") - cfg.set_debug_mode(True) - if args.memory_type: supported_memory = get_supported_memory_backends() chosen = args.memory_type From 5e6d0b620a582f2f143d91df1921d44c22e648df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=AB=E5=B0=94=E7=B4=A2?= Date: Fri, 14 Apr 2023 11:38:29 +0800 Subject: [PATCH 64/74] Resolving Unicode encoding issues Solve the problem that Chinese, Japanese, Korean and other non-English languages are all encoded in Unicode when writing ai_settings.yaml configuration. --- scripts/ai_config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/ai_config.py b/scripts/ai_config.py index 4eb076ef..89a4e07e 100644 --- a/scripts/ai_config.py +++ b/scripts/ai_config.py @@ -70,8 +70,8 @@ class AIConfig: """ config = {"ai_name": self.ai_name, "ai_role": self.ai_role, "ai_goals": self.ai_goals} - with open(config_file, "w") as file: - yaml.dump(config, file) + with open(config_file, "w", encoding='utf-8') as file: + yaml.dump(config, file, allow_unicode=True) def construct_full_prompt(self) -> str: """ From 2fd96b68bdc9e7ab48b34eeff8d96e32b9eafdcf Mon Sep 17 00:00:00 2001 From: meta-fx Date: Fri, 14 Apr 2023 01:28:47 -0500 Subject: [PATCH 65/74] Added new line and elevenlabs elements back to the env --- .env.template | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.env.template b/.env.template index aa7b04e9..e22ce4f5 100644 --- a/.env.template +++ b/.env.template @@ -104,4 +104,12 @@ USE_MAC_OS_TTS=False ### STREAMELEMENTS # USE_BRIAN_TTS - Use Brian TTS or not (Default: False) -USE_BRIAN_TTS=False \ No newline at end of file +USE_BRIAN_TTS=False + +### ELEVENLABS +# ELEVENLABS_API_KEY - Eleven Labs API key (Example: my-elevenlabs-api-key) +# ELEVENLABS_VOICE_1_ID - Eleven Labs voice 1 ID (Example: my-voice-id-1) +# ELEVENLABS_VOICE_2_ID - Eleven Labs voice 2 ID (Example: my-voice-id-2) +ELEVENLABS_API_KEY=your-elevenlabs-api-key +ELEVENLABS_VOICE_1_ID=your-voice-id-1 +ELEVENLABS_VOICE_2_ID=your-voice-id-2 From 1612069594402540da4116f9f599b47091b8f041 Mon Sep 17 00:00:00 2001 From: meta-fx Date: Fri, 14 Apr 2023 02:18:17 -0500 Subject: [PATCH 66/74] Fixed E302 expected 2 blank lines, found 1 --- scripts/speak.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/speak.py b/scripts/speak.py index d71b5bca..3afa591d 100644 --- a/scripts/speak.py +++ b/scripts/speak.py @@ -52,6 +52,7 @@ def eleven_labs_speech(text, voice_index=0): print("Response content:", response.content) return False + def brian_speech(text): """Speak text using Brian with the streamelements API""" tts_url = f"https://api.streamelements.com/kappa/v2/speech?voice=Brian&text={text}" @@ -69,6 +70,7 @@ def brian_speech(text): print("Response content:", response.content) return False + def gtts_speech(text): tts = gtts.gTTS(text) with mutex_lock: From e147788c72535779ed094a101c4739aa1e8bdb8c Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Fri, 14 Apr 2023 10:33:34 +0200 Subject: [PATCH 67/74] Update .env.template BROWSE_CHUNK_MAX_LENGTH default value --- .env.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.template b/.env.template index 7ff03cab..733597d9 100644 --- a/.env.template +++ b/.env.template @@ -4,7 +4,7 @@ # EXECUTE_LOCAL_COMMANDS - Allow local command execution (Example: False) EXECUTE_LOCAL_COMMANDS=False # BROWSE_CHUNK_MAX_LENGTH - When browsing website, define the length of chunk stored in memory -BROWSE_CHUNK_MAX_LENGTH=4000 +BROWSE_CHUNK_MAX_LENGTH=8192 # BROWSE_SUMMARY_MAX_TOKEN - Define the maximum length of the summary generated by GPT agent when browsing website BROWSE_SUMMARY_MAX_TOKEN=300 # USER_AGENT - Define the user-agent used by the requests library to browse website (string) From 6403bf112790b34fa122bdd519703e4b110f6875 Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Fri, 14 Apr 2023 10:35:30 +0200 Subject: [PATCH 68/74] Update data_ingestion.py fixed linting --- scripts/data_ingestion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/data_ingestion.py b/scripts/data_ingestion.py index 32811166..9addc34b 100644 --- a/scripts/data_ingestion.py +++ b/scripts/data_ingestion.py @@ -6,6 +6,7 @@ from file_operations import ingest_file, search_files cfg = Config() + def configure_logging(): logging.basicConfig(filename='log-ingestion.txt', filemode='a', @@ -43,7 +44,6 @@ def main(): args = parser.parse_args() - # Initialize memory memory = get_memory(cfg, init=args.init) print('Using memory of type: ' + memory.__class__.__name__) From c0462dbe7768d41ac205644987ad0fa9f14a5afc Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Fri, 14 Apr 2023 10:35:52 +0200 Subject: [PATCH 69/74] Update file_operations.py fixed linting --- scripts/file_operations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/file_operations.py b/scripts/file_operations.py index ed5aa4ec..1a072561 100644 --- a/scripts/file_operations.py +++ b/scripts/file_operations.py @@ -42,6 +42,7 @@ def split_file(content, max_length=4000, overlap=0): yield chunk start += max_length - overlap + def read_file(filename): """Read a file and return the contents""" try: From 475edd3b40c8769b22519083af2106bcbd08e559 Mon Sep 17 00:00:00 2001 From: Mike Kelly Date: Fri, 14 Apr 2023 12:32:33 +0100 Subject: [PATCH 70/74] make the path reference in logger more robust --- scripts/logger.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/logger.py b/scripts/logger.py index 91bdb6f6..4c7e588f 100644 --- a/scripts/logger.py +++ b/scripts/logger.py @@ -24,7 +24,8 @@ For console handler: simulates typing class Logger(metaclass=Singleton): def __init__(self): # create log directory if it doesn't exist - log_dir = os.path.join('..', 'logs') + this_files_dir_path = os.path.dirname(__file__) + log_dir = os.path.join(this_files_dir_path, '../logs') if not os.path.exists(log_dir): os.makedirs(log_dir) From 9e27e0165d15d441e58e5771e22957ae4fbd0063 Mon Sep 17 00:00:00 2001 From: Mike Kelly Date: Fri, 14 Apr 2023 12:36:18 +0100 Subject: [PATCH 71/74] gitignore the logs file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cf6e75df..0a98328c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ last_run_ai_settings.yaml .idea/* auto-gpt.json log.txt +logs # Coverage reports .coverage From b18530a9854f7b3e0cd5fb8333221bb0000cc4cb Mon Sep 17 00:00:00 2001 From: sagarishere <5121817+sagarishere@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:31:45 +0300 Subject: [PATCH 72/74] update to modern python format syntax update to modern python format syntax no logic change --- scripts/spinner.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/spinner.py b/scripts/spinner.py index df1f4ddf..d2321529 100644 --- a/scripts/spinner.py +++ b/scripts/spinner.py @@ -17,10 +17,10 @@ class Spinner: def spin(self): """Spin the spinner""" while self.running: - sys.stdout.write(next(self.spinner) + " " + self.message + "\r") + sys.stdout.write(f"{next(self.spinner)} {self.message}\r") sys.stdout.flush() time.sleep(self.delay) - sys.stdout.write('\r' + ' ' * (len(self.message) + 2) + '\r') + sys.stdout.write(f"\r{' ' * (len(self.message) + 2)}\r") def __enter__(self): """Start the spinner""" @@ -32,5 +32,5 @@ class Spinner: """Stop the spinner""" self.running = False self.spinner_thread.join() - sys.stdout.write('\r' + ' ' * (len(self.message) + 2) + '\r') + sys.stdout.write(f"\r{' ' * (len(self.message) + 2)}\r") sys.stdout.flush() From a67818648ed722e4e50133e01c0c2f189dfec05c Mon Sep 17 00:00:00 2001 From: Maiko Bossuyt Date: Fri, 14 Apr 2023 18:10:42 +0200 Subject: [PATCH 73/74] Update browse.py linting --- scripts/browse.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/browse.py b/scripts/browse.py index a4a41744..ef22de03 100644 --- a/scripts/browse.py +++ b/scripts/browse.py @@ -137,6 +137,7 @@ def create_message(chunk, question): "content": f"\"\"\"{chunk}\"\"\" Using the above text, please answer the following question: \"{question}\" -- if the question cannot be answered using the text, please summarize the text." } + def summarize_text(url, text, question): """Summarize text using the LLM model""" if not text: From 2ba0cb24dc84fae271d6466f00cc082cc8c44a4e Mon Sep 17 00:00:00 2001 From: Mike Kelly Date: Fri, 14 Apr 2023 16:39:29 +0100 Subject: [PATCH 74/74] execute python via shell if already running in a container --- scripts/execute_code.py | 93 +++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/scripts/execute_code.py b/scripts/execute_code.py index dbd62c22..45263d02 100644 --- a/scripts/execute_code.py +++ b/scripts/execute_code.py @@ -19,53 +19,60 @@ def execute_python_file(file): if not os.path.isfile(file_path): return f"Error: File '{file}' does not exist." - try: - client = docker.from_env() - - image_name = 'python:3.10' + if we_are_running_in_a_docker_container(): + result = subprocess.run(f'python {file_path}', capture_output=True, encoding="utf8", shell=True) + if result.returncode == 0: + return result.stdout + else: + return f"Error: {result.stderr}" + else: try: - client.images.get(image_name) - print(f"Image '{image_name}' found locally") - except docker.errors.ImageNotFound: - print(f"Image '{image_name}' not found locally, pulling from Docker Hub") - # Use the low-level API to stream the pull response - low_level_client = docker.APIClient() - for line in low_level_client.pull(image_name, stream=True, decode=True): - # Print the status and progress, if available - status = line.get('status') - progress = line.get('progress') - if status and progress: - print(f"{status}: {progress}") - elif status: - print(status) + client = docker.from_env() - # You can replace 'python:3.8' with the desired Python image/version - # You can find available Python images on Docker Hub: - # https://hub.docker.com/_/python - container = client.containers.run( - image_name, - f'python {file}', - volumes={ - os.path.abspath(WORKSPACE_FOLDER): { - 'bind': '/workspace', - 'mode': 'ro'}}, - working_dir='/workspace', - stderr=True, - stdout=True, - detach=True, - ) + image_name = 'python:3.10' + try: + client.images.get(image_name) + print(f"Image '{image_name}' found locally") + except docker.errors.ImageNotFound: + print(f"Image '{image_name}' not found locally, pulling from Docker Hub") + # Use the low-level API to stream the pull response + low_level_client = docker.APIClient() + for line in low_level_client.pull(image_name, stream=True, decode=True): + # Print the status and progress, if available + status = line.get('status') + progress = line.get('progress') + if status and progress: + print(f"{status}: {progress}") + elif status: + print(status) - output = container.wait() - logs = container.logs().decode('utf-8') - container.remove() + # You can replace 'python:3.8' with the desired Python image/version + # You can find available Python images on Docker Hub: + # https://hub.docker.com/_/python + container = client.containers.run( + image_name, + f'python {file}', + volumes={ + os.path.abspath(WORKSPACE_FOLDER): { + 'bind': '/workspace', + 'mode': 'ro'}}, + working_dir='/workspace', + stderr=True, + stdout=True, + detach=True, + ) - # print(f"Execution complete. Output: {output}") - # print(f"Logs: {logs}") + output = container.wait() + logs = container.logs().decode('utf-8') + container.remove() - return logs + # print(f"Execution complete. Output: {output}") + # print(f"Logs: {logs}") - except Exception as e: - return f"Error: {str(e)}" + return logs + + except Exception as e: + return f"Error: {str(e)}" def execute_shell(command_line): @@ -86,3 +93,7 @@ def execute_shell(command_line): os.chdir(current_dir) return output + + +def we_are_running_in_a_docker_container(): + os.path.exists('/.dockerenv')