mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-19 15:04:26 +01:00
Refactor parsing module and move JSON fix function to appropriate location
This commit is contained in:
@@ -3,18 +3,24 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
import json
|
import json
|
||||||
|
<<<<<<< HEAD
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
=======
|
||||||
|
from typing import Any, Dict, Union
|
||||||
|
from colorama import Fore
|
||||||
|
from regex import regex
|
||||||
|
>>>>>>> d3d8253b (Refactor parsing module and move JSON fix function to appropriate location)
|
||||||
from autogpt.config import Config
|
from autogpt.config import Config
|
||||||
from autogpt.json_fixes.auto_fix import fix_json
|
from autogpt.json_fixes.auto_fix import fix_json
|
||||||
from autogpt.json_fixes.bracket_termination import balance_braces
|
from autogpt.json_fixes.bracket_termination import balance_braces
|
||||||
from autogpt.json_fixes.escaping import fix_invalid_escape
|
from autogpt.json_fixes.escaping import fix_invalid_escape
|
||||||
from autogpt.json_fixes.missing_quotes import add_quotes_to_property_names
|
from autogpt.json_fixes.missing_quotes import add_quotes_to_property_names
|
||||||
from autogpt.logs import logger
|
from autogpt.logs import logger
|
||||||
|
from autogpt.speech import say_text
|
||||||
|
|
||||||
CFG = Config()
|
CFG = Config()
|
||||||
|
|
||||||
|
|
||||||
JSON_SCHEMA = """
|
JSON_SCHEMA = """
|
||||||
{
|
{
|
||||||
"command": {
|
"command": {
|
||||||
@@ -38,7 +44,6 @@ JSON_SCHEMA = """
|
|||||||
def correct_json(json_to_load: str) -> str:
|
def correct_json(json_to_load: str) -> str:
|
||||||
"""
|
"""
|
||||||
Correct common JSON errors.
|
Correct common JSON errors.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
json_to_load (str): The JSON string.
|
json_to_load (str): The JSON string.
|
||||||
"""
|
"""
|
||||||
@@ -72,7 +77,7 @@ def correct_json(json_to_load: str) -> str:
|
|||||||
|
|
||||||
def fix_and_parse_json(
|
def fix_and_parse_json(
|
||||||
json_to_load: str, try_to_fix_with_gpt: bool = True
|
json_to_load: str, try_to_fix_with_gpt: bool = True
|
||||||
) -> str | dict[Any, Any]:
|
) -> Dict[Any, Any]:
|
||||||
"""Fix and parse JSON string
|
"""Fix and parse JSON string
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -110,7 +115,11 @@ def fix_and_parse_json(
|
|||||||
|
|
||||||
def try_ai_fix(
|
def try_ai_fix(
|
||||||
try_to_fix_with_gpt: bool, exception: Exception, json_to_load: str
|
try_to_fix_with_gpt: bool, exception: Exception, json_to_load: str
|
||||||
|
<<<<<<< HEAD
|
||||||
) -> str | dict[Any, Any]:
|
) -> str | dict[Any, Any]:
|
||||||
|
=======
|
||||||
|
) -> Dict[Any, Any]:
|
||||||
|
>>>>>>> d3d8253b (Refactor parsing module and move JSON fix function to appropriate location)
|
||||||
"""Try to fix the JSON with the AI
|
"""Try to fix the JSON with the AI
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -126,7 +135,7 @@ def try_ai_fix(
|
|||||||
"""
|
"""
|
||||||
if not try_to_fix_with_gpt:
|
if not try_to_fix_with_gpt:
|
||||||
raise exception
|
raise exception
|
||||||
|
if CFG.debug_mode:
|
||||||
logger.warn(
|
logger.warn(
|
||||||
"Warning: Failed to parse AI output, attempting to fix."
|
"Warning: Failed to parse AI output, attempting to fix."
|
||||||
"\n If you see this warning frequently, it's likely that"
|
"\n If you see this warning frequently, it's likely that"
|
||||||
@@ -140,5 +149,39 @@ def try_ai_fix(
|
|||||||
return json.loads(ai_fixed_json)
|
return json.loads(ai_fixed_json)
|
||||||
# This allows the AI to react to the error message,
|
# This allows the AI to react to the error message,
|
||||||
# which usually results in it correcting its ways.
|
# which usually results in it correcting its ways.
|
||||||
logger.error("Failed to fix AI output, telling the AI.")
|
# logger.error("Failed to fix AI output, telling the AI.")
|
||||||
return json_to_load
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def attempt_to_fix_json_by_finding_outermost_brackets(json_string: str):
|
||||||
|
if CFG.speak_mode and CFG.debug_mode:
|
||||||
|
say_text(
|
||||||
|
"I have received an invalid JSON response from the OpenAI API. "
|
||||||
|
"Trying to fix it now."
|
||||||
|
)
|
||||||
|
logger.error("Attempting to fix JSON by finding outermost brackets\n")
|
||||||
|
|
||||||
|
try:
|
||||||
|
json_pattern = regex.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)
|
||||||
|
logger.typewriter_log(
|
||||||
|
title="Apparently json was fixed.", title_color=Fore.GREEN
|
||||||
|
)
|
||||||
|
if CFG.speak_mode and CFG.debug_mode:
|
||||||
|
say_text("Apparently json was fixed.")
|
||||||
|
else:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
except (json.JSONDecodeError, ValueError):
|
||||||
|
if CFG.debug_mode:
|
||||||
|
logger.error(f"Error: Invalid JSON: {json_string}\n")
|
||||||
|
if CFG.speak_mode:
|
||||||
|
say_text("Didn't work. I will have to ignore this response then.")
|
||||||
|
logger.error("Error: Invalid JSON, setting it to empty JSON now.\n")
|
||||||
|
json_string = {}
|
||||||
|
|
||||||
|
return fix_and_parse_json(json_string)
|
||||||
|
|||||||
Reference in New Issue
Block a user