Skip to content
Snippets Groups Projects
Unverified Commit ece8f2dd authored by Bryce Freshcorn's avatar Bryce Freshcorn Committed by GitHub
Browse files

ReAct output parser changes (#13459)

parent 07a0c666
No related branches found
No related tags found
No related merge requests found
......@@ -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:
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment