跳到主要内容

MLflow 3

MLflow 3 Landing Page

了解下一代 MLflow,它旨在简化您的 AI 实验,并加速您从想法到生产的历程。MLflow 3 为 GenAI 工作流带来了最先进的支持,能够将生成式 AI 模型无缝集成到您的项目中。

什么是 MLflow 3?

MLflow 3 为机器学习模型、AI 应用程序和代理提供了同类最佳的实验跟踪、可观察性和性能评估!使用 MLflow 3,您可以比以往任何时候都更容易地

  • 所有环境中集中跟踪和分析您的模型、提示、代理和 AI 应用程序的性能,从开发笔记本中的交互式查询到生产批处理或实时服务部署。
  • 通过 MLflow 的跟踪和评估功能支持的增强型性能比较体验,为生产选择最佳的模型、提示、代理和 AI 应用程序。

MLflow 3 的新功能有哪些?

以下是 MLflow 3 新功能的简要亮点

🎯 改进的 GenAI 模型跟踪

MLflow 3 引入了专门为 GenAI 应用程序(而不仅仅是模型工件)设计的版本控制机制。新的 LoggedModel 实体充当元数据中心,将每个概念应用程序版本与其特定的外部代码(例如 Git 提交)、配置以及其他 MLflow 实体(如跟踪和评估运行)链接起来。新的版本控制机制同样适用于传统的 ML 模型和深度学习检查点。

🔗 全面的性能跟踪和可观察性

增强的模型跟踪提供了模型、运行、跟踪、提示和评估指标之间的全面谱系。新的以模型为中心的设计允许您汇总来自不同开发环境和生产环境的跟踪和指标,从而实现模型版本之间的丰富比较。

📊 生产级 GenAI 评估

MLflow 的评估和监控功能可帮助您在其生命周期的各个阶段系统地衡量、改进和维护 GenAI 应用程序的质量。从开发到生产,使用相同的质量评分器来确保您的应用程序在管理成本和延迟的同时提供准确、可靠的响应。

👥 人工干预反馈

实际的 GenAI 应用程序需要人工监督。MLflow 3 现在跟踪模型预测的人工注释和反馈,从而实现简化的“人工干预”评估周期。这创造了一个协作环境,数据科学家、领域专家和利益相关者可以有效地共同改进模型质量。(注意:目前在 Databricks Managed MLflow 中可用。开源版本将在未来几个月内发布。)

⚡️ 最先进的提示优化

将提示工程从艺术转化为科学。MLflow Prompt Registry 现在包含基于最先进研究构建的提示优化功能,允许您使用评估反馈和带标签的数据集自动改进提示。这包括版本控制、跟踪和系统化的提示工程工作流。

📚 重新设计的网站和文档

MLflow 文档和网站已完全重新设计,以支持两条主要用户旅程:GenAI 开发和经典机器学习工作流。新结构为 GenAI 功能(包括 LLM、提示工程和跟踪)以及传统的 ML 功能(如实验跟踪、模型注册表、部署和评估)提供了专用部分。

开始使用

通过运行以下命令安装 MLflow 3

pip install 'mlflow>=3.1'

资源:🌐 新网站 | 📖 文档 | 🎉 发行说明

快速入门

先决条件

运行以下命令来安装 MLflow 3 和 OpenAI 程序包。

pip install mlflow openai -U

在 CLI 中设置 OPENAI_API_KEY 环境变量以进行 OpenAI API 身份验证。

export OPENAI_API_KEY=your_api_key_here

本快速入门演示了如何使用 MLflow 3 创建一个具有提示工程的生成式 AI 应用程序并对其进行评估。它重点介绍了 LoggedModel 谱系功能与运行和跟踪的集成,展示了 GenAI 工作流的无缝跟踪和可观察性。

注册提示模板

首先,我们创建一个提示模板并将其注册到 MLflow Prompt Registry

import mlflow

# define a prompt template
prompt_template = """\
You are an expert AI assistant. Answer the user's question with clarity, accuracy, and conciseness.

## Question:
{{question}}

## Guidelines:
- Keep responses factual and to the point.
- If relevant, provide examples or step-by-step instructions.
- If the question is ambiguous, clarify before answering.

Respond below:
"""

# register the prompt
prompt = mlflow.genai.register_prompt(
name="ai_assistant_prompt",
template=prompt_template,
commit_message="Initial version of AI assistant",
)

切换到提示选项卡以查看已注册的提示

The MLflow UI showing a prompt version

向 OpenAI 发出请求

在此步骤中,我们设置一个活动模型来对跟踪进行分组。启用自动日志记录后,在请求期间生成的所有跟踪都将链接到活动模型。

from openai import OpenAI

