mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2026-01-31 11:54:30 +01:00
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.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user