ContextPositionMetric
New Scorer API
We just released a new evals API called Scorers, with a more ergonomic API and more metadata stored for error analysis, and more flexibility to evaluate data structures. It’s fairly simple to migrate, but we will continue to support the existing Evals API.
ContextPositionMetric
クラスは、クエリと出力に対するコンテキストノードの関連性に基づいて、それらがどの程度適切に順序付けされているかを評価します。位置重み付けスコアリングを使用して、最も関連性の高いコンテキスト部分がシーケンスの早い段階に現れることの重要性を強調します。
基本的な使い方
import { openai } from "@ai-sdk/openai";
import { ContextPositionMetric } from "@mastra/evals/llm";
// Configure the model for evaluation
const model = openai("gpt-4o-mini");
const metric = new ContextPositionMetric(model, {
context: [
"Photosynthesis is a biological process used by plants to create energy from sunlight.",
"The process of photosynthesis produces oxygen as a byproduct.",
"Plants need water and nutrients from the soil to grow.",
],
});
const result = await metric.measure(
"What is photosynthesis?",
"Photosynthesis is the process by which plants convert sunlight into energy.",
);
console.log(result.score); // Position score from 0-1
console.log(result.info.reason); // Explanation of the score
コンストラクターのパラメーター
model:
ModelConfig
コンテキストの位置評価に使用されるモデルの設定
options:
ContextPositionMetricOptions
このメトリックの設定オプション
ContextPositionMetricOptions
scale?:
number
= 1
スコアの最大値
context:
string[]
取得順に並んだコンテキスト要素の配列
measure() のパラメーター
input:
string
元のクエリまたはプロンプト
output:
string
評価対象となる生成された応答
戻り値
score:
number
ポジションスコア(0からスケール、デフォルトは0-1)
info:
object
スコアの理由を含むオブジェクト
string
reason:
string
スコアの詳細な説明
スコアリングの詳細
このメトリクスは、バイナリの関連性評価と位置に基づく重み付けを通じて、コンテキストの配置を評価します。
スコアリングプロセス
-
コンテキストの関連性を評価します:
- 各要素にバイナリ判定(はい/いいえ)を割り当てる
- シーケンス内の位置を記録する
- 関連性の理由を記録する
-
位置の重みを適用します:
- 先頭の位置ほど重みが大きい(重み = 1/(位置 + 1))
- 関連する要素の重みを合計する
- 最大可能スコアで正規化する
最終スコア: (weighted_sum / max_possible_sum) * scale
スコアの解釈
(0 から scale、デフォルトは 0-1)
- 1.0: 最適 - 最も関連性の高いコンテキストが最初
- 0.7-0.9: 良好 - 関連性の高いコンテキストが主に早い段階
- 0.4-0.6: 混在 - 関連性の高いコンテキストが散在
- 0.1-0.3: 最適でない - 関連性の高いコンテキストが主に後半
- 0.0: 不適切な順序 - 関連性の高いコンテキストが最後または欠落
分析付きの例
import { openai } from "@ai-sdk/openai";
import { ContextPositionMetric } from "@mastra/evals/llm";
// Configure the model for evaluation
const model = openai("gpt-4o-mini");
const metric = new ContextPositionMetric(model, {
context: [
"A balanced diet is important for health.",
"Exercise strengthens the heart and improves blood circulation.",
"Regular physical activity reduces stress and anxiety.",
"Exercise equipment can be expensive.",
],
});
const result = await metric.measure(
"What are the benefits of exercise?",
"Regular exercise improves cardiovascular health and mental wellbeing.",
);
// Example output:
// {
// score: 0.5,
// info: {
// reason: "The score is 0.5 because while the second and third contexts are highly
// relevant to the benefits of exercise, they are not optimally positioned at
// the beginning of the sequence. The first and last contexts are not relevant
// to the query, which impacts the position-weighted scoring."
// }
// }