评估数据集
利用结构化评估数据转换您的 GenAI 测试体验
评估数据集是系统化 GenAI 应用测试的基础。它们提供了一种集中管理测试数据、基本事实期望和评估结果的方法,使您能够自信地衡量和提高 AI 应用的质量。
评估数据集需要一个带有 **SQL 后端**(PostgreSQL、MySQL、SQLite 或 MSSQL)的 MLflow Tracking Server。此功能在 FileStore(基于本地文件系统的跟踪)中**不可用**。如果需要简单的本地 MLflow 配置,请在启动 MLflow 时使用 sqlite 选项。
快速入门:构建您的第一个评估数据集
创建评估数据集有几种方法,每种方法都适用于 GenAI 开发过程的不同阶段。
创建评估数据集最简单的方法是通过 MLflow UI。导航到要关联评估数据集的实验,然后通过提供唯一的名称直接创建新数据集。添加记录后,您可以在 UI 中查看数据集的条目。
从核心上讲,评估数据集由 **输入** 和 **期望** 组成。 **输出** 是可选的附加项,可以添加到评估数据集中,以便使用评分器进行事后评估。添加这些元素可以直接从跟踪记录(traces)、字典或 Pandas DataFrame 完成。
- 从跟踪记录构建
- 从字典构建
- 从 DataFrame 构建
import mlflow
from mlflow.genai.datasets import create_dataset, set_dataset_tags
# 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"},
)
# Optionally, add additional tags to your dataset.
# Tags can be used to search for datasets with search_datasets API
set_dataset_tags(
dataset_id=dataset.dataset_id,
tags={"environment": "dev", "validation_version": "1.3"},
)
# First, retrieve traces that will become the basis of the dataset
traces = mlflow.search_traces(
experiment_ids=["0"],
max_results=20,
filter_string="attributes.name = 'chat_completion'",
return_type="list", # Returns list[Trace]
)
# Add expectations to the traces
for trace in traces:
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=20,
return_type="list",
)
# 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 (ground truth)
test_cases = [
{
"inputs": {
"question": "How do I reset my password?",
"context": "user_support",
},
"expectations": {
"expected_answer": (
"To reset your password, click 'Forgot Password' on the login page, "
"enter your email, and follow the link sent to your inbox"
),
"must_contain_steps": True,
"expected_tone": "helpful",
},
},
{
"inputs": {
"question": "What are your refund policies?",
"context": "customer_service",
},
"expectations": {
"expected_answer": (
"We offer full refunds within 30 days of purchase. "
"Refunds after 30 days are subject to approval."
),
"must_include_timeframe": True,
"must_mention_exceptions": True,
},
},
]
dataset.merge_records(test_cases)
import pandas as pd
from mlflow.genai.datasets import create_dataset
# Create dataset
dataset = create_dataset(
name="benchmark_dataset",
experiment_id=["0"],
tags={"source": "benchmark", "version": "2024.1"},
)
# Create DataFrame with inputs and expectations (ground truth)
df = pd.DataFrame(
[
{
"inputs": {
"question": "What is MLflow?",
"domain": "general",
},
"expectations": {
"expected_answer": "MLflow is an open-source platform for ML",
"must_mention": ["tracking", "experiments", "models"],
},
},
{
"inputs": {
"question": "How do I track experiments?",
"domain": "technical",
},
"expectations": {
"expected_answer": "Use mlflow.start_run() and mlflow.log_params()",
"must_mention": ["log_params", "log_metrics"],
},
},
{
"inputs": {
"question": "Explain model versioning",
"domain": "technical",
},
"expectations": {
"expected_answer": "Model Registry provides versioning",
"must_mention": ["Model Registry", "versions"],
},
},
]
)
# Add records from DataFrame
dataset.merge_records(df)
了解来源类型
评估数据集中的每条记录都有一个 **来源类型**,用于跟踪其出处。这使您能够按数据来源分析模型性能,并了解哪些类型的测试数据最有价值。
TRACE(跟踪记录)
来自生产跟踪记录——通过 mlflow.search_traces() 添加跟踪记录时自动分配
HUMAN
主题专家注释——对于带有期望(基本事实)的记录自动推断
CODE
程序化生成的测试用例——对于没有期望的记录自动推断
DOCUMENT(文档)
从文档或规范中提取的测试用例——必须使用来源元数据明确指定
来源类型是根据记录特征自动推断的,但在需要时可以明确覆盖。有关详细的推断规则和示例,请参阅 SDK 指南。
为什么要使用评估数据集?
集中化测试管理
将所有测试用例、预期输出和评估标准存储在一个地方。告别分散的 CSV 文件或硬编码的测试数据。
一致的评估来源
维护测试数据的具体表示形式,随着项目的演进而可以重复使用。消除手动测试,避免为每次迭代重复组装评估数据。
系统化测试
超越即席测试,实现系统化评估。定义明确的期望,并跨部署保持一致地衡量性能。
协作改进
让您的整个团队贡献测试用例和期望。跨项目和团队共享评估数据集。
评估循环
评估数据集弥合了 GenAI 开发生命周期中跟踪生成与评估执行之间的关键鸿沟。当您测试应用程序并捕获带有期望的跟踪记录时,**评估数据集会将这些单个测试用例转换为物化的、可重用的评估套件**。这创建了一个一致的、不断发展的评估记录集合,随您的应用程序一起增长——每次迭代都会添加新的测试用例,同时保留历史测试覆盖率。与其在每个开发周期后丢失有价值的测试场景,不如构建一个全面的评估资产,它可以立即评估实现更改和改进的质量。
评估循环
主要特性
基本事实管理
定义并维护测试用例的预期输出。捕获有关什么是构成 AI 系统正确行为的专家知识。
Schema 演变
自动跟踪测试数据的结构如何演变。添加新字段和测试维度,而不会破坏现有评估。
增量更新
通过从生产中添加新案例,持续改进您的测试套件。随着您对正确行为理解的演变而更新期望。
灵活的标签
使用标签组织数据集,以便轻松发现和过滤。跟踪元数据,如数据来源、注释指南和质量级别。
性能跟踪
监控您的应用程序随时间在相同测试数据上的表现。识别跨部署的回归和改进。
实验集成
将数据集链接到 MLflow 实验,以实现完整的可追溯性。了解在每次模型评估中使用了哪些测试数据。
后续步骤
准备好改进您的 GenAI 测试了吗?从这些资源开始