From 66b36e9879662867b9d31f1ffc18605e07bb437d Mon Sep 17 00:00:00 2001 From: Wlad Date: Mon, 10 Apr 2023 15:35:10 +0200 Subject: [PATCH 01/10] fix json parser - messy --- scripts/main.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index f96afeb16..876ab5e45 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -44,21 +44,50 @@ def print_to_console( max_typing_speed = max_typing_speed * 0.95 print() +def attempt_to_fix_json_by_finding_outermost_brackets(json_string): + # Attempt to fix JSON by finding the outermost brackets + # This is a hacky solution to fix JSON that is missing the outermost brackets + # This is a common issue with the OpenAI API + # This is not a perfect solution, but it works for the most common cases + # it removes unnecessary text around the JSON + if cfg.speak_mode: + speak.say_text("I have received an invalid JSON response from the OpenAI API. Trying to fix it now.") + print_to_console("Attempting to fix JSON by finding outermost brackets\n", Fore.RED,"") + try: + # Find the first bracket + first_bracket_index = json_string.index("{") + # Find the last bracket + last_bracket_index = json_string.rindex("}") + # Slice the string to get the JSON + json_string = json_string[first_bracket_index:last_bracket_index + 1] + if cfg.speak_mode: + speak.say_text("Apparently I managed to fix it.") + except json.JSONDecodeError as e: + if cfg.speak_mode: + speak.say_text("Didn't work. I will have to ignore this response then.") + print_to_console("Error: Invalid JSON, setting it to empty JSON now.\n", Fore.RED, "") + json_string = {} + return json_string def print_assistant_thoughts(assistant_reply): global ai_name global cfg try: - # Parse and print Assistant response - assistant_reply_json = fix_and_parse_json(assistant_reply) + try: + # Parse and print Assistant response + assistant_reply_json = fix_and_parse_json(assistant_reply) + except json.JSONDecodeError as e: + print_to_console("Error: Invalid JSON in assistant thoughts\n", Fore.RED, assistant_reply) + assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets(assistant_reply) + assistant_reply_json = fix_and_parse_json(assistant_reply_json) # Check if assistant_reply_json is a string and attempt to parse it into a JSON object if isinstance(assistant_reply_json, str): try: assistant_reply_json = json.loads(assistant_reply_json) except json.JSONDecodeError as e: - print_to_console("Error: Invalid JSON\n", Fore.RED, assistant_reply) - assistant_reply_json = {} + print_to_console("Error: Invalid JSON in assistant thoughts\n", Fore.RED, assistant_reply) + assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets(assistant_reply_json) assistant_thoughts_reasoning = None assistant_thoughts_plan = None @@ -95,8 +124,11 @@ def print_assistant_thoughts(assistant_reply): if cfg.speak_mode and assistant_thoughts_speak: speak.say_text(assistant_thoughts_speak) - except json.decoder.JSONDecodeError: + except json.decoder.JSONDecodeError as e: print_to_console("Error: Invalid JSON\n", Fore.RED, assistant_reply) + print_to_console("Stack trace:\n", Fore.RED, e.with_traceback) + if cfg.speak_mode: + speak.say_text("I have received an invalid JSON response from the OpenAI API. I cannot ignore this response.") # All other errors, return "Error: + error message" except Exception as e: From bb36634b46af4daf17812bb1884d5b7c45e54605 Mon Sep 17 00:00:00 2001 From: Wlad Date: Mon, 10 Apr 2023 19:40:55 +0200 Subject: [PATCH 02/10] fix line 328 error --- scripts/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index 876ab5e45..cb2860267 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -331,7 +331,7 @@ while True: user_input, full_message_history, memory, - cfg.fast_token_limit, cfg.debug) # TODO: This hardcodes the model to use GPT3.5. Make this an argument + 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) From 33d1fd3d6d955278392bca6fb95b191d008780b6 Mon Sep 17 00:00:00 2001 From: Wladastic Date: Mon, 10 Apr 2023 21:30:21 +0200 Subject: [PATCH 03/10] attempt fix --- scripts/main.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 654434e8b..bfc46fd55 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -58,28 +58,30 @@ def print_to_console( print() def attempt_to_fix_json_by_finding_outermost_brackets(json_string): - # Attempt to fix JSON by finding the outermost brackets - # This is a hacky solution to fix JSON that is missing the outermost brackets - # This is a common issue with the OpenAI API - # This is not a perfect solution, but it works for the most common cases - # it removes unnecessary text around the JSON if cfg.speak_mode: speak.say_text("I have received an invalid JSON response from the OpenAI API. Trying to fix it now.") - print_to_console("Attempting to fix JSON by finding outermost brackets\n", Fore.RED,"") + print_to_console("Attempting to fix JSON by finding outermost brackets\n", Fore.RED, "") + try: - # Find the first bracket - first_bracket_index = json_string.index("{") - # Find the last bracket - last_bracket_index = json_string.rindex("}") - # Slice the string to get the JSON - json_string = json_string[first_bracket_index:last_bracket_index + 1] - if cfg.speak_mode: - speak.say_text("Apparently I managed to fix it.") - except json.JSONDecodeError as e: + # Use regex to search for JSON objects + import re + json_pattern = re.compile(r"\{(?:[^{}]|(?R))*\}") + json_match = json_pattern.search(json_string) + + if json_match: + # Extract the valid JSON object from the string + json_string = json_match.group(0) + if cfg.speak_mode: + speak.say_text("Apparently I managed to fix it.") + else: + raise ValueError("No valid JSON object found") + + except (json.JSONDecodeError, ValueError) as e: if cfg.speak_mode: speak.say_text("Didn't work. I will have to ignore this response then.") print_to_console("Error: Invalid JSON, setting it to empty JSON now.\n", Fore.RED, "") json_string = {} + return json_string def print_assistant_thoughts(assistant_reply): From ca4afca5dadb696292e5ca26ae16665ac6245153 Mon Sep 17 00:00:00 2001 From: Wlad Date: Mon, 10 Apr 2023 21:48:00 +0200 Subject: [PATCH 04/10] add log when json was fixed. --- scripts/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index bfc46fd55..df07cb264 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -71,8 +71,9 @@ def attempt_to_fix_json_by_finding_outermost_brackets(json_string): if json_match: # Extract the valid JSON object from the string json_string = json_match.group(0) + print_to_console("Apparently json was fixed.", Fore.GREEN,"") if cfg.speak_mode: - speak.say_text("Apparently I managed to fix it.") + speak.say_text("Apparently json was fixed.") else: raise ValueError("No valid JSON object found") From 4ad1fc14a4869e9d59ae2920e36c286621097360 Mon Sep 17 00:00:00 2001 From: Wladastic Date: Mon, 10 Apr 2023 22:59:16 +0200 Subject: [PATCH 05/10] use regex instead of re --- scripts/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index df07cb264..6a4723d4e 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -64,8 +64,8 @@ def attempt_to_fix_json_by_finding_outermost_brackets(json_string): try: # Use regex to search for JSON objects - import re - json_pattern = re.compile(r"\{(?:[^{}]|(?R))*\}") + import regex + json_pattern = regex.compile(r"\{(?:[^{}]|(?R))*\}") json_match = json_pattern.search(json_string) if json_match: From 82379bb4168bc9e31e93043b28008720d42ffe38 Mon Sep 17 00:00:00 2001 From: Wlad Date: Tue, 11 Apr 2023 01:02:45 +0200 Subject: [PATCH 06/10] fix dump --- scripts/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 6a4723d4e..3c1c789cd 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -140,10 +140,8 @@ def print_assistant_thoughts(assistant_reply): # Speak the assistant's thoughts if cfg.speak_mode and assistant_thoughts_speak: speak.say_text(assistant_thoughts_speak) - except json.decoder.JSONDecodeError as e: print_to_console("Error: Invalid JSON\n", Fore.RED, assistant_reply) - print_to_console("Stack trace:\n", Fore.RED, e.with_traceback) if cfg.speak_mode: speak.say_text("I have received an invalid JSON response from the OpenAI API. I cannot ignore this response.") @@ -363,6 +361,8 @@ while True: # Get command name and arguments try: command_name, arguments = cmd.get_command(assistant_reply) + if cfg.speak_mode: + speak.say_text(f"I want to execute {command_name}") except Exception as e: print_to_console("Error: \n", Fore.RED, str(e)) From 87a2cf029dccc92b6da90960c326ca8b773aa9ed Mon Sep 17 00:00:00 2001 From: Wlad Date: Tue, 11 Apr 2023 01:30:07 +0200 Subject: [PATCH 07/10] try another fix --- scripts/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index cb91771b1..bf27953b4 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -370,11 +370,11 @@ while True: cfg.fast_token_limit) # TODO: This hardcodes the model to use GPT3.5. Make this an argument # Print Assistant thoughts - assistant_reply = print_assistant_thoughts(assistant_reply) + print_assistant_thoughts(assistant_reply) # Get command name and arguments try: - command_name, arguments = cmd.get_command(assistant_reply) + 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: From a86948fa23dee75f6c38e69da8523dcfc003c0c6 Mon Sep 17 00:00:00 2001 From: Wlad Date: Tue, 11 Apr 2023 01:31:18 +0200 Subject: [PATCH 08/10] include broken json in error log --- scripts/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index bf27953b4..498c1b59b 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -71,7 +71,7 @@ def print_to_console( def attempt_to_fix_json_by_finding_outermost_brackets(json_string): if cfg.speak_mode: speak.say_text("I have received an invalid JSON response from the OpenAI API. Trying to fix it now.") - print_to_console("Attempting to fix JSON by finding outermost brackets\n", Fore.RED, "") + print_to_console("Attempting to fix JSON by finding outermost brackets\n", Fore.RED, json_string) try: # Use regex to search for JSON objects From 365ef9b50b499f284148e70abeb5276b3fbb7d97 Mon Sep 17 00:00:00 2001 From: Wlad Date: Tue, 11 Apr 2023 01:57:49 +0200 Subject: [PATCH 09/10] cleanup --- scripts/main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 498c1b59b..979203ea0 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -69,9 +69,9 @@ def print_to_console( print() def attempt_to_fix_json_by_finding_outermost_brackets(json_string): - if cfg.speak_mode: - speak.say_text("I have received an invalid JSON response from the OpenAI API. Trying to fix it now.") - print_to_console("Attempting to fix JSON by finding outermost brackets\n", Fore.RED, json_string) + if cfg.speak_mode and cfg.debug: + speak.say_text("I have received an invalid JSON response from the OpenAI API. Trying to fix it now.") + print_to_console("Attempting to fix JSON by finding outermost brackets\n", Fore.RED, "") try: # Use regex to search for JSON objects @@ -83,8 +83,8 @@ def attempt_to_fix_json_by_finding_outermost_brackets(json_string): # Extract the valid JSON object from the string json_string = json_match.group(0) print_to_console("Apparently json was fixed.", Fore.GREEN,"") - if cfg.speak_mode: - speak.say_text("Apparently json was fixed.") + if cfg.speak_mode and cfg.debug: + speak.say_text("Apparently json was fixed.") else: raise ValueError("No valid JSON object found") From 4063483b87518b7c6a1c55b45f5dcb09c179f82e Mon Sep 17 00:00:00 2001 From: Wlad Date: Wed, 12 Apr 2023 10:56:54 +0200 Subject: [PATCH 10/10] add my fixes --- scripts/chat.py | 8 ++++---- scripts/main.py | 4 ++-- scripts/speak.py | 12 +++++++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/scripts/chat.py b/scripts/chat.py index 23e5b5014..91bb2412c 100644 --- a/scripts/chat.py +++ b/scripts/chat.py @@ -64,14 +64,14 @@ def chat_with_ai( model = cfg.fast_llm_model # TODO: Change model from hardcode to argument # Reserve 1000 tokens for the response - if cfg.debug: + if cfg.debug_mode: print(f"Token limit: {token_limit}") send_token_limit = token_limit - 1000 - relevant_memory = permanent_memory.get_relevant(str(full_message_history[-5:]), 10) + relevant_memory = permanent_memory.get_relevant(str(full_message_history[-9:]), 10) - if cfg.debug: + if cfg.debug_mode: print('Memory Stats: ', permanent_memory.get_stats()) next_message_to_add_index, current_tokens_used, insertion_index, current_context = generate_context( @@ -110,7 +110,7 @@ def chat_with_ai( # assert tokens_remaining >= 0, "Tokens remaining is negative. This should never happen, please submit a bug report at https://www.github.com/Torantulino/Auto-GPT" # Debug print the current context - if cfg.debug: + if cfg.debug_mode: print(f"Token limit: {token_limit}") print(f"Send Token Count: {current_tokens_used}") print(f"Tokens remaining for response: {tokens_remaining}") diff --git a/scripts/main.py b/scripts/main.py index 4e950d8bc..1d878afed 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -67,7 +67,7 @@ def print_to_console( print() def attempt_to_fix_json_by_finding_outermost_brackets(json_string): - if cfg.speak_mode and cfg.debug: + if cfg.speak_mode and cfg.debug_mode: speak.say_text("I have received an invalid JSON response from the OpenAI API. Trying to fix it now.") print_to_console("Attempting to fix JSON by finding outermost brackets\n", Fore.RED, "") @@ -81,7 +81,7 @@ def attempt_to_fix_json_by_finding_outermost_brackets(json_string): # Extract the valid JSON object from the string json_string = json_match.group(0) print_to_console("Apparently json was fixed.", Fore.GREEN,"") - if cfg.speak_mode and cfg.debug: + if cfg.speak_mode and cfg.debug_mode: speak.say_text("Apparently json was fixed.") else: raise ValueError("No valid JSON object found") diff --git a/scripts/speak.py b/scripts/speak.py index 08b0c1c98..1f636a4d1 100644 --- a/scripts/speak.py +++ b/scripts/speak.py @@ -46,15 +46,21 @@ def gtts_speech(text): playsound("speech.mp3", True) os.remove("speech.mp3") -def macos_tts_speech(text): - os.system(f'say "{text}"') +def macos_tts_speech(text, voice_index=0): + if voice_index == 0: + os.system(f'say "{text}"') + else: + if voice_index == 1: + os.system(f'say -v "Ava (Premium)" "{text}"') + else: + os.system(f'say -v Samantha "{text}"') def say_text(text, voice_index=0): def speak(): if not cfg.elevenlabs_api_key: if cfg.use_mac_os_tts == 'True': - macos_tts_speech(text) + macos_tts_speech(text, voice_index) else: gtts_speech(text) else: