mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-01 02:43:37 +00:00
better parsing of agent output (#418)
This commit is contained in:
parent
0d7aa1ee99
commit
ee3b8e89b3
@ -1,6 +1,7 @@
|
|||||||
"""Attempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf."""
|
"""Attempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
from typing import Any, Callable, List, NamedTuple, Optional, Tuple
|
from typing import Any, Callable, List, NamedTuple, Optional, Tuple
|
||||||
|
|
||||||
from langchain.agents.agent import Agent, AgentExecutor
|
from langchain.agents.agent import Agent, AgentExecutor
|
||||||
@ -28,22 +29,14 @@ class ChainConfig(NamedTuple):
|
|||||||
|
|
||||||
def get_action_and_input(llm_output: str) -> Tuple[str, str]:
|
def get_action_and_input(llm_output: str) -> Tuple[str, str]:
|
||||||
"""Parse out the action and input from the LLM output."""
|
"""Parse out the action and input from the LLM output."""
|
||||||
ps = [p for p in llm_output.split("\n") if p]
|
if FINAL_ANSWER_ACTION in llm_output:
|
||||||
if ps[-1].startswith("Final Answer"):
|
return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1]
|
||||||
directive = ps[-1][len(FINAL_ANSWER_ACTION) :]
|
regex = r"Action: (.*?)\nAction Input: (.*)"
|
||||||
return "Final Answer", directive
|
match = re.search(regex, llm_output)
|
||||||
if not ps[-1].startswith("Action Input: "):
|
if not match:
|
||||||
raise ValueError(
|
raise ValueError(f"Could not parse LLM output: `{llm_output}`")
|
||||||
"The last line does not have an action input, "
|
action = match.group(1)
|
||||||
"something has gone terribly wrong."
|
action_input = match.group(2)
|
||||||
)
|
|
||||||
if not ps[-2].startswith("Action: "):
|
|
||||||
raise ValueError(
|
|
||||||
"The second to last line does not have an action, "
|
|
||||||
"something has gone terribly wrong."
|
|
||||||
)
|
|
||||||
action = ps[-2][len("Action: ") :]
|
|
||||||
action_input = ps[-1][len("Action Input: ") :]
|
|
||||||
return action, action_input.strip(" ").strip('"')
|
return action, action_input.strip(" ").strip('"')
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,6 +34,21 @@ def test_get_final_answer() -> None:
|
|||||||
assert action_input == "1994"
|
assert action_input == "1994"
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_final_answer_multiline() -> None:
|
||||||
|
"""Test getting final answer that is multiline."""
|
||||||
|
llm_output = (
|
||||||
|
"Thought: I need to search for NBA\n"
|
||||||
|
"Action: Search\n"
|
||||||
|
"Action Input: NBA\n"
|
||||||
|
"Observation: founded in 1994 and 1993\n"
|
||||||
|
"Thought: I can now answer the question\n"
|
||||||
|
"Final Answer: 1994\n1993"
|
||||||
|
)
|
||||||
|
action, action_input = get_action_and_input(llm_output)
|
||||||
|
assert action == "Final Answer"
|
||||||
|
assert action_input == "1994\n1993"
|
||||||
|
|
||||||
|
|
||||||
def test_bad_action_input_line() -> None:
|
def test_bad_action_input_line() -> None:
|
||||||
"""Test handling when no action input found."""
|
"""Test handling when no action input found."""
|
||||||
llm_output = "Thought: I need to search for NBA\n" "Action: Search\n" "Thought: NBA"
|
llm_output = "Thought: I need to search for NBA\n" "Action: Search\n" "Thought: NBA"
|
||||||
|
Loading…
Reference in New Issue
Block a user