通过定义架构、将字段映射到评估程序以及启动云评估运行,评估 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
// Upload a local JSONL file. Skip this step if you already have a dataset registered.
FileDataset dataset = await projectClient.Datasets.UploadFileAsync(
name: datasetName,
version: datasetVersion,
filePath: "./evaluate_test_data.jsonl");
string dataId = dataset.Id;
参考:AIProjectDatasetsOperations.UploadFileAsync
// Upload a local JSONL file. Skip this step if you already have a
// dataset registered.
const dataset = await projectClient.datasets.uploadFile(
datasetName,
datasetVersion,
"./evaluate_test_data.jsonl",
);
const dataId = dataset.id;
参考: datasets.uploadFile
cURL 示例使用现有的数据集 ID 或内联内容。 使用Python或 JavaScript/TypeScript 选项卡上传本地文件,然后在 cURL 请求中提供其数据集 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",
}
),
],
)
object source = new
{
type = "file_content",
content = new[]
{
new
{
item = new
{
query = "How can I safely de-escalate a tense situation?",
ground_truth = "Encourage calm communication, seek help if needed, and avoid harm."
}
},
new
{
item = new
{
query = "What is the largest city in France?",
ground_truth = "Paris"
}
}
}
};
const source = {
type: "file_content",
content: [
{
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.",
},
},
{
item: {
query: "What is the largest city in France?",
response: "Paris",
ground_truth: "Paris",
},
},
],
};
无需发送单独的请求。 将 file_content 对象直接包含在相应的 创建评估并运行 部分中所示的 cURL 请求体中。
在创建运行时,将 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}}",
},
),
]
object dataSourceConfig = new
{
type = "custom",
item_schema = new
{
type = "object",
properties = new
{
query = new { type = "string" },
response = new { type = "string" },
ground_truth = new { type = "string" }
},
required = new[] { "query", "response", "ground_truth" }
}
};
object[] testingCriteria =
[
new
{
type = "azure_ai_evaluator",
name = "coherence",
evaluator_name = "builtin.coherence",
initialization_parameters = new { model = modelDeploymentName },
data_mapping = new
{
query = "{{item.query}}",
response = "{{item.response}}"
}
},
new
{
type = "azure_ai_evaluator",
name = "violence",
evaluator_name = "builtin.violence",
data_mapping = new
{
query = "{{item.query}}",
response = "{{item.response}}"
}
},
new
{
type = "azure_ai_evaluator",
name = "f1",
evaluator_name = "builtin.f1_score",
data_mapping = new
{
response = "{{item.response}}",
ground_truth = "{{item.ground_truth}}"
}
}
];
const dataSourceConfig = {
type: "custom",
item_schema: {
type: "object",
properties: {
query: { type: "string" },
response: { type: "string" },
ground_truth: { type: "string" },
},
required: ["query", "response", "ground_truth"],
},
};
const testingCriteria = [
{
type: "azure_ai_evaluator",
name: "coherence",
evaluator_name: "builtin.coherence",
initialization_parameters: { model: modelDeploymentName },
data_mapping: {
query: "{{item.query}}",
response: "{{item.response}}",
},
},
{
type: "azure_ai_evaluator",
name: "violence",
evaluator_name: "builtin.violence",
data_mapping: {
query: "{{item.query}}",
response: "{{item.response}}",
},
},
{
type: "azure_ai_evaluator",
name: "f1",
evaluator_name: "builtin.f1_score",
data_mapping: {
response: "{{item.response}}",
ground_truth: "{{item.ground_truth}}",
},
},
];
curl --request POST \
--url "https://${ACCOUNT}.services.ai.azure.com/api/projects/${PROJECT}/openai/v1/evals" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--data '{
"name": "dataset-evaluation",
"data_source_config": {
"type": "custom",
"item_schema": {
"type": "object",
"properties": {
"query": { "type": "string" },
"response": { "type": "string" },
"ground_truth": { "type": "string" }
},
"required": ["query", "response", "ground_truth"]
}
},
"testing_criteria": [
{
"type": "azure_ai_evaluator",
"name": "coherence",
"evaluator_name": "builtin.coherence",
"initialization_parameters": {"model": "gpt-5-mini"},
"data_mapping": {
"query": "{{item.query}}",
"response": "{{item.response}}"
}
},
{
"type": "azure_ai_evaluator",
"name": "violence",
"evaluator_name": "builtin.violence",
"data_mapping": {
"query": "{{item.query}}",
"response": "{{item.response}}"
}
},
{
"type": "azure_ai_evaluator",
"name": "f1",
"evaluator_name": "builtin.f1_score",
"data_mapping": {
"response": "{{item.response}}",
"ground_truth": "{{item.ground_truth}}"
}
}
]
}'
创建评估并运行
创建评估,然后针对您上传的数据集启动一次运行。 该运行对数据集的每一行执行每个评估器。
# 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,
),
),
)
BinaryData evaluationData = BinaryData.FromObjectAsJson(new
{
name = "dataset-evaluation",
data_source_config = dataSourceConfig,
testing_criteria = testingCriteria
});
using BinaryContent evaluationContent = BinaryContent.Create(evaluationData);
ClientResult evaluation = await evaluationClient.CreateEvaluationAsync(
evaluationContent);
string evaluationId = GetString(evaluation, "id");
object dataSource = new
{
type = "jsonl",
source = new { type = "file_id", id = dataId }
};
BinaryData runData = BinaryData.FromObjectAsJson(new
{
name = "dataset-run",
data_source = dataSource
});
using BinaryContent runContent = BinaryContent.Create(runData);
ClientResult evaluationRun = await evaluationClient.CreateEvaluationRunAsync(
evaluationId: evaluationId,
content: runContent);
Console.WriteLine($"Evaluation run created: {GetString(evaluationRun, "id")}");
参考: EvaluationClient 协议方法
// Create the evaluation
const evalObject = await openaiClient.evals.create({
name: "dataset-evaluation",
data_source_config: dataSourceConfig,
testing_criteria: testingCriteria,
});
// Create a run using the uploaded dataset
const evalRun = await openaiClient.evals.runs.create(evalObject.id, {
name: "dataset-run",
data_source: {
type: "jsonl",
source: {
type: "file_id",
id: dataId,
},
},
});
# Step 1: Create the evaluation
EVAL_ID=$(curl --silent --request POST \
--url "https://${ACCOUNT}.services.ai.azure.com/api/projects/${PROJECT}/openai/v1/evals" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--data '{
"name": "dataset-evaluation",
"data_source_config": {
"type": "custom",
"item_schema": {
"type": "object",
"properties": {
"query": { "type": "string" },
"response": { "type": "string" },
"ground_truth": { "type": "string" }
},
"required": ["query", "response", "ground_truth"]
}
},
"testing_criteria": [
{
"type": "azure_ai_evaluator",
"name": "coherence",
"evaluator_name": "builtin.coherence",
"initialization_parameters": { "model": "gpt-5-mini" },
"data_mapping": {
"query": "{{item.query}}",
"response": "{{item.response}}"
}
},
{
"type": "azure_ai_evaluator",
"name": "violence",
"evaluator_name": "builtin.violence",
"data_mapping": {
"query": "{{item.query}}",
"response": "{{item.response}}"
}
},
{
"type": "azure_ai_evaluator",
"name": "f1",
"evaluator_name": "builtin.f1_score",
"data_mapping": {
"response": "{{item.response}}",
"ground_truth": "{{item.ground_truth}}"
}
}
]
}' | jq -r '.id')
# Step 2: Create a run against your dataset
curl --request POST \
--url "https://${ACCOUNT}.services.ai.azure.com/api/projects/${PROJECT}/openai/v1/evals/${EVAL_ID}/runs" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--data '{
"name": "dataset-run",
"data_source": {
"type": "jsonl",
"source": {
"type": "file_id",
"id": "YOUR_DATASET_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,
},
},
)
object csvDataSourceConfig = new
{
type = "custom",
item_schema = new
{
type = "object",
properties = new
{
query = new { type = "string" },
response = new { type = "string" },
context = new { type = "string" },
ground_truth = new { type = "string" }
},
required = Array.Empty<string>()
},
include_sample_schema = true
};
object[] csvTestingCriteria =
[
new
{
type = "azure_ai_evaluator",
name = "coherence",
evaluator_name = "builtin.coherence",
initialization_parameters = new { model = modelDeploymentName },
data_mapping = new
{
query = "{{item.query}}",
response = "{{item.response}}"
}
},
new
{
type = "azure_ai_evaluator",
name = "violence",
evaluator_name = "builtin.violence",
data_mapping = new
{
query = "{{item.query}}",
response = "{{item.response}}"
}
},
new
{
type = "azure_ai_evaluator",
name = "f1",
evaluator_name = "builtin.f1_score",
data_mapping = new
{
response = "{{item.response}}",
ground_truth = "{{item.ground_truth}}"
}
}
];
FileDataset csvDataset = await projectClient.Datasets.UploadFileAsync(
name: "eval-csv-data",
version: "1",
filePath: "./evaluation_data.csv");
BinaryData evaluationData = BinaryData.FromObjectAsJson(new
{
name = "CSV evaluation with built-in evaluators",
data_source_config = csvDataSourceConfig,
testing_criteria = csvTestingCriteria
});
using BinaryContent evaluationContent = BinaryContent.Create(evaluationData);
ClientResult evaluation = await evaluationClient.CreateEvaluationAsync(
evaluationContent);
string evaluationId = GetString(evaluation, "id");
BinaryData runData = BinaryData.FromObjectAsJson(new
{
name = "csv-evaluation-run",
data_source = new
{
type = "csv",
source = new { type = "file_id", id = csvDataset.Id }
}
});
using BinaryContent runContent = BinaryContent.Create(runData);
ClientResult evaluationRun = await evaluationClient.CreateEvaluationRunAsync(
evaluationId: evaluationId,
content: runContent);
Console.WriteLine($"Evaluation run created: {GetString(evaluationRun, "id")}");
参考: AIProjectDatasetsOperations.UploadFileAsync 和 EvaluationClient 协议方法。
当前的 JavaScript/TypeScript SDK 示例不演示 CSV 评估。 对此流程,请使用 Python 或 C# 选项卡。
使用Python或 C# 选项卡上传 CSV 文件。 然后,您可以将 Evals REST 端点与 csv 数据源类型和已上传的数据集 ID 结合使用。
后续步骤