コンテキスト位置の評価
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
を使用して、最も関連性の高いコンテキストセグメントに基づいてレスポンスが適切に裏付けられているかを評価します。このメトリックは query
と response
を受け取り、スコアと理由を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
高位置の例
この例では、提供されたコンテキストの最初の文を使って、応答がクエリに直接答えています。周辺のコンテキストも一貫して応答を補強し、歴史的・政治的・機能的な観点から裏付けるため、強い位置的整合性が得られます。
import { openai } from "@ai-sdk/openai";
import { ContextPositionMetric } from "@mastra/evals/llm";
const metric = new ContextPositionMetric(openai("gpt-4o-mini"), {
context: [
"The capital of France is Paris.",
"Paris has been the capital since 508 CE.",
"Paris serves as France's political center.",
"The capital city hosts the French government."
]
});
const query = "What is the capital of France?";
const response = "The capital of France is Paris.";
const result = await metric.measure(query, response);
console.log(result);
高位置の出力
関連情報がコンテキストの冒頭にあり、ノイズや不要な情報に邪魔されることなく応答を直接裏付けているため、この出力は満点となります。
{
score: 1,
info: {
reason: 'The score is 1 because all provided context directly supports the output by confirming that Paris is the capital of France, with each statement reinforcing the answer through historical, political, and functional relevance.'
}
}
混在位置の例
この例では、応答が非常に関連性の高い情報に、文脈の後半にある追加の詳細を組み合わせています。体重に関する事実は質問に答えていますが、関連性の低い事実を含めることで、応答の位置的な精度が下がっています。
import { openai } from "@ai-sdk/openai";
import { ContextPositionMetric } from "@mastra/evals/llm";
const metric = new ContextPositionMetric(openai("gpt-4o-mini"), {
context: [
"Elephants are herbivores.",
"Adult elephants can weigh up to 13,000 pounds.",
"Elephants are the largest land animals.",
"Elephants eat plants and grass."
]
});
const query = "How much do elephants weigh?";
const response = "Adult elephants can weigh up to 13,000 pounds, making them the largest land animals.";
const result = await metric.measure(query, response);
console.log(result);
混在位置の出力
関連情報は含まれているものの、優先度の低い文脈と混在しているため、出力は中程度のスコアになります。これにより、文脈の順序と応答の焦点の整合性が弱まります。
{
score: 0.4,
info: {
reason: 'スコアが0.4であるのは、応答に成体のゾウの体重に関する直接的な答えが含まれており、これは高い関連性がある一方で、ゾウが草食動物であることや食性に関する、体重の理解に寄与しない不要な情報も含まれているためです。関連性の高い文脈と低い文脈が併存していることが、全体として低めのスコアにつながっています。'
}
}
低位置の例
この例では、応答が文脈の末尾にある関連情報を参照しています。文脈の前半には無関係な説明的詳細が含まれており、裏付け情報が期待される提示順序を乱しています。
import { openai } from "@ai-sdk/openai";
import { ContextPositionMetric } from "@mastra/evals/llm";
const metric = new ContextPositionMetric(openai("gpt-4o-mini"), {
context: [
"Rainbows appear in the sky.",
"Rainbows have different colors.",
"Rainbows are curved in shape.",
"Rainbows form when sunlight hits water droplets."
]
});
const query = "How do rainbows form?";
const response = "Rainbows are created when sunlight interacts with water droplets in the air.";
const result = await metric.measure(query, response);
console.log(result);
低位置の出力
主要な裏付け情報が文脈の後半に現れ、前半の内容がクエリに対してほとんど、あるいは全く価値を持たないため、出力のスコアは低くなります。
{
score: 0.12,
info: {
reason: 'The score is 0.12 because the relevant context directly explains how rainbows form, while the other statements provide information that is either unrelated or only tangentially related to the formation process.'
}
}
メトリクスの設定
期待される情報の並びを表す context
配列を指定して、ContextPositionMetric
インスタンスを作成できます。scale
などのオプションパラメーターを設定して、取り得る最大スコアを指定することも可能です。
const metric = new ContextPositionMetric(openai("gpt-4o-mini"), {
context: [""],
scale: 1
});
設定オプションの一覧については、ContextPositionMetric を参照してください。
結果の理解
ContextPositionMetric
は次の形式の結果を返します:
{
score: number,
info: {
reason: string
}
}
位置スコア
0〜1 の範囲の位置スコア:
- 1.0: 完璧な配置 – 最も関連性の高い情報が最初に出現する。
- 0.7–0.9: 良好な配置 – 関連情報の多くが冒頭にある。
- 0.4–0.6: ばらつきのある配置 – 関連情報が全体に散在している。
- 0.1–0.3: 弱い配置 – 関連情報の多くが末尾にある。
- 0.0: 不適切な配置 – まったく無関係、または順序が逆。
位置に関する情報
スコアの根拠。以下の点を含む:
- クエリおよび応答に対するコンテキストの関連性
- コンテキストシーケンス内での関連コンテンツの位置
- 後半より前半のコンテキストを重視する度合い
- コンテキストの全体的な構成と構造