@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
54 lines (38 loc) • 1.73 kB
Markdown
# 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
```typescript
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
**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
**scorer** (`MastraScorer`): The MastraScorer instance that has the matching id (or name) property.
## Error handling
This method throws a `MastraError` if:
- No scorer with the specified id is found
- No scorers are registered with the Mastra instance
```typescript
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')
}
}
```
## Related
- [getScorer()](https://mastra.ai/reference/core/getScorer): Get a scorer by its registration key
- [listScorers()](https://mastra.ai/reference/core/listScorers): Get all registered scorers
- [createScorer()](https://mastra.ai/reference/evals/create-scorer): Learn how to create scorers with ids