attempt fix

This commit is contained in:
Wladastic
2023-04-10 21:30:21 +02:00
parent 1f9d66b745
commit 33d1fd3d6d

View File

@@ -58,28 +58,30 @@ def print_to_console(
print() print()
def attempt_to_fix_json_by_finding_outermost_brackets(json_string): 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: if cfg.speak_mode:
speak.say_text("I have received an invalid JSON response from the OpenAI API. Trying to fix it now.") 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: try:
# Find the first bracket # Use regex to search for JSON objects
first_bracket_index = json_string.index("{") import re
# Find the last bracket json_pattern = re.compile(r"\{(?:[^{}]|(?R))*\}")
last_bracket_index = json_string.rindex("}") json_match = json_pattern.search(json_string)
# Slice the string to get the JSON
json_string = json_string[first_bracket_index:last_bracket_index + 1] if json_match:
if cfg.speak_mode: # Extract the valid JSON object from the string
speak.say_text("Apparently I managed to fix it.") json_string = json_match.group(0)
except json.JSONDecodeError as e: 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: if cfg.speak_mode:
speak.say_text("Didn't work. I will have to ignore this response then.") 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, "") print_to_console("Error: Invalid JSON, setting it to empty JSON now.\n", Fore.RED, "")
json_string = {} json_string = {}
return json_string return json_string
def print_assistant_thoughts(assistant_reply): def print_assistant_thoughts(assistant_reply):