生成 MLflow 评估数据集

若要系统地测试和改进 GenAI 应用程序,请使用评估数据集。 评估数据集是一组选定的示例输入(已标记(具有已知预期输出)或未标记(无实数答案)。 评估数据集可通过以下方式帮助你提高应用的性能:

  • 通过在生产中对已知有问题的示例进行修复方案测试来提高质量。
  • 防止回归。 创建一组必须始终正常工作的示例“黄金集”。
  • 比较应用版本。 针对相同的数据测试不同的提示、模型或应用逻辑。
  • 以特定功能为目标。 为安全、域知识或边缘案例构建专用数据集。
  • 在 LLMOps 中跨不同环境验证应用。

MLflow 评估数据集存储在 Unity 目录中,该目录提供内置版本控制、世系、共享和治理。

要求

若要创建评估数据集,必须具有对 CREATE TABLE Unity 目录架构的权限。

如何生成评估数据集

可通过 3 种方法创建评估数据集:

本页介绍如何创建 MLflow 评估数据集。 还可以使用其他类型的数据集(例如 Pandas 数据帧或字典列表)快速开始。 请参阅 GenAI 的 MLflow 评估示例

步骤 1:创建数据集

第一步是创建 MLflow 托管的评估数据集。 MLflow 托管的评估数据集会随时间跟踪更改,并维护与单个评估结果的链接。

使用 UI

按照以下录音操作,使用用户界面创建评估数据集

使用 UI 创建评估数据集

使用 SDK

通过程序搜索踪迹并将它们添加到数据集中,创建评估数据集。

import mlflow
import mlflow.genai.datasets
import time
from databricks.connect import DatabricksSession

# 0. If you are using a local development environment, connect to Serverless Spark which powers MLflow's evaluation dataset service
spark = DatabricksSession.builder.remote(serverless=True).getOrCreate()

# 1. Create an evaluation dataset

# Replace with a Unity Catalog schema where you have CREATE TABLE permission
uc_schema = "workspace.default"
# This table will be created in the above UC schema
evaluation_dataset_table_name = "email_generation_eval"

eval_dataset = mlflow.genai.datasets.create_dataset(
    name=f"{uc_schema}.{evaluation_dataset_table_name}",
)
print(f"Created evaluation dataset: {uc_schema}.{evaluation_dataset_table_name}")

步骤 2:将记录添加到数据集

方法 1:从现有跟踪创建

构建相关评估数据集的最有效方法之一是直接从 MLflow 跟踪捕获的应用程序的历史交互中挑选示例。 您可以使用 MLflow 监控 UI 或 SDK 从跟踪创建数据集。

使用 UI

按照以下记录,使用 UI 将现有生产跟踪添加到数据集中

trace

使用 SDK

使用程序检索跟踪信息,然后将其添加到数据集。 有关如何使用筛选器的详细信息,请参阅search_traces()参考页。 可以使用筛选器来识别成功、失败、生产使用或其他属性的追踪。

import mlflow

# 2. Search for traces
traces = mlflow.search_traces(
    filter_string="attributes.status = 'OK'",
    order_by=["attributes.timestamp_ms DESC"],
    tags.environment = 'production',
    max_results=10
)

print(f"Found {len(traces)} successful traces")

# 3. Add the traces to the evaluation dataset
eval_dataset = eval_dataset.merge_records(traces)
print(f"Added {len(traces)} records to evaluation dataset")

# Preview the dataset
df = eval_dataset.to_df()
print(f"\nDataset preview:")
print(f"Total records: {len(df)}")
print("\nSample record:")
sample = df.iloc[0]
print(f"Inputs: {sample['inputs']}")

方法 2:从域专家标签创建

利用 MLflow 标记会话中捕获的域专家的反馈,使用真实标签增强您的评估数据集。 在执行这些步骤之前,请按照 收集域专家反馈 指南创建标记会话。

import mlflow.genai.labeling as labeling

# Get a labeling sessions
all_sessions = labeling.get_labeling_sessions()
print(f"Found {len(all_sessions)} sessions")

for session in all_sessions:
    print(f"- {session.name} (ID: {session.labeling_session_id})")
    print(f"  Assigned users: {session.assigned_users}")

# Sync from the labeling session to the dataset

all_sessions[0].sync(dataset_name=f"{uc_schema}.{evaluation_dataset_table_name}")

方法 3:从头开始生成或导入现有

可以导入现有数据集或从头开始策划示例。 数据必须匹配(或转换以匹配) 评估数据集架构

# Define comprehensive test cases
evaluation_examples = [
    {
        "inputs": {"question": "What is MLflow?"},
        "expected": {
            "expected_response": "MLflow is an open source platform for managing the end-to-end machine learning lifecycle.",
            "expected_facts": [
                "open source platform",
                "manages ML lifecycle",
                "experiment tracking",
                "model deployment"
            ]
        },
    },
]

eval_dataset = eval_dataset.merge_records(evaluation_examples)

方法 4:使用综合数据设定种子

生成综合数据可以通过快速创建各种输入并覆盖边缘事例来扩展测试工作。 请参阅合成评估集

步骤 3:更新现有数据集

import mlflow.genai.datasets
import pandas as pd

# Load existing dataset
dataset = mlflow.genai.datasets.get_dataset(name="catalog.schema.eval_dataset")

# Add new test cases
new_cases = [
    {
        "inputs": {"question": "What are MLflow models?"},
        "expectations": {
            "expected_facts": ["model packaging", "deployment", "registry"],
            "min_response_length": 100
        }
    }
]

# Merge new cases
dataset = dataset.merge_records(new_cases)

局限性

如果需要为用例放宽上述任何限制,请联系 Databricks 代表。

后续步骤

参考指南