跳到主要内容

getScorerById()

getScorerById() 方法通过搜索 Scorer 的 id 属性(或作为回退的 name 属性)而非注册键来获取 Scorer。当你知道 Scorer 的 id,但不一定知道它在 Mastra 实例中的注册方式时,此方法非常有用。

使用示例
使用示例的直接链接

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