跳到主要内容

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')
}
}