From 9e74df2404a72c1b141bbec8a034eef0383b3238 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Tue, 28 Mar 2023 08:14:09 -0700 Subject: [PATCH] Fix issue#1645: Parse llm_output even there's newline (#2092) (#2099) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix issue#1645: Parse either whitespace or newline after 'Action Input:' in llm_output in mrkl agent. Unittests added accordingly. Co-authored-by: ₿ingnan.ΞTH --- langchain/agents/mrkl/base.py | 3 ++- tests/unit_tests/agents/test_mrkl.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/langchain/agents/mrkl/base.py b/langchain/agents/mrkl/base.py index 64f0ed71..2667936b 100644 --- a/langchain/agents/mrkl/base.py +++ b/langchain/agents/mrkl/base.py @@ -40,7 +40,8 @@ def get_action_and_input(llm_output: str) -> Tuple[str, str]: """ if FINAL_ANSWER_ACTION in llm_output: return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1].strip() - regex = r"Action: (.*?)[\n]*Action Input: (.*)" + # \s matches against tab/newline/whitespace + regex = r"Action: (.*?)[\n]*Action Input:[\s]*(.*)" match = re.search(regex, llm_output, re.DOTALL) if not match: raise ValueError(f"Could not parse LLM output: `{llm_output}`") diff --git a/tests/unit_tests/agents/test_mrkl.py b/tests/unit_tests/agents/test_mrkl.py index 6c87f914..15079238 100644 --- a/tests/unit_tests/agents/test_mrkl.py +++ b/tests/unit_tests/agents/test_mrkl.py @@ -27,6 +27,17 @@ def test_get_action_and_input_whitespace() -> None: assert action_input == "NBA" +def test_get_action_and_input_newline() -> None: + """Test getting an action from text where Action Input is a code snippet.""" + llm_output = ( + "Now I need to write a unittest for the function.\n\n" + "Action: Python\nAction Input:\n```\nimport unittest\n\nunittest.main()\n```" + ) + action, action_input = get_action_and_input(llm_output) + assert action == "Python" + assert action_input == "```\nimport unittest\n\nunittest.main()\n```" + + def test_get_final_answer() -> None: """Test getting final answer.""" llm_output = (