追踪 Mistral

MLflow 追踪确保对您与 Mistral AI 模型的交互的可观测性。当通过调用 mlflow.mistral.autolog() 函数启用 Mistral 自动追踪时,Mistral SDK 的使用将自动记录在交互式开发过程中生成的追踪。
MLflow 自动捕获有关 Mistral 调用的以下信息
- 提示和完成响应
- 延迟
- 模型名称
- Token 用量(输入、输出和总 Token 数)
- 附加元数据,例如
temperature、max_tokens(如果指定) - 如果响应中返回函数调用
- 如果抛出任何异常
开始使用
安装依赖项
pip install 'mlflow[genai]' mistralai
启动 MLflow 服务器
- 本地 (pip)
- 本地 (docker)
如果您有本地 Python 环境 >= 3.10,您可以使用 mlflow CLI 命令在本地启动 MLflow 服务器。
mlflow server
MLflow 还提供了一个 Docker Compose 文件,用于启动带有 postgres 数据库和 minio 服务器的本地 MLflow 服务器。
git clone --depth 1 --filter=blob:none --sparse https://github.com/mlflow/mlflow.git
cd mlflow
git sparse-checkout set docker-compose
cd docker-compose
cp .env.dev.example .env
docker compose up -d
有关更多详细信息,请参阅 说明,例如覆盖默认环境变量。
启用跟踪并进行 API 调用
使用 mlflow.mistral.autolog() 启用追踪,并像平常一样进行 API 调用。
import os
from mistralai import Mistral
import mlflow
# Enable auto-tracing for Mistral
mlflow.mistral.autolog()
# Set a tracking URI and an experiment
mlflow.set_tracking_uri("https://:5000")
mlflow.set_experiment("Mistral")
# Configure your API key
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
# Use the chat complete method to create new chat
chat_response = client.chat.complete(
model="mistral-small-latest",
messages=[
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence.",
},
],
)
print(chat_response.choices[0].message)
在 MLflow UI 中查看跟踪
浏览至 MLflow UI,地址为 https://:5000(或您的 MLflow 服务器 URL),您应该能看到 Mistral API 调用的追踪。
支持的 API
MLflow 支持对以下 Mistral API 进行自动追踪
| 聊天 | 函数调用 | 流式传输 | 异步 | 图像 | Embeddings | Agents |
|---|---|---|---|---|---|---|
| ✅ | ✅ | - | ✅ (*1) | - | - | - |
(*1) 异步支持已在 MLflow 3.5.0 中添加。
如需支持更多 API,请在 GitHub 上提交 功能请求。
示例
基本示例
import os
from mistralai import Mistral
import mlflow
# Turn on auto tracing for Mistral AI by calling mlflow.mistral.autolog()
mlflow.mistral.autolog()
# Configure your API key.
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
# Use the chat complete method to create new chat.
chat_response = client.chat.complete(
model="mistral-small-latest",
messages=[
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence.",
},
],
)
print(chat_response.choices[0].message)
Token 用量
MLflow >= 3.2.0 支持 Mistral 的 token 使用量追踪。每次 LLM 调用的 token 使用量将记录在 mlflow.chat.tokenUsage 属性中。整个追踪的总 token 使用量可在追踪信息对象的 token_usage 字段中找到。
import json
import mlflow
mlflow.mistral.autolog()
# Configure your API key.
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
# Use the chat complete method to create new chat.
chat_response = client.chat.complete(
model="mistral-small-latest",
messages=[
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence.",
},
],
)
# Get the trace object just created
last_trace_id = mlflow.get_last_active_trace_id()
trace = mlflow.get_trace(trace_id=last_trace_id)
# Print the token usage
total_usage = trace.info.token_usage
print("== Total token usage: ==")
print(f" Input tokens: {total_usage['input_tokens']}")
print(f" Output tokens: {total_usage['output_tokens']}")
print(f" Total tokens: {total_usage['total_tokens']}")
# Print the token usage for each LLM call
print("\n== Detailed usage for each LLM call: ==")
for span in trace.data.spans:
if usage := span.get_attribute("mlflow.chat.tokenUsage"):
print(f"{span.name}:")
print(f" Input tokens: {usage['input_tokens']}")
print(f" Output tokens: {usage['output_tokens']}")
print(f" Total tokens: {usage['total_tokens']}")
== Total token usage: ==
Input tokens: 16
Output tokens: 25
Total tokens: 41
== Detailed usage for each LLM call: ==
Chat.complete:
Input tokens: 16
Output tokens: 25
Total tokens: 41
禁用自动跟踪
可以通过调用 mlflow.mistral.autolog(disable=True) 或 mlflow.autolog(disable=True) 全局禁用 Mistral 的自动追踪。