> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Rubric Scorer **添加于:** `@mastra/evals@1.3.0` `createRubricScorer()` 函数会创建一个 LLM-as-judge Scorer,根据 rubric(标准清单)对 Agent 输出评分。它返回**二元**分数:仅当所有必需标准都满足时返回 `1`,否则返回 `0`。`reason` 会列出每项标准的判定结果,使 Agent 准确了解需要修正的内容。 该 Scorer 旨在直接用于 [`isTaskComplete`](https://mastra.zisheng.pro/reference/streaming/agents/stream)。由于 `isTaskComplete` 将 `score === 1` 视为“任务完成”,并将 `reason` 作为反馈重新注入对话,因此 Agent 会持续迭代,直至满足 rubric(或达到 `maxSteps`)。 ## 参数 **model** (`MastraModelConfig`): 用于根据 rubric 对输出评分的语言模型。通常使用较小、成本较低的模型即可完成评分。 **criteria** (`RubricCriterion[] | string`): 用于评分的 rubric。字符串会被视为以换行符分隔的清单(每行成为一项必需标准)。若省略,则在运行时从 request/additional context 的 rubric 值中读取;若无法解析出任何值,Scorer 不执行任何操作并返回 1。 **options** (`RubricScorerOptions`): Scorer 的配置选项 ## `.run()` 返回值 **score** (`number`): 所有必需标准均满足时为 1,否则为 0(再乘以 scale)。 **reason** (`string`): 逐项说明哪些标准已满足或未满足以及原因。isTaskComplete 会将此文本作为反馈重新注入对话。 ## 与 isTaskComplete 配合使用 定义一次 rubric,将 Scorer 添加到 `isTaskComplete`,Agent 就会自行修正,直至满足 rubric: ```typescript import { Agent } from '@mastra/core/agent' import { createRubricScorer } from '@mastra/evals/scorers/prebuilt' const supervisor = new Agent({ id: 'supervisor', instructions: `You coordinate research and writing using specialized agents. Delegate to research-agent for facts, then writing-agent for content.`, model: 'openai/gpt-5.6-sol', agents: { researchAgent, writingAgent }, }) const rubricScorer = createRubricScorer({ model: 'openai/gpt-5-mini', criteria: [ { description: 'The response includes an analysis section' }, { description: 'The response includes concrete recommendations' }, ], }) const stream = await supervisor.stream('Research AI in education', { maxSteps: 10, isTaskComplete: { scorers: [rubricScorer], strategy: 'all', }, }) ``` ## 字符串 rubric 以换行符分隔的字符串会被解析为多项标准,并移除常见的列表标记(`-`、`*`、`1.`)。每一行都会成为一项必需标准: ```typescript const rubricScorer = createRubricScorer({ model: 'openai/gpt-5-mini', criteria: `- All tests pass in the test suite - The function is named find_duplicates and accepts a single list argument`, }) ``` ## 可选标准 将标准标记为可选,可在不阻止任务完成的情况下对其评分和报告: ```typescript const rubricScorer = createRubricScorer({ model: 'openai/gpt-5-mini', criteria: [ { description: 'Includes an analysis section', required: true }, { description: 'Includes citations', required: false }, ], }) ``` ## 每次 run 动态设置 rubric 当没有向 factory 传入 `criteria` 时,Scorer 会从 run 的 request context、additional context 或输入中解析 `rubric` 值。这样无需重新构建 Scorer,单个 Scorer 实例即可在每次 run 中根据不同 rubric 评分: ```typescript const rubricScorer = createRubricScorer({ model: 'openai/gpt-5-mini', }) await supervisor.stream('Write find_duplicates', { isTaskComplete: { scorers: [rubricScorer] }, requestContext: { rubric: '- All tests pass\n- The function is named find_duplicates', }, }) ``` 如果无法解析出 rubric,Scorer 会返回 `1`,不会阻止循环完成。 ## 评分详情 该 Scorer 分两个阶段运行: 1. **评分**:Judge 模型独立评估每项标准,并返回逐项判定(`satisfied` / 未满足)及原因。 2. **计算分数**:仅当所有必需标准都为 `satisfied` 时,结果才为 `1`,否则为 `0`。如果没有标准被标记为必需,则所有标准都将被视为必需。 `reason` 会汇总结果并列出每项标准及其判定,因此评分失败时,Agent 会获得有针对性且有用的反馈,而不是笼统的“try again”。 ## 相关内容 - [stream() 中的 isTaskComplete](https://mastra.zisheng.pro/reference/streaming/agents/stream) - [Supervisor Agent](https://mastra.zisheng.pro/docs/capabilities/subagents) - [createScorer](https://mastra.zisheng.pro/reference/evals/create-scorer)