Update output_parser.py (#10430)

- Description: Updated output parser for mrkl to remove any
hallucination actions after the final answer; this was encountered when
using Anthropic claude v2 for planning; reopening PR with updated unit
tests
- Issue: #10278 
- Dependencies: N/A
- Twitter handle: @johnreynolds
pull/10653/head
John Reynolds 1 year ago committed by GitHub
parent 790010703b
commit 68901e1e40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,11 +29,20 @@ class MRKLOutputParser(AgentOutputParser):
r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
)
action_match = re.search(regex, text, re.DOTALL)
if action_match:
if includes_answer:
if action_match and includes_answer:
if text.find(FINAL_ANSWER_ACTION) < text.find(action_match.group(0)):
# if final answer is before the hallucination, return final answer
start_index = text.find(FINAL_ANSWER_ACTION) + len(FINAL_ANSWER_ACTION)
end_index = text.find("\n\n", start_index)
return AgentFinish(
{"output": text[start_index:end_index].strip()}, text[:end_index]
)
else:
raise OutputParserException(
f"{FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: {text}"
)
if action_match:
action = action_match.group(1).strip()
action_input = action_match.group(2)
tool_input = action_input.strip(" ")

@ -52,11 +52,25 @@ def test_missing_action_input() -> None:
)
def test_final_answer_and_parsable_action() -> None:
llm_output = """Final Answer: The best pizza to eat is margaritta
I can use the `foo` tool to achieve the goal.
def test_final_answer_before_parsable_action() -> None:
llm_output = """Final Answer: The best pizza to eat is margaritta
Action: foo
Action Input: bar
"""
agent_finish: AgentFinish = mrkl_output_parser.parse(llm_output) # type: ignore
assert (
agent_finish.return_values.get("output")
== "The best pizza to eat is margaritta"
)
def test_final_answer_after_parsable_action() -> None:
llm_output = """
Observation: I can use the `foo` tool to achieve the goal.
Action: foo
Action Input: bar
Final Answer: The best pizza to eat is margaritta
"""
with pytest.raises(OutputParserException) as exception_info:
mrkl_output_parser.parse(llm_output)

Loading…
Cancel
Save