mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
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 <brillliantz@outlook.com>
This commit is contained in:
parent
0bee219cb3
commit
9e74df2404
@ -40,7 +40,8 @@ def get_action_and_input(llm_output: str) -> Tuple[str, str]:
|
|||||||
"""
|
"""
|
||||||
if FINAL_ANSWER_ACTION in llm_output:
|
if FINAL_ANSWER_ACTION in llm_output:
|
||||||
return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1].strip()
|
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)
|
match = re.search(regex, llm_output, re.DOTALL)
|
||||||
if not match:
|
if not match:
|
||||||
raise ValueError(f"Could not parse LLM output: `{llm_output}`")
|
raise ValueError(f"Could not parse LLM output: `{llm_output}`")
|
||||||
|
@ -27,6 +27,17 @@ def test_get_action_and_input_whitespace() -> None:
|
|||||||
assert action_input == "NBA"
|
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:
|
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