跳到主要内容

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 实体(如追踪和评估运行)关联起来。新的版本控制机制也适用于传统机器学习模型和深度学习检查点。

🔗 全面的性能跟踪与可观测性

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

📊 生产级 GenAI 评估

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

👥 人工干预反馈

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

⚡️ 最先进的提示词优化

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

📚 重新设计的网站和文档

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

开始使用

通过运行以下命令安装 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 提示词注册表中。

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,
...
)

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

注意

MLflow 不再要求在记录模型之前启动 Run,因为模型在 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 网关客户端 API:请改用部署 API

重大变更

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

与 MLflow 2.x 的兼容性

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