> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # getScorerById() `getScorerById()` 方法透過搜尋 Scorer 的 `id` 屬性(或作為備援的 `name` 屬性)而非註冊鍵來取得 Scorer。當你知道 Scorer 的 id,但不一定知道它在 Mastra 執行個體中的註冊方式時,此方法非常有用。 ## 使用範例 ```typescript import { mastra } from './mastra' // Get a scorer by its id property const relevancyScorer = mastra.getScorerById('answer-relevancy-scorer') const weatherAgent = mastra.getAgent('weatherAgent') // Use the scorer to evaluate an AI output await weatherAgent.generate('What is the weather in Rome', { scorers: { answerRelevancy: { scorer: relevancyScorer, }, }, }) ``` ## 參數 **id** (`string`): 要取得的 Scorer id 屬性。它應與使用 createScorer() 建立 Scorer 時指定的 id 欄位相符。此方法也會依 name 屬性進行備援搜尋。 ## 傳回值 **scorer** (`MastraScorer`): 具有相符 id(或 name)屬性的 MastraScorer 執行個體。 ## 錯誤處理 以下情況會擲回 `MastraError`: - 找不到具有指定 id 的 Scorer - Mastra 執行個體未註冊任何 Scorer ```typescript try { const scorer = mastra.getScorerById('non-existent-scorer') } catch (error) { if (error.id === 'MASTRA_GET_SCORER_BY_ID_NOT_FOUND') { console.log('Scorer with that id not found') } } ``` ## 相關內容 - [getScorer()](https://mastra.zisheng.pro/zh-TW/reference/core/getScorer): 依註冊鍵取得 Scorer - [listScorers()](https://mastra.zisheng.pro/zh-TW/reference/core/listScorers): 取得所有已註冊的 Scorer - [createScorer()](https://mastra.zisheng.pro/zh-TW/reference/evals/create-scorer): 瞭解如何建立具有 id 的 Scorer