Skip to content

litellm_tracer

ChatCompletion

Bases: ModelResponse

Wrapper of litellm.types.utils.ModelResponse with ImpactsOutput

ChatCompletionChunk

Bases: ModelResponse

Wrapper of litellm.types.utils.ModelResponse with ImpactsOutput

LiteLLMInstrumentor()

Instrumentor initialized by EcoLogits to automatically wrap all LiteLLM calls

Source code in ecologits/tracers/litellm_tracer.py
def __init__(self) -> None:
    self.wrapped_methods = [
        {
            "module": litellm,
            "name": "completion",
            "wrapper": litellm_chat_wrapper,
        },
        {
            "module": litellm,
            "name": "acompletion",
            "wrapper": litellm_async_chat_wrapper,
        },
    ]

litellm_match_model(model_name)

Match according provider and model from a litellm model_name.

Parameters:

Name Type Description Default
model_name str

Name of the model as used in litellm.

required

Returns:

Type Description
Optional[tuple[str, str]]

A tuple (provider, model_name) matching a record of the ModelRepository.

Source code in ecologits/tracers/litellm_tracer.py
def litellm_match_model(model_name: str) -> Optional[tuple[str, str]]:
    """
    Match according provider and model from a litellm model_name.

    Args:
        model_name: Name of the model as used in litellm.

    Returns:
        A tuple (provider, model_name) matching a record of the ModelRepository.
    """
    candidate = process.extractOne(
        query=model_name,
        choices=_model_choices,
        scorer=fuzz.token_sort_ratio,
        score_cutoff=51
    )
    if candidate is not None:
        provider, model_name = candidate[0].split("/", 1)
        return provider, model_name
    return None

litellm_chat_wrapper(wrapped, instance, args, kwargs)

Function that wraps a LiteLLM answer with computed impacts

Parameters:

Name Type Description Default
wrapped Callable

Callable that returns the LLM response

required
instance Completions

Never used - for compatibility with wrapt

required
args Any

Arguments of the callable

required
kwargs Any

Keyword arguments of the callable

required

Returns:

Type Description
Union[ChatCompletion, CustomStreamWrapper]

A wrapped ChatCompletion or CustomStreamWrapper with impacts

Source code in ecologits/tracers/litellm_tracer.py
def litellm_chat_wrapper(
    wrapped: Callable,
    instance: Completions,
    args: Any,
    kwargs: Any
) -> Union[ChatCompletion, CustomStreamWrapper]:
    """
    Function that wraps a LiteLLM answer with computed impacts

    Args:
        wrapped: Callable that returns the LLM response
        instance: Never used - for compatibility with `wrapt`
        args: Arguments of the callable
        kwargs: Keyword arguments of the callable

    Returns:
        A wrapped `ChatCompletion` or `CustomStreamWrapper` with impacts
    """

    if kwargs.get("stream", False):
        return litellm_chat_wrapper_stream(wrapped, instance, args, kwargs)
    else:
        return litellm_chat_wrapper_non_stream(wrapped, instance, args, kwargs)

litellm_async_chat_wrapper(wrapped, instance, args, kwargs) async

Function that wraps a LiteLLM answer with computed impacts in async mode

Parameters:

Name Type Description Default
wrapped Callable

Async callable that returns the LLM response

required
instance AsyncCompletions

Never used - for compatibility with wrapt

required
args Any

Arguments of the callable

required
kwargs Any

Keyword arguments of the callable

required

Returns:

Type Description
Union[ChatCompletion, CustomStreamWrapper]

A wrapped ChatCompletion or CustomStreamWrapper with impacts

Source code in ecologits/tracers/litellm_tracer.py
async def litellm_async_chat_wrapper(
    wrapped: Callable,
    instance: AsyncCompletions,
    args: Any,
    kwargs: Any
) -> Union[ChatCompletion,CustomStreamWrapper]:
    """
    Function that wraps a LiteLLM answer with computed impacts in async mode

    Args:
        wrapped: Async callable that returns the LLM response
        instance: Never used - for compatibility with `wrapt`
        args: Arguments of the callable
        kwargs: Keyword arguments of the callable

    Returns:
        A wrapped `ChatCompletion` or `CustomStreamWrapper` with impacts
    """
    if kwargs.get("stream", False):
        return litellm_async_chat_wrapper_stream(wrapped, instance, args, kwargs)
    else:
        return await litellm_async_chat_wrapper_base(wrapped, instance, args, kwargs)