> Discover all available pages from the documentation index: https://mastra.zisheng.pro/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/reference/core/getScorer): 按注册键获取 Scorer - [listScorers()](https://mastra.zisheng.pro/reference/core/listScorers): 获取所有已注册的 Scorer - [createScorer()](https://mastra.zisheng.pro/reference/evals/create-scorer): 了解如何创建带 id 的 Scorer