コンテキスト精度の評価
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
を使用して、レスポンスが提供されたコンテキストの中で最も関連性の高い部分に基づいているかどうかを評価します。メトリックは query
と response
を受け取り、スコアと、その理由を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
高精度の例
この例では、応答はクエリに直接関係するコンテキストのみに基づいています。すべてのコンテキストが回答を支えており、その結果、精度スコアは高くなります。
import { openai } from "@ai-sdk/openai";
import { ContextPrecisionMetric } from "@mastra/evals/llm";
const metric = new ContextPrecisionMetric(openai("gpt-4o-mini"), {
context: [
"Photosynthesis converts sunlight into energy.",
"Plants use chlorophyll for photosynthesis.",
"Photosynthesis produces oxygen as a byproduct.",
"The process requires sunlight and chlorophyll."
]
});
const query = "What is photosynthesis and how does it work?";
const response = "Photosynthesis is a process where plants convert sunlight into energy using chlorophyll, producing oxygen as a byproduct.";
const result = await metric.measure(query, response);
console.log(result);
高精度の出力
すべてのコンテキスト文が無関係な情報を含まず、クエリへの回答に直接寄与しているため、出力は満点となります。
{
score: 1,
info: {
reason: 'The score is 1 because all context nodes are relevant and provide direct definitions, explanations, and essential components of photosynthesis, with the first node defining the process, followed by supporting details about chlorophyll and oxygen production, all in optimal ranking order.'
}
}
混合精度の例
この例では、応答に1つ以上の関連するコンテキスト項目が使われていますが、無関係または注意をそらす情報も含まれています。これにより、全体的な精度スコアが下がります。
import { openai } from "@ai-sdk/openai";
import { ContextPrecisionMetric } from "@mastra/evals/llm";
const metric = new ContextPrecisionMetric(openai("gpt-4o-mini"), {
context: [
"Volcanoes are openings in the Earth's crust.",
"Volcanoes can be active, dormant, or extinct.",
"Hawaii has many active volcanoes.",
"The Pacific Ring of Fire has many volcanoes."
]
});
const query = "What are the different types of volcanoes?";
const response = "Volcanoes can be classified as active, dormant, or extinct based on their activity status.";
const result = await metric.measure(query, response);
console.log(result);
混合精度の出力
関連するコンテキストを用いてはいるものの、無関係または不要な情報に紛れているため、全体の精度が低下し、中程度のスコアになります。
{
score: 0.58,
info: {
reason: 'The score is 0.58 because while the second and third nodes provided direct definitions and examples of volcano types, the first and fourth nodes were irrelevant, leading to a lower precision score. The relevant nodes were not optimally ordered, as the most useful context was not the first, which affected the overall effectiveness.'
}
}
低精度の例
この例では、応答は提供されたコンテキストのごく一部しか利用していません。コンテキストの大半はクエリと無関係で、その結果、精度スコアが低くなります。
import { openai } from "@ai-sdk/openai";
import { ContextPrecisionMetric } from "@mastra/evals/llm";
const metric = new ContextPrecisionMetric(openai("gpt-4o-mini"), {
context: [
"The Nile River is in Africa.",
"The Nile is the longest river.",
"Ancient Egyptians used the Nile.",
"The Nile flows north."
]
});
const query = "Which direction does the Nile River flow?";
const response = "The Nile River flows northward.";
const result = await metric.measure(query, response);
console.log(result);
低精度の出力
この出力は低いスコアになります。クエリに関連するコンテキストが1つしかなく、残りは無関係で応答に寄与していないためです。
{
score: 0.25,
info: {
reason: "スコアが0.25なのは、ナイル川の流れる方向に関する質問に直接答えているのが4つ目のコンテキストノードだけであり、最初の3つのノードは無関係で有用な情報を提供していないためです。これは、取得されたコンテキスト全体の関連性に大きな制約があることを示しており、その大半が期待される出力に寄与しなかったことを浮き彫りにしています。"
}
}
メトリクスの設定
関連する背景情報を表す context
配列を指定して、ContextPrecisionMetric
インスタンスを作成できます。最大スコアを設定する scale
など、任意のパラメータも設定できます。
const metric = new ContextPrecisionMetric(openai("gpt-4o-mini"), {
context: [""],
scale: 1
});
設定可能なオプションの一覧については、ContextPrecisionMetric を参照してください。
結果の理解
ContextPrecisionMetric
は次の形式の結果を返します:
{
score: number,
info: {
reason: string
}
}
精度スコア
0〜1 の間の精度スコア:
- 1.0: 完全な精度 — すべてのコンテキスト項目が関連しており、活用されている。
- 0.7〜0.9: 高い精度 — ほとんどのコンテキスト項目が関連している。
- 0.4〜0.6: ばらつきのある精度 — 一部のコンテキスト項目が関連している。
- 0.1〜0.3: 低い精度 — ごく一部のコンテキスト項目のみが関連している。
- 0.0: 精度なし — どのコンテキスト項目も関連していない。
精度に関する情報
スコアの説明。以下の詳細を含みます:
- 各コンテキスト項目がクエリおよび応答にどの程度関連しているか。
- 関連する項目が応答に含まれていたかどうか。
- 無関係なコンテキストが誤って含まれていなかったか。
- 提供されたコンテキストに照らして、応答の全体的な有用性と焦点の定まり具合。