[1607] Sourcery is detecting linting issues in autogpt/json_fixes/auto_fix.py
parent
1073954fb7
commit
df4f160846
|
@ -1,14 +1,15 @@
|
||||||
"""This module contains the function to fix JSON strings using GPT-3."""
|
"""This module contains the function to fix JSON strings using GPT-3."""
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from autogpt.llm_utils import call_ai_function
|
from autogpt.llm_utils import call_ai_function
|
||||||
from autogpt.logs import logger
|
from autogpt.logs import logger
|
||||||
|
|
||||||
|
|
||||||
def fix_json(json_str: str, schema: str) -> str:
|
def fix_json(json_string: str, schema: str) -> str:
|
||||||
"""Fix the given JSON string to make it parseable and fully compliant with the provided schema."""
|
"""Fix the given JSON string to make it parseable and fully compliant with the provided schema."""
|
||||||
# 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_string: str, schema:str=None) -> str:"
|
||||||
args = [f"'''{json_str}'''", f"'''{schema}'''"]
|
args = [f"'''{json_string}'''", f"'''{schema}'''"]
|
||||||
description_string = (
|
description_string = (
|
||||||
"Fixes the provided JSON string to make it parseable"
|
"Fixes the provided JSON string to make it parseable"
|
||||||
" and fully compliant with the provided schema.\n If an object or"
|
" and fully compliant with the provided schema.\n If an object or"
|
||||||
|
@ -18,13 +19,13 @@ def fix_json(json_str: str, schema: str) -> str:
|
||||||
)
|
)
|
||||||
|
|
||||||
# If it doesn't already start with a "`", add one:
|
# If it doesn't already start with a "`", add one:
|
||||||
if not json_str.startswith("`"):
|
if not json_string.startswith("`"):
|
||||||
json_str = "```json\n" + json_str + "\n```"
|
json_string = "```json\n" + json_string + "\n```"
|
||||||
result_string = call_ai_function(
|
result_string = call_ai_function(
|
||||||
function_string, args, description_string, model=cfg.fast_llm_model
|
function_string, args, description_string, model=cfg.fast_llm_model
|
||||||
)
|
)
|
||||||
logger.debug("------------ JSON FIX ATTEMPT ---------------")
|
logger.debug("------------ JSON FIX ATTEMPT ---------------")
|
||||||
logger.debug(f"Original JSON: {json_str}")
|
logger.debug(f"Original JSON: {json_string}")
|
||||||
logger.debug("-----------")
|
logger.debug("-----------")
|
||||||
logger.debug(f"Fixed JSON: {result_string}")
|
logger.debug(f"Fixed JSON: {result_string}")
|
||||||
logger.debug("----------- END OF FIX ATTEMPT ----------------")
|
logger.debug("----------- END OF FIX ATTEMPT ----------------")
|
||||||
|
@ -32,9 +33,9 @@ def fix_json(json_str: str, schema: str) -> str:
|
||||||
try:
|
try:
|
||||||
json.loads(result_string) # just check the validity
|
json.loads(result_string) # just check the validity
|
||||||
return result_string
|
return result_string
|
||||||
except: # noqa: E722
|
except json.JSONDecodeError: # noqa: E722
|
||||||
# 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_string}' "+call_stack)
|
||||||
return "failed"
|
return "failed"
|
||||||
|
|
Loading…
Reference in New Issue