评估数据集
使用结构化评估数据转换您的 GenAI 测试
评估数据集是系统性 GenAI 应用测试的基础。它们提供了一种集中管理测试数据、事实依据期望和评估结果的方式,使您能够自信地衡量和改进 AI 应用的质量。
快速入门:构建您的第一个评估数据集
创建评估数据集有多种方法,每种方法都适用于 GenAI 开发过程的不同阶段。期望是有效评估的基石—它们定义了 AI 输出的测量依据,从而能够在迭代过程中进行系统性的质量评估。
- 从跟踪构建
- 从字典构建
- 从 DataFrame 构建
import mlflow
from mlflow.genai.datasets import create_dataset
# Create your evaluation dataset
dataset = create_dataset(
name="production_validation_set",
experiment_id=["0"], # "0" is the default experiment
tags={"team": "ml-platform", "stage": "validation"},
)
# First, retrieve traces that will become the basis of the dataset
# Request list format to work with individual Trace objects
traces = mlflow.search_traces(
experiment_ids=["0"],
max_results=50,
filter_string="attributes.name = 'chat_completion'",
return_type="list", # Returns list[Trace] for direct manipulation
)
# Add expectations to the traces
for trace in traces[:20]:
# Expectations can be structured metrics
mlflow.log_expectation(
trace_id=trace.info.trace_id,
name="output_quality",
value={"relevance": 0.95, "accuracy": 1.0, "contains_citation": True},
)
# They can also be specific expected text
mlflow.log_expectation(
trace_id=trace.info.trace_id,
name="expected_answer",
value="The correct answer should include step-by-step instructions for password reset with email verification",
)
# Retrieve the traces with added expectations
annotated_traces = mlflow.search_traces(
experiment_ids=["0"], max_results=100, return_type="list" # Get list[Trace] objects
)
# Merge the list of Trace objects directly into your dataset
dataset.merge_records(annotated_traces)
import mlflow
from mlflow.genai.datasets import create_dataset
# Create dataset with manual test cases
dataset = create_dataset(
name="regression_test_suite",
experiment_id=["0", "1"], # Multiple experiments
tags={"type": "regression", "priority": "critical"},
)
# Define test cases with expected outputs
test_cases = [
{
"inputs": {
"question": "How do I reset my password?",
"context": "user_support",
},
"expectations": {
"answer": "To reset your password, click 'Forgot Password' on the login page, enter your email, and follow the link sent to your inbox",
"contains_steps": True,
"tone": "helpful",
"max_response_time": 2.0,
},
},
{
"inputs": {
"question": "What are your refund policies?",
"context": "customer_service",
},
"expectations": {
"includes_timeframe": True,
"mentions_exceptions": True,
"accuracy": 1.0,
},
},
]
dataset.merge_records(test_cases)
import pandas as pd
import mlflow
from mlflow.genai.datasets import create_dataset
# Create dataset from existing test data
dataset = create_dataset(
name="benchmark_dataset",
experiment_id=["0"], # Use your experiment ID
tags={"source": "benchmark", "version": "2024.1"},
)
# Method 1: Use traces from search_traces (default returns DataFrame)
# search_traces returns a pandas DataFrame by default when pandas is installed
traces_df = mlflow.search_traces(
experiment_ids=["0"], # Search in your experiment
max_results=100
# No return_type specified - defaults to "pandas"
)
# The DataFrame from search_traces can be passed directly
dataset.merge_records(traces_df)
# Method 2: Create your own DataFrame with inputs and expectations
# You can also create a DataFrame with the expected structure
custom_df = pd.DataFrame(
{
"inputs.question": [
"What is MLflow?",
"How do I track experiments?",
"Explain model versioning",
],
"inputs.domain": ["general", "technical", "technical"],
"expectations.relevance": [1.0, 0.95, 0.9],
"expectations.technical_accuracy": [1.0, 1.0, 0.95],
"expectations.includes_examples": [True, True, False],
"tags.priority": ["high", "medium", "medium"], # Optional tags
"tags.reviewed": [True, True, False],
}
)
# merge_records accepts DataFrames with inputs, expectations, and tags columns
dataset.merge_records(custom_df)
为什么需要评估数据集?
集中式测试管理
将所有测试用例、预期输出和评估标准集中存储在一处。告别分散的 CSV 文件或硬编码的测试数据。
一致的评估来源
维护可随着项目发展而重复使用的具体测试数据表示。消除手动测试,避免为每次迭代反复组装评估数据。
系统化测试
从临时测试转向系统化评估。定义明确的期望,并在不同部署之间一致地衡量性能。
协作改进
让您的整个团队都能贡献测试用例和期望。跨项目和团队共享评估数据集。
评估循环
评估数据集弥合了 GenAI 开发生命周期中跟踪生成和评估执行之间的关键差距。当您测试应用程序并捕获带有期望的跟踪时,评估数据集将这些单独的测试用例转化为具体、可重用的评估套件。这会创建一个一致且不断发展的评估记录集合,随着您的应用程序的增长而扩展—每次迭代都会添加新的测试用例,同时保留历史测试覆盖率。您将构建一个全面的评估资产,可以立即评估实现中的更改和改进的质量,而不是在每个开发周期后丢失有价值的测试场景。
评估循环
主要特性
事实依据管理
定义并维护测试用例的预期输出。捕获关于 AI 系统正确行为的专家知识。
模式演进
自动跟踪测试数据结构随时间的演变。在不破坏现有评估的情况下添加新字段和测试维度。
增量更新
通过添加生产中的新案例,持续改进您的测试套件。随着您对正确行为理解的演变而更新期望。
灵活标记
使用标签组织数据集,以便于发现和过滤。跟踪元数据,如数据源、注释指南和质量级别。
性能跟踪
监控您的应用程序随时间在相同测试数据上的性能。识别不同部署中的回归和改进。
实验集成
将数据集链接到 MLflow 实验以实现完整的追溯性。了解每个模型评估使用了哪些测试数据。
核心概念
数据集结构
了解评估数据集如何组织测试输入、期望和元数据
SDK 指南
关于以编程方式创建和管理评估数据集的完整指南
评估集成
了解如何将数据集与 MLflow 的评估框架结合使用
后续步骤
准备好改进您的 GenAI 测试了吗?从这些资源开始