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')
}
}
相關內容「相關內容」的直接連結
- getScorer(): 依註冊鍵取得 Scorer
- listScorers(): 取得所有已註冊的 Scorer
- createScorer(): 瞭解如何建立具有 id 的 Scorer