メインコンテンツへ移動

getScorerById()

getScorerById() メソッドは、登録キーではなく Scorer の id プロパティ(見つからない場合は name プロパティ)を検索して取得します。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')
}
}