你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。

使用 Microsoft Foundry SDK 评估现有数据集

通过定义架构、将字段映射到评估程序以及启动云评估运行,评估 JSONL 或 CSV 数据中的预计算响应。

先决条件

这些示例使用在 设置 SDK 客户端中配置的 SDK 客户端。

准备输入数据

大多数评估方案都需要输入数据。 可以通过两种方式提供数据:

提示

如果你没有人工精心整理的数据集,可以先快速构建一个。 在预启动或流量不足时使用 “生成综合评估数据集 ”,或者 将代理跟踪转换为评估数据集 ,以从实际生产流量生成数据集。

上传 JSONL 或 CSV 文件,以在 Foundry 项目中创建版本控制数据集。 数据集支持在多个评估运行中进行版本管理和重复使用。 使用此方法进行生产测试和 CI/CD 工作流。

准备一个 JSONL 文件,每行包含一个 JSON 对象,其中包含评估人员需要的字段。

{"query": "What is machine learning?", "response": "Machine learning is a subset of AI.", "ground_truth": "Machine learning is a type of AI that learns from data."}
{"query": "Explain neural networks.", "response": "Neural networks are computing systems inspired by biological neural networks.", "ground_truth": "Neural networks are a set of algorithms modeled after the human brain."}

或者准备一个 CSV 文件,其中的列标题与您的评估器字段相匹配:

query,response,ground_truth
What is machine learning?,Machine learning is a subset of AI.,Machine learning is a type of AI that learns from data.
Explain neural networks.,Neural networks are computing systems inspired by biological neural networks.,Neural networks are a set of algorithms modeled after the human brain.
# Upload a local JSONL file. Skip this step if you already have a dataset registered.
data_id = project_client.datasets.upload_file(
    name=dataset_name,
    version=dataset_version,
    file_path="./evaluate_test_data.jsonl",
).id

提供内联数据

若要快速试验小型预计算数据集,请使用 file_content 在评估请求中直接提供数据。

source = SourceFileContent(
    type="file_content",
    content=[
        SourceFileContentContent(
            item={
                "query": "How can I safely de-escalate a tense situation?",
                "response": "Encourage calm communication, seek help if needed, and avoid harm.",
                "ground_truth": "Encourage calm communication, seek help if needed, and avoid harm.",
            }
        ),
        SourceFileContentContent(
            item={
                "query": "What is the largest city in France?",
                "response": "Paris",
                "ground_truth": "Paris",
            }
        ),
    ],
)

在创建运行时,将 source 作为数据源配置中的 "source" 字段传递。 默认情况下,以下数据集部分使用 file_id 。

按数据集格式划分的源类型支持情况

两种数据集格式都支持文件和内联源。

数据集格式 file_id file_content
数据集 (jsonl) 是的 是的
CSV (csv) 是的 是的

评估 JSONL 数据集

使用 jsonl 数据源类型评估 JSONL 文件中的预计算响应。 如果已有模型输出并想要评估其质量,则此方案非常有用。

提示

在开始之前,请完成 客户端设置 和 准备输入数据。

定义数据架构和评估器

指定与 JSONL 字段匹配的架构,然后选择要运行的计算器(测试条件)。 使用 {{item.field}} 语法,通过 data_mapping 将评估器输入项连接到数据集中的字段。 包含每个评估器所需的输入,即使在数据集使用标准字段名称(例如 query、response 和 ground_truth)的情况下也是如此。 有关每个计算器所需的输入,请参阅 内置计算器。

data_source_config = DataSourceConfigCustom(
    type="custom",
    item_schema={
        "type": "object",
        "properties": {
            "query": {"type": "string"},
            "response": {"type": "string"},
            "ground_truth": {"type": "string"},
        },
        "required": ["query", "response", "ground_truth"],
    },
)

testing_criteria = [
    TestingCriterionAzureAIEvaluator(
        type="azure_ai_evaluator",
        name="coherence",
        evaluator_name="builtin.coherence",
        initialization_parameters={"model": model_deployment_name},
        data_mapping={
            "query": "{{item.query}}",
            "response": "{{item.response}}",
        },
    ),
    TestingCriterionAzureAIEvaluator(
        type="azure_ai_evaluator",
        name="violence",
        evaluator_name="builtin.violence",
        data_mapping={
            "query": "{{item.query}}",
            "response": "{{item.response}}",
        },
    ),
]

