Skip to content

Commit

Permalink
add support for langfuse with litellm (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhancockio authored Dec 6, 2024
1 parent 4405136 commit ade3934
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/crewai/llm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import sys
import threading
import warnings
Expand Down Expand Up @@ -128,6 +129,7 @@ def __init__(
litellm.drop_params = True
litellm.set_verbose = False
self.set_callbacks(callbacks)
self.set_env_callbacks()

def call(self, messages: List[Dict[str, str]], callbacks: List[Any] = []) -> str:
with suppress_warnings():
Expand Down Expand Up @@ -202,3 +204,39 @@ def set_callbacks(self, callbacks: List[Any]):
litellm._async_success_callback.remove(callback)

litellm.callbacks = callbacks

def set_env_callbacks(self):
"""
Sets the success and failure callbacks for the LiteLLM library from environment variables.
This method reads the `LITELLM_SUCCESS_CALLBACKS` and `LITELLM_FAILURE_CALLBACKS`
environment variables, which should contain comma-separated lists of callback names.
It then assigns these lists to `litellm.success_callback` and `litellm.failure_callback`,
respectively.
If the environment variables are not set or are empty, the corresponding callback lists
will be set to empty lists.
Example:
LITELLM_SUCCESS_CALLBACKS="langfuse,langsmith"
LITELLM_FAILURE_CALLBACKS="langfuse"
This will set `litellm.success_callback` to ["langfuse", "langsmith"] and
`litellm.failure_callback` to ["langfuse"].
"""
success_callbacks_str = os.environ.get("LITELLM_SUCCESS_CALLBACKS", "")
success_callbacks = []
if success_callbacks_str:
success_callbacks = [
callback.strip() for callback in success_callbacks_str.split(",")
]

failure_callbacks_str = os.environ.get("LITELLM_FAILURE_CALLBACKS", "")
failure_callbacks = []
if failure_callbacks_str:
failure_callbacks = [
callback.strip() for callback in failure_callbacks_str.split(",")
]

litellm.success_callback = success_callbacks
litellm.failure_callback = failure_callbacks

0 comments on commit ade3934

Please sign in to comment.