给你的 AI 应用来一场“大考”!

B站影视 内地电影 2025-10-31 14:38 1

摘要:人工智能正在彻底改变我们构建软件的方式,但同时也带来了新的挑战:你如何确定你的 AI 应用真的给出了正确答案,并产生了理想结果?

人工智能正在彻底改变我们构建软件的方式,但同时也带来了新的挑战:你如何确定你的 AI 应用真的给出了正确答案,并产生了理想结果?

答案就是——评估(Evaluations,简称 “evals”)。它提供了一种结构化的方式来衡量 AI 输出的质量,让你对结果更有信心。

在这篇文章中,我们将带你了解 AI 评估的核心理念,展 示 Microsoft.Extensions.AI.Evaluation库如何无缝集成到 .NET 智能应用开发流程中,并通过一个简单示例手把手教你如何在项目中使用它。

作为开发者,我们习惯用单元测试来验证代码。如今,随着越来越多应用引入大语言模型(LLM)生成的内容,我们也需要一种方式来验证 AI 的行为是否符合预期。

•正确性(Correctness)•相关性(Relevance)•安全性(Safety)•用户意图匹配度(User Intent)•甚至你自定义的领域特定标准

通过将评估集成进开发流程,你可以在问题上线前就发现它们,还能对不同模型或提示词(prompts)进行基准测试,持续提升 AI 功能的质量。

为什么选择 Microsoft.Extensions.AI.Evaluation?

这套库为 .NET 智能应用提供了完整的评估构建模块,具备以下核心优势:

✅ 与现有工作流无缝集成•支持 MSTest、xUnit、NUnit 等主流测试框架•可在 Test Explorer、dotnet test或 CI/CD 管道中直接运行•也支持在生产环境中进行在线评估,并将评分上传至遥测仪表盘丰富、经研究验证的评估指标

库中包含多个 NuGet 包,覆盖三大评估维度:

内容安全(Safety)

•评估 AI 回答是否存在:受保护内容、仇恨言论、暴力、代码漏洞等风险•基于 Azure AI Foundry Evaluation 服务,嵌入“负责任 AI”实践

质量(Quality)

•评估回答的:相关性、连贯性、完整性•还支持评估 AI Agent 的任务执行能力、意图理解、工具调用准确性•需要连接 LLM 才能运行

自然语言处理(NLP)

•提供经典文本相似度指标:BLEU、GLEU、F1•无需 LLM,纯算法评估,适合机器翻译等场景

所有内置评估器均可扩展。你还能自定义领域专属评估器或存储后端。

️ 开箱即用的交互式报告通过 CLI 工具

•支持按标签/关键词筛选场景•可下钻查看单条评估详情•还能追踪历史趋势,观察质量变化

无需自己搭建仪表盘,开箱即用!

⚡ 更快、更省钱的测试

自动缓存 LLM 响应:相同输入不再重复调用模型•CI/CD 中仅对变更的 prompt 触发新请求•显著降低延迟与 Token 成本

☁️ 支持 Azure Blob 存储

•评估结果与缓存可自动持久化到 Azure Blob•团队共享历史数据、支持合规审计•也支持本地磁盘或自定义存储后端

️ 深度集成 Azure DevOps

•在 CI/CD 管道中自动运行 AI 评估•通过 Marketplace 插件直接在流水线中查看报告•把 AI 质量检查变成“一等公民”

模块化 & 高度可扩展

•按需使用:不需要缓存?跳过即可•自定义评估器、指标、报告层、存储方案•所有数据均为 JSON 可序列化,方便对接自有系统

Microsoft.Extensions.AI.Evaluation 基于 Microsoft.Extensions.AI(MEAI)抽象层构建,与你的 AI 应用天然兼容。

三大核心层:

1.核心构建块

•IEvaluator、EvaluationMetric等接口,支持自定义扩展

2.即用型评估器

•覆盖质量、安全、NLP 三大维度,开箱即用

3.报告与集成

•CLI 工具 + Azure DevOps 插件 + 本地/云存储支持

整个流程就像写普通单元测试一样自然:写代码 → 跑评估 → 看报告 → 持续优化

下面是一个使用 Quality 评估器的简单示例。

1.配置 LLM 连接(以 Azure OpenAI 为例)SET EVAL_SAMPLE_AZURE_OPENAI_ENDPOINT=https://SET EVAL_SAMPLE_AZURE_OPENAI_MODEL=gpt-4o

