Skip to content

Commit

Permalink
simplify parsing of the final answer (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwchase17 authored Jan 16, 2023
1 parent 2a54e73 commit 1ac3319
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions langchain/agents/mrkl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from langchain.llms.base import BaseLLM
from langchain.prompts import PromptTemplate

FINAL_ANSWER_ACTION = "Final Answer: "
FINAL_ANSWER_ACTION = "Final Answer:"


class ChainConfig(NamedTuple):
Expand All @@ -30,7 +30,7 @@ class ChainConfig(NamedTuple):
def get_action_and_input(llm_output: str) -> Tuple[str, str]:
"""Parse out the action and input from the 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: (.*)"
match = re.search(regex, llm_output)
if not match:
Expand Down
15 changes: 15 additions & 0 deletions tests/unit_tests/agents/test_mrkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ def test_get_final_answer() -> None:
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:
"""Test getting final answer that is multiline."""
llm_output = (
Expand Down

0 comments on commit 1ac3319

Please sign in to comment.