コンテキスト想起の評価
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
を使用して、提供されたコンテキストの関連情報がどの程度適切に回答へ反映されているかを評価します。このメトリクスは query
と response
を受け取り、スコアと、その理由を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
高リコールの例
この例では、応答にコンテキストの情報がすべて含まれています。各要素が正確に思い出され、出力で表現されているため、リコールスコアは満点です。
import { openai } from "@ai-sdk/openai";
import { ContextualRecallMetric } from "@mastra/evals/llm";
const metric = new ContextualRecallMetric(openai("gpt-4o-mini"), {
context: [
"Product features include cloud sync.",
"Offline mode is available.",
"Supports multiple devices."
]
});
const query = "What are the key features of the product?";
const response = "The product features cloud synchronization, offline mode support, and the ability to work across multiple devices.";
const result = await metric.measure(query, response);
console.log(result);
高リコールの出力
応答にコンテキストの全要素が含まれているため、出力は満点となります。コンテキストで言及された各機能が正確に想起され、過不足なく統合されています。
{
score: 1,
info: {
reason: 'The score is 1 because all elements of the expected output are fully supported by the corresponding nodes in retrieval context, specifically node(s) that detail cloud synchronization, offline mode support, and multi-device functionality.'
}
}
混合リコールの例
この例では、応答に一部のコンテキスト要素が含まれる一方で、無関係な内容も混在しています。無関係な情報が含まれることで、全体的なリコールスコアが下がります。
import { openai } from "@ai-sdk/openai";
import { ContextualRecallMetric } from "@mastra/evals/llm";
const metric = new ContextualRecallMetric(openai("gpt-4o-mini"), {
context: [
"Python is a high-level programming language.",
"Python emphasizes code readability.",
"Python supports multiple programming paradigms.",
"Python is widely used in data science."
]
});
const query = "What are Python's key characteristics?";
const response = "Python is a high-level programming language. It is also a type of snake.";
const result = await metric.measure(query, response);
console.log(result);
混合リコールの出力
出力は、関連するコンテキスト文を1つ含む一方で、元のコンテキストに裏付けられていない無関係な内容も含むため、中程度のスコアとなります。
{
score: 0.25,
info: {
reason: "The score is 0.25 because while the sentence 'Python is a high-level programming language' aligns with node 1 in the retrieval context, the lack of mention of other relevant information from nodes 2, 3, and 4 indicates significant gaps in the overall context."
}
}
リコールが低い例
この例では、応答に関連するコンテキストがほとんど、またはまったく含まれていません。応答内の情報の大半が根拠に欠けるため、リコールのスコアが低くなります。
import { openai } from "@ai-sdk/openai";
import { ContextualRecallMetric } from "@mastra/evals/llm";
const metric = new ContextualRecallMetric(openai("gpt-4o-mini"), {
context: [
"The solar system has eight planets.",
"Mercury is closest to the Sun.",
"Venus is the hottest planet.",
"Mars is called the Red Planet."
]
});
const query = "Tell me about the solar system.";
const response = "Jupiter is the largest planet in the solar system.";
const result = await metric.measure(query, response);
console.log(result);
リコールが低い出力
この出力は、応答にコンテキストに存在しない情報が含まれ、提供された詳細を無視しているため、低いスコアとなります。コンテキスト項目は回答にまったく取り入れられていません。
{
score: 0,
info: {
reason: "The score is 0 because the output lacks any relevant information from the node(s) in retrieval context, failing to address key aspects such as the number of planets, Mercury's position, Venus's temperature, and Mars's nickname."
}
}
メトリクスの設定
応答に関連する背景情報を表す context
配列を指定して、ContextualRecallMetric
のインスタンスを作成できます。スコア範囲を定義するための scale
など、任意のパラメーターも設定できます。
const metric = new ContextualRecallMetric(openai("gpt-4o-mini"), {
context: [""],
scale: 1
});
設定オプションの一覧については、ContextualRecallMetric を参照してください。
結果の理解
ContextualRecallMetric
は次の形式の結果を返します:
{
score: number,
info: {
reason: string
}
}
リコールスコア
0〜1の範囲のリコールスコア:
- 1.0: 完全にリコール — すべてのコンテキスト情報を活用。
- 0.7〜0.9: 高リコール — ほとんどのコンテキスト情報を活用。
- 0.4〜0.6: 中程度のリコール — 一部のコンテキスト情報を活用。
- 0.1〜0.3: 低リコール — わずかなコンテキスト情報のみを活用。
- 0.0: リコールなし — コンテキスト情報を未活用。
リコール情報
スコアの根拠。次のような詳細を含みます:
- 情報の取り込み状況
- 欠落しているコンテキスト
- 応答の完全性
- 全体的なリコール品質