getScorer()
getScorer() 方法按注册键获取在 Mastra 实例中注册的指定 Scorer。此方法提供对 Scorer 的类型安全访问;如果找不到所请求的 Scorer,则抛出错误。
使用示例使用示例的直接链接
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
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(): 获取所有已注册的 Scorer
- getScorerById(): 按 id 属性获取 Scorer
- 自定义 Scorer: 了解如何创建自定义 Scorer