2022-12-07 05:52:48 +00:00
|
|
|
"""Unit tests for agents."""
|
|
|
|
|
|
|
|
from typing import Any, List, Mapping, Optional
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2022-12-30 01:30:31 +00:00
|
|
|
from langchain.agents import AgentExecutor, Tool, initialize_agent
|
|
|
|
from langchain.callbacks.base import CallbackManager
|
2022-12-07 05:52:48 +00:00
|
|
|
from langchain.llms.base import LLM
|
2022-12-30 01:30:31 +00:00
|
|
|
from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler
|
2022-12-07 05:52:48 +00:00
|
|
|
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
class FakeListLLM(LLM, BaseModel):
|
2022-12-07 05:52:48 +00:00
|
|
|
"""Fake LLM for testing that outputs elements of a list."""
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
responses: List[str]
|
|
|
|
i: int = -1
|
2022-12-07 05:52:48 +00:00
|
|
|
|
2022-12-15 15:53:32 +00:00
|
|
|
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
2022-12-07 05:52:48 +00:00
|
|
|
"""Increment counter, and then return response in that index."""
|
|
|
|
self.i += 1
|
|
|
|
print(self.i)
|
|
|
|
print(self.responses)
|
|
|
|
return self.responses[self.i]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _identifying_params(self) -> Mapping[str, Any]:
|
|
|
|
return {}
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
@property
|
|
|
|
def _llm_type(self) -> str:
|
|
|
|
"""Return type of llm."""
|
|
|
|
return "fake_list"
|
|
|
|
|
2022-12-07 05:52:48 +00:00
|
|
|
|
2022-12-30 01:30:31 +00:00
|
|
|
def _get_agent(**kwargs: Any) -> AgentExecutor:
|
|
|
|
"""Get agent for testing."""
|
2022-12-07 05:52:48 +00:00
|
|
|
bad_action_name = "BadAction"
|
|
|
|
responses = [
|
|
|
|
f"I'm turning evil\nAction: {bad_action_name}\nAction Input: misalignment",
|
|
|
|
"Oh well\nAction: Final Answer\nAction Input: curses foiled again",
|
|
|
|
]
|
2022-12-13 14:46:01 +00:00
|
|
|
fake_llm = FakeListLLM(responses=responses)
|
2022-12-07 05:52:48 +00:00
|
|
|
tools = [
|
|
|
|
Tool("Search", lambda x: x, "Useful for searching"),
|
|
|
|
Tool("Lookup", lambda x: x, "Useful for looking up things in a table"),
|
|
|
|
]
|
|
|
|
agent = initialize_agent(
|
2022-12-30 01:30:31 +00:00
|
|
|
tools, fake_llm, agent="zero-shot-react-description", verbose=True, **kwargs
|
2022-12-07 05:52:48 +00:00
|
|
|
)
|
2022-12-30 01:30:31 +00:00
|
|
|
return agent
|
|
|
|
|
|
|
|
|
|
|
|
def test_agent_bad_action() -> None:
|
|
|
|
"""Test react chain when bad action given."""
|
|
|
|
agent = _get_agent()
|
2022-12-07 05:52:48 +00:00
|
|
|
output = agent.run("when was langchain made")
|
|
|
|
assert output == "curses foiled again"
|
2022-12-29 13:21:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_agent_stopped_early() -> None:
|
|
|
|
"""Test react chain when bad action given."""
|
2022-12-30 01:30:31 +00:00
|
|
|
agent = _get_agent(max_iterations=0)
|
|
|
|
output = agent.run("when was langchain made")
|
|
|
|
assert output == "Agent stopped due to max iterations."
|
|
|
|
|
|
|
|
|
2023-01-02 22:06:46 +00:00
|
|
|
def test_agent_with_callbacks_global() -> None:
|
|
|
|
"""Test react chain with callbacks by setting verbose globally."""
|
|
|
|
import langchain
|
|
|
|
|
|
|
|
langchain.verbose = True
|
2022-12-30 01:30:31 +00:00
|
|
|
handler = FakeCallbackHandler()
|
2023-01-03 22:12:43 +00:00
|
|
|
manager = CallbackManager(handlers=[handler])
|
2022-12-30 01:30:31 +00:00
|
|
|
tool = "Search"
|
2022-12-29 13:21:11 +00:00
|
|
|
responses = [
|
2022-12-30 01:30:31 +00:00
|
|
|
f"FooBarBaz\nAction: {tool}\nAction Input: misalignment",
|
2022-12-29 13:21:11 +00:00
|
|
|
"Oh well\nAction: Final Answer\nAction Input: curses foiled again",
|
|
|
|
]
|
2022-12-30 02:34:47 +00:00
|
|
|
fake_llm = FakeListLLM(responses=responses, callback_manager=manager, verbose=True)
|
2022-12-29 13:21:11 +00:00
|
|
|
tools = [
|
|
|
|
Tool("Search", lambda x: x, "Useful for searching"),
|
|
|
|
]
|
|
|
|
agent = initialize_agent(
|
|
|
|
tools,
|
|
|
|
fake_llm,
|
|
|
|
agent="zero-shot-react-description",
|
|
|
|
verbose=True,
|
2022-12-30 01:30:31 +00:00
|
|
|
callback_manager=manager,
|
2022-12-29 13:21:11 +00:00
|
|
|
)
|
2023-01-02 22:06:46 +00:00
|
|
|
|
|
|
|
output = agent.run("when was langchain made")
|
|
|
|
assert output == "curses foiled again"
|
|
|
|
|
|
|
|
# 1 top level chain run, 2 LLMChain runs, 2 LLM runs, 1 tool run
|
|
|
|
assert handler.starts == 6
|
|
|
|
# 1 extra agent end
|
|
|
|
assert handler.ends == 7
|
|
|
|
assert handler.errors == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_agent_with_callbacks_local() -> None:
|
|
|
|
"""Test react chain with callbacks by setting verbose locally."""
|
|
|
|
import langchain
|
|
|
|
|
|
|
|
langchain.verbose = False
|
|
|
|
handler = FakeCallbackHandler()
|
2023-01-03 22:12:43 +00:00
|
|
|
manager = CallbackManager(handlers=[handler])
|
2023-01-02 22:06:46 +00:00
|
|
|
tool = "Search"
|
|
|
|
responses = [
|
|
|
|
f"FooBarBaz\nAction: {tool}\nAction Input: misalignment",
|
|
|
|
"Oh well\nAction: Final Answer\nAction Input: curses foiled again",
|
|
|
|
]
|
|
|
|
fake_llm = FakeListLLM(responses=responses, callback_manager=manager, verbose=True)
|
|
|
|
tools = [
|
|
|
|
Tool("Search", lambda x: x, "Useful for searching"),
|
|
|
|
]
|
|
|
|
agent = initialize_agent(
|
|
|
|
tools,
|
|
|
|
fake_llm,
|
|
|
|
agent="zero-shot-react-description",
|
|
|
|
verbose=True,
|
|
|
|
callback_manager=manager,
|
|
|
|
)
|
|
|
|
|
2022-12-30 03:23:02 +00:00
|
|
|
agent.agent.llm_chain.verbose = True
|
2022-12-30 01:30:31 +00:00
|
|
|
|
2022-12-29 13:21:11 +00:00
|
|
|
output = agent.run("when was langchain made")
|
2022-12-30 01:30:31 +00:00
|
|
|
assert output == "curses foiled again"
|
|
|
|
|
2022-12-30 03:23:15 +00:00
|
|
|
# 1 top level chain run, 2 LLMChain runs, 2 LLM runs, 1 tool run
|
|
|
|
assert handler.starts == 6
|
|
|
|
# 1 extra agent end
|
2022-12-30 02:34:47 +00:00
|
|
|
assert handler.ends == 7
|
|
|
|
assert handler.errors == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_agent_with_callbacks_not_verbose() -> None:
|
|
|
|
"""Test react chain with callbacks but not verbose."""
|
2023-01-02 22:06:46 +00:00
|
|
|
import langchain
|
|
|
|
|
|
|
|
langchain.verbose = False
|
2022-12-30 02:34:47 +00:00
|
|
|
handler = FakeCallbackHandler()
|
2023-01-03 22:12:43 +00:00
|
|
|
manager = CallbackManager(handlers=[handler])
|
2022-12-30 02:34:47 +00:00
|
|
|
tool = "Search"
|
|
|
|
responses = [
|
|
|
|
f"FooBarBaz\nAction: {tool}\nAction Input: misalignment",
|
|
|
|
"Oh well\nAction: Final Answer\nAction Input: curses foiled again",
|
|
|
|
]
|
|
|
|
fake_llm = FakeListLLM(responses=responses, callback_manager=manager)
|
|
|
|
tools = [
|
|
|
|
Tool("Search", lambda x: x, "Useful for searching"),
|
|
|
|
]
|
|
|
|
agent = initialize_agent(
|
|
|
|
tools,
|
|
|
|
fake_llm,
|
|
|
|
agent="zero-shot-react-description",
|
|
|
|
callback_manager=manager,
|
|
|
|
)
|
|
|
|
|
|
|
|
output = agent.run("when was langchain made")
|
|
|
|
assert output == "curses foiled again"
|
|
|
|
|
2022-12-30 01:30:31 +00:00
|
|
|
# 1 top level chain run, 2 LLMChain runs, 2 LLM runs, 1 tool run
|
2022-12-30 02:34:47 +00:00
|
|
|
assert handler.starts == 0
|
|
|
|
assert handler.ends == 0
|
2022-12-30 01:30:31 +00:00
|
|
|
assert handler.errors == 0
|