> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # Rubric 評分器 **新增於:** `@mastra/evals@1.3.0` `createRubricScorer()` 函式會建立 LLM-as-judge 評分器,依照 rubric(條件檢查清單)評定 Agent 的輸出。它會傳回**二元**分數:只有滿足每項必要條件時才為 `1`,否則為 `0`。`reason` 會列出每項條件的判定結果,讓 Agent 確切知道需要修正的內容。 此評分器可直接搭配 [`isTaskComplete`](https://mastra.zisheng.pro/zh-TW/reference/streaming/agents/stream) 使用。由於 `isTaskComplete` 會將 `score === 1` 視為「任務已完成」,並把 `reason` 當作意見回饋注入對話,因此 Agent 會持續反覆修正,直到符合 rubric(或達到 `maxSteps`)為止。 ## 參數 **model** (`MastraModelConfig`): 用來依照 rubric 評定輸出的語言模型。通常使用較小、成本較低的模型就足以進行評定。 **criteria** (`RubricCriterion[] | string`): 用於評定的 rubric。字串會視為以換行分隔的檢查清單(每一行都會成為必要條件)。若省略,執行時會從 request/additional context 的 rubric 值讀取 rubric;若無法解析出任何值,評分器便不執行任何操作並傳回 1。 **options** (`RubricScorerOptions`): 評分器的設定選項 ## `.run()` 傳回值 **score** (`number`): 每項必要條件皆滿足時為 1,否則為 0(乘以 scale)。 **reason** (`string`): 逐項說明哪些條件已滿足、哪些未滿足及其原因。這就是 isTaskComplete 注入對話中作為意見回饋的文字。 ## 搭配 isTaskComplete 使用 定義一次 rubric,將評分器附加至 `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 }, ], }) ``` ## 每次執行時動態指定 rubric 若未將 `criteria` 傳入 factory,評分器會從此次執行的 request context、additional context 或輸入解析 `rubric` 值。如此一來,同一個評分器執行個體就能在每次執行時評定不同的 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,評分器會傳回 `1`,且不會阻止迴圈繼續。 ## 評分詳情 此評分器分兩個階段執行: 1. **評定**:裁判模型會分別評估每項條件,並傳回各條件的判定結果(`satisfied`/未滿足)及理由。 2. **評分**:只有每項必要條件皆為 `satisfied` 時,結果才會是 `1`;否則為 `0`。若沒有任何條件標示為必要,則所有條件都會視為必要條件。 `reason` 會摘要結果,並列出每項條件及其判定結果;因此評定未通過時,Agent 會收到具體且實用的意見回饋,而非籠統的「請再試一次」。 ## 相關資源 - [stream() 上的 isTaskComplete](https://mastra.zisheng.pro/zh-TW/reference/streaming/agents/stream) - [Supervisor Agent](https://mastra.zisheng.pro/zh-TW/docs/capabilities/subagents) - [createScorer](https://mastra.zisheng.pro/zh-TW/reference/evals/create-scorer)