From 775a27eea0f92482be6853ec841d811b1feec38d Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Tue, 5 Dec 2023 15:21:04 +0100 Subject: [PATCH] refactor: Modify instruction in prompt for increased JSON output reliability - Update the instruction in the prompt strategy to ensure the response is pure JSON. - Remove unnecessary text and make the instruction clearer. - Also update the error logging to include the received JSON content. This commit refactors the code in the `one_shot.py` file and the `utilities.py` file. --- .../autogpt/autogpt/agents/prompt_strategies/one_shot.py | 7 ++----- autogpts/autogpt/autogpt/json_utils/utilities.py | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py b/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py index 3680ee64a..8b8a69706 100644 --- a/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py +++ b/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py @@ -315,12 +315,9 @@ class OneShotAgentPromptStrategy(PromptStrategy): ) instruction = ( - ( - "Respond strictly with a JSON object containing your thoughts, " - "and a tool_call specifying the next command to use." - ) + "Respond with pure JSON containing your thoughts, " "and invoke a tool." if use_functions_api - else "Respond strictly with a JSON object." + else "Respond with pure JSON." ) return ( diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.py index b967a41fc..80ef8cee3 100644 --- a/autogpts/autogpt/autogpt/json_utils/utilities.py +++ b/autogpts/autogpt/autogpt/json_utils/utilities.py @@ -30,6 +30,6 @@ def extract_dict_from_response(response_content: str) -> dict[str, Any]: 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}") + logger.debug(f"Invalid JSON received in response:\n{response_content}") # TODO: How to raise an error here without causing the program to exit? return {}