From 8c7fe30d57087c8b713438606ccf3f9ca0a5483c Mon Sep 17 00:00:00 2001 From: Jacques Verre Date: Sun, 2 Feb 2025 21:01:14 +0000 Subject: [PATCH] Updated behavior with Opik tracking is disabled --- .../src/opik/api_objects/opik_client.py | 1 + sdks/python/src/opik/config.py | 9 +++-- .../integrations/langchain/opik_tracer.py | 34 +++++++++++++++++++ .../langchain/test_langchain.py | 25 ++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/sdks/python/src/opik/api_objects/opik_client.py b/sdks/python/src/opik/api_objects/opik_client.py index 9c8016897f..5de7ff8267 100644 --- a/sdks/python/src/opik/api_objects/opik_client.py +++ b/sdks/python/src/opik/api_objects/opik_client.py @@ -68,6 +68,7 @@ def __init__( api_key=api_key, ) config.check_for_misconfiguration(config_) + self._config = config_ self._workspace: str = config_.workspace self._project_name: str = config_.project_name diff --git a/sdks/python/src/opik/config.py b/sdks/python/src/opik/config.py index 881733f40c..cc9fe68922 100644 --- a/sdks/python/src/opik/config.py +++ b/sdks/python/src/opik/config.py @@ -261,8 +261,13 @@ def check_for_misconfiguration(config: OpikConfig) -> None: ) # does not detect all OSS installations workspace_is_default = config.workspace == OPIK_WORKSPACE_DEFAULT_NAME api_key_configured = config.api_key is not None + tracking_disabled = config.track_disable - if cloud_installation and (not api_key_configured or workspace_is_default): + if ( + cloud_installation + and (not api_key_configured or workspace_is_default) + and not tracking_disabled + ): print() LOGGER.error( "========================\n" @@ -273,7 +278,7 @@ def check_for_misconfiguration(config: OpikConfig) -> None: ) return - if localhost_installation and not workspace_is_default: + if localhost_installation and not workspace_is_default and not tracking_disabled: print() LOGGER.error( "========================\n" diff --git a/sdks/python/src/opik/integrations/langchain/opik_tracer.py b/sdks/python/src/opik/integrations/langchain/opik_tracer.py index c5b12f81d6..7314067768 100644 --- a/sdks/python/src/opik/integrations/langchain/opik_tracer.py +++ b/sdks/python/src/opik/integrations/langchain/opik_tracer.py @@ -286,38 +286,72 @@ def created_traces(self) -> List[trace.Trace]: """ return self._created_traces + def _skip_tracking(self) -> bool: + config = self._opik_client._config + if config.track_disable: + return True + + return False + def _on_llm_start(self, run: "Run") -> None: """Process the LLM Run upon start.""" + if self._skip_tracking(): + return + self._process_start_span(run) def _on_llm_end(self, run: "Run") -> None: """Process the LLM Run.""" + if self._skip_tracking(): + return + self._process_end_span(run) def _on_llm_error(self, run: "Run") -> None: """Process the LLM Run upon error.""" + if self._skip_tracking(): + return + self._process_end_span_with_error(run) def _on_chain_start(self, run: "Run") -> None: """Process the Chain Run upon start.""" + if self._skip_tracking(): + return + self._process_start_span(run) def _on_chain_end(self, run: "Run") -> None: """Process the Chain Run.""" + if self._skip_tracking(): + return + self._process_end_span(run) def _on_chain_error(self, run: "Run") -> None: """Process the Chain Run upon error.""" + if self._skip_tracking(): + return + self._process_end_span_with_error(run) def _on_tool_start(self, run: "Run") -> None: """Process the Tool Run upon start.""" + if self._skip_tracking(): + return + self._process_start_span(run) def _on_tool_end(self, run: "Run") -> None: """Process the Tool Run.""" + if self._skip_tracking(): + return + self._process_end_span(run) def _on_tool_error(self, run: "Run") -> None: """Process the Tool Run upon error.""" + if self._skip_tracking(): + return + self._process_end_span_with_error(run) diff --git a/sdks/python/tests/library_integration/langchain/test_langchain.py b/sdks/python/tests/library_integration/langchain/test_langchain.py index 4dd34bde68..c9afe1e54a 100644 --- a/sdks/python/tests/library_integration/langchain/test_langchain.py +++ b/sdks/python/tests/library_integration/langchain/test_langchain.py @@ -4,6 +4,7 @@ from langchain.llms import fake from langchain.prompts import PromptTemplate +import os import opik from opik import context_storage from opik.api_objects import opik_client, span, trace @@ -750,3 +751,27 @@ def f(): assert len(fake_backend.span_trees) == 1 assert len(callback.created_traces()) == 0 assert_equal(EXPECTED_SPANS_TREE, fake_backend.span_trees[0]) + + +def test_langchain_callback__disabled_tracking(fake_backend): + os.environ["OPIK_TRACK_DISABLE"] = "true" + + llm = fake.FakeListLLM( + responses=["I'm sorry, I don't think I'm talented enough to write a synopsis"] + ) + + template = "Given the title of play, write a synopsys for that. Title: {title}." + + prompt_template = PromptTemplate(input_variables=["title"], template=template) + + synopsis_chain = prompt_template | llm + test_prompts = {"title": "Documentary about Bigfoot in Paris"} + + callback = OpikTracer() + synopsis_chain.invoke(input=test_prompts, config={"callbacks": [callback]}) + + callback.flush() + os.environ["OPIK_TRACK_DISABLE"] = "false" + + assert len(fake_backend.trace_trees) == 0 + assert len(callback.created_traces()) == 0