diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.py index 028eb67c..6f968397 100644 --- a/autogpts/autogpt/autogpt/json_utils/utilities.py +++ b/autogpts/autogpt/autogpt/json_utils/utilities.py @@ -1,7 +1,7 @@ """Utilities for the json_fixes package.""" -import re import ast import logging +import re from typing import Any logger = logging.getLogger(__name__) @@ -9,7 +9,7 @@ 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 ``` - pattern = r'```([\s\S]*?)```' + pattern = r"```([\s\S]*?)```" match = re.search(pattern, response_content) if match: @@ -18,7 +18,7 @@ def extract_dict_from_response(response_content: str) -> dict[str, Any]: response_content = response_content.lstrip("json") else: # The string may contain JSON. - json_pattern = r'{.*}' + json_pattern = r"{.*}" match = re.search(json_pattern, response_content) if match: diff --git a/autogpts/autogpt/tests/unit/test_utils.py b/autogpts/autogpt/tests/unit/test_utils.py index f56e2b97..9a783ce4 100644 --- a/autogpts/autogpt/tests/unit/test_utils.py +++ b/autogpts/autogpt/tests/unit/test_utils.py @@ -194,14 +194,18 @@ def test_extract_json_from_response_wrapped_in_code_block(valid_json_response: d extract_dict_from_response(emulated_response_from_openai) == valid_json_response ) -def test_extract_json_from_response_wrapped_in_code_block_with_language(valid_json_response: dict): + +def test_extract_json_from_response_wrapped_in_code_block_with_language( + valid_json_response: dict, +): emulated_response_from_openai = "```json" + str(valid_json_response) + "```" assert ( extract_dict_from_response(emulated_response_from_openai) == valid_json_response ) + def test_extract_json_from_response_json_contained_in_string(valid_json_response: dict): emulated_response_from_openai = "sentence1" + str(valid_json_response) + "sentence2" assert ( extract_dict_from_response(emulated_response_from_openai) == valid_json_response - ) \ No newline at end of file + )