Skip to main content

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");
}
}

🌐 Related