Skip to content

Commit

Permalink
fix: Improve parsing of JSON in LLM responses
Browse files Browse the repository at this point in the history
- Ignored any content preceding the first opening brace in the response
- Updated `utilities.py` file in the `autogpt` package
  • Loading branch information
Pwuts committed Nov 16, 2023
1 parent 58292d5 commit 0b3aea9
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion autogpts/autogpt/autogpt/json_utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0b3aea9

Please sign in to comment.