Skip to main content

getScorerById()

getScorerById() 方法通过搜索其 id 属性(或者作为备用的 name 属性)而不是注册键来检索评分器。当你知道评分器的 ID,但不一定知道它是如何在 Mastra 实例中注册的时,这非常有用。

🌐 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.

使用示例
Direct link to 使用示例

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

参数
Direct link to 参数

🌐 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.

返回
Direct link to 返回

🌐 Returns

scorer:

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

错误处理
Direct link to 错误处理

🌐 Error Handling

如果出现以下情况,此方法将抛出 MastraError

🌐 This method throws a MastraError if:

  • 未找到具有指定 ID 的得分器
  • 没有在 Mastra 实例中注册评分者
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