AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(agent/text_processing): Fix `extract_information` LLM response parsing
Reinier van der Leerbb7f5abc6c91
diff --git a/autogpts/autogpt/autogpt/json_utils/utilities.py b/autogpts/autogpt/autogpt/json_utils/utilities.pyindex 2bc0eb2c87..c81ff2271d 100644--- a/autogpts/autogpt/autogpt/json_utils/utilities.py+++ b/autogpts/autogpt/autogpt/json_utils/utilities.py@@ -29,3 +29,27 @@ def extract_dict_from_response(response_content: str) -> dict[str, Any]:f"non-dict value {repr(result)}")return result+++def extract_list_from_response(response_content: str) -> list[Any]:+ # Sometimes the response includes the JSON in a code block with ```+ pattern = r"```(?:json|JSON)*([\s\S]*?)```"+ match = re.search(pattern, response_content)++ if match:+ response_content = match.group(1).strip()+ else:+ # The string may contain JSON.+ json_pattern = r"\[[\s\S]*\]"+ match = re.search(json_pattern, response_content)++ if match:+ response_content = match.group()++ result = json.loads(response_content)+ if not isinstance(result, list):+ raise ValueError(+ f"Response '''{response_content}''' evaluated to "+ f"non-list value {repr(result)}"+ )+ return resultdiff --git a/autogpts/autogpt/autogpt/processing/text.py b/autogpts/autogpt/autogpt/processing/text.pyindex c69ed7e618..de0e1bf3f7 100644--- a/autogpts/autogpt/autogpt/processing/text.py+++ b/autogpts/autogpt/autogpt/processing/text.py@@ -1,5 +1,4 @@"""Text processing functions"""-import jsonimport loggingimport mathfrom typing import Iterator, Optional, TypeVar@@ -13,6 +12,7 @@ from autogpt.core.resource.model_providers import (ChatModelProvider,ModelTokenizer,)+from autogpt.json_utils.utilities import extract_list_from_responselogger = logging.getLogger(__name__)@@ -161,7 +161,9 @@ async def _process_text(temperature=0.5,max_tokens=max_result_tokens,completion_parser=lambda s: (- json.loads(s.content) if output_type is not str else None+ extract_list_from_response(s.content)+ if output_type is not str+ else None),)