跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

getScorerById()

The getScorerById() method retrieves a scorer by searching for its id property (or name property as a fallback) rather than the registration key. This is useful when you know the scorer's id but not necessarily how it was registered in the Mastra instance.

Usage example
Usage example的直接链接

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

Parameters
Parameters的直接链接

id:

string
The id property of the scorer to retrieve. This should match the 'id' field specified when creating the scorer with createScorer(). The method will also search by 'name' property as a fallback.

Returns
Returns的直接链接

scorer:

MastraScorer
The MastraScorer instance that has the matching id (or name) property.

Error handling
Error handling的直接链接

This method throws a MastraError if:

  • No scorer with the specified id is found
  • No scorers are registered with the Mastra instance
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')
}
}
On this page