ContextualRecallMetric
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.
ContextualRecallMetric
クラスは、LLMの応答が提供されたコンテキストからすべての関連情報をどれだけ効果的に組み込んでいるかを評価します。これは、参照文書からの重要な情報が応答に正常に含まれているかどうかを測定し、精度よりも完全性に焦点を当てています。
基本的な使い方
import { openai } from "@ai-sdk/openai";
import { ContextualRecallMetric } from "@mastra/evals/llm";
// Configure the model for evaluation
const model = openai("gpt-4o-mini");
const metric = new ContextualRecallMetric(model, {
context: [
"Product features: cloud synchronization capability",
"Offline mode available for all users",
"Supports multiple devices simultaneously",
"End-to-end encryption for all data",
],
});
const result = await metric.measure(
"What are the key features of the product?",
"The product includes cloud sync, offline mode, and multi-device support.",
);
console.log(result.score); // Score from 0-1
コンストラクタのパラメータ
model:
LanguageModel
コンテキストリコールを評価するために使用されるモデルの設定
options:
ContextualRecallMetricOptions
メトリックの設定オプション
ContextualRecallMetricOptions
scale?:
number
= 1
最大スコア値
context:
string[]
照合対象となる参照ドキュメントや情報の配列
measure() のパラメーター
input:
string
元のクエリまたはプロンプト
output:
string
評価対象となるLLMの応答
戻り値
score:
number
リコールスコア(0からスケール、デフォルトは0-1)
info:
object
スコアの理由を含むオブジェクト
string
reason:
string
スコアの詳細な説明
スコアリングの詳細
このメトリックは、応答内容と関連するコンテキスト項目を比較することでリコールを評価します。
スコアリングプロセス
-
情報のリコールを評価します:
- コンテキスト内の関連項目を特定
- 正しくリコールされた情報を追跡
- リコールの完全性を測定
-
リコールスコアを計算:
- 正しくリコールされた項目をカウント
- 総関連項目数と比較
- カバレッジ比率を算出
最終スコア:(correctly_recalled_items / total_relevant_items) * 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 { ContextualRecallMetric } from "@mastra/evals/llm";
// Configure the model for evaluation
const model = openai("gpt-4o-mini");
const metric = new ContextualRecallMetric(model, {
scale: 100, // Use 0-100 scale instead of 0-1
context: [
"All data is encrypted at rest and in transit",
"Two-factor authentication (2FA) is mandatory",
"Regular security audits are performed",
"Incident response team available 24/7",
],
});
const result = await metric.measure(
"Summarize the company's security measures",
"The company implements encryption for data protection and requires 2FA for all users.",
);
// Example output:
// {
// score: 50, // Only half of the security measures were mentioned
// info: {
// reason: "The score is 50 because only half of the security measures were mentioned
// in the response. The response missed the regular security audits and incident
// response team information."
// }
// }