2023-01-04 15:54:25 +00:00
|
|
|
"""Test CallbackManager."""
|
2023-04-30 18:14:09 +00:00
|
|
|
from typing import List, Tuple
|
2023-01-04 15:54:25 +00:00
|
|
|
|
2023-02-14 23:06:14 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-04-30 18:14:09 +00:00
|
|
|
from langchain.callbacks.base import BaseCallbackHandler
|
|
|
|
from langchain.callbacks.manager import AsyncCallbackManager, CallbackManager
|
|
|
|
from langchain.callbacks.stdout import StdOutCallbackHandler
|
|
|
|
from langchain.schema import AgentAction, AgentFinish, LLMResult
|
2023-02-14 23:06:14 +00:00
|
|
|
from tests.unit_tests.callbacks.fake_callback_handler import (
|
|
|
|
BaseFakeCallbackHandler,
|
|
|
|
FakeAsyncCallbackHandler,
|
|
|
|
FakeCallbackHandler,
|
|
|
|
)
|
2023-01-04 15:54:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _test_callback_manager(
|
2023-04-30 18:14:09 +00:00
|
|
|
manager: CallbackManager, *handlers: BaseFakeCallbackHandler
|
2023-01-27 01:38:13 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test the CallbackManager."""
|
2023-06-25 04:03:31 +00:00
|
|
|
run_managers = manager.on_llm_start({}, ["prompt"])
|
|
|
|
for run_manager in run_managers:
|
|
|
|
run_manager.on_llm_end(LLMResult(generations=[]))
|
|
|
|
run_manager.on_llm_error(Exception())
|
|
|
|
run_manager.on_llm_new_token("foo")
|
|
|
|
run_manager.on_text("foo")
|
2023-04-30 18:14:09 +00:00
|
|
|
|
|
|
|
run_manager_chain = manager.on_chain_start({"name": "foo"}, {})
|
|
|
|
run_manager_chain.on_chain_end({})
|
|
|
|
run_manager_chain.on_chain_error(Exception())
|
|
|
|
run_manager_chain.on_agent_action(AgentAction(tool_input="foo", log="", tool=""))
|
|
|
|
run_manager_chain.on_agent_finish(AgentFinish(log="", return_values={}))
|
|
|
|
run_manager_chain.on_text("foo")
|
|
|
|
|
|
|
|
run_manager_tool = manager.on_tool_start({}, "")
|
|
|
|
run_manager_tool.on_tool_end("")
|
|
|
|
run_manager_tool.on_tool_error(Exception())
|
|
|
|
run_manager_tool.on_text("foo")
|
2023-02-14 23:06:14 +00:00
|
|
|
_check_num_calls(handlers)
|
|
|
|
|
|
|
|
|
|
|
|
async def _test_callback_manager_async(
|
|
|
|
manager: AsyncCallbackManager, *handlers: BaseFakeCallbackHandler
|
|
|
|
) -> None:
|
|
|
|
"""Test the CallbackManager."""
|
2023-06-25 04:03:31 +00:00
|
|
|
run_managers = await manager.on_llm_start({}, ["prompt"])
|
|
|
|
for run_manager in run_managers:
|
|
|
|
await run_manager.on_llm_end(LLMResult(generations=[]))
|
|
|
|
await run_manager.on_llm_error(Exception())
|
|
|
|
await run_manager.on_llm_new_token("foo")
|
|
|
|
await run_manager.on_text("foo")
|
2023-04-30 18:14:09 +00:00
|
|
|
|
|
|
|
run_manager_chain = await manager.on_chain_start({"name": "foo"}, {})
|
|
|
|
await run_manager_chain.on_chain_end({})
|
|
|
|
await run_manager_chain.on_chain_error(Exception())
|
|
|
|
await run_manager_chain.on_agent_action(
|
|
|
|
AgentAction(tool_input="foo", log="", tool="")
|
|
|
|
)
|
|
|
|
await run_manager_chain.on_agent_finish(AgentFinish(log="", return_values={}))
|
|
|
|
await run_manager_chain.on_text("foo")
|
|
|
|
|
|
|
|
run_manager_tool = await manager.on_tool_start({}, "")
|
|
|
|
await run_manager_tool.on_tool_end("")
|
|
|
|
await run_manager_tool.on_tool_error(Exception())
|
|
|
|
await run_manager_tool.on_text("foo")
|
2023-02-14 23:06:14 +00:00
|
|
|
_check_num_calls(handlers)
|
|
|
|
|
|
|
|
|
|
|
|
def _check_num_calls(handlers: Tuple[BaseFakeCallbackHandler, ...]) -> None:
|
2023-01-27 01:38:13 +00:00
|
|
|
for handler in handlers:
|
2023-04-30 18:14:09 +00:00
|
|
|
assert handler.starts == 4
|
2023-01-04 15:54:25 +00:00
|
|
|
assert handler.ends == 4
|
|
|
|
assert handler.errors == 3
|
2023-04-30 18:14:09 +00:00
|
|
|
assert handler.text == 3
|
2023-01-04 15:54:25 +00:00
|
|
|
|
2023-04-30 18:14:09 +00:00
|
|
|
assert handler.llm_starts == 1
|
|
|
|
assert handler.llm_ends == 1
|
|
|
|
assert handler.llm_streams == 1
|
2023-01-04 15:54:25 +00:00
|
|
|
|
2023-04-30 18:14:09 +00:00
|
|
|
assert handler.chain_starts == 1
|
|
|
|
assert handler.chain_ends == 1
|
|
|
|
|
|
|
|
assert handler.tool_starts == 1
|
|
|
|
assert handler.tool_ends == 1
|
2023-01-27 01:38:13 +00:00
|
|
|
|
|
|
|
|
2023-04-30 18:14:09 +00:00
|
|
|
def test_callback_manager() -> None:
|
2023-01-04 15:54:25 +00:00
|
|
|
"""Test the CallbackManager."""
|
|
|
|
handler1 = FakeCallbackHandler()
|
|
|
|
handler2 = FakeCallbackHandler()
|
Add support for tags (#5898)
<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.
Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.
After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.
Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->
<!-- Remove if not applicable -->
Fixes # (issue)
#### Before submitting
<!-- If you're adding a new integration, please include:
1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use
See contribution guidelines for more information on how to write tests,
lint
etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
#### Who can review?
Tag maintainers/contributors who might be interested:
<!-- For a quicker response, figure out the right person to tag with @
@hwchase17 - project lead
Tracing / Callbacks
- @agola11
Async
- @agola11
DataLoaders
- @eyurtsev
Models
- @hwchase17
- @agola11
Agents / Tools / Toolkits
- @vowelparrot
VectorStores / Retrievers / Memory
- @dev2049
-->
2023-06-13 19:30:59 +00:00
|
|
|
manager = CallbackManager(handlers=[handler1, handler2])
|
2023-04-30 18:14:09 +00:00
|
|
|
_test_callback_manager(manager, handler1, handler2)
|
2023-01-04 15:54:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_ignore_llm() -> None:
|
|
|
|
"""Test ignore llm param for callback handlers."""
|
2023-04-30 18:14:09 +00:00
|
|
|
handler1 = FakeCallbackHandler(ignore_llm_=True)
|
|
|
|
handler2 = FakeCallbackHandler()
|
2023-01-04 15:54:25 +00:00
|
|
|
manager = CallbackManager(handlers=[handler1, handler2])
|
2023-06-25 04:03:31 +00:00
|
|
|
run_managers = manager.on_llm_start({}, ["prompt"])
|
|
|
|
for run_manager in run_managers:
|
|
|
|
run_manager.on_llm_end(LLMResult(generations=[]))
|
|
|
|
run_manager.on_llm_error(Exception())
|
2023-01-04 15:54:25 +00:00
|
|
|
assert handler1.starts == 0
|
|
|
|
assert handler1.ends == 0
|
|
|
|
assert handler1.errors == 0
|
|
|
|
assert handler2.starts == 1
|
|
|
|
assert handler2.ends == 1
|
|
|
|
assert handler2.errors == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_ignore_chain() -> None:
|
|
|
|
"""Test ignore chain param for callback handlers."""
|
2023-04-30 18:14:09 +00:00
|
|
|
handler1 = FakeCallbackHandler(ignore_chain_=True)
|
|
|
|
handler2 = FakeCallbackHandler()
|
2023-01-04 15:54:25 +00:00
|
|
|
manager = CallbackManager(handlers=[handler1, handler2])
|
2023-04-30 18:14:09 +00:00
|
|
|
run_manager = manager.on_chain_start({"name": "foo"}, {})
|
|
|
|
run_manager.on_chain_end({})
|
|
|
|
run_manager.on_chain_error(Exception())
|
2023-01-04 15:54:25 +00:00
|
|
|
assert handler1.starts == 0
|
|
|
|
assert handler1.ends == 0
|
|
|
|
assert handler1.errors == 0
|
|
|
|
assert handler2.starts == 1
|
|
|
|
assert handler2.ends == 1
|
|
|
|
assert handler2.errors == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_ignore_agent() -> None:
|
|
|
|
"""Test ignore agent param for callback handlers."""
|
2023-04-30 18:14:09 +00:00
|
|
|
handler1 = FakeCallbackHandler(ignore_agent_=True)
|
|
|
|
handler2 = FakeCallbackHandler()
|
2023-01-04 15:54:25 +00:00
|
|
|
manager = CallbackManager(handlers=[handler1, handler2])
|
2023-04-30 18:14:09 +00:00
|
|
|
run_manager = manager.on_tool_start({}, "")
|
|
|
|
run_manager.on_tool_end("")
|
|
|
|
run_manager.on_tool_error(Exception())
|
2023-01-04 15:54:25 +00:00
|
|
|
assert handler1.starts == 0
|
|
|
|
assert handler1.ends == 0
|
|
|
|
assert handler1.errors == 0
|
|
|
|
assert handler2.starts == 1
|
2023-04-30 18:14:09 +00:00
|
|
|
assert handler2.ends == 1
|
2023-01-04 15:54:25 +00:00
|
|
|
assert handler2.errors == 1
|
|
|
|
|
|
|
|
|
2023-02-14 23:06:14 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_async_callback_manager() -> None:
|
|
|
|
"""Test the AsyncCallbackManager."""
|
2023-04-30 18:14:09 +00:00
|
|
|
handler1 = FakeAsyncCallbackHandler()
|
2023-02-14 23:06:14 +00:00
|
|
|
handler2 = FakeAsyncCallbackHandler()
|
Add support for tags (#5898)
<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.
Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.
After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.
Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->
<!-- Remove if not applicable -->
Fixes # (issue)
#### Before submitting
<!-- If you're adding a new integration, please include:
1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use
See contribution guidelines for more information on how to write tests,
lint
etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
#### Who can review?
Tag maintainers/contributors who might be interested:
<!-- For a quicker response, figure out the right person to tag with @
@hwchase17 - project lead
Tracing / Callbacks
- @agola11
Async
- @agola11
DataLoaders
- @eyurtsev
Models
- @hwchase17
- @agola11
Agents / Tools / Toolkits
- @vowelparrot
VectorStores / Retrievers / Memory
- @dev2049
-->
2023-06-13 19:30:59 +00:00
|
|
|
manager = AsyncCallbackManager(handlers=[handler1, handler2])
|
2023-02-14 23:06:14 +00:00
|
|
|
await _test_callback_manager_async(manager, handler1, handler2)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_async_callback_manager_sync_handler() -> None:
|
|
|
|
"""Test the AsyncCallbackManager."""
|
2023-04-30 18:14:09 +00:00
|
|
|
handler1 = FakeCallbackHandler()
|
2023-02-14 23:06:14 +00:00
|
|
|
handler2 = FakeAsyncCallbackHandler()
|
2023-04-30 18:14:09 +00:00
|
|
|
handler3 = FakeAsyncCallbackHandler()
|
Add support for tags (#5898)
<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.
Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.
After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.
Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->
<!-- Remove if not applicable -->
Fixes # (issue)
#### Before submitting
<!-- If you're adding a new integration, please include:
1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use
See contribution guidelines for more information on how to write tests,
lint
etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
#### Who can review?
Tag maintainers/contributors who might be interested:
<!-- For a quicker response, figure out the right person to tag with @
@hwchase17 - project lead
Tracing / Callbacks
- @agola11
Async
- @agola11
DataLoaders
- @eyurtsev
Models
- @hwchase17
- @agola11
Agents / Tools / Toolkits
- @vowelparrot
VectorStores / Retrievers / Memory
- @dev2049
-->
2023-06-13 19:30:59 +00:00
|
|
|
manager = AsyncCallbackManager(handlers=[handler1, handler2, handler3])
|
2023-04-05 07:31:42 +00:00
|
|
|
await _test_callback_manager_async(manager, handler1, handler2, handler3)
|
2023-04-30 18:14:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_callback_manager_inheritance() -> None:
|
|
|
|
handler1, handler2, handler3, handler4 = (
|
|
|
|
FakeCallbackHandler(),
|
|
|
|
FakeCallbackHandler(),
|
|
|
|
FakeCallbackHandler(),
|
|
|
|
FakeCallbackHandler(),
|
|
|
|
)
|
|
|
|
|
Add support for tags (#5898)
<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.
Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.
After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.
Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->
<!-- Remove if not applicable -->
Fixes # (issue)
#### Before submitting
<!-- If you're adding a new integration, please include:
1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use
See contribution guidelines for more information on how to write tests,
lint
etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
#### Who can review?
Tag maintainers/contributors who might be interested:
<!-- For a quicker response, figure out the right person to tag with @
@hwchase17 - project lead
Tracing / Callbacks
- @agola11
Async
- @agola11
DataLoaders
- @eyurtsev
Models
- @hwchase17
- @agola11
Agents / Tools / Toolkits
- @vowelparrot
VectorStores / Retrievers / Memory
- @dev2049
-->
2023-06-13 19:30:59 +00:00
|
|
|
callback_manager1 = CallbackManager(handlers=[handler1, handler2])
|
2023-04-30 18:14:09 +00:00
|
|
|
assert callback_manager1.handlers == [handler1, handler2]
|
|
|
|
assert callback_manager1.inheritable_handlers == []
|
|
|
|
|
Add support for tags (#5898)
<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.
Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.
After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.
Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->
<!-- Remove if not applicable -->
Fixes # (issue)
#### Before submitting
<!-- If you're adding a new integration, please include:
1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use
See contribution guidelines for more information on how to write tests,
lint
etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
#### Who can review?
Tag maintainers/contributors who might be interested:
<!-- For a quicker response, figure out the right person to tag with @
@hwchase17 - project lead
Tracing / Callbacks
- @agola11
Async
- @agola11
DataLoaders
- @eyurtsev
Models
- @hwchase17
- @agola11
Agents / Tools / Toolkits
- @vowelparrot
VectorStores / Retrievers / Memory
- @dev2049
-->
2023-06-13 19:30:59 +00:00
|
|
|
callback_manager2 = CallbackManager(handlers=[])
|
2023-04-30 18:14:09 +00:00
|
|
|
assert callback_manager2.handlers == []
|
|
|
|
assert callback_manager2.inheritable_handlers == []
|
|
|
|
|
|
|
|
callback_manager2.set_handlers([handler1, handler2])
|
|
|
|
assert callback_manager2.handlers == [handler1, handler2]
|
|
|
|
assert callback_manager2.inheritable_handlers == [handler1, handler2]
|
|
|
|
|
|
|
|
callback_manager2.set_handlers([handler3, handler4], inherit=False)
|
|
|
|
assert callback_manager2.handlers == [handler3, handler4]
|
|
|
|
assert callback_manager2.inheritable_handlers == []
|
|
|
|
|
|
|
|
callback_manager2.add_handler(handler1)
|
|
|
|
assert callback_manager2.handlers == [handler3, handler4, handler1]
|
|
|
|
assert callback_manager2.inheritable_handlers == [handler1]
|
|
|
|
|
|
|
|
callback_manager2.add_handler(handler2, inherit=False)
|
|
|
|
assert callback_manager2.handlers == [handler3, handler4, handler1, handler2]
|
|
|
|
assert callback_manager2.inheritable_handlers == [handler1]
|
|
|
|
|
|
|
|
run_manager = callback_manager2.on_chain_start({"name": "foo"}, {})
|
|
|
|
child_manager = run_manager.get_child()
|
|
|
|
assert child_manager.handlers == [handler1]
|
|
|
|
assert child_manager.inheritable_handlers == [handler1]
|
|
|
|
|
|
|
|
run_manager_tool = child_manager.on_tool_start({}, "")
|
|
|
|
assert run_manager_tool.handlers == [handler1]
|
|
|
|
assert run_manager_tool.inheritable_handlers == [handler1]
|
|
|
|
|
|
|
|
child_manager2 = run_manager_tool.get_child()
|
|
|
|
assert child_manager2.handlers == [handler1]
|
|
|
|
assert child_manager2.inheritable_handlers == [handler1]
|
|
|
|
|
|
|
|
|
2023-06-13 14:14:11 +00:00
|
|
|
def test_callback_manager_configure(monkeypatch: pytest.MonkeyPatch) -> None:
|
2023-04-30 18:14:09 +00:00
|
|
|
"""Test callback manager configuration."""
|
2023-06-13 14:14:11 +00:00
|
|
|
monkeypatch.setenv("LANGCHAIN_TRACING_V2", "false")
|
|
|
|
monkeypatch.setenv("LANGCHAIN_TRACING", "false")
|
2023-04-30 18:14:09 +00:00
|
|
|
handler1, handler2, handler3, handler4 = (
|
|
|
|
FakeCallbackHandler(),
|
|
|
|
FakeCallbackHandler(),
|
|
|
|
FakeCallbackHandler(),
|
|
|
|
FakeCallbackHandler(),
|
|
|
|
)
|
|
|
|
|
|
|
|
inheritable_callbacks: List[BaseCallbackHandler] = [handler1, handler2]
|
|
|
|
local_callbacks: List[BaseCallbackHandler] = [handler3, handler4]
|
|
|
|
configured_manager = CallbackManager.configure(
|
|
|
|
inheritable_callbacks=inheritable_callbacks,
|
|
|
|
local_callbacks=local_callbacks,
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(configured_manager.handlers) == 5
|
|
|
|
assert len(configured_manager.inheritable_handlers) == 2
|
|
|
|
assert configured_manager.inheritable_handlers == inheritable_callbacks
|
|
|
|
assert configured_manager.handlers[:4] == inheritable_callbacks + local_callbacks
|
|
|
|
assert isinstance(configured_manager.handlers[4], StdOutCallbackHandler)
|
|
|
|
assert isinstance(configured_manager, CallbackManager)
|
|
|
|
|
Add support for tags (#5898)
<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.
Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.
After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.
Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->
<!-- Remove if not applicable -->
Fixes # (issue)
#### Before submitting
<!-- If you're adding a new integration, please include:
1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use
See contribution guidelines for more information on how to write tests,
lint
etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
#### Who can review?
Tag maintainers/contributors who might be interested:
<!-- For a quicker response, figure out the right person to tag with @
@hwchase17 - project lead
Tracing / Callbacks
- @agola11
Async
- @agola11
DataLoaders
- @eyurtsev
Models
- @hwchase17
- @agola11
Agents / Tools / Toolkits
- @vowelparrot
VectorStores / Retrievers / Memory
- @dev2049
-->
2023-06-13 19:30:59 +00:00
|
|
|
async_local_callbacks = AsyncCallbackManager(handlers=[handler3, handler4])
|
2023-04-30 18:14:09 +00:00
|
|
|
async_configured_manager = AsyncCallbackManager.configure(
|
|
|
|
inheritable_callbacks=inheritable_callbacks,
|
|
|
|
local_callbacks=async_local_callbacks,
|
|
|
|
verbose=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(async_configured_manager.handlers) == 4
|
|
|
|
assert len(async_configured_manager.inheritable_handlers) == 2
|
|
|
|
assert async_configured_manager.inheritable_handlers == inheritable_callbacks
|
|
|
|
assert async_configured_manager.handlers == inheritable_callbacks + [
|
|
|
|
handler3,
|
|
|
|
handler4,
|
|
|
|
]
|
|
|
|
assert isinstance(async_configured_manager, AsyncCallbackManager)
|