2022-10-27 04:02:23 +00:00
|
|
|
"""Unit tests for ReAct."""
|
|
|
|
|
2022-11-09 06:17:10 +00:00
|
|
|
from typing import Any, List, Mapping, Optional, Union
|
2022-10-27 04:02:23 +00:00
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2022-11-22 14:16:26 +00:00
|
|
|
from langchain.agents.react.base import ReActChain, ReActDocstoreAgent
|
|
|
|
from langchain.agents.tools import Tool
|
2022-10-27 04:02:23 +00:00
|
|
|
from langchain.docstore.base import Docstore
|
|
|
|
from langchain.docstore.document import Document
|
|
|
|
from langchain.llms.base import LLM
|
2022-11-20 04:32:45 +00:00
|
|
|
from langchain.prompts.prompt import PromptTemplate
|
2022-12-19 02:51:23 +00:00
|
|
|
from langchain.schema import AgentAction
|
2022-10-27 04:02:23 +00:00
|
|
|
|
|
|
|
_PAGE_CONTENT = """This is a page about LangChain.
|
|
|
|
|
|
|
|
It is a really cool framework.
|
|
|
|
|
|
|
|
What isn't there to love about langchain?
|
|
|
|
|
|
|
|
Made in 2022."""
|
|
|
|
|
2022-11-20 04:32:45 +00:00
|
|
|
_FAKE_PROMPT = PromptTemplate(input_variables=["input"], template="{input}")
|
2022-10-27 04:02:23 +00:00
|
|
|
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
class FakeListLLM(LLM, BaseModel):
|
2022-10-27 04:02:23 +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
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _llm_type(self) -> str:
|
|
|
|
"""Return type of llm."""
|
|
|
|
return "fake_list"
|
2022-10-27 04:02:23 +00:00
|
|
|
|
2022-12-15 15:53:32 +00:00
|
|
|
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
2022-10-27 04:02:23 +00:00
|
|
|
"""Increment counter, and then return response in that index."""
|
|
|
|
self.i += 1
|
|
|
|
return self.responses[self.i]
|
|
|
|
|
2022-11-09 06:17:10 +00:00
|
|
|
@property
|
|
|
|
def _identifying_params(self) -> Mapping[str, Any]:
|
|
|
|
return {}
|
|
|
|
|
2022-10-27 04:02:23 +00:00
|
|
|
|
|
|
|
class FakeDocstore(Docstore):
|
|
|
|
"""Fake docstore for testing purposes."""
|
|
|
|
|
2022-11-01 04:18:52 +00:00
|
|
|
def search(self, search: str) -> Union[str, Document]:
|
2022-10-27 04:02:23 +00:00
|
|
|
"""Return the fake document."""
|
|
|
|
document = Document(page_content=_PAGE_CONTENT)
|
2022-11-01 04:18:52 +00:00
|
|
|
return document
|
2022-10-27 04:02:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_predict_until_observation_normal() -> None:
|
|
|
|
"""Test predict_until_observation when observation is made normally."""
|
2023-03-29 21:38:21 +00:00
|
|
|
outputs = ["foo\nAction: Search[foo]"]
|
2022-12-13 14:46:01 +00:00
|
|
|
fake_llm = FakeListLLM(responses=outputs)
|
2022-11-22 14:16:26 +00:00
|
|
|
tools = [
|
2023-02-18 21:40:43 +00:00
|
|
|
Tool(name="Search", func=lambda x: x, description="foo"),
|
|
|
|
Tool(name="Lookup", func=lambda x: x, description="bar"),
|
2022-11-22 14:16:26 +00:00
|
|
|
]
|
|
|
|
agent = ReActDocstoreAgent.from_llm_and_tools(fake_llm, tools)
|
2022-12-19 02:51:23 +00:00
|
|
|
output = agent.plan([], input="")
|
|
|
|
expected_output = AgentAction("Search", "foo", outputs[0])
|
|
|
|
assert output == expected_output
|
2022-10-27 04:02:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_predict_until_observation_repeat() -> None:
|
|
|
|
"""Test when no action is generated initially."""
|
2022-11-22 14:16:26 +00:00
|
|
|
outputs = ["foo", " Search[foo]"]
|
2022-12-13 14:46:01 +00:00
|
|
|
fake_llm = FakeListLLM(responses=outputs)
|
2022-11-22 14:16:26 +00:00
|
|
|
tools = [
|
2023-02-18 21:40:43 +00:00
|
|
|
Tool(name="Search", func=lambda x: x, description="foo"),
|
|
|
|
Tool(name="Lookup", func=lambda x: x, description="bar"),
|
2022-11-22 14:16:26 +00:00
|
|
|
]
|
|
|
|
agent = ReActDocstoreAgent.from_llm_and_tools(fake_llm, tools)
|
2022-12-19 02:51:23 +00:00
|
|
|
output = agent.plan([], input="")
|
2023-03-29 21:38:21 +00:00
|
|
|
expected_output = AgentAction("Search", "foo", "foo\nAction: Search[foo]")
|
2022-12-19 02:51:23 +00:00
|
|
|
assert output == expected_output
|
2022-10-27 04:02:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_react_chain() -> None:
|
|
|
|
"""Test react chain."""
|
|
|
|
responses = [
|
2023-03-29 21:38:21 +00:00
|
|
|
"I should probably search\nAction: Search[langchain]",
|
|
|
|
"I should probably lookup\nAction: Lookup[made]",
|
|
|
|
"Ah okay now I know the answer\nAction: Finish[2022]",
|
2022-10-27 04:02:23 +00:00
|
|
|
]
|
2022-12-13 14:46:01 +00:00
|
|
|
fake_llm = FakeListLLM(responses=responses)
|
2022-10-27 04:02:23 +00:00
|
|
|
react_chain = ReActChain(llm=fake_llm, docstore=FakeDocstore())
|
2022-11-22 14:16:26 +00:00
|
|
|
output = react_chain.run("when was langchain made")
|
|
|
|
assert output == "2022"
|
2022-10-27 04:02:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_react_chain_bad_action() -> None:
|
|
|
|
"""Test react chain when bad action given."""
|
2022-12-07 05:52:48 +00:00
|
|
|
bad_action_name = "BadAction"
|
2022-10-27 04:02:23 +00:00
|
|
|
responses = [
|
2023-03-29 21:38:21 +00:00
|
|
|
f"I'm turning evil\nAction: {bad_action_name}[langchain]",
|
|
|
|
"Oh well\nAction: Finish[curses foiled again]",
|
2022-10-27 04:02:23 +00:00
|
|
|
]
|
2022-12-13 14:46:01 +00:00
|
|
|
fake_llm = FakeListLLM(responses=responses)
|
2022-10-27 04:02:23 +00:00
|
|
|
react_chain = ReActChain(llm=fake_llm, docstore=FakeDocstore())
|
2022-12-07 05:52:48 +00:00
|
|
|
output = react_chain.run("when was langchain made")
|
|
|
|
assert output == "curses foiled again"
|