你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
当前查看:Foundry(经典)门户版本 - 切换到新版Foundry门户
重要
本文中标记为预览的项目目前为预览版。 此预览版在没有服务级别协议的情况下提供,Microsoft不建议将其用于生产工作负荷。 某些功能可能不受支持,或者可能具有受限功能。 有关详细信息,请参阅 Microsoft Azure 预览版的使用条款。
本文介绍如何在云中运行评估(预览版),以便在测试数据集上进行预部署测试。 借助 Azure AI 评估 SDK,可以在计算机和云中本地运行评估。 例如,对小型测试数据运行本地评估以评估生成 AI 应用程序原型,然后进入预部署测试,对大型数据集运行评估。
对大多数方案使用云评估,尤其是在大规模测试时,将评估集成到持续集成和持续交付(CI/CD)管道中,或执行预部署测试。 在云中运行评估无需管理本地计算基础结构并支持大规模自动化测试工作流。 部署后,您可以选择 持续评估 您的代理以进行部署后监控。
使用 Foundry SDK 时,它会在 Foundry 项目中记录评估结果,以提高可观测性。 此功能支持所有Microsoft特选的内置评估器。 和你自己的 自定义评估器。 您的评估器可以在 评估库 中找到,并具有相同的项目范围和基于角色的访问控制。
先决条件
支持聊天补全的 GPT 模型的 Azure OpenAI 部署(例如
gpt-5-mini)。在 Foundry 项目中的 Foundry User 角色。
重要
Foundry RBAC 角色最近已更名。 Foundry 用户、Foundry 所有者、Foundry 帐户所有者和 Foundry 项目经理之前的名称分别为“Azure AI 用户”、“Azure AI 所有者”、“Azure AI 帐户所有者”和“Azure AI 项目经理”。 在重命名推出时,你仍可能会在某些位置看到以前的名称。重命名后,角色 ID 和核心权限保持不变。
(可选)可以使用 自己的存储帐户 来运行评估。
注意
某些评估功能具有区域限制。 有关详细信息 ,请参阅支持的区域 。
开始
安装 Microsoft Foundry SDK 项目客户端以在云中运行评估:
pip install azure-ai-projects azure-identity为 Foundry 资源设置环境变量:
import os # Required environment variables: endpoint = os.environ["PROJECT_ENDPOINT"] # https://<account>.services.ai.azure.com/api/projects/<project> model_endpoint = os.environ["MODEL_ENDPOINT"] # https://<account>.services.ai.azure.com model_api_key = os.environ["MODEL_API_KEY"] model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] # E.g. gpt-5-mini # Optional: Reuse an existing dataset. dataset_name = os.environ.get("DATASET_NAME", "dataset-test") dataset_version = os.environ.get("DATASET_VERSION", "1.0")定义要在云中运行评估的客户端:
import os from azure.identity import DefaultAzureCredential from azure.ai.projects import AIProjectClient # Create the project client (Foundry project and credentials): project_client = AIProjectClient( endpoint=endpoint, credential=DefaultAzureCredential(), )
准备输入数据
# 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
若要详细了解用于评估生成 AI 应用程序的输入数据格式,请参阅:
若要详细了解用于评估代理的输入数据格式,请参阅 Evaluate Azure AI 代理和 Evaluate 其他代理。
指定评估者
from azure.ai.projects.models import (
EvaluatorConfiguration,
EvaluatorIds,
)
# Built-in evaluator configurations:
evaluators = {
"relevance": EvaluatorConfiguration(
id=EvaluatorIds.RELEVANCE.value,
init_params={"deployment_name": model_deployment_name},
data_mapping={
"query": "${data.query}",
"response": "${data.response}",
},
),
"violence": EvaluatorConfiguration(
id=EvaluatorIds.VIOLENCE.value,
init_params={"azure_ai_project": endpoint},
),
"bleu_score": EvaluatorConfiguration(
id=EvaluatorIds.BLEU_SCORE.value,
),
}
创建评估
最后,提交远程评估运行:
from azure.ai.projects.models import (
Evaluation,
InputDataset
)
# Create an evaluation with the dataset and evaluators specified.
evaluation = Evaluation(
display_name="Cloud evaluation",
description="Evaluation of dataset",
data=InputDataset(id=data_id),
evaluators=evaluators,
)
# Run the evaluation.
evaluation_response = project_client.evaluations.create(
evaluation,
headers={
"model-endpoint": model_endpoint,
"api-key": model_api_key,
},
)
print("Created evaluation:", evaluation_response.name)
print("Status:", evaluation_response.status)
指定自定义评估器
注意
此功能不支持 Foundry 项目。 请使用 Foundry Hub 项目。
基于代码的自定义评估器
将自定义评估器注册到 Azure AI Hub 项目并提取评估器 ID。
from azure.ai.ml import MLClient
from azure.ai.ml.entities import Model
from promptflow.client import PFClient
# Define ml_client to register the custom evaluator.
ml_client = MLClient(
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
resource_group_name=os.environ["AZURE_RESOURCE_GROUP"],
workspace_name=os.environ["AZURE_PROJECT_NAME"],
credential=DefaultAzureCredential()
)
# Load the evaluator from the module.
from answer_len.answer_length import AnswerLengthEvaluator
# Convert it to an evaluation flow, and save it locally.
pf_client = PFClient()
local_path = "answer_len_local"
pf_client.flows.save(entry=AnswerLengthEvaluator, path=local_path)
# Specify the evaluator name that appears in the evaluator catalog.
evaluator_name = "AnswerLenEvaluator"
# Register the evaluator to the evaluator catalog.
custom_evaluator = Model(
path=local_path,
name=evaluator_name,
description="Evaluator calculating answer length.",
)
registered_evaluator = ml_client.evaluators.create_or_update(custom_evaluator)
print("Registered evaluator id:", registered_evaluator.id)
# Registered evaluators have versioning. You can always reference any version available.
versioned_evaluator = ml_client.evaluators.get(evaluator_name, version=1)
print("Versioned evaluator id:", registered_evaluator.id)
注册自定义计算器后,在 计算器目录中查看它。 在 Foundry 项目中,选择 评估,然后选择 评估器目录。
基于提示的自定义评估器
使用此示例注册自定义 FriendlinessEvaluator,它是根据基于提示的评估程序所述生成的:
# Import your prompt-based custom evaluator.
from friendliness.friend import FriendlinessEvaluator
# Define your deployment.
model_config = dict(
azure_endpoint=os.environ.get("AZURE_ENDPOINT"),
azure_deployment=os.environ.get("AZURE_DEPLOYMENT_NAME"),
api_version=os.environ.get("AZURE_API_VERSION"),
api_key=os.environ.get("AZURE_API_KEY"),
type="azure_openai"
)
# Define ml_client to register the custom evaluator.
ml_client = MLClient(
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
resource_group_name=os.environ["AZURE_RESOURCE_GROUP"],
workspace_name=os.environ["AZURE_PROJECT_NAME"],
credential=DefaultAzureCredential()
)
# # Convert the evaluator to evaluation flow and save it locally.
local_path = "friendliness_local"
pf_client = PFClient()
pf_client.flows.save(entry=FriendlinessEvaluator, path=local_path)
# Specify the evaluator name that appears in the evaluator catalog.
evaluator_name = "FriendlinessEvaluator"
# Register the evaluator to the evaluator catalog.
custom_evaluator = Model(
path=local_path,
name=evaluator_name,
description="prompt-based evaluator measuring response friendliness.",
)
registered_evaluator = ml_client.evaluators.create_or_update(custom_evaluator)
print("Registered evaluator id:", registered_evaluator.id)
# Registered evaluators have versioning. You can always reference any version available.
versioned_evaluator = ml_client.evaluators.get(evaluator_name, version=1)
print("Versioned evaluator id:", registered_evaluator.id)
注册自定义计算器后,可以在 计算器目录中查看它。 在 Foundry 项目中,选择 评估,然后选择 评估器目录。