From ece8f2ddc4669b5cc820261329bf68bf99999e48 Mon Sep 17 00:00:00 2001 From: Bryce Freshcorn <26725654+brycecf@users.noreply.github.com> Date: Wed, 15 May 2024 20:03:27 -0400 Subject: [PATCH] ReAct output parser changes (#13459) --- .../core/agent/react/output_parser.py | 9 +++++---- .../agent/react/test_react_output_parser.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/llama-index-core/llama_index/core/agent/react/output_parser.py b/llama-index-core/llama_index/core/agent/react/output_parser.py index 93a241daa4..d013fb66cc 100644 --- a/llama-index-core/llama_index/core/agent/react/output_parser.py +++ b/llama-index-core/llama_index/core/agent/react/output_parser.py @@ -15,7 +15,7 @@ from llama_index.core.types import BaseOutputParser def extract_tool_use(input_text: str) -> Tuple[str, str, str]: pattern = ( - r"\s*Thought: (.*?)\nAction: ([a-zA-Z0-9_]+).*?\nAction Input: .*?(\{.*\})" + r"\s*Thought: (.*?)\n+Action: ([a-zA-Z0-9_]+).*?\n+Action Input: .*?(\{.*\})" ) match = re.search(pattern, input_text, re.DOTALL) @@ -97,15 +97,16 @@ class ReActOutputParser(BaseOutputParser): is_streaming=is_streaming, ) + # An "Action" should take priority over an "Answer" + if "Action:" in output: + return parse_action_reasoning_step(output) + if "Answer:" in output: thought, answer = extract_final_response(output) return ResponseReasoningStep( thought=thought, response=answer, is_streaming=is_streaming ) - if "Action:" in output: - return parse_action_reasoning_step(output) - raise ValueError(f"Could not parse output: {output}") def format(self, output: str) -> str: diff --git a/llama-index-core/tests/agent/react/test_react_output_parser.py b/llama-index-core/tests/agent/react/test_react_output_parser.py index 4ee41d91ea..1e7525fb02 100644 --- a/llama-index-core/tests/agent/react/test_react_output_parser.py +++ b/llama-index-core/tests/agent/react/test_react_output_parser.py @@ -22,6 +22,22 @@ def test_extract_tool_use() -> None: mock_input_text = """\ Thought: I need to use a tool to help me answer the question. Action: add +Action Input: {"a": 1, "b": 1} +""" + thought, action, action_input = extract_tool_use(mock_input_text) + assert thought == "I need to use a tool to help me answer the question." + assert action == "add" + assert action_input == '{"a": 1, "b": 1}' + + +def test_extract_tool_use_multiline() -> None: + mock_input_text = """\ +Thought: I need to use a tool to help me answer the question. + +Action: add + + + Action Input: {"a": 1, "b": 1} """ thought, action, action_input = extract_tool_use(mock_input_text) -- GitLab