> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # getScorer() `getScorer()` 方法按注册键获取在 Mastra 实例中注册的指定 Scorer。此方法提供对 Scorer 的类型安全访问;如果找不到所请求的 Scorer,则抛出错误。 ## 使用示例 ```typescript import { mastra } from './mastra' // Get a specific scorer by key const relevancyScorer = mastra.getScorer('relevancyScorer') 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, }, }, }) ``` ## 参数 **key** (`string`): 要获取的 Scorer 的注册键。它应与在 Mastra 构造函数中注册 Scorer 时使用的键匹配。 ## 返回值 **scorer** (`MastraScorer`): 与所提供键关联的 MastraScorer 实例。 ## 错误处理 以下情况会抛出 `MastraError`: - 未找到具有指定键的 Scorer - Mastra 实例未注册任何 Scorer ```typescript try { const scorer = mastra.getScorer('nonExistentScorer') } catch (error) { if (error.id === 'MASTRA_GET_SCORER_NOT_FOUND') { console.log('Scorer not found, using default evaluation') } } ``` ## 相关内容 - [listScorers()](https://mastra.zisheng.pro/reference/core/listScorers): 获取所有已注册的 Scorer - [getScorerById()](https://mastra.zisheng.pro/reference/core/getScorerById): 按 id 属性获取 Scorer - [自定义 Scorer](https://mastra.zisheng.pro/docs/evals/custom-scorers): 了解如何创建自定义 Scorer