> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # getScorerById() `getScorerById()` 方法會搜尋評分器的 `id` 屬性(並以 `name` 屬性作為後備),而非使用註冊鍵來擷取評分器。當你知道評分器的 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`): 要擷取的評分器 id 屬性。此值應與使用 createScorer() 建立評分器時指定的 'id' 欄位相符。此方法亦會以 'name' 屬性作為後備進行搜尋。 ## 傳回值 **scorer** (`MastraScorer`): 具有相符 id(或 name)屬性的 MastraScorer 實例。 ## 錯誤處理 在下列情況下,此方法會擲回 `MastraError`: - 找不到具有指定 id 的評分器 - Mastra 實例中未註冊任何評分器 ```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-HK/reference/core/getScorer):按註冊鍵取得評分器 - [listScorers()](https://mastra.zisheng.pro/zh-HK/reference/core/listScorers):取得所有已註冊的評分器 - [createScorer()](https://mastra.zisheng.pro/zh-HK/reference/evals/create-scorer):了解如何建立具有 id 的評分器