创建评估并运行

创建评估,然后针对您上传的数据集启动一次运行。 该运行对数据集的每一行执行每个评估器。

# Create the evaluation
eval_object = openai_client.evals.create(
    name="dataset-evaluation",
    data_source_config=data_source_config,
    testing_criteria=testing_criteria,
)

# Create a run using the uploaded dataset
eval_run = openai_client.evals.runs.create(
    eval_id=eval_object.id,
    name="dataset-run",
    data_source=CreateEvalJSONLRunDataSourceParam(
        type="jsonl",
        source=SourceFileID(
            type="file_id",
            id=data_id,
        ),
    ),
)

有关完整的可运行示例,请参阅 GitHub 上的 sample_evaluations_builtin_with_dataset_id.py 。 若要轮询完成结果和解释结果,请参阅获取云评估结果。

评估 CSV 数据集

使用 csv 数据源类型评估 CSV 文件中的预计算响应。 此方案的工作方式与 数据集评估 相同,但接受 CSV 文件而不是 JSONL。 如果数据已采用电子表格或表格格式,请使用 CSV。

提示

在开始之前,请完成 客户端设置 和 准备输入数据。

准备 CSV 文件

创建一个 CSV 文件,其中包含与计算器所需的字段匹配的列标题。 每行表示一个测试用例。

query,response,context,ground_truth
What is cloud computing?,Cloud computing delivers computing services over the internet.,Cloud computing is a technology for on-demand resource delivery.,Cloud computing is the delivery of computing services including servers storage and databases over the internet.
What is machine learning?,Machine learning is a subset of AI that learns from data.,Machine learning is a branch of artificial intelligence.,Machine learning is a type of AI that enables computers to learn from data without being explicitly programmed.
Explain neural networks.,Neural networks are computing systems inspired by biological neural networks.,Neural networks are used in deep learning.,Neural networks are a set of algorithms modeled after the human brain designed to recognize patterns.

上传并运行

将 CSV 文件作为数据集上传。 然后,使用 csv 数据源类型创建评估。 架构定义和计算器配置与 JSONL 评估相同。 唯一的区别在于数据源中的 "type": "csv"。

# Upload the CSV file
data_id = project_client.datasets.upload_file(
    name="eval-csv-data",
    version="1",
    file_path="./evaluation_data.csv",
).id

# Define the schema matching your CSV columns
data_source_config = DataSourceConfigCustom(
    type="custom",
    item_schema={
        "type": "object",
        "properties": {
            "query": {"type": "string"},
            "response": {"type": "string"},
            "context": {"type": "string"},
            "ground_truth": {"type": "string"},
        },
        "required": [],
    },
    include_sample_schema=True,
)

# Define evaluators that use the standard CSV columns
testing_criteria = [
    TestingCriterionAzureAIEvaluator(
        type="azure_ai_evaluator",
        name="coherence",
        evaluator_name="builtin.coherence",
        initialization_parameters={"model": model_deployment_name},
        data_mapping={
            "query": "{{item.query}}",
            "response": "{{item.response}}",
        },
    ),
    TestingCriterionAzureAIEvaluator(
        type="azure_ai_evaluator",
        name="violence",
        evaluator_name="builtin.violence",
        data_mapping={
            "query": "{{item.query}}",
            "response": "{{item.response}}",
        },
    ),
]

# Create the evaluation
eval_object = openai_client.evals.create(
    name="CSV evaluation with built-in evaluators",
    data_source_config=data_source_config,
    testing_criteria=testing_criteria,
)

# Create a run using the CSV data source type
eval_run = openai_client.evals.runs.create(
    eval_id=eval_object.id,
    name="csv-evaluation-run",
    data_source={
        "type": "csv",
        "source": {
            "type": "file_id",
            "id": data_id,
        },
    },
)

后续步骤