如何在 Mastra evals 中设置 gates 与分数阈值得到 passed、scored 或 failed 判定 如何在 Mastra evals 中设置 gates 与分数阈值得到 passed、scored 或 failed 判定【免费下载链接】mastraMastra is the modern TypeScript framework for AI-powered applications and agents.项目地址: https://gitcode.com/GitHub_Trending/ma/mastra如果你的 Agent 评测目前只有零散的分数缺少一个这次运行到底算不算通过的信号Mastra 的 gates and verdicts 机制可以解决这个问题在runEvals中声明必须得满分的 gates硬性要求和带分数阈值的 tracked scorers质量指标一次评测结束后直接从result.verdict拿到passed、scored或failed三种判定之一不需要自己写断言逻辑。本文适用场景是用runEvals来自mastra/core/evals对 Agent 做批量评测并在 CI 或本地脚本中依据判定结果决定流水线是否失败。准备条件按照 Evals 概览 的安装说明安装mastra/evals包npm install mastra/evalslatestrunEvals本身从mastra/core/evals导入Quick Checkschecks从mastra/evals/checks导入。此外你还需要一个可评测的 targetAgent 或 Workflow和一组评测数据。gates、阈值与 verdict 的判定规则先明确三者含义后面的配置都围绕它们展开Gates通过gates字段传入的 scorer会在每条数据上先于普通 scorers 执行。一个 gate 必须在所有数据项上平均分达到 1.0 才算通过。任何 scorer 都可以作为 gate返回二值 1/0 分数的 Quick Checks 是天然的选择。Thresholds把 scorer 包成{ scorer, threshold }即为带阈值的 tracked metric。阈值与 scorer 在所有数据项上的平均分比较数字表示最低分达到或超过即通过如{ scorer, threshold: 0.7 }含min和/或max的对象区间检查如{ scorer, threshold: { max: 0.3 } }。max适用于分数高反而是坏事的 scorer如幻觉、毒性min/max必须都在 0 和 1 之间。Verdict 判定规则在所有数据项处理完后计算failed至少一个 gate 在数据项上的平均分低于 1.0scored所有 gate 通过但至少一个 threshold scorer 未达到阈值passed所有 gate 得 1.0 且所有阈值都达标。一个重要的边界当既没有 gates 也没有带阈值的 scorer 时verdict字段会被省略runEvals的行为与之前完全一致。配置 gates 与阈值一个完整示例下面示例来自主文档 Gates and verdicts其中weatherAgent是你的评测目标faithfulnessScorer是你自己的 scorer 实例文档示例中从../scorers导入替换为你项目中对应的实例即可// src/evals/weather-eval.ts import { runEvals } from mastra/core/evals import { checks } from mastra/evals/checks import { weatherAgent } from ../agents import { faithfulnessScorer } from ../scorers const result await runEvals({ data: [{ input: What is the weather in Brooklyn? }], target: weatherAgent, // Gates: must all score 1.0 or the run fails gates: [checks.calledTool(get_weather), checks.noToolErrors()], // Scorers: tracked with optional thresholds scorers: [ { scorer: faithfulnessScorer, threshold: 0.7 }, checks.includes(Brooklyn), // no threshold tracked only ], }) console.log(result.verdict) // passed | scored | failed示例逐段说明gates: [checks.calledTool(get_weather), checks.noToolErrors()]表达两条硬性要求agent 必须调用get_weather工具、工具调用不能有错误。任一 gate 平均低于 1.0 即整体failed。{ scorer: faithfulnessScorer, threshold: 0.7 }是数字阈值最低分 0.7。未达阈值只会让判定变成scored不会直接failed。checks.includes(Brooklyn)是裸 scorer不带阈值分数会出现在result.scores中但不影响 verdict。阈值还支持区间形式文档给出的三种写法scorers: [ { scorer: faithfulnessScorer, threshold: 0.7 }, // min threshold (number shorthand) { scorer: hallucinationScorer, threshold: { max: 0.3 } }, // max threshold — high score bad { scorer: verbosityScorer, threshold: { min: 0.3, max: 0.8 } }, // range threshold toneScorer, // bare scorer, no threshold — tracked only ]对应的result.thresholdResults文档示例输出如下数值仅为文档示例实际取决于你的数据[ { id: faithfulness, passed: true, averageScore: 0.85, threshold: 0.7 }, { id: hallucination, passed: true, averageScore: 0.1, threshold: { max: 0.3 } }, { id: verbosity, passed: false, averageScore: 0.9, threshold: { min: 0.3, max: 0.8 } }, ]只跑 gate 的确定性 CI 检查如果只做确定性的通过/失败检查、不追踪质量指标提供至少一个 gate 时scorers是可选的const result await runEvals({ data: [{ input: What is the weather in Brooklyn? }], target: weatherAgent, gates: [checks.calledTool(get_weather), checks.noToolErrors()], })注意一个硬性约束必须至少提供一个 scorer 或 gate两者都没有的runEvals调用会抛出错误。读取结果verdict、gateResults 与 thresholdResultsrunEvals返回对象中与判定相关的字段详见 runEvals 参考文档verdictpassed | scored | failed仅当提供了 gates 或带阈值的 scorer 时存在gateResults每个 gate 跨全部数据项的平均结果每条含id、passedboolean、score0–1。文档示例[{ id: check-called-tool, passed: true, score: 1 }]thresholdResults每个带阈值 scorer 的平均结果每条含id、passed、averageScore、threshold。在 CI 中用 verdict 作为单一信号并按文档给出的模式区分处理两种未通过情形const result await runEvals({ data: testDataset, target: myAgent, gates: [checks.calledTool(search), checks.noToolErrors()], scorers: [{ scorer: faithfulnessScorer, threshold: 0.7 }], }) if (result.verdict failed) { console.error( Gate failures:, result.gateResults?.filter(g !g.passed), ) process.exit(1) } if (result.verdict scored) { console.warn( Threshold misses:, result.thresholdResults?.filter(t !t.passed), ) }也就是说gate 失败会打印未通过的 gate 并以退出码 1 终止流水线阈值未达标只打印警告不终止。这个hard fail 与 soft warn的分层由 gates 和 thresholds 的语义天然保证。验证与限制验证方式运行runEvals后检查result.verdict并用result.gateResults/result.thresholdResults中的passed字段定位具体是哪个 gate 或哪个 scorer 未达标。未配置 gates 与阈值 scorer 的调用没有verdict字段不要在这种调用上读取result.verdict。gate 的判定依据是跨所有数据项的平均分达到 1.0所以二值型 check 只要有一条数据项不满足平均分就会低于 1.0整体判定为failed。若希望把 Quick Checks 的分数持久化到存储需要把 check 注册到 Mastra 实例配合storage详见 Score persistence 一节不注册只影响持久化不影响本次评测的判定结果。进一步阅读Gates and verdicts 文档本文所有规则的原始出处runEvals 参考完整参数与返回类型包括多轮turns场景下逐轮 gates/scorers 如何折叠进整体 verdictQuick Checks所有可用的零 LLM 断言includes、calledTool、toolOrder、maxToolCalls等Running scorers in CI把runEvals放进 Vitest/Jest/Mocha 的 CI 集成模式。【免费下载链接】mastraMastra is the modern TypeScript framework for AI-powered applications and agents.项目地址: https://gitcode.com/GitHub_Trending/ma/mastra创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考