コンテキスト適合性の評価
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.
ContextRelevancyMetric
を使って、取得したコンテキストが元のクエリにどれだけ適合しているかを評価します。このメトリックは query
と response
を入力として受け取り、スコアと、その理由を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
高関連性の例
この例では、応答はクエリに直接関係するコンテキストのみを使用します。すべてのコンテキスト項目が回答を支えており、結果として関連性スコアは満点になります。
import { openai } from "@ai-sdk/openai";
import { ContextRelevancyMetric } from "@mastra/evals/llm";
const metric = new ContextRelevancyMetric(openai("gpt-4o-mini"), {
context: [
"Einstein won the Nobel Prize for his discovery of the photoelectric effect.",
"He published his theory of relativity in 1905.",
"His work revolutionized modern physics."
]
});
const query = "What were some of Einstein's achievements?";
const response = "Einstein won the Nobel Prize for discovering the photoelectric effect and published his groundbreaking theory of relativity.";
const result = await metric.measure(query, response);
console.log(result);
高関連性の出力
すべてのコンテキスト文が、無関係な情報を含めずにクエリへの回答に直接寄与しているため、この出力は満点となります。
{
score: 1,
info: {
reason: "The score is 1 because the retrieval context directly addresses the input by highlighting Einstein's significant achievements, making it entirely relevant."
}
}
関連性が混在する例
この例では、応答に1つ以上の関連するコンテキスト項目が使われていますが、無関係または有用性の低い情報も含まれています。これにより、全体の関連性スコアが低下します。
import { openai } from "@ai-sdk/openai";
import { ContextRelevancyMetric } from "@mastra/evals/llm";
const metric = new ContextRelevancyMetric(openai("gpt-4o-mini"), {
context: [
"Solar eclipses occur when the Moon blocks the Sun.",
"The Moon moves between the Earth and Sun during eclipses.",
"The Moon is visible at night.",
"The Moon has no atmosphere."
]
});
const query = "What causes solar eclipses?";
const response =
"Solar eclipses happen when the Moon moves between Earth and the Sun, blocking sunlight.";
const result = await metric.measure(query, response);
console.log(result);
関連性が混在する出力
この出力は、日食の仕組みに関する関連コンテキストを含みつつ、全体的な関連性を薄める無関係な事実も含んでいるため、中程度のスコアになります。
{
score: 0.5,
info: {
reason: "スコアが0.5であるのは、取得されたコンテキストに入力と無関係な記述(例:「The Moon is visible at night」「The Moon has no atmosphere」)が含まれており、これらは日食の原因を説明していないためです。関連情報の不足が、文脈的な関連性スコアを大きく下げています。"
}
}
関連性が低い例
この例では、文脈の大半がクエリと無関係です。関連する項目は1つだけのため、関連性スコアは低くなります。
import { openai } from "@ai-sdk/openai";
import { ContextRelevancyMetric } from "@mastra/evals/llm";
const metric = new ContextRelevancyMetric(openai("gpt-4o-mini"), {
context: [
"The Great Barrier Reef is in Australia.",
"Coral reefs need warm water to survive.",
"Marine life depends on coral reefs.",
"The capital of Australia is Canberra."
]
});
const query = "What is the capital of Australia?";
const response = "The capital of Australia is Canberra.";
const result = await metric.measure(query, response);
console.log(result);
関連性が低い出力
この出力は、クエリに関連する文脈項目が1つしかないためスコアが低くなります。残りの項目は、応答を裏付けない無関係な情報を含んでいます。
{
score: 0.25,
info: {
reason: "The score is 0.25 because the retrieval context contains statements that are completely irrelevant to the input question about the capital of Australia. For instance, 'The Great Barrier Reef is in Australia' and 'Coral reefs need warm water to survive' do not provide any geographical or political information related to the capital, thus failing to address the inquiry."
}
}
メトリクスの設定
クエリに関連する背景情報を表す context
配列を指定して、ContextRelevancyMetric
のインスタンスを作成できます。スコアの範囲を定義する scale
などのオプションパラメータも設定できます。
const metric = new ContextRelevancyMetric(openai("gpt-4o-mini"), {
context: [""],
scale: 1
});
設定オプションの全一覧については、ContextRelevancyMetric を参照してください。
結果の理解
ContextRelevancyMetric
は次の形式の結果を返します:
{
score: number,
info: {
reason: string
}
}
関連度スコア
関連度スコアは 0 から 1 の範囲です:
- 1.0: 完全に関連 — すべてのコンテキストがクエリに直接関連。
- 0.7–0.9: 高い関連 — ほとんどのコンテキストがクエリに関連。
- 0.4–0.6: 一部関連 — 一部のコンテキストがクエリに関連。
- 0.1–0.3: 低い関連 — ごく一部のコンテキストのみがクエリに関連。
- 0.0: 無関連 — クエリに関連するコンテキストがない。
関連情報
スコアの根拠となる説明。以下の詳細を含みます:
- 入力クエリとの関連性
- コンテキストからの記述の抽出
- 応答に対する有用性
- コンテキストの総合的な品質