Skip to content

Commit

Permalink
Updated behavior with Opik tracking is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jverre committed Feb 2, 2025
1 parent 547eeb5 commit 8c7fe30
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions sdks/python/src/opik/api_objects/opik_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions sdks/python/src/opik/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
34 changes: 34 additions & 0 deletions sdks/python/src/opik/integrations/langchain/opik_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 25 additions & 0 deletions sdks/python/tests/library_integration/langchain/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 8c7fe30

Please sign in to comment.