快速检查
Quick Checks 是不使用 LLM、可组合的微型 Scorer,用于常见断言。它们可以添加到任何使用 Scorer 的现有 scorers: [...] 数组中,包括 runEvals、实时评分、实验和 Studio。
在内部,它们是标准的 createScorer() 实例,因此与其他 Scorer 具有相同的可观测性、存储能力和 pipeline 集成。
使用示例使用示例的直接链接
import { checks } from '@mastra/evals/checks'
import { runEvals } from '@mastra/core/evals'
import { myAgent } from '../agents'
const result = await runEvals({
data: [{ input: 'What is the weather in Brooklyn?' }],
target: myAgent,
scorers: [
checks.includes('sunny'),
checks.calledTool('get_weather'),
checks.toolOrder(['get_weather', 'summarize']),
checks.noToolErrors(),
],
})
console.log(result.scores)
文本检查文本检查的直接链接
checks.includes(expected, options?)checksincludesexpected-options的直接链接
如果 Agent 的输出文本包含预期子字符串,则得 1 分,否则得 0 分。
checks.includes('sunny')
checks.includes('Sunny', { ignoreCase: false })
expected:
options.ignoreCase?:
返回值:找到时为 1,否则为 0。
checks.excludes(unwanted, options?)checksexcludesunwanted-options的直接链接
如果 Agent 的输出文本不包含该子字符串,则得 1 分,否则得 0 分。
checks.excludes('error')
checks.excludes('Error', { ignoreCase: false })
unwanted:
options.ignoreCase?:
返回值:未出现时为 1,否则为 0。
checks.equals(expected, options?)checksequalsexpected-options的直接链接
经过可选的规范化后,如果输出文本与预期字符串完全相等,则得 1 分,否则得 0 分。
checks.equals('Hello, world!')
checks.equals('Hello', { ignoreCase: false })
expected:
options.ignoreCase?:
返回值:相等时为 1,否则为 0。
checks.matches(pattern, options?)checksmatchespattern-options的直接链接
如果输出与正则表达式匹配,则得 1 分。
checks.matches(/\d+°[FC]/)
checks.matches(/^hello$/, { exact: true })
pattern:
options.exact?:
返回值:匹配时为 1,否则为 0。
checks.similarity(expected, options?)checkssimilarityexpected-options的直接链接
使用 Dice 系数返回输出与预期字符串之间的字符串相似度分数(0-1)。设置 threshold 时,改为返回二元值 1/0。
checks.similarity('Sunny, 72°F')
checks.similarity('Sunny, 72°F', { threshold: 0.7 })
expected:
options.threshold?:
options.ignoreCase?:
返回值:原始相似度分数(0-1);设置 threshold 时,返回二元值 1/0。
Tool 调用检查Tool 调用检查的直接链接
checks.calledTool(toolName, options?)checkscalledtooltoolname-options的直接链接
如果 Agent 调用指定 Tool 的次数至少达到要求,则得 1 分。
checks.calledTool('get_weather')
checks.calledTool('search', { times: 2 })
toolName:
options.times?:
返回值:调用次数至少达到 times 时为 1,否则为 0。
checks.didNotCall(toolName)checksdidnotcalltoolname的直接链接
如果 Agent 未调用指定 Tool,则得 1 分。
checks.didNotCall('delete_user')
toolName:
返回值:未调用该 Tool 时为 1,否则为 0。
checks.toolOrder(expectedOrder)checkstoolorderexpectedorder的直接链接
如果 Tool 按指定顺序调用,则得 1 分。采用宽松匹配,预期 Tool 之间可以出现其他 Tool 调用。
checks.toolOrder(['search', 'summarize', 'respond'])
expectedOrder:
返回值:满足预期顺序时为 1,否则为 0。
checks.maxToolCalls(max)checksmaxtoolcallsmax的直接链接
如果 Agent 使用的 Tool 调用不超过 max 次,则得 1 分。
checks.maxToolCalls(5)
max:
返回值:未超过限制时为 1,否则为 0。
checks.usedNoTools()checksusednotools的直接链接
如果 Agent 完全没有调用 Tool,则得 1 分。
checks.usedNoTools()
返回值:未调用 Tool 时为 1,否则为 0。
checks.noToolErrors()checksnotoolerrors的直接链接
如果没有任何 Tool 调用进入错误状态,则得 1 分。该检查既能检测错误结果(存在 result.error),也能检测未完成的 Tool 调用(state === 'call')。
checks.noToolErrors()
返回值:所有 Tool 调用均成功时为 1,否则为 0。
将检查与其他 Scorer 组合将检查与其他 Scorer 组合的直接链接
快速检查可以与基于 LLM 和基于代码的 Scorer 组合在同一个 scorers 数组中:
import { checks } from '@mastra/evals/checks'
import { createAnswerRelevancyScorer } from '@mastra/evals/scorers/prebuilt'
import { runEvals } from '@mastra/core/evals'
import { myAgent } from '../agents'
const result = await runEvals({
data: [{ input: 'What is the weather in Brooklyn?' }],
target: myAgent,
scorers: [
// Zero-LLM checks
checks.includes('Brooklyn'),
checks.calledTool('get_weather'),
checks.noToolErrors(),
// LLM-based scorer
createAnswerRelevancyScorer({ model: 'openai/gpt-5-mini' }),
],
})