langchain/tests/unit_tests/agents/test_mrkl.py

167 lines
5.7 KiB
Python
Raw Normal View History

2022-11-05 21:41:53 +00:00
"""Test MRKL functionality."""
2023-04-16 20:15:21 +00:00
from typing import Tuple
2022-11-05 21:41:53 +00:00
import pytest
2023-04-16 20:15:21 +00:00
from langchain.agents.mrkl.base import ZeroShotAgent
from langchain.agents.mrkl.output_parser import MRKLOutputParser
2022-11-26 14:03:08 +00:00
from langchain.agents.mrkl.prompt import FORMAT_INSTRUCTIONS, PREFIX, SUFFIX
2022-11-22 14:16:26 +00:00
from langchain.agents.tools import Tool
2022-11-20 04:32:45 +00:00
from langchain.prompts import PromptTemplate
from langchain.schema import AgentAction, OutputParserException
2022-11-05 21:41:53 +00:00
from tests.unit_tests.llms.fake_llm import FakeLLM
2023-04-16 20:15:21 +00:00
def get_action_and_input(text: str) -> Tuple[str, str]:
output = MRKLOutputParser().parse(text)
if isinstance(output, AgentAction):
return output.tool, str(output.tool_input)
2023-04-16 20:15:21 +00:00
else:
return "Final Answer", output.return_values["output"]
2022-11-05 21:41:53 +00:00
def test_get_action_and_input() -> None:
"""Test getting an action from text."""
llm_output = (
"Thought: I need to search for NBA\n" "Action: Search\n" "Action Input: NBA"
)
action, action_input = get_action_and_input(llm_output)
assert action == "Search"
assert action_input == "NBA"
2023-01-22 00:03:48 +00:00
def test_get_action_and_input_whitespace() -> None:
"""Test getting an action from text."""
llm_output = "Thought: I need to search for NBA\nAction: Search \nAction Input: NBA"
action, action_input = get_action_and_input(llm_output)
assert action == "Search"
assert action_input == "NBA"
def test_get_action_and_input_newline() -> None:
"""Test getting an action from text where Action Input is a code snippet."""
llm_output = (
"Now I need to write a unittest for the function.\n\n"
"Action: Python\nAction Input:\n```\nimport unittest\n\nunittest.main()\n```"
)
action, action_input = get_action_and_input(llm_output)
assert action == "Python"
assert action_input == "```\nimport unittest\n\nunittest.main()\n```"
def test_get_action_and_input_newline_after_keyword() -> None:
"""Test getting an action and action input from the text
when there is a new line before the action
(after the keywords "Action:" and "Action Input:")
"""
llm_output = """
I can use the `ls` command to list the contents of the directory \
and `grep` to search for the specific file.
Action:
Terminal
Action Input:
ls -l ~/.bashrc.d/
"""
action, action_input = get_action_and_input(llm_output)
assert action == "Terminal"
assert action_input == "ls -l ~/.bashrc.d/\n"
def test_get_action_and_input_sql_query() -> None:
"""Test getting the action and action input from the text
when the LLM output is a well formed SQL query
"""
llm_output = """
I should query for the largest single shift payment for every unique user.
Action: query_sql_db
Action Input: \
SELECT "UserName", MAX(totalpayment) FROM user_shifts GROUP BY "UserName" """
action, action_input = get_action_and_input(llm_output)
assert action == "query_sql_db"
assert (
action_input
== 'SELECT "UserName", MAX(totalpayment) FROM user_shifts GROUP BY "UserName"'
)
2022-11-05 21:41:53 +00:00
def test_get_final_answer() -> None:
"""Test getting final answer."""
Raise an exception in MKRL and Chat Output Parsers if parsing text which contains both an action and a final answer (#5609) Raises exception if OutputParsers receive a response with both a valid action and a final answer Currently, if an OutputParser receives a response which includes both an action and a final answer, they return a FinalAnswer object. This allows the parser to accept responses which propose an action and hallucinate an answer without the action being parsed or taken by the agent. This PR changes the logic to: 1. store a variable checking whether a response contains the `FINAL_ANSWER_ACTION` (this is the easier condition to check). 2. store a variable checking whether the response contains a valid action 3. if both are present, raise a new exception stating that both are present 4. if an action is present, return an AgentAction 5. if an answer is present, return an AgentAnswer 6. if neither is present, raise the relevant exception based around the action format (these have been kept consistent with the prior exception messages) Disclaimer: * Existing mock data included strings which did include an action and an answer. This might indicate that prioritising returning AgentAnswer was always correct, and I am patching out desired behaviour? @hwchase17 to advice. Curious if there are allowed cases where this is not hallucinating, and we do want the LLM to output an action which isn't taken. * I have not passed `send_to_llm` through this new exception Fixes #5601 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 - project lead @vowelparrot
2023-06-04 21:40:49 +00:00
llm_output = "Thought: I can now answer the question\n" "Final Answer: 1994"
2022-11-05 21:41:53 +00:00
action, action_input = get_action_and_input(llm_output)
2022-11-22 14:16:26 +00:00
assert action == "Final Answer"
2022-11-05 21:41:53 +00:00
assert action_input == "1994"
def test_get_final_answer_new_line() -> None:
"""Test getting final answer."""
Raise an exception in MKRL and Chat Output Parsers if parsing text which contains both an action and a final answer (#5609) Raises exception if OutputParsers receive a response with both a valid action and a final answer Currently, if an OutputParser receives a response which includes both an action and a final answer, they return a FinalAnswer object. This allows the parser to accept responses which propose an action and hallucinate an answer without the action being parsed or taken by the agent. This PR changes the logic to: 1. store a variable checking whether a response contains the `FINAL_ANSWER_ACTION` (this is the easier condition to check). 2. store a variable checking whether the response contains a valid action 3. if both are present, raise a new exception stating that both are present 4. if an action is present, return an AgentAction 5. if an answer is present, return an AgentAnswer 6. if neither is present, raise the relevant exception based around the action format (these have been kept consistent with the prior exception messages) Disclaimer: * Existing mock data included strings which did include an action and an answer. This might indicate that prioritising returning AgentAnswer was always correct, and I am patching out desired behaviour? @hwchase17 to advice. Curious if there are allowed cases where this is not hallucinating, and we do want the LLM to output an action which isn't taken. * I have not passed `send_to_llm` through this new exception Fixes #5601 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 - project lead @vowelparrot
2023-06-04 21:40:49 +00:00
llm_output = "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"
2022-12-25 14:53:36 +00:00
def test_get_final_answer_multiline() -> None:
"""Test getting final answer that is multiline."""
Raise an exception in MKRL and Chat Output Parsers if parsing text which contains both an action and a final answer (#5609) Raises exception if OutputParsers receive a response with both a valid action and a final answer Currently, if an OutputParser receives a response which includes both an action and a final answer, they return a FinalAnswer object. This allows the parser to accept responses which propose an action and hallucinate an answer without the action being parsed or taken by the agent. This PR changes the logic to: 1. store a variable checking whether a response contains the `FINAL_ANSWER_ACTION` (this is the easier condition to check). 2. store a variable checking whether the response contains a valid action 3. if both are present, raise a new exception stating that both are present 4. if an action is present, return an AgentAction 5. if an answer is present, return an AgentAnswer 6. if neither is present, raise the relevant exception based around the action format (these have been kept consistent with the prior exception messages) Disclaimer: * Existing mock data included strings which did include an action and an answer. This might indicate that prioritising returning AgentAnswer was always correct, and I am patching out desired behaviour? @hwchase17 to advice. Curious if there are allowed cases where this is not hallucinating, and we do want the LLM to output an action which isn't taken. * I have not passed `send_to_llm` through this new exception Fixes #5601 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 - project lead @vowelparrot
2023-06-04 21:40:49 +00:00
llm_output = "Thought: I can now answer the question\n" "Final Answer: 1994\n1993"
2022-12-25 14:53:36 +00:00
action, action_input = get_action_and_input(llm_output)
assert action == "Final Answer"
assert action_input == "1994\n1993"
2022-11-05 21:41:53 +00:00
def test_bad_action_input_line() -> None:
"""Test handling when no action input found."""
llm_output = "Thought: I need to search for NBA\n" "Action: Search\n" "Thought: NBA"
with pytest.raises(OutputParserException) as e_info:
2022-11-05 21:41:53 +00:00
get_action_and_input(llm_output)
assert e_info.value.observation is not None
2022-11-05 21:41:53 +00:00
def test_bad_action_line() -> None:
"""Test handling when no action found."""
2022-11-05 21:41:53 +00:00
llm_output = (
"Thought: I need to search for NBA\n" "Thought: Search\n" "Action Input: NBA"
)
with pytest.raises(OutputParserException) as e_info:
2022-11-05 21:41:53 +00:00
get_action_and_input(llm_output)
assert e_info.value.observation is not None
2022-11-05 21:41:53 +00:00
Raise an exception in MKRL and Chat Output Parsers if parsing text which contains both an action and a final answer (#5609) Raises exception if OutputParsers receive a response with both a valid action and a final answer Currently, if an OutputParser receives a response which includes both an action and a final answer, they return a FinalAnswer object. This allows the parser to accept responses which propose an action and hallucinate an answer without the action being parsed or taken by the agent. This PR changes the logic to: 1. store a variable checking whether a response contains the `FINAL_ANSWER_ACTION` (this is the easier condition to check). 2. store a variable checking whether the response contains a valid action 3. if both are present, raise a new exception stating that both are present 4. if an action is present, return an AgentAction 5. if an answer is present, return an AgentAnswer 6. if neither is present, raise the relevant exception based around the action format (these have been kept consistent with the prior exception messages) Disclaimer: * Existing mock data included strings which did include an action and an answer. This might indicate that prioritising returning AgentAnswer was always correct, and I am patching out desired behaviour? @hwchase17 to advice. Curious if there are allowed cases where this is not hallucinating, and we do want the LLM to output an action which isn't taken. * I have not passed `send_to_llm` through this new exception Fixes #5601 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 - project lead @vowelparrot
2023-06-04 21:40:49 +00:00
def test_valid_action_and_answer_raises_exception() -> None:
"""Test handling when both an action and answer are found."""
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: 1994"
)
with pytest.raises(OutputParserException):
get_action_and_input(llm_output)
2022-11-05 21:41:53 +00:00
def test_from_chains() -> None:
"""Test initializing from chains."""
chain_configs = [
2022-11-22 14:16:26 +00:00
Tool(name="foo", func=lambda x: "foo", description="foobar1"),
Tool(name="bar", func=lambda x: "bar", description="foobar2"),
2022-11-05 21:41:53 +00:00
]
2022-11-22 14:16:26 +00:00
agent = ZeroShotAgent.from_llm_and_tools(FakeLLM(), chain_configs)
2022-11-05 21:41:53 +00:00
expected_tools_prompt = "foo: foobar1\nbar: foobar2"
expected_tool_names = "foo, bar"
2022-11-26 14:03:08 +00:00
expected_template = "\n\n".join(
[
PREFIX,
expected_tools_prompt,
FORMAT_INSTRUCTIONS.format(tool_names=expected_tool_names),
SUFFIX,
]
2022-11-05 21:41:53 +00:00
)
2022-11-22 14:16:26 +00:00
prompt = agent.llm_chain.prompt
2022-11-20 04:32:45 +00:00
assert isinstance(prompt, PromptTemplate)
2022-11-05 21:41:53 +00:00
assert prompt.template == expected_template