Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving Resilience of MRKL Agent #3269

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions langchain/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,9 @@ def _take_next_step(
color=color,
**tool_run_kwargs,
)
elif agent_action.tool == "Invalid LLM Output":
# Passing the error message regarding the invalid format in llm output
observation = str(agent_action.tool_input)
else:
tool_run_kwargs = self.agent.tool_run_logging_kwargs()
observation = InvalidTool().run(
Expand Down
12 changes: 11 additions & 1 deletion langchain/agents/mrkl/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
match = re.search(regex, text, re.DOTALL)
if not match:
raise OutputParserException(f"Could not parse LLM output: `{text}`")
if not re.search(r"Action\s*\d*\s*:(.*?)", text, re.DOTALL):
error_message = "Invalid Format: Missing 'Action:' after 'Thought:'"
elif not re.search(
r"\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)", text, re.DOTALL
):
error_message = (
"Invalid Format: Missing 'Action Input:' after 'Action:'"
)
else:
raise OutputParserException(f"Could not parse LLM output: `{text}`")
return AgentAction("Invalid LLM Output", error_message, text)
action = match.group(1).strip()
action_input = match.group(2)
return AgentAction(action, action_input.strip(" ").strip('"'), text)
10 changes: 6 additions & 4 deletions tests/unit_tests/agents/test_mrkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,19 @@ def test_get_final_answer_multiline() -> None:
def test_bad_action_input_line() -> None:
"""Test handling when no action input found."""
llm_output = "Thought: I need to search for NBA\n" "Action: Search\n" "Thought: NBA"
with pytest.raises(OutputParserException):
get_action_and_input(llm_output)
action, action_input = get_action_and_input(llm_output)
assert action == "Invalid LLM Output"
assert action_input == "Invalid Format: Missing 'Action Input:' after 'Action:'"


def test_bad_action_line() -> None:
"""Test handling when no action input found."""
llm_output = (
"Thought: I need to search for NBA\n" "Thought: Search\n" "Action Input: NBA"
)
with pytest.raises(OutputParserException):
get_action_and_input(llm_output)
action, action_input = get_action_and_input(llm_output)
assert action == "Invalid LLM Output"
assert action_input == "Invalid Format: Missing 'Action:' after 'Thought:'"


def test_from_chains() -> None:
Expand Down