Observability with Mistral AI and Maxim

Maxim

Install and Import Required Modules

You need to install mistralai and maxim-py packages from pypy

!pip install mistralai maxim-py

Set the environment variables

You can sign up on Maxim and create a new Api Key from Settings. After that go to Logs section and create a new Log Repository, you will receive a Log Repository Id. Get ready with your Mistral Api Key also.

import os
from google.colab import userdata

MAXIM_API_KEY=userdata.get("MAXIM_API_KEY")
MAXIM_LOG_REPO_ID=userdata.get("MAXIM_REPO_ID")
MISTRAL_API_KEY=userdata.get("MISTRAL_API_KEY")

os.environ["MAXIM_API_KEY"] = MAXIM_API_KEY
os.environ["MAXIM_LOG_REPO_ID"] = MAXIM_LOG_REPO_ID
os.environ["MISTRAL_API_KEY"] = MISTRAL_API_KEY

Initialize logger

Create an instance of Maxim Logger

from maxim import Maxim

logger = Maxim().logger()

Make LLM calls using MaximMistralClient

Make a call to Mistral via Mistral Api Client provided by Maxim, define the model you want to use and list of messages.

from mistralai import Mistral
from maxim.logger.mistral import MaximMistralClient
import os

with MaximMistralClient(Mistral(
    api_key=os.getenv("MISTRAL_API_KEY", ""),
), logger) as mistral:

    res = mistral.chat.complete(
        model="mistral-medium-latest",
        messages=[
            {
                "content": "Who is the best French painter? Answer in one short sentence.",
                "role": "user",
            },
        ]
    )

    # Handle response
    print(res)

To check the logs shared by Mistral SDK with Maxim -

  1. Go to Logs section in Maxim Platform
  2. Go to the respective Log Repository you created.
  3. Switch to Logs from top tab view and analyse the traces received

Gif

Async LLM call

async with MaximMistralClient(Mistral(
    api_key=os.getenv('MISTRAL_API_KEY', ''),
), logger) as mistral:

    response = await mistral.chat.complete_async(
        model='mistral-small-latest',
        messages=[
            {
                'role': 'user',
                'content': 'Explain the difference between async and sync programming in Python in one sentence.'
            }
        ]
    )
    print(response)

Feedback


If you have any feedback or requests, please create a GitHub Issue.