回答の関連性評価
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.
AnswerRelevancyMetric
を使用して、応答が元のクエリにどれだけ関連しているかを評価します。メトリクスは query
と response
を受け取り、スコアと理由を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
関連性が高い例
この例では、応答が入力クエリに対して、具体的で関連性の高い情報を用いて的確に回答しています。
import { openai } from "@ai-sdk/openai";
import { AnswerRelevancyMetric } from "@mastra/evals/llm";
const metric = new AnswerRelevancyMetric(openai("gpt-4o-mini"));
const query = "What are the health benefits of regular exercise?";
const response = "Regular exercise improves cardiovascular health, strengthens muscles, boosts metabolism, and enhances mental well-being through the release of endorphins.";
const result = await metric.measure(query, response);
console.log(result);
関連性が高い出力
この出力は、無関係な情報を含めずにクエリに正確に答えているため、高いスコアを獲得しています。
{
score: 1,
info: {
reason: 'The score is 1 because the output directly addresses the question by providing multiple explicit health benefits of regular exercise, including improvements in cardiovascular health, muscle strength, metabolism, and mental well-being. Each point is relevant and contributes to a comprehensive understanding of the health benefits.'
}
}
部分的な関連性の例
この例では、応答が質問の一部には答えているものの、直接的に関連しない追加情報が含まれています。
import { openai } from "@ai-sdk/openai";
import { AnswerRelevancyMetric } from "@mastra/evals/llm";
const metric = new AnswerRelevancyMetric(openai("gpt-4o-mini"));
const query = "What should a healthy breakfast include?";
const response =
"A nutritious breakfast should include whole grains and protein. However, the timing of your breakfast is just as important - studies show eating within 2 hours of waking optimizes metabolism and energy levels throughout the day.";
const result = await metric.measure(query, response);
console.log(result);
部分的な関連性の出力
この出力は、質問に部分的にしか答えていないため、スコアが低くなります。関連する情報は含まれている一方で、無関係な詳細が全体の関連性を下げています。
{
score: 0.25,
info: {
reason: 'スコアが 0.25 なのは、健全な朝食の構成要素として全粒穀物とタンパク質に言及しており、直接的な回答になっているためです。しかし、朝食のタイミングやそれが代謝や一日のエネルギーレベルに及ぼす影響に関する追加情報は、質問に直接関係していないため、全体の関連性スコアが低くなっています。'
}
}
関連性が低い例
この例では、応答がクエリに答えておらず、内容もまったく無関係です。
import { openai } from "@ai-sdk/openai";
import { AnswerRelevancyMetric } from "@mastra/evals/llm";
const metric = new AnswerRelevancyMetric(openai("gpt-4o-mini"));
const query = "What are the benefits of meditation?";
const response = "The Great Wall of China is over 13,000 miles long and was built during the Ming Dynasty to protect against invasions.";
const result = await metric.measure(query, response);
console.log(result);
関連性が低い出力
この出力は、クエリに答えておらず関連情報も提供していないため、スコアは0となります。
{
score: 0,
info: {
reason: 'The score is 0 because the output about the Great Wall of China is completely unrelated to the benefits of meditation, providing no relevant information or context that addresses the input question.'
}
}
メトリクスの設定
任意のパラメータを調整することで、AnswerRelevancyMetric
のスコア計算方法をカスタマイズできます。たとえば、uncertaintyWeight
は不確かな応答にどれだけ重みを与えるかを制御し、scale
は到達可能な最大スコアを設定します。
const metric = new AnswerRelevancyMetric(openai("gpt-4o-mini"), {
uncertaintyWeight: 0.3,
scale: 1,
});
設定可能なオプションの一覧は、AnswerRelevancyMetric を参照してください。
結果の理解
AnswerRelevancyMetric
は次の形式の結果を返します:
{
score: number,
info: {
reason: string
}
}
関連性スコア
0〜1 の関連性スコア:
- 1.0: 応答は関連性が高く焦点が定まっており、クエリに完全に答えている。
- 0.7–0.9: 応答は概ねクエリに答えているが、わずかに無関係な内容を含む場合がある。
- 0.4–0.6: 応答は部分的にクエリに答えているが、関連情報と無関係な情報が混在している。
- 0.1–0.3: 応答は関連する内容が最小限で、クエリの意図を大きく外している。
- 0.0: 応答は完全に無関係で、クエリにまったく答えていない。
関連性に関する情報
スコアの説明で、次のような詳細を含む:
- クエリと応答の整合性
- コンテンツの焦点と関連性
- 応答を改善するための提案