Skip to content

Commit

Permalink
[NA] Relax litellm version and make litellm monitoring optional (#1122)
Browse files Browse the repository at this point in the history
* Make litellm monitoring optional for old litellm versions

* Remove litellm version restriction
  • Loading branch information
alexkuzmik authored Jan 23, 2025
1 parent 9418491 commit 57114bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sdks/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"click",
"httpx<0.28.0",
"levenshtein<1.0.0",
"litellm>=1.49.1",
"litellm",
"openai<2.0.0",
"pydantic-settings>=2.0.0,<3.0.0",
"pydantic>=2.0.0,<3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def generate_provider_response(
all_kwargs = {**self._completion_kwargs, **valid_litellm_params}

if opik_monitor.enabled_in_config():
all_kwargs = opik_monitor.add_opik_monitoring_to_params(all_kwargs)
all_kwargs = opik_monitor.try_add_opik_monitoring_to_params(all_kwargs)

response = self._engine.completion(
model=self.model_name, messages=messages, **all_kwargs
Expand Down Expand Up @@ -222,7 +222,7 @@ async def agenerate_provider_response(self, **kwargs: Any) -> ModelResponse:
all_kwargs = {**self._completion_kwargs, **valid_litellm_params}

if opik_monitor.enabled_in_config():
all_kwargs = opik_monitor.add_opik_monitoring_to_params(all_kwargs)
all_kwargs = opik_monitor.try_add_opik_monitoring_to_params(all_kwargs)

response = await self._engine.completion(
model=self.model_name, messages=messages, **all_kwargs
Expand Down
14 changes: 11 additions & 3 deletions sdks/python/src/opik/evaluation/models/litellm/opik_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
from typing import Any, Dict

import litellm
from litellm.integrations.opik import opik as litellm_opik_logger

try:
from litellm.integrations.opik import opik as litellm_opik_logger
except ImportError:
litellm_opik_logger = None


from opik import opik_context
from opik import config


def add_opik_monitoring_to_params(params: Dict[str, Any]) -> Dict[str, Any]:
def try_add_opik_monitoring_to_params(params: Dict[str, Any]) -> Dict[str, Any]:
if litellm_opik_logger is None:
return params

already_decorated = hasattr(litellm.completion, "opik_tracked")
if already_decorated:
return params
Expand Down Expand Up @@ -70,5 +78,5 @@ def _ensure_params_have_callback(params: Dict[str, Any]) -> Dict[str, Any]:


@functools.lru_cache
def _callback_instance() -> litellm_opik_logger.OpikLogger:
def _callback_instance() -> "litellm_opik_logger.OpikLogger": # type: ignore
return litellm_opik_logger.OpikLogger()

0 comments on commit 57114bb

Please sign in to comment.