Skip to content

ecologits.tracers.mistralai_tracer

ChatCompletionResponse

Bases: ChatCompletionResponse

Wrapper of mistralai.client.models.ChatCompletionResponse with ImpactsOutput

CompletionChunk

Bases: CompletionChunk

Wrapper of mistralai.client.models.CompletionChunk with ImpactsOutput

MistralAIInstrumentor()

Instrumentor initialized by EcoLogits to automatically wrap all MistralAI calls

Source code in ecologits/tracers/mistralai_tracer.py
def __init__(self) -> None:
    self.wrapped_methods = [
        {
            "module": Chat.__module__,
            "name": "Chat.complete",
            "wrapper": mistralai_chat_wrapper,
        },
        {
            "module": Chat.__module__,
            "name": "Chat.complete_async",
            "wrapper": mistralai_async_chat_wrapper,
        },
        {
            "module": Chat.__module__,
            "name": "Chat.stream",
            "wrapper": mistralai_chat_wrapper_stream,
        },
        {
            "module": Chat.__module__,
            "name": "Chat.stream_async",
            "wrapper": mistralai_async_chat_wrapper_stream,
        },
    ]

mistralai_chat_wrapper(wrapped, instance, args, kwargs)

Function that wraps a MistralAI answer with computed impacts

Parameters:

Name Type Description Default
wrapped Callable

Callable that returns the LLM response

required
instance Chat

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
ChatCompletionResponse

A wrapped ChatCompletionResponse with impacts

Source code in ecologits/tracers/mistralai_tracer.py
def mistralai_chat_wrapper(
    wrapped: Callable, instance: Chat, args: Any, kwargs: Any  # noqa: ARG001
) -> ChatCompletionResponse:
    """
    Function that wraps a MistralAI 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 `ChatCompletionResponse` with impacts
    """
    if not is_tracking_enabled(PROVIDER):
        return wrapped(*args, **kwargs)
    trackers = get_active_trackers(PROVIDER)
    timer_start = time.perf_counter()
    response = wrapped(*args, **kwargs)
    request_latency = time.perf_counter() - timer_start
    impacts = _llm_impacts(
        provider=PROVIDER,
        model_name=response.model,
        output_token_count=response.usage.completion_tokens,
        request_latency=request_latency,
        electricity_mix_zone=EcoLogits.config.electricity_mix_zone
    )
    if impacts is not None:
        if EcoLogits.config.opentelemetry:
            EcoLogits.config.opentelemetry.record_request(
                input_tokens=response.usage.prompt_tokens,
                output_tokens=response.usage.completion_tokens,
                request_latency=request_latency,
                impacts=impacts,
                provider=PROVIDER,
                model=response.model,
                endpoint="/chat/completions"
            )

        record_impacts(trackers, impacts)
        return ChatCompletionResponse.model_construct(**dict(response), impacts=impacts)
    else:
        return response

mistralai_chat_wrapper_stream(wrapped, instance, args, kwargs)

Function that wraps a MistralAI answer with computed impacts in streaming mode

Parameters:

Name Type Description Default
wrapped Callable

Callable that returns the LLM response

required
instance Chat

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
Iterable[CompletionEvent]

A wrapped Iterable[CompletionEvent] with impacts

Source code in ecologits/tracers/mistralai_tracer.py
def mistralai_chat_wrapper_stream(
    wrapped: Callable, instance: Chat, args: Any, kwargs: Any  # noqa: ARG001
) -> Iterable[CompletionEvent]:
    """
    Function that wraps a MistralAI answer with computed impacts in streaming mode

    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 `Iterable[CompletionEvent]` with impacts
    """
    if not is_tracking_enabled(PROVIDER):
        return wrapped(*args, **kwargs)
    trackers = get_active_trackers(PROVIDER)
    timer_start = time.perf_counter()
    stream = wrapped(*args, **kwargs)
    return ImpactsStream(stream, _stream_generator(stream, timer_start, trackers))

mistralai_async_chat_wrapper(wrapped, instance, args, kwargs) async

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

Parameters:

Name Type Description Default
wrapped Callable

Async callable that returns the LLM response

required
instance Chat

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
ChatCompletionResponse

A wrapped ChatCompletionResponse with impacts

Source code in ecologits/tracers/mistralai_tracer.py
async def mistralai_async_chat_wrapper(
    wrapped: Callable,
    instance: Chat,  # noqa: ARG001
    args: Any,
    kwargs: Any,
) -> ChatCompletionResponse:
    """
    Function that wraps a MistralAI 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 `ChatCompletionResponse` with impacts
    """
    if not is_tracking_enabled(PROVIDER):
        return await wrapped(*args, **kwargs)
    trackers = get_active_trackers(PROVIDER)
    timer_start = time.perf_counter()
    response = await wrapped(*args, **kwargs)
    request_latency = time.perf_counter() - timer_start
    impacts = _llm_impacts(
        provider=PROVIDER,
        model_name=response.model,
        output_token_count=response.usage.completion_tokens,
        request_latency=request_latency,
        electricity_mix_zone=EcoLogits.config.electricity_mix_zone
    )
    if impacts is not None:
        if EcoLogits.config.opentelemetry:
            EcoLogits.config.opentelemetry.record_request(
                input_tokens=response.usage.prompt_tokens,
                output_tokens=response.usage.completion_tokens,
                request_latency=request_latency,
                impacts=impacts,
                provider=PROVIDER,
                model=response.model,
                endpoint="/chat/completions"
            )

        record_impacts(trackers, impacts)
        return ChatCompletionResponse.model_construct(**dict(response), impacts=impacts)
    else:
        return response

mistralai_async_chat_wrapper_stream(wrapped, instance, args, kwargs) async

Function that wraps a MistralAI answer with computed impacts in streaming and async mode

Parameters:

Name Type Description Default
wrapped Callable

Callable that returns the LLM response

required
instance Chat

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
AsyncGenerator[CompletionEvent, None]

A wrapped AsyncGenerator[CompletionEvent, None] with impacts

Source code in ecologits/tracers/mistralai_tracer.py
async def mistralai_async_chat_wrapper_stream(
    wrapped: Callable,
    instance: Chat,  # noqa: ARG001
    args: Any,
    kwargs: Any,
) -> AsyncGenerator[CompletionEvent, None]:
    """
    Function that wraps a MistralAI answer with computed impacts in streaming and async mode

    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 `AsyncGenerator[CompletionEvent, None]` with impacts
    """
    if not is_tracking_enabled(PROVIDER):
        return await wrapped(*args, **kwargs)
    trackers = get_active_trackers(PROVIDER)
    timer_start = time.perf_counter()
    stream = await wrapped(*args, **kwargs)
    return AsyncImpactsStream(stream, _generator(stream, timer_start, trackers))