Merge branch 'master' into dev

This commit is contained in:
Andres Caicedo
2023-04-09 15:42:53 +02:00
28 changed files with 918 additions and 95 deletions

View File

@@ -25,6 +25,7 @@ def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True):
"""
try:
json_str = json_str.replace('\t', '')
return json.loads(json_str)
except Exception as e:
# Let's do something manually - sometimes GPT responds with something BEFORE the braces:
@@ -40,7 +41,7 @@ def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True):
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.")
# 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, cfg.debug)
if ai_fixed_json != "failed":
return json.loads(ai_fixed_json)
else:
@@ -53,7 +54,7 @@ def fix_json(json_str: str, schema: str, debug=False) -> str:
"""Fix the given JSON string to make it parseable and fully complient with the provided schema."""
# Try to fix the JSON using gpt:
function_string = "def fix_json(json_str: str, schema:str=None) -> str:"
args = [json_str, schema]
args = [f"'''{json_str}'''", f"'''{schema}'''"]
description_string = """Fixes the provided JSON string to make it parseable and fully complient with the provided schema.\n If an object or field specifed in the schema isn't contained within the correct JSON, it is ommited.\n This function is brilliant at guessing when the format is incorrect."""
# If it doesn't already start with a "`", add one:
@@ -70,7 +71,8 @@ def fix_json(json_str: str, schema: str, debug=False) -> str:
print("----------- END OF FIX ATTEMPT ----------------")
try:
return json.loads(result_string)
json.loads(result_string) # just check the validity
return result_string
except:
# Get the call stack:
# import traceback