dirtyjson -> json

Fixes the missing UserDict (caused by migration from python 3.9 to ^3.10)
This commit is contained in:
Veylkh
2023-04-03 14:28:22 +02:00
parent 439a7ffe7d
commit f72afc7558
2 changed files with 6 additions and 8 deletions

View File

@@ -1,6 +1,5 @@
beautifulsoup4 beautifulsoup4
colorama==0.4.6 colorama==0.4.6
dirtyjson==1.0.
openai==0.27.2 openai==0.27.2
playsound==1.3.0 playsound==1.3.0
python-dotenv==1.0.0 python-dotenv==1.0.0

View File

@@ -1,4 +1,4 @@
import dirtyjson import json
from call_ai_function import call_ai_function from call_ai_function import call_ai_function
from config import Config from config import Config
cfg = Config() cfg = Config()
@@ -24,7 +24,7 @@ def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True):
""" """
try: try:
return dirtyjson.loads(json_str) return json.loads(json_str)
except Exception as e: except Exception as e:
# Let's do something manually - sometimes GPT responds with something BEFORE the braces: # Let's do something manually - sometimes GPT responds with something BEFORE the braces:
# "I'm sorry, I don't understand. Please try again."{"text": "I'm sorry, I don't understand. Please try again.", "confidence": 0.0} # "I'm sorry, I don't understand. Please try again."{"text": "I'm sorry, I don't understand. Please try again.", "confidence": 0.0}
@@ -34,21 +34,20 @@ def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True):
json_str = json_str[brace_index:] json_str = json_str[brace_index:]
last_brace_index = json_str.rindex("}") last_brace_index = json_str.rindex("}")
json_str = json_str[:last_brace_index+1] json_str = json_str[:last_brace_index+1]
return dirtyjson.loads(json_str) return json.loads(json_str)
except Exception as e: except Exception as e:
if try_to_fix_with_gpt: if try_to_fix_with_gpt:
print(f"Warning: Failed to parse AI output, attempting to fix.\n If you see this warning frequently, it's likely that your prompt is confusing the AI. Try changing it up slightly.") print(f"Warning: Failed to parse AI output, attempting to fix.\n If you see this warning frequently, it's likely that your prompt is confusing the AI. Try changing it up slightly.")
# Now try to fix this up using the ai_functions # Now try to fix this up using the ai_functions
ai_fixed_json = fix_json(json_str, json_schema, False) ai_fixed_json = fix_json(json_str, json_schema, False)
if ai_fixed_json != "failed": if ai_fixed_json != "failed":
return dirtyjson.loads(ai_fixed_json) return json.loads(ai_fixed_json)
else: else:
print(f"Failed to fix ai output, telling the AI.") # This allows the AI to react to the error message, which usually results in it correcting its ways. print(f"Failed to fix ai output, telling the AI.") # This allows the AI to react to the error message, which usually results in it correcting its ways.
return json_str return json_str
else: else:
raise e raise e
# TODO: Make debug a global config var
def fix_json(json_str: str, schema: str, debug=False) -> str: def fix_json(json_str: str, schema: str, debug=False) -> str:
# Try to fix the JSON using gpt: # Try to fix the JSON using gpt:
function_string = "def fix_json(json_str: str, schema:str=None) -> str:" function_string = "def fix_json(json_str: str, schema:str=None) -> str:"
@@ -68,10 +67,10 @@ def fix_json(json_str: str, schema: str, debug=False) -> str:
print(f"Fixed JSON: {result_string}") print(f"Fixed JSON: {result_string}")
print("----------- END OF FIX ATTEMPT ----------------") print("----------- END OF FIX ATTEMPT ----------------")
try: try:
return dirtyjson.loads(result_string) return json.loads(result_string)
except: except:
# Get the call stack: # Get the call stack:
# import traceback # import traceback
# call_stack = traceback.format_exc() # call_stack = traceback.format_exc()
# print(f"Failed to fix JSON: '{json_str}' "+call_stack) # print(f"Failed to fix JSON: '{json_str}' "+call_stack)
return "failed" return "failed"