simplify parsing of the final answer (#621)

harrison/sql-agent
Harrison Chase 1 year ago committed by GitHub
parent 2a54e73fec
commit 1ac3319e45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,7 +10,7 @@ from langchain.agents.tools import Tool
from langchain.llms.base import BaseLLM
from langchain.prompts import PromptTemplate
FINAL_ANSWER_ACTION = "Final Answer: "
FINAL_ANSWER_ACTION = "Final Answer:"
class ChainConfig(NamedTuple):
@ -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:

@ -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 = (

Loading…
Cancel
Save