explicitly set global verbosity flag in unit tests that test callbacks (#502)

If another file anywhere in `unit_tests` sets `langchain.verbose =
True`, it messes up all of the tests that check for no callbacks because
the `None` for verbose gets overridden by the global verbosity flag. By
explicitly setting it in unit tests, we bypass that potential issue
This commit is contained in:
Ankush Gola 2023-01-02 14:06:46 -08:00 committed by GitHub
parent aae3609aa8
commit db5c8e0c42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 2 deletions

View File

@ -65,8 +65,11 @@ def test_agent_stopped_early() -> None:
assert output == "Agent stopped due to max iterations."
def test_agent_with_callbacks() -> None:
"""Test react chain with callbacks."""
def test_agent_with_callbacks_global() -> None:
"""Test react chain with callbacks by setting verbose globally."""
import langchain
langchain.verbose = True
handler = FakeCallbackHandler()
manager = CallbackManager([handler])
tool = "Search"
@ -85,6 +88,41 @@ def test_agent_with_callbacks() -> None:
verbose=True,
callback_manager=manager,
)
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()
manager = CallbackManager([handler])
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,
)
agent.agent.llm_chain.verbose = True
output = agent.run("when was langchain made")
@ -99,6 +137,9 @@ def test_agent_with_callbacks() -> None:
def test_agent_with_callbacks_not_verbose() -> None:
"""Test react chain with callbacks but not verbose."""
import langchain
langchain.verbose = False
handler = FakeCallbackHandler()
manager = CallbackManager([handler])
tool = "Search"

View File

@ -150,6 +150,10 @@ def test_run_with_callback() -> None:
def test_run_with_callback_not_verbose() -> None:
"""Test run method works when callback manager is passed and not verbose."""
import langchain
langchain.verbose = False
handler = FakeCallbackHandler()
chain = FakeChain(callback_manager=CallbackManager([handler]))
output = chain.run("bar")

View File

@ -17,6 +17,10 @@ def test_llm_with_callbacks() -> None:
def test_llm_with_callbacks_not_verbose() -> None:
"""Test LLM callbacks but not verbose."""
import langchain
langchain.verbose = False
handler = FakeCallbackHandler()
llm = FakeLLM(callback_manager=CallbackManager([handler]))
output = llm("foo")