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.
pull/6497/head
Reinier van der Leer 2023-12-05 15:21:04 +01:00
parent b8b792e844
commit 775a27eea0
No known key found for this signature in database
GPG Key ID: CDC1180FDAE06193
2 changed files with 3 additions and 6 deletions

View File

@ -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 (

View File

@ -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 {}