建议使用 GPT-4o 或更新模型(如 GPT-4.1、GPT-5),评估提示已针对 OpenAI 模型优化。

2.创建 MSTest 项目并安装 NuGet 包

安装以下包:

•Azure.AI.OpenAI•Azure.Identity•Microsoft.Extensions.AI.Evaluation•Microsoft.Extensions.AI.Evaluation.Quality•Microsoft.Extensions.AI.Evaluation.Reporting•Microsoft.Extensions.AI.OpenAI(选择最新预发布版)using Azure.Identity;using Microsoft.Extensions.AI;using Microsoft.Extensions.AI.Evaluation;using Microsoft.Extensions.AI.Evaluation.Quality;using Microsoft.Extensions.AI.Evaluation.Reporting;namespace EvaluationTests;[TestClass]public class Test1{ private static readonly ReportingConfiguration s_reportingConfig = CreateReportingConfiguration; [TestMethod] public async Task Test1_DistanceBetweenEarthAndVenus { await using ScenarioRun scenarioRun = await s_reportingConfig.CreateScenarioRunAsync("Scenarios.Venus"); (IList await GetAstronomyConversationAsync( chatClient: scenarioRun.ChatConfiguration!.ChatClient, query: "How far is the planet Venus from the Earth at its closest and furthest points?"); List new GroundednessEvaluatorContext(""" Distance between Venus and Earth at inferior conjunction: ~23–25 million miles. At superior conjunction: ~160–164 million miles. """) ]; EvaluationResult result = await scenarioRun.EvaluateAsync(messages, response, additionalContext); NumericMetric groundedness = result.Get Assert.IsFalse(groundedness.Interpretation!.Failed); } [TestMethod] public async Task Test2_DistanceBetweenEarthAndMars { await using ScenarioRun scenarioRun = await s_reportingConfig.CreateScenarioRunAsync("Scenarios.Mars"); (IList await GetAstronomyConversationAsync( chatClient: scenarioRun.ChatConfiguration!.ChatClient, query: "How far is the planet Mars from the Earth at its closest and furthest points?"); List new GroundednessEvaluatorContext(""" Distance between Mars and Earth at closest approach: ~34–62 million miles. At farthest: ~249–250 million miles. """) ]; EvaluationResult result = await scenarioRun.EvaluateAsync(messages, response, additionalContext); NumericMetric coherence = result.Get Assert.IsFalse(coherence.Interpretation!.Failed); } private static ReportingConfiguration CreateReportingConfiguration { string endpoint = Environment.GetEnvironmentVariable("EVAL_SAMPLE_AZURE_OPENAI_ENDPOINT")!; string model = Environment.GetEnvironmentVariable("EVAL_SAMPLE_AZURE_OPENAI_MODEL")!; var client = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential); IChatClient chatClient = client.GetChatClient(deploymentName: model).AsIChatClient; return DiskBasedReportingConfiguration.Create( storageRootPath: "./eval-results", evaluators: [new CoherenceEvaluator, new FluencyEvaluator, new GroundednessEvaluator], chatConfiguration: new ChatConfiguration(chatClient), enableResponseCaching: true); } private static async Task Messages, ChatResponse ModelResponse)> GetAstronomyConversationAsync( IChatClient chatClient, string query) { const string SystemPrompt = """ You are an AI assistant that can answer questions related to astronomy. Keep your responses concise staying under 100 words as much as possible. Use the imperial measurement system for all measurements in your response. """; List new(ChatRole.System, SystemPrompt), new(ChatRole.User, query) ]; var chatOptions = new ChatOptions { Temperature = 0.0f, ResponseFormat = ChatResponseFormat.Text }; ChatResponse response = await chatClient.GetResponseAsync(messages, chatOptions); return (messages, response); }}4.运行测试并生成报告

首次运行会调用 LLM,后续因缓存会更快。

安装 CLI 工具:

dotnet tool install Microsoft.Extensions.AI.Evaluation.Console --create-manifest-if-needed

生成并打开 HTML 报告:

dotnet aieval report -p ./bin/Debug/net8.0/eval-results -o ./report.html --open

报告将自动在浏览器中打开,支持交互式查看各项指标详情!

快去试试吧!你的 AI 应用值得一场“大考”!

来源:opendotnet

相关推荐