getScorer()
getScorer() 方法会使用其注册键从 Mastra 实例中检索一个已注册的特定评分器。该方法提供对评分器的类型安全访问,如果未找到请求的评分器,则会抛出错误。
🌐 The getScorer() method retrieves a specific scorer that was registered with the Mastra instance using its registration key. This method provides type-safe access to scorers and throws an error if the requested scorer is not found.
使用示例Direct link to 使用示例
🌐 Usage Example
import { mastra } from "./mastra";
// Get a specific scorer by key
const relevancyScorer = mastra.getScorer("relevancyScorer");
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,
},
},
});
参数Direct link to 参数
🌐 Parameters
key:
string
The registration key of the scorer to retrieve. This should match a key used when registering scorers in the Mastra constructor.
返回Direct link to 返回
🌐 Returns
scorer:
MastraScorer
The MastraScorer instance associated with the provided key.
错误处理Direct link to 错误处理
🌐 Error Handling
如果出现以下情况,此方法将抛出 MastraError:
🌐 This method throws a MastraError if:
- 未找到具有指定键的评分器
- 没有在 Mastra 实例中注册评分者
try {
const scorer = mastra.getScorer("nonExistentScorer");
} catch (error) {
if (error.id === "MASTRA_GET_SCORER_NOT_FOUND") {
console.log("Scorer not found, using default evaluation");
}
}
相关Direct link to 相关
🌐 Related
- listScorers() - 获取所有已注册的得分者
- getScorerById() - 根据其 id 属性获取评分器
- 自定义评分器 - 学习如何创建自定义评分器