diff options
Diffstat (limited to 'autogpts/autogpt/autogpt/json_utils/utilities.py')
-rw-r--r-- | autogpts/autogpt/autogpt/json_utils/utilities.py | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.py index 876106ade..0521372e0 100644 --- a/autogpts/autogpt/autogpt/json_utils/utilities.py +++ b/autogpts/autogpt/autogpt/json_utils/utilities.py @@ -1,6 +1,7 @@ """Utilities for the json_fixes package.""" import ast import logging +import re from typing import Any logger = logging.getLogger(__name__) @@ -8,15 +9,25 @@ logger = logging.getLogger(__name__) def extract_dict_from_response(response_content: str) -> dict[str, Any]: # Sometimes the response includes the JSON in a code block with ``` - if response_content.startswith("```") and response_content.endswith("```"): - # Discard the first and last ```, then re-join in case the response naturally included ``` - response_content = "```".join(response_content.split("```")[1:-1]) + pattern = r"```(?:json|JSON)*([\s\S]*?)```" + match = re.search(pattern, response_content) - # response content comes from OpenAI as a Python `str(content_dict)`, literal_eval reverses this - try: - return ast.literal_eval(response_content) - except BaseException as e: - logger.info(f"Error parsing JSON response with literal_eval {e}") - logger.debug(f"Invalid JSON received in response: {response_content}") - # TODO: How to raise an error here without causing the program to exit? - return {} + if match: + response_content = match.group(1).strip() + else: + # The string may contain JSON. + json_pattern = r"{[\s\S]*}" + match = re.search(json_pattern, response_content) + + if match: + response_content = match.group() + + # Response content comes from OpenAI as a Python `str(content_dict)`. + # `literal_eval` does the reverse of `str(dict)`. + result = ast.literal_eval(response_content) + if not isinstance(result, dict): + raise ValueError( + f"Response '''{response_content}''' evaluated to " + f"non-dict value {repr(result)}" + ) + return result |