This commit is contained in:
Harrison Chase 2022-11-21 12:03:29 -08:00
parent 505cb2eb62
commit 314a098fb6
2 changed files with 18 additions and 9 deletions

View File

@ -30,7 +30,7 @@ def test_get_final_answer() -> None:
"Final Answer: 1994" "Final Answer: 1994"
) )
action, action_input = get_action_and_input(llm_output) action, action_input = get_action_and_input(llm_output)
assert action == "Final Answer: " assert action == "Final Answer"
assert action_input == "1994" assert action_input == "1994"

View File

@ -9,6 +9,7 @@ from langchain.docstore.document import Document
from langchain.llms.base import LLM from langchain.llms.base import LLM
from langchain.prompts.prompt import PromptTemplate from langchain.prompts.prompt import PromptTemplate
from langchain.routing_chains.react.base import ReActChain, ReActDocstoreRouter from langchain.routing_chains.react.base import ReActChain, ReActDocstoreRouter
from langchain.routing_chains.tools import Tool
_PAGE_CONTENT = """This is a page about LangChain. _PAGE_CONTENT = """This is a page about LangChain.
@ -50,23 +51,31 @@ class FakeDocstore(Docstore):
def test_predict_until_observation_normal() -> None: def test_predict_until_observation_normal() -> None:
"""Test predict_until_observation when observation is made normally.""" """Test predict_until_observation when observation is made normally."""
outputs = ["foo\nAction 1: search[foo]"] outputs = ["foo\nAction 1: Search[foo]"]
fake_llm = FakeListLLM(outputs) fake_llm = FakeListLLM(outputs)
router_chain = ReActDocstoreRouter(llm=fake_llm) tools = [
Tool("Search", lambda x: x),
Tool("Lookup", lambda x: x),
]
router_chain = ReActDocstoreRouter.from_llm_and_tools(fake_llm, tools)
output = router_chain.route("") output = router_chain.route("")
assert output.log == outputs[0] assert output.log == outputs[0]
assert output.tool == "search" assert output.tool == "Search"
assert output.tool_input == "foo" assert output.tool_input == "foo"
def test_predict_until_observation_repeat() -> None: def test_predict_until_observation_repeat() -> None:
"""Test when no action is generated initially.""" """Test when no action is generated initially."""
outputs = ["foo", " search[foo]"] outputs = ["foo", " Search[foo]"]
fake_llm = FakeListLLM(outputs) fake_llm = FakeListLLM(outputs)
router_chain = ReActDocstoreRouter(llm=fake_llm) tools = [
Tool("Search", lambda x: x),
Tool("Lookup", lambda x: x),
]
router_chain = ReActDocstoreRouter.from_llm_and_tools(fake_llm, tools)
output = router_chain.route("") output = router_chain.route("")
assert output.log == "foo\nAction 1: search[foo]" assert output.log == "foo\nAction 1: Search[foo]"
assert output.tool == "search" assert output.tool == "Search"
assert output.tool_input == "foo" assert output.tool_input == "foo"
@ -91,5 +100,5 @@ def test_react_chain_bad_action() -> None:
] ]
fake_llm = FakeListLLM(responses) fake_llm = FakeListLLM(responses)
react_chain = ReActChain(llm=fake_llm, docstore=FakeDocstore()) react_chain = ReActChain(llm=fake_llm, docstore=FakeDocstore())
with pytest.raises(ValueError): with pytest.raises(KeyError):
react_chain.run("when was langchain made") react_chain.run("when was langchain made")