> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # 快速检查 Quick Checks 是不使用 LLM、可组合的微型 Scorer,用于常见断言。它们可以添加到任何使用 Scorer 的现有 `scorers: [...]` 数组中,包括 `runEvals`、实时评分、实验和 Studio。 在内部,它们是标准的 `createScorer()` 实例,因此与其他 Scorer 具有相同的可观测性、存储能力和 pipeline 集成。 ## 使用示例 ```typescript 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?)` 如果 Agent 的输出文本包含预期子字符串,则得 1 分,否则得 0 分。 ```typescript checks.includes('sunny') checks.includes('Sunny', { ignoreCase: false }) ``` **expected** (`string`): 要在输出中搜索的子字符串。 **options.ignoreCase** (`boolean`): 不区分大小写进行匹配。 (Default: `true`) 返回值:找到时为 `1`,否则为 `0`。 ### `checks.excludes(unwanted, options?)` 如果 Agent 的输出文本不包含该子字符串,则得 1 分,否则得 0 分。 ```typescript checks.excludes('error') checks.excludes('Error', { ignoreCase: false }) ``` **unwanted** (`string`): 不得出现在输出中的子字符串。 **options.ignoreCase** (`boolean`): 不区分大小写进行匹配。 (Default: `true`) 返回值:未出现时为 `1`,否则为 `0`。 ### `checks.equals(expected, options?)` 经过可选的规范化后,如果输出文本与预期字符串完全相等,则得 1 分,否则得 0 分。 ```typescript checks.equals('Hello, world!') checks.equals('Hello', { ignoreCase: false }) ``` **expected** (`string`): 输出必须完全匹配的字符串。 **options.ignoreCase** (`boolean`): 不区分大小写进行匹配。 (Default: `true`) 返回值:相等时为 `1`,否则为 `0`。 ### `checks.matches(pattern, options?)` 如果输出与正则表达式匹配,则得 1 分。 ```typescript checks.matches(/\d+°[FC]/) checks.matches(/^hello$/, { exact: true }) ``` **pattern** (`RegExp`): 用于测试输出的正则表达式。 **options.exact** (`boolean`): 添加 ^ 和 $ 锚点,使 pattern 与整个输出匹配。 (Default: `false`) 返回值:匹配时为 `1`,否则为 `0`。 ### `checks.similarity(expected, options?)` 使用 Dice 系数返回输出与预期字符串之间的字符串相似度分数(0-1)。设置 `threshold` 时,改为返回二元值 1/0。 ```typescript checks.similarity('Sunny, 72°F') checks.similarity('Sunny, 72°F', { threshold: 0.7 }) ``` **expected** (`string`): 用于比较的参考字符串。 **options.threshold** (`number`): 返回 1 所需的最低相似度分数(0-1)。省略时返回原始相似度分数。 **options.ignoreCase** (`boolean`): 不区分大小写进行比较。 (Default: `true`) 返回值:原始相似度分数(0-1);设置 `threshold` 时,返回二元值 `1`/`0`。 ## Tool 调用检查 ### `checks.calledTool(toolName, options?)` 如果 Agent 调用指定 Tool 的次数至少达到要求,则得 1 分。 ```typescript checks.calledTool('get_weather') checks.calledTool('search', { times: 2 }) ``` **toolName** (`string`): 要查找的 Tool 名称。 **options.times** (`number`): Tool 必须被调用的最少次数。 (Default: `1`) 返回值:调用次数至少达到 `times` 时为 `1`,否则为 `0`。 ### `checks.didNotCall(toolName)` 如果 Agent 未调用指定 Tool,则得 1 分。 ```typescript checks.didNotCall('delete_user') ``` **toolName** (`string`): 不得出现的 Tool 名称。 返回值:未调用该 Tool 时为 `1`,否则为 `0`。 ### `checks.toolOrder(expectedOrder)` 如果 Tool 按指定顺序调用,则得 1 分。采用宽松匹配,预期 Tool 之间可以出现其他 Tool 调用。 ```typescript checks.toolOrder(['search', 'summarize', 'respond']) ``` **expectedOrder** (`string[]`): 预期调用顺序中的 Tool 名称。它们必须作为实际 Tool 调用的子序列出现。 返回值:满足预期顺序时为 `1`,否则为 `0`。 ### `checks.maxToolCalls(max)` 如果 Agent 使用的 Tool 调用不超过 `max` 次,则得 1 分。 ```typescript checks.maxToolCalls(5) ``` **max** (`number`): 允许的最大 Tool 调用次数。 返回值:未超过限制时为 `1`,否则为 `0`。 ### `checks.usedNoTools()` 如果 Agent 完全没有调用 Tool,则得 1 分。 ```typescript checks.usedNoTools() ``` 返回值:未调用 Tool 时为 `1`,否则为 `0`。 ### `checks.noToolErrors()` 如果没有任何 Tool 调用进入错误状态,则得 1 分。该检查既能检测错误结果(存在 `result.error`),也能检测未完成的 Tool 调用(`state === 'call'`)。 ```typescript checks.noToolErrors() ``` 返回值:所有 Tool 调用均成功时为 `1`,否则为 `0`。 ## 将检查与其他 Scorer 组合 快速检查可以与基于 LLM 和基于代码的 Scorer 组合在同一个 `scorers` 数组中: ```typescript 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' }), ], }) ``` ## 相关内容 - [Quick Checks 概述](https://mastra.zisheng.pro/docs/evals/quick-checks) - [内置 Scorer](https://mastra.zisheng.pro/docs/evals/built-in-scorers) - [`createScorer()` 参考](https://mastra.zisheng.pro/reference/evals/create-scorer) - [`runEvals()` 参考](https://mastra.zisheng.pro/reference/evals/run-evals) - [自定义 Scorer](https://mastra.zisheng.pro/docs/evals/custom-scorers)