Reach out
← Back to Cookbook

mistral mlflow tracing

Details

File: third_party/MLflow/mistral-mlflow-tracing.ipynb

Type: Jupyter Notebook

Use Cases: Evaluation, RAG

Integrations: Mlflow

Content

Notebook content (JSON format):

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Observability with Mistral AI and MLflow\n",
    "\n",
    "This is an example for leveraging MLflow's auto tracing capabilities for Mistral AI.\n",
    "\n",
    "More information about MLflow Tracing is available [here](https://mlflow.org/docs/latest/llms/tracing/index.html).\n",
    "\n",
    "## Getting Started\n",
    "\n",
    "Install `mistralai` and `mlflow` (current versions as of 4-Feb-2025)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install mistralai==1.5.0\n",
    "!pip install mlflow==2.20.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from mistralai import Mistral\n",
    "\n",
    "import mlflow\n",
    "\n",
    "# Turn on auto tracing for Mistral AI by calling mlflow.mistral.autolog()\n",
    "mlflow.mistral.autolog()\n",
    "\n",
    "# Configure your API key.\n",
    "client = Mistral(api_key=os.environ[\"MISTRAL_API_KEY\"])\n",
    "\n",
    "# Use the chat complete method to create new chat.\n",
    "chat_response = client.chat.complete(\n",
    "    model=\"mistral-small-latest\",\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"Who is the best French painter? Answer in one short sentence.\",\n",
    "        },\n",
    "    ],\n",
    ")\n",
    "print(chat_response.choices[0].message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tracing\n",
    "\n",
    "To see the MLflow tracing, open the MLflow UI in the same directory and the same virtualenv where you run this notebook. \n",
    "\n",
    "### Launch the UI\n",
    "\n",
    "Open a terminal and run this command:\n",
    "\n",
    "`mlflow ui`\n",
    "\n",
    "![Screenshot of launching the MLflow UI via command line](mlflow-ui-launch.png)\n",
    "\n",
    "### View the traces in the browser\n",
    "\n",
    "Open your browser and connect to the MLflow UI port (default: http://localhost:5000)\n",
    "\n",
    "![GIF animation of browsing a trace in the MLflow UI](mlflow-tracing-chat-complete.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}