createScorer
Mastra は、入出力ペアを評価するためのカスタムスコアラーを定義できる、統一的な createScorer
ファクトリを提供します。各評価ステップでは、ネイティブの JavaScript 関数または LLM ベースのプロンプトオブジェクトを使用できます。カスタムスコアラーは、Agents や Workflow のステップに追加できます。
カスタムスコアラーの作成方法
createScorer
ファクトリを使って、名前、説明、任意の judge 設定を指定し、スコアラーを定義します。続いてステップメソッドをチェーンして評価パイプラインを構築します。最低でも generateScore
ステップは必須です。
const scorer = createScorer({
name: "My Custom Scorer",
description: "Evaluates responses based on custom criteria",
judge: {
model: myModel,
instructions: "You are an expert evaluator..."
}
})
.preprocess({ /* step config */ })
.analyze({ /* step config */ })
.generateScore(({ run, results }) => {
// 数値を返す
})
.generateReason({ /* step config */ });
createScorer のオプション
name:
description:
judge:
この関数は、ステップメソッドをチェーンできるスコアラービルダーを返します。.run()
メソッドとその入出力の詳細は、MastraScorer リファレンスを参照してください。
Judge オブジェクト
model:
instructions:
型安全性
より良い型推論と IntelliSense のサポートのために、scorer を作成する際に入出力の型を指定できます:
import { createScorer, ScorerRunInputForAgent, ScorerRunOutputForAgent } from '@mastra/core/scorers';
// エージェント評価における完全な型安全性のために
const agentScorer = createScorer<ScorerRunInputForAgent, ScorerRunOutputForAgent>({
name: 'Agent Response Quality',
description: 'Evaluates agent responses'
})
.preprocess(({ run }) => {
// run.input は ScorerRunInputForAgent として型付けされています
const userMessage = run.input.inputMessages[0]?.content;
return { userMessage };
})
.generateScore(({ run, results }) => {
// run.output は ScorerRunOutputForAgent として型付けされています
const response = run.output[0]?.content;
return response.length > 10 ? 1.0 : 0.5;
});
// カスタムの入出力型を使う場合
type CustomInput = { query: string; context: string[] };
type CustomOutput = { answer: string; confidence: number };
const customScorer = createScorer<CustomInput, CustomOutput>({
name: 'Custom Scorer',
description: 'Evaluates custom data'
})
.generateScore(({ run }) => run.output.confidence);
組み込みのエージェント型
ScorerRunInputForAgent
- エージェント評価用のinputMessages
、rememberedMessages
、systemMessages
、taggedSystemMessages
を含みますScorerRunOutputForAgent
- エージェントの応答メッセージの配列
これらの型を使用することで、オートコンプリート、コンパイル時検証、スコアリングロジックのドキュメント性が向上します。
ステップメソッドのシグネチャ
preprocess
分析の前にデータを抽出または変換できる任意の前処理ステップ。
関数モード:
関数: ({ run, results }) => any
run.input:
run.output:
run.runId:
run.runtimeContext:
results:
戻り値: any
このメソッドは任意の値を返せます。返り値は後続のステップで preprocessStepResult
として利用できます。
プロンプトオブジェクトモード:
description:
outputSchema:
createPrompt:
judge:
analyze
入力・出力および前処理されたデータを処理する任意の分析ステップ。
関数モード:
関数: ({ run, results }) => any
run.input:
run.output:
run.runId:
run.runtimeContext:
results.preprocessStepResult:
戻り値: any
このメソッドは任意の値を返せます。返り値は後続のステップで analyzeStepResult
として利用できます。
プロンプトオブジェクトモード:
description:
outputSchema:
createPrompt:
judge:
generateScore
最終的な数値スコアを算出するための必須ステップです。
関数モード:
関数: ({ run, results }) => number
run.input:
run.output:
run.runId:
run.runtimeContext:
results.preprocessStepResult:
results.analyzeStepResult:
戻り値: number
このメソッドは数値スコアを返す必要があります。
プロンプトオブジェクトモード:
description:
outputSchema:
createPrompt:
judge:
プロンプトオブジェクトモードを使用する場合は、LLM の出力を数値スコアに変換する calculateScore
関数も提供する必要があります:
calculateScore:
generateReason
スコアの根拠を説明する任意ステップです。
関数モード:
関数: ({ run, results, score }) => string
run.input:
run.output:
run.runId:
run.runtimeContext:
results.preprocessStepResult:
results.analyzeStepResult:
score:
戻り値: string
このメソッドはスコアの説明を表す文字列を返す必要があります。
プロンプトオブジェクトモード:
description:
createPrompt:
judge:
すべてのステップ関数は非同期関数にできます。