Skip to content

Commit 1ac3319

Browse files
authored
simplify parsing of the final answer (#621)
1 parent 2a54e73 commit 1ac3319

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

langchain/agents/mrkl/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from langchain.llms.base import BaseLLM
1111
from langchain.prompts import PromptTemplate
1212

13-
FINAL_ANSWER_ACTION = "Final Answer: "
13+
FINAL_ANSWER_ACTION = "Final Answer:"
1414

1515

1616
class ChainConfig(NamedTuple):
@@ -30,7 +30,7 @@ class ChainConfig(NamedTuple):
3030
def get_action_and_input(llm_output: str) -> Tuple[str, str]:
3131
"""Parse out the action and input from the LLM output."""
3232
if FINAL_ANSWER_ACTION in llm_output:
33-
return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1]
33+
return "Final Answer", llm_output.split(FINAL_ANSWER_ACTION)[-1].strip()
3434
regex = r"Action: (.*?)\nAction Input: (.*)"
3535
match = re.search(regex, llm_output)
3636
if not match:

tests/unit_tests/agents/test_mrkl.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def test_get_final_answer() -> None:
3434
assert action_input == "1994"
3535

3636

37+
def test_get_final_answer_new_line() -> None:
38+
"""Test getting final answer."""
39+
llm_output = (
40+
"Thought: I need to search for NBA\n"
41+
"Action: Search\n"
42+
"Action Input: NBA\n"
43+
"Observation: founded in 1994\n"
44+
"Thought: I can now answer the question\n"
45+
"Final Answer:\n1994"
46+
)
47+
action, action_input = get_action_and_input(llm_output)
48+
assert action == "Final Answer"
49+
assert action_input == "1994"
50+
51+
3752
def test_get_final_answer_multiline() -> None:
3853
"""Test getting final answer that is multiline."""
3954
llm_output = (

0 commit comments

Comments
 (0)