aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Reinier van der Leer <pwuts@agpt.co> 2024-01-19 15:49:32 +0100
committerGravatar Reinier van der Leer <pwuts@agpt.co> 2024-01-19 15:49:32 +0100
commitfaf5f9e5a4de3f8b0c17b9c725465a8a44f298b1 (patch)
tree064b72b4cc2c8bd2afdaf347bb17d60cb80de32f
parentfix(agent): Fix "ChatModelResponse not subscriptable" errors in `summarize_te... (diff)
downloadAuto-GPT-faf5f9e5a4de3f8b0c17b9c725465a8a44f298b1.tar.gz
Auto-GPT-faf5f9e5a4de3f8b0c17b9c725465a8a44f298b1.tar.bz2
Auto-GPT-faf5f9e5a4de3f8b0c17b9c725465a8a44f298b1.zip
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.
-rw-r--r--autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py12
-rw-r--r--autogpts/autogpt/autogpt/json_utils/utilities.py2
2 files changed, 13 insertions, 1 deletions
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: