跳到主要内容

GenAI 应用的版本跟踪

MLflow 的 LoggedModel 为您的整个 GenAI 应用提供系统化的版本控制——代码、配置、评估和追踪。停止迷失在“哪个版本有效”的问题中,通过完整的应用生命周期管理建立信心进行构建。

MLflow UI showing LoggedModel with linked traces for version tracking

为什么版本控制对 GenAI 至关重要

GenAI 应用是具有相互依赖组件的复杂系统。没有系统化的版本控制,开发会变得混乱,部署也会带来风险。

消除“它昨天还能用”的综合症

确切了解是哪种代码、提示和配置的组合产生了任何结果。在完整的上下文中重现成功并调试失败。

带着数据驱动的信心进行部署

使用质量得分、成本和延迟等指标客观比较应用版本。根据证据而非直觉选择性能最佳的版本。

跟踪每次更改的影响

关联代码提交、配置更改和评估结果。当质量下降时,精确查出是什么以及何时发生了更改。

保持生产环境的可审计性

确切知道何时部署了哪个版本。这对于合规性、事件响应和监管要求至关重要。

LoggedModel 如何支持 GenAI 版本控制

MLflow 的 LoggedModel 调整了传统的 ML 模型版本控制以适应 GenAI 应用。它不再仅仅跟踪模型权重,而是成为一个协调 AI 系统所有活动部件的综合元数据中心。

应用状态快照

每个 LoggedModel 版本捕获一个完整的应用状态——代码引用、配置、依赖项和性能数据都包含在一个版本化的实体中。

灵活的代码管理

链接到外部 git 提交以实现轻量级版本控制,或直接打包代码以进行部署。选择适合您工作流程的方法。

自动追踪关联

当您设置活动模型上下文时,所有后续追踪都会自动链接到该版本。无需手动记账。

在 5 分钟内开始版本跟踪

只需几行代码,即可将混乱的 GenAI 开发转变为系统化的版本控制。

通过 Git 集成进行自动版本跟踪

将您的应用版本与 git 提交关联,以实现完整的可追溯性

python
import mlflow
import openai
import os

# Fix: Added missing import
os.environ["OPENAI_API_KEY"] = "your-api-key-here"

# Configure MLflow experiment
mlflow.set_experiment("customer-support-agent")

# Get current git commit using MLflow's built-in utilities
from mlflow.utils.git_utils import get_git_commit

git_commit = get_git_commit(".")
if git_commit:
git_commit = git_commit[:8] # Use short hash
else:
git_commit = "local-dev" # Fallback if not in git repo

# Create version identifier
app_name = "customer_support_agent"
version_name = f"{app_name}-{git_commit}"

# Set active model context - all traces will link to this version
mlflow.set_active_model(name=version_name)

# Enable automatic tracing
mlflow.openai.autolog()

# Your application code - now automatically versioned and traced
client = openai.OpenAI()
test_questions = [
"How do I reset my password?",
"What are your business hours?",
"Can I get a refund for my order?",
]

for question in test_questions:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": question}],
temperature=0.7,
max_tokens=1000,
)
# ✅ Automatically: traced, versioned, and linked to git commit

自动发生的事情

  • 每次 LLM 调用都会生成详细的追踪
  • 所有追踪都链接到您的特定应用版本
  • Git 提交提供了精确的代码可重现性
  • 可以客观地比较版本性能

版本管理变得简单

python
# Create a new version for experimentation
with mlflow.set_active_model(name=f"agent-v2-{new_commit}"):
# Test new prompt engineering approach
improved_response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": "You are a helpful customer support agent. Be concise and actionable.",
},
{"role": "user", "content": question},
],
temperature=0.3, # Lower temperature for consistency
max_tokens=500, # More focused responses
)
# ✅ New version automatically tracked with different configurations

上下文管理器自动处理版本切换——干净、明确且无错误。

系统地比较版本

python
import pandas as pd

# Evaluate multiple versions against the same test set
eval_data = pd.DataFrame(
{
"inputs": test_questions,
"expected_categories": ["account", "business_info", "billing"],
}
)

# Version A: Original configuration
results_v1 = mlflow.evaluate(
model_uri=f"models:/{app_name}-{commit_v1}",
data=eval_data,
extra_metrics=[
mlflow.metrics.toxicity(),
mlflow.metrics.latency(),
mlflow.metrics.flesch_kincaid_grade_level(),
],
)

# Version B: Improved prompts
results_v2 = mlflow.evaluate(
model_uri=f"models:/{app_name}-{commit_v2}",
data=eval_data,
extra_metrics=[
mlflow.metrics.toxicity(),
mlflow.metrics.latency(),
mlflow.metrics.flesch_kincaid_grade_level(),
],
)

# ✅ Side-by-side comparison shows which version performs better

客观指标消除了版本选择中的猜测。

先决条件

准备好实施系统化的版本跟踪了吗?您将需要

  • MLflow 3.0+ (pip install --upgrade 'mlflow[genai]>=3.1')
  • 您的应用代码的 Git 仓库
  • Python 3.10+
  • LLM API 访问 (OpenAI, Anthropic 等)
提示

对于 Databricks 托管的 MLflow 追踪:pip install --upgrade 'mlflow[genai,databricks]>=3.1'


高级版本跟踪功能

掌握了基本的版本跟踪后,请探索以下用于生产 GenAI 应用的高级模式。

从上面的代码示例开始,然后随着应用的复杂性增加,探索高级功能。