# set an active model for linking traces, a model named `openai_model` will be created
mlflow.set_active_model(name="openai_model")

# turn on autologging for automatic tracing
mlflow.openai.autolog()

# Initialize OpenAI client
client = OpenAI()

question = "What is MLflow?"
response = (
client.chat.completions.create(
messages=[{"role": "user", "content": prompt.format(question=question)}],
model="gpt-4o-mini",
temperature=0.1,
max_tokens=2000,
)
.choices[0]
.message.content
)

# get the active model id
active_model_id = mlflow.get_active_model_id()
print(f"Current active model id: {active_model_id}")

mlflow.search_traces(model_id=active_model_id)
# trace_id trace ... assessments request_id
# 0 7bb4569d3d884e3e87b1d8752276a13c Trace(trace_id=7bb4569d3d884e3e87b1d8752276a13c) ... [] 7bb4569d3d884e3e87b1d8752276a13c
# [1 rows x 12 columns]

生成的跟踪可以在已记录模型的跟踪选项卡中查看

The MLflow UI showing the logged model traces

使用 GenAI 指标评估响应

最后,我们使用不同的指标评估响应,并将结果记录到一次运行和当前活动模型中。

from mlflow.metrics.genai import answer_correctness, answer_similarity, faithfulness

# ground truth result for evaluation
mlflow_ground_truth = (
"MLflow is an open-source platform for managing "
"the end-to-end machine learning (ML) lifecycle. It was developed by Databricks, "
"a company that specializes in big data and machine learning solutions. MLflow is "
"designed to address the challenges that data scientists and machine learning "
"engineers face when developing, training, and deploying machine learning models."
)

# Define evaluation metrics
metrics = {
"answer_similarity": answer_similarity(model="openai:/gpt-4o"),
"answer_correctness": answer_correctness(model="openai:/gpt-4o"),
"faithfulness": faithfulness(model="openai:/gpt-4o"),
}
# Calculate metrics based on the input, response and ground truth
# The evaluation metrics are callables that can be invoked directly
answer_similarity_score = metrics["answer_similarity"](
predictions=response, inputs=question, targets=mlflow_ground_truth
).scores[0]
answer_correctness_score = metrics["answer_correctness"](
predictions=response, inputs=question, targets=mlflow_ground_truth
).scores[0]
faithfulness_score = metrics["faithfulness"](
predictions=response, inputs=question, context=mlflow_ground_truth
).scores[0]

# Start a run to represent the evaluation process
with mlflow.start_run() as run:
# Log metrics and pass model_id to link the metrics
mlflow.log_metrics(
{
"answer_similarity": answer_similarity_score,
"answer_correctness": answer_correctness_score,
"faithfulness": faithfulness_score,
},
model_id=active_model_id,
)

导航到实验的模型选项卡,以查看新创建的 LoggedModel。评估指标、模型 ID、源运行、参数和其他详细信息显示在模型详细信息页面上,提供了模型性能和谱系的全面概述。

The MLflow UI showing the models tab

The MLflow UI showing the logged model

点击 source_run 会带您进入具有所有指标的评估运行页面

The MLflow UI showing the run and logged model

MLflow 3 演示

探索下面的示例,了解 MLflow 3 的强大功能如何在不同领域得到应用。

迁移指南

MLflow 3 引入了一些关键的 API 更改,同时也移除了一些过时的功能。本指南将帮助您顺利过渡到最新版本。

主要更改

MLflow 2.xMLflow 3
log_model API 用法

记录模型时传递 artifact_path

  with mlflow.start_run():
mlflow.pyfunc.log_model(
artifact_path="model",
python_model=python_model,
...
)

记录模型时传递名称。这将允许您稍后使用此名称搜索 LoggedModels,artifact_path 参数已被弃用。

注意

MLflow 不再要求在记录模型之前启动 Run,因为 Models 已成为 MLflow 3 中的首等实体。您可以直接调用 log_model API,而无需 mlflow.start_run() 上下文管理器来记录模型。

mlflow.pyfunc.log_model(
name="model",
python_model=python_model,
...
)
模型工件存储位置模型工件存储为运行工件。模型工件存储在模型工件位置。注意:这会影响 list_artifacts API 的行为。

已移除的功能

  • MLflow Recipes
  • Flavors:以下模型 Flavors 不再受支持
    • fastai
    • mleap
  • AI Gateway 客户端 API:请改用 deployments API

重大更改

有关 MLflow 3 中重大更改的完整列表,请参阅此页面

与 MLflow 2.x 的兼容性

我们强烈建议您升级客户端和服务器到 MLflow 3.x 以获得最佳体验。客户端和服务器版本之间的不匹配可能导致意外行为。