mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-05 12:48:12 +00:00
simplify parsing of the final answer (#621)
This commit is contained in:
parent
2a54e73fec
commit
1ac3319e45
@ -30,7 +30,7 @@ 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."""
|
||||||
if FINAL_ANSWER_ACTION in llm_output:
|
if FINAL_ANSWER_ACTION in llm_output:
|
||||||
return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1]
|
return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1].strip()
|
||||||
regex = r"Action: (.*?)\nAction Input: (.*)"
|
regex = r"Action: (.*?)\nAction Input: (.*)"
|
||||||
match = re.search(regex, llm_output)
|
match = re.search(regex, llm_output)
|
||||||
if not match:
|
if not match:
|
||||||
|
@ -34,6 +34,21 @@ def test_get_final_answer() -> None:
|
|||||||
assert action_input == "1994"
|
assert action_input == "1994"
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_final_answer_new_line() -> None:
|
||||||
|
"""Test getting final answer."""
|
||||||
|
llm_output = (
|
||||||
|
"Thought: I need to search for NBA\n"
|
||||||
|
"Action: Search\n"
|
||||||
|
"Action Input: NBA\n"
|
||||||
|
"Observation: founded in 1994\n"
|
||||||
|
"Thought: I can now answer the question\n"
|
||||||
|
"Final Answer:\n1994"
|
||||||
|
)
|
||||||
|
action, action_input = get_action_and_input(llm_output)
|
||||||
|
assert action == "Final Answer"
|
||||||
|
assert action_input == "1994"
|
||||||
|
|
||||||
|
|
||||||
def test_get_final_answer_multiline() -> None:
|
def test_get_final_answer_multiline() -> None:
|
||||||
"""Test getting final answer that is multiline."""
|
"""Test getting final answer that is multiline."""
|
||||||
llm_output = (
|
llm_output = (
|
||||||
|
Loading…
Reference in New Issue
Block a user