From faf5f9e5a4de3f8b0c17b9c725465a8a44f298b1 Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Fri, 19 Jan 2024 15:49:32 +0100 Subject: fix(agent): Fix `extract_dict_from_response` flakiness - The `extract_dict_from_response` function, which is supposed to reliably extract a JSON object from an LLM's response, positively discriminated objects defined on a single line, causing issues. --- .../autogpt/autogpt/agents/prompt_strategies/one_shot.py | 12 ++++++++++++ autogpts/autogpt/autogpt/json_utils/utilities.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py b/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py index 6fe0f4894..e8c726c18 100644 --- a/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py +++ b/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py @@ -391,7 +391,19 @@ class OneShotAgentPromptStrategy(PromptStrategy): if not response.content: raise InvalidAgentResponseError("Assistant response has no text content") + self.logger.debug( + "LLM response content:" + + ( + f"\n{response.content}" + if "\n" in response.content + else f" '{response.content}'" + ) + ) assistant_reply_dict = extract_dict_from_response(response.content) + self.logger.debug( + "Validating object extracted from LLM response:\n" + f"{json.dumps(assistant_reply_dict, indent=4)}" + ) _, errors = self.response_schema.validate_object( object=assistant_reply_dict, diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.py index fe203b290..e5445dcb8 100644 --- a/autogpts/autogpt/autogpt/json_utils/utilities.py +++ b/autogpts/autogpt/autogpt/json_utils/utilities.py @@ -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"{[\s\S]*}" match = re.search(json_pattern, response_content) if match: -- cgit v1.2.3