mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
Fix for: [Changed regex to cover new line before action serious.](https://github.com/hwchase17/langchain/issues/3365) --- This PR fixes the issue where `ValueError: Could not parse LLM output:` was thrown on seems to be valid input. Changed regex to cover new lines before action serious (after the keywords "Action:" and "Action Input:"). regex101: https://regex101.com/r/CXl1kB/1 --------- Co-authored-by: msarskus <msarskus@cisco.com>
This commit is contained in:
parent
696f840426
commit
a4d85f7fd5
@ -18,7 +18,9 @@ class MRKLOutputParser(AgentOutputParser):
|
|||||||
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
||||||
)
|
)
|
||||||
# \s matches against tab/newline/whitespace
|
# \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)
|
match = re.search(regex, text, re.DOTALL)
|
||||||
if not match:
|
if not match:
|
||||||
raise OutputParserException(f"Could not parse LLM output: `{text}`")
|
raise OutputParserException(f"Could not parse LLM output: `{text}`")
|
||||||
|
@ -50,6 +50,27 @@ def test_get_action_and_input_newline() -> None:
|
|||||||
assert action_input == "```\nimport unittest\n\nunittest.main()\n```"
|
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:
|
def test_get_final_answer() -> None:
|
||||||
"""Test getting final answer."""
|
"""Test getting final answer."""
|
||||||
llm_output = (
|
llm_output = (
|
||||||
|
Loading…
Reference in New Issue
Block a user