mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-19 03:01:29 +00:00
Raise an exception in MKRL and Chat Output Parsers if parsing text which contains both an action and a final answer (#5609)
Raises exception if OutputParsers receive a response with both a valid action and a final answer Currently, if an OutputParser receives a response which includes both an action and a final answer, they return a FinalAnswer object. This allows the parser to accept responses which propose an action and hallucinate an answer without the action being parsed or taken by the agent. This PR changes the logic to: 1. store a variable checking whether a response contains the `FINAL_ANSWER_ACTION` (this is the easier condition to check). 2. store a variable checking whether the response contains a valid action 3. if both are present, raise a new exception stating that both are present 4. if an action is present, return an AgentAction 5. if an answer is present, return an AgentAnswer 6. if neither is present, raise the relevant exception based around the action format (these have been kept consistent with the prior exception messages) Disclaimer: * Existing mock data included strings which did include an action and an answer. This might indicate that prioritising returning AgentAnswer was always correct, and I am patching out desired behaviour? @hwchase17 to advice. Curious if there are allowed cases where this is not hallucinating, and we do want the LLM to output an action which isn't taken. * I have not passed `send_to_llm` through this new exception Fixes #5601 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 - project lead @vowelparrot
This commit is contained in:
parent
c112d7334d
commit
26ec845921
@ -13,17 +13,24 @@ class ChatOutputParser(AgentOutputParser):
|
|||||||
return FORMAT_INSTRUCTIONS
|
return FORMAT_INSTRUCTIONS
|
||||||
|
|
||||||
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
||||||
if FINAL_ANSWER_ACTION in text:
|
includes_answer = FINAL_ANSWER_ACTION in text
|
||||||
return AgentFinish(
|
|
||||||
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
action = text.split("```")[1]
|
action = text.split("```")[1]
|
||||||
response = json.loads(action.strip())
|
response = json.loads(action.strip())
|
||||||
|
includes_action = "action" in response and "action_input" in response
|
||||||
|
if includes_answer and includes_action:
|
||||||
|
raise OutputParserException(
|
||||||
|
"Parsing LLM output produced a final answer "
|
||||||
|
f"and a parse-able action: {text}"
|
||||||
|
)
|
||||||
return AgentAction(response["action"], response["action_input"], text)
|
return AgentAction(response["action"], response["action_input"], text)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
raise OutputParserException(f"Could not parse LLM output: {text}")
|
if not includes_answer:
|
||||||
|
raise OutputParserException(f"Could not parse LLM output: {text}")
|
||||||
|
return AgentFinish(
|
||||||
|
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _type(self) -> str:
|
def _type(self) -> str:
|
||||||
|
@ -13,44 +13,50 @@ class MRKLOutputParser(AgentOutputParser):
|
|||||||
return FORMAT_INSTRUCTIONS
|
return FORMAT_INSTRUCTIONS
|
||||||
|
|
||||||
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
||||||
if FINAL_ANSWER_ACTION in text:
|
includes_answer = FINAL_ANSWER_ACTION in text
|
||||||
return AgentFinish(
|
|
||||||
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
|
||||||
)
|
|
||||||
# \s matches against tab/newline/whitespace
|
|
||||||
regex = (
|
regex = (
|
||||||
r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
|
r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
|
||||||
)
|
)
|
||||||
match = re.search(regex, text, re.DOTALL)
|
action_match = re.search(regex, text, re.DOTALL)
|
||||||
if not match:
|
if action_match:
|
||||||
if not re.search(r"Action\s*\d*\s*:[\s]*(.*?)", text, re.DOTALL):
|
if includes_answer:
|
||||||
raise OutputParserException(
|
raise OutputParserException(
|
||||||
f"Could not parse LLM output: `{text}`",
|
"Parsing LLM output produced both a final answer "
|
||||||
observation="Invalid Format: Missing 'Action:' after 'Thought:'",
|
f"and a parse-able action: {text}"
|
||||||
llm_output=text,
|
|
||||||
send_to_llm=True,
|
|
||||||
)
|
)
|
||||||
elif not re.search(
|
action = action_match.group(1).strip()
|
||||||
r"[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)", text, re.DOTALL
|
action_input = action_match.group(2)
|
||||||
):
|
tool_input = action_input.strip(" ")
|
||||||
raise OutputParserException(
|
# ensure if its a well formed SQL query we don't remove any trailing " chars
|
||||||
f"Could not parse LLM output: `{text}`",
|
if tool_input.startswith("SELECT ") is False:
|
||||||
observation="Invalid Format:"
|
tool_input = tool_input.strip('"')
|
||||||
" Missing 'Action Input:' after 'Action:'",
|
|
||||||
llm_output=text,
|
|
||||||
send_to_llm=True,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise OutputParserException(f"Could not parse LLM output: `{text}`")
|
|
||||||
action = match.group(1).strip()
|
|
||||||
action_input = match.group(2)
|
|
||||||
|
|
||||||
tool_input = action_input.strip(" ")
|
return AgentAction(action, tool_input, text)
|
||||||
# ensure if its a well formed SQL query we don't remove any trailing " chars
|
|
||||||
if tool_input.startswith("SELECT ") is False:
|
|
||||||
tool_input = tool_input.strip('"')
|
|
||||||
|
|
||||||
return AgentAction(action, tool_input, text)
|
elif includes_answer:
|
||||||
|
return AgentFinish(
|
||||||
|
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
||||||
|
)
|
||||||
|
|
||||||
|
if not re.search(r"Action\s*\d*\s*:[\s]*(.*?)", text, re.DOTALL):
|
||||||
|
raise OutputParserException(
|
||||||
|
f"Could not parse LLM output: `{text}`",
|
||||||
|
observation="Invalid Format: Missing 'Action:' after 'Thought:'",
|
||||||
|
llm_output=text,
|
||||||
|
send_to_llm=True,
|
||||||
|
)
|
||||||
|
elif not re.search(
|
||||||
|
r"[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)", text, re.DOTALL
|
||||||
|
):
|
||||||
|
raise OutputParserException(
|
||||||
|
f"Could not parse LLM output: `{text}`",
|
||||||
|
observation="Invalid Format:"
|
||||||
|
" Missing 'Action Input:' after 'Action:'",
|
||||||
|
llm_output=text,
|
||||||
|
send_to_llm=True,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise OutputParserException(f"Could not parse LLM output: `{text}`")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _type(self) -> str:
|
def _type(self) -> str:
|
||||||
|
@ -90,14 +90,7 @@ def test_get_action_and_input_sql_query() -> None:
|
|||||||
|
|
||||||
def test_get_final_answer() -> None:
|
def test_get_final_answer() -> None:
|
||||||
"""Test getting final answer."""
|
"""Test getting final answer."""
|
||||||
llm_output = (
|
llm_output = "Thought: I can now answer the question\n" "Final Answer: 1994"
|
||||||
"Thought: I need to search for NBA\n"
|
|
||||||
"Action: Search\n"
|
|
||||||
"Action Input: NBA\n"
|
|
||||||
"Observation: founded in 1994\n"
|
|
||||||
"Thought: I can now answer the question\n"
|
|
||||||
"Final Answer: 1994"
|
|
||||||
)
|
|
||||||
action, action_input = get_action_and_input(llm_output)
|
action, action_input = get_action_and_input(llm_output)
|
||||||
assert action == "Final Answer"
|
assert action == "Final Answer"
|
||||||
assert action_input == "1994"
|
assert action_input == "1994"
|
||||||
@ -105,14 +98,7 @@ def test_get_final_answer() -> None:
|
|||||||
|
|
||||||
def test_get_final_answer_new_line() -> None:
|
def test_get_final_answer_new_line() -> None:
|
||||||
"""Test getting final answer."""
|
"""Test getting final answer."""
|
||||||
llm_output = (
|
llm_output = "Thought: I can now answer the question\n" "Final Answer:\n1994"
|
||||||
"Thought: I need to search for NBA\n"
|
|
||||||
"Action: Search\n"
|
|
||||||
"Action Input: NBA\n"
|
|
||||||
"Observation: founded in 1994\n"
|
|
||||||
"Thought: I can now answer the question\n"
|
|
||||||
"Final Answer:\n1994"
|
|
||||||
)
|
|
||||||
action, action_input = get_action_and_input(llm_output)
|
action, action_input = get_action_and_input(llm_output)
|
||||||
assert action == "Final Answer"
|
assert action == "Final Answer"
|
||||||
assert action_input == "1994"
|
assert action_input == "1994"
|
||||||
@ -120,14 +106,7 @@ def test_get_final_answer_new_line() -> None:
|
|||||||
|
|
||||||
def test_get_final_answer_multiline() -> None:
|
def test_get_final_answer_multiline() -> None:
|
||||||
"""Test getting final answer that is multiline."""
|
"""Test getting final answer that is multiline."""
|
||||||
llm_output = (
|
llm_output = "Thought: I can now answer the question\n" "Final Answer: 1994\n1993"
|
||||||
"Thought: I need to search for NBA\n"
|
|
||||||
"Action: Search\n"
|
|
||||||
"Action Input: NBA\n"
|
|
||||||
"Observation: founded in 1994 and 1993\n"
|
|
||||||
"Thought: I can now answer the question\n"
|
|
||||||
"Final Answer: 1994\n1993"
|
|
||||||
)
|
|
||||||
action, action_input = get_action_and_input(llm_output)
|
action, action_input = get_action_and_input(llm_output)
|
||||||
assert action == "Final Answer"
|
assert action == "Final Answer"
|
||||||
assert action_input == "1994\n1993"
|
assert action_input == "1994\n1993"
|
||||||
@ -151,6 +130,20 @@ def test_bad_action_line() -> None:
|
|||||||
assert e_info.value.observation is not None
|
assert e_info.value.observation is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_valid_action_and_answer_raises_exception() -> None:
|
||||||
|
"""Test handling when both an action and answer are found."""
|
||||||
|
llm_output = (
|
||||||
|
"Thought: I need to search for NBA\n"
|
||||||
|
"Action: Search\n"
|
||||||
|
"Action Input: NBA\n"
|
||||||
|
"Observation: founded in 1994\n"
|
||||||
|
"Thought: I can now answer the question\n"
|
||||||
|
"Final Answer: 1994"
|
||||||
|
)
|
||||||
|
with pytest.raises(OutputParserException):
|
||||||
|
get_action_and_input(llm_output)
|
||||||
|
|
||||||
|
|
||||||
def test_from_chains() -> None:
|
def test_from_chains() -> None:
|
||||||
"""Test initializing from chains."""
|
"""Test initializing from chains."""
|
||||||
chain_configs = [
|
chain_configs = [
|
||||||
|
Loading…
Reference in New Issue
Block a user