Skip to content

The agent engineering platform.

[Fix #3365]: Changed regex to cover new line before action serious (#3367)

Mindaugas Sharskusa4d85f7fd5d9
diff --git a/langchain/agents/mrkl/output_parser.py b/langchain/agents/mrkl/output_parser.py
index 0309609bb9..6f809eb5af 100644
--- a/langchain/agents/mrkl/output_parser.py
+++ b/langchain/agents/mrkl/output_parser.py
@@ -18,7 +18,9 @@ class MRKLOutputParser(AgentOutputParser):
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
)
# \s matches against tab/newline/whitespace
- regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
+ regex = (
+ r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\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}`")
diff --git a/tests/unit_tests/agents/test_mrkl.py b/tests/unit_tests/agents/test_mrkl.py
index 68c71b300e..d7d60b9dfd 100644
--- a/tests/unit_tests/agents/test_mrkl.py
+++ b/tests/unit_tests/agents/test_mrkl.py
@@ -50,6 +50,27 @@ def test_get_action_and_input_newline() -> None:
assert action_input == "```\nimport unittest\n\nunittest.main()\n```"
+def test_get_action_and_input_newline_after_keyword() -> None:
+ """Test getting an action and action input from the text
+ when there is a new line before the action
+ (after the keywords "Action:" and "Action Input:")
+ """
+ llm_output = """
+ I can use the `ls` command to list the contents of the directory \
+ and `grep` to search for the specific file.
+
+ Action:
+ Terminal
+
+ Action Input:
+ ls -l ~/.bashrc.d/
+ """
+
+ action, action_input = get_action_and_input(llm_output)
+ assert action == "Terminal"
+ assert action_input == "ls -l ~/.bashrc.d/\n"
+
+
def test_get_final_answer() -> None:
"""Test getting final answer."""
llm_output = (