fix: Improve parsing of JSON in LLM responses

- Ignored any content preceding the first opening brace in the response
- Updated `utilities.py` file in the `autogpt` package
This commit is contained in:
Reinier van der Leer
2023-11-16 14:33:09 +01:00
parent 58292d59bc
commit 0b3aea92d1

View File

@@ -10,7 +10,10 @@ 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])
response_content = "```".join(response_content.split("```")[1:-1]).strip()
if (ob_pos := response_content.index("{")) > 0:
response_content = response_content[ob_pos:]
# response content comes from OpenAI as a Python `str(content_dict)`, literal_eval reverses this
try: