langchain/libs/community/tests/unit_tests/callbacks/tracers/test_comet.py
Bagatur ed58eeb9c5
community[major], core[patch], langchain[patch], experimental[patch]: Create langchain-community (#14463)
Moved the following modules to new package langchain-community in a backwards compatible fashion:

```
mv langchain/langchain/adapters community/langchain_community
mv langchain/langchain/callbacks community/langchain_community/callbacks
mv langchain/langchain/chat_loaders community/langchain_community
mv langchain/langchain/chat_models community/langchain_community
mv langchain/langchain/document_loaders community/langchain_community
mv langchain/langchain/docstore community/langchain_community
mv langchain/langchain/document_transformers community/langchain_community
mv langchain/langchain/embeddings community/langchain_community
mv langchain/langchain/graphs community/langchain_community
mv langchain/langchain/llms community/langchain_community
mv langchain/langchain/memory/chat_message_histories community/langchain_community
mv langchain/langchain/retrievers community/langchain_community
mv langchain/langchain/storage community/langchain_community
mv langchain/langchain/tools community/langchain_community
mv langchain/langchain/utilities community/langchain_community
mv langchain/langchain/vectorstores community/langchain_community
mv langchain/langchain/agents/agent_toolkits community/langchain_community
mv langchain/langchain/cache.py community/langchain_community
mv langchain/langchain/adapters community/langchain_community
mv langchain/langchain/callbacks community/langchain_community/callbacks
mv langchain/langchain/chat_loaders community/langchain_community
mv langchain/langchain/chat_models community/langchain_community
mv langchain/langchain/document_loaders community/langchain_community
mv langchain/langchain/docstore community/langchain_community
mv langchain/langchain/document_transformers community/langchain_community
mv langchain/langchain/embeddings community/langchain_community
mv langchain/langchain/graphs community/langchain_community
mv langchain/langchain/llms community/langchain_community
mv langchain/langchain/memory/chat_message_histories community/langchain_community
mv langchain/langchain/retrievers community/langchain_community
mv langchain/langchain/storage community/langchain_community
mv langchain/langchain/tools community/langchain_community
mv langchain/langchain/utilities community/langchain_community
mv langchain/langchain/vectorstores community/langchain_community
mv langchain/langchain/agents/agent_toolkits community/langchain_community
mv langchain/langchain/cache.py community/langchain_community
```

Moved the following to core
```
mv langchain/langchain/utils/json_schema.py core/langchain_core/utils
mv langchain/langchain/utils/html.py core/langchain_core/utils
mv langchain/langchain/utils/strings.py core/langchain_core/utils
cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py
rm langchain/langchain/utils/env.py
```

See .scripts/community_split/script_integrations.sh for all changes
2023-12-11 13:53:30 -08:00

99 lines
3.1 KiB
Python

import uuid
from types import SimpleNamespace
from unittest import mock
from langchain_core.outputs import LLMResult
from langchain_community.callbacks.tracers import comet
def test_comet_tracer__trace_chain_with_single_span__happyflow() -> None:
# Setup mocks
chain_module_mock = mock.Mock()
chain_instance_mock = mock.Mock()
chain_module_mock.Chain.return_value = chain_instance_mock
span_module_mock = mock.Mock()
span_instance_mock = mock.MagicMock()
span_instance_mock.__api__start__ = mock.Mock()
span_instance_mock.__api__end__ = mock.Mock()
span_module_mock.Span.return_value = span_instance_mock
experiment_info_module_mock = mock.Mock()
experiment_info_module_mock.get.return_value = "the-experiment-info"
chain_api_module_mock = mock.Mock()
comet_ml_api_mock = SimpleNamespace(
chain=chain_module_mock,
span=span_module_mock,
experiment_info=experiment_info_module_mock,
chain_api=chain_api_module_mock,
flush="not-used-in-this-test",
)
# Create tracer
with mock.patch.object(
comet, "import_comet_llm_api", return_value=comet_ml_api_mock
):
tracer = comet.CometTracer()
run_id_1 = uuid.UUID("9d878ab3-e5ca-4218-aef6-44cbdc90160a")
run_id_2 = uuid.UUID("4f31216e-7c26-4027-a5fd-0bbf9ace17dc")
# Parent run
tracer.on_chain_start(
{"name": "chain-input"},
{"input": "chain-input-prompt"},
parent_run_id=None,
run_id=run_id_1,
)
# Check that chain was created
chain_module_mock.Chain.assert_called_once_with(
inputs={"input": "chain-input-prompt"},
metadata=None,
experiment_info="the-experiment-info",
)
# Child run
tracer.on_llm_start(
{"name": "span-input"},
["span-input-prompt"],
parent_run_id=run_id_1,
run_id=run_id_2,
)
# Check that Span was created and attached to chain
span_module_mock.Span.assert_called_once_with(
inputs={"prompts": ["span-input-prompt"]},
category=mock.ANY,
metadata=mock.ANY,
name=mock.ANY,
)
span_instance_mock.__api__start__(chain_instance_mock)
# Child run end
tracer.on_llm_end(
LLMResult(generations=[], llm_output={"span-output-key": "span-output-value"}),
run_id=run_id_2,
)
# Check that Span outputs are set and span is ended
span_instance_mock.set_outputs.assert_called_once()
actual_span_outputs = span_instance_mock.set_outputs.call_args[1]["outputs"]
assert {
"llm_output": {"span-output-key": "span-output-value"},
"generations": [],
}.items() <= actual_span_outputs.items()
span_instance_mock.__api__end__()
# Parent run end
tracer.on_chain_end({"chain-output-key": "chain-output-value"}, run_id=run_id_1)
# Check that chain outputs are set and chain is logged
chain_instance_mock.set_outputs.assert_called_once()
actual_chain_outputs = chain_instance_mock.set_outputs.call_args[1]["outputs"]
assert ("chain-output-key", "chain-output-value") in actual_chain_outputs.items()
chain_api_module_mock.log_chain.assert_called_once_with(chain_instance_mock)