跳到主要内容

Tracing Quickstart

本快速入门指南将引导您完成使用 MLflow Tracing 设置简单的 GenAI 应用程序。您将在不到 10 分钟的时间内启用跟踪、运行基本应用程序,并在 MLflow UI 中探索生成的跟踪。

先决条件

请确保您已启动 MLflow 服务器。如果您尚未运行 MLflow 服务器,请按照以下简单步骤进行启动。

Python 环境: Python 3.10+

为了最快的设置,您可以通过 pip 安装 mlflow Python 包并本地启动 MLflow 服务器。

bash
pip install --upgrade mlflow
mlflow server

创建 MLflow 实验

您的 GenAI 应用程序将发送到 MLflow 服务器的追踪会被分组到 MLflow 实验中。我们建议为每个 GenAI 应用程序创建一个实验。

让我们使用 MLflow UI 创建一个新的 MLflow 实验,以便您可以开始发送您的追踪。

New Experiment
  1. 在浏览器中导航到 MLflow UI,地址为 https://:5000
  2. 点击右上角的
    Create
    按钮。
  3. 输入实验名称,然后点击“Create”。

您可以暂时将 Artifact Location 字段留空。这是一个高级配置,用于覆盖 MLflow 存储实验数据的位置。

依赖项

要将您的 GenAI 应用程序连接到 MLflow 服务器,您需要安装 MLflow 客户端 SDK。

bash
pip install --upgrade mlflow openai>=1.0.0
信息

虽然本指南以使用 OpenAI SDK 的示例为特色,但相同的步骤也适用于其他 LLM 提供商,包括 Anthropic、Google、Bedrock 等。

有关 MLflow 支持的 LLM 提供商的完整列表,请参阅 LLM 集成概述

开始追踪

创建实验后,您就可以连接到 MLflow 服务器并开始从您的 GenAI 应用程序发送追踪了。

python
import mlflow
from openai import OpenAI

# Specify the tracking URI for the MLflow server.
mlflow.set_tracking_uri("https://:5000")

# Specify the experiment you just created for your GenAI application.
mlflow.set_experiment("My Application")

# Enable automatic tracing for all OpenAI API calls.
mlflow.openai.autolog()

client = OpenAI()
# The trace of the following is sent to the MLflow server.
client.chat.completions.create(
model="o4-mini",
messages=[
{"role": "system", "content": "You are a helpful weather assistant."},
{"role": "user", "content": "What's the weather like in Seattle?"},
],
)

在 MLflow UI 上查看您的追踪

运行上述代码后,转到 MLflow UI,选择“My Application”实验,然后选择“Traces”选项卡。它应该显示新创建的追踪。

Single Trace
Single Trace

下一步

恭喜您使用 MLflow 发送了您的第一个追踪!既然您已经掌握了基础知识,以下是加深对追踪理解的推荐下一步。