ContextPrecisionMetric
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.
ContextPrecisionMetric
クラスは、期待される出力を生成するために取得されたコンテキストノードがどの程度関連性があり、正確であるかを評価します。これは判定ベースのシステムを使用して各コンテキスト部分の貢献を分析し、位置に基づいた重み付けスコアリングを提供します。
基本的な使い方
import { openai } from "@ai-sdk/openai";
import { ContextPrecisionMetric } from "@mastra/evals/llm";
// Configure the model for evaluation
const model = openai("gpt-4o-mini");
const metric = new ContextPrecisionMetric(model, {
context: [
"Photosynthesis is a biological process used by plants to create energy from sunlight.",
"Plants need water and nutrients from the soil to grow.",
"The process of photosynthesis produces oxygen as a byproduct.",
],
});
const result = await metric.measure(
"What is photosynthesis?",
"Photosynthesis is the process by which plants convert sunlight into energy.",
);
console.log(result.score); // Precision score from 0-1
console.log(result.info.reason); // Explanation of the score
コンストラクタのパラメータ
model:
LanguageModel
コンテキストの関連性を評価するために使用されるモデルの設定
options:
ContextPrecisionMetricOptions
このメトリックの設定オプション
ContextPrecisionMetricOptions
scale?:
number
= 1
スコアの最大値
context:
string[]
取得順に並んだコンテキスト要素の配列
measure() のパラメーター
input:
string
元のクエリまたはプロンプト
output:
string
評価対象となる生成された応答
戻り値
score:
number
適合率スコア(0からスケール、デフォルトは0-1)
info:
object
スコアの理由を含むオブジェクト
string
reason:
string
スコアの詳細な説明
スコアリングの詳細
このメトリックは、バイナリ関連性評価と平均適合率(MAP)スコアリングを通じてコンテキストの精度を評価します。
スコアリングプロセス
-
バイナリ関連性スコアを割り当てます:
- 関連するコンテキスト:1
- 関連しないコンテキスト:0
-
平均適合率を計算します:
- 各位置での適合率を算出
- 先頭の位置により大きな重みを付与
- 設定されたスケールに正規化
最終スコア:Mean Average Precision * 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 { ContextPrecisionMetric } from "@mastra/evals/llm";
// Configure the model for evaluation
const model = openai("gpt-4o-mini");
const metric = new ContextPrecisionMetric(model, {
context: [
"Exercise strengthens the heart and improves blood circulation.",
"A balanced diet is important for health.",
"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.75,
// info: {
// reason: "The score is 0.75 because the first and third contexts are highly relevant
// to the benefits mentioned in the output, while the second and fourth contexts
// are not directly related to exercise benefits. The relevant contexts are well-positioned
// at the beginning and middle of the sequence."
// }
// }