忠実性評価
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.
FaithfulnessMetric
を使用すると、提供されたコンテキストにより裏付けられている内容のみを応答が主張しているかを評価できます。このメトリックは query
と response
を受け取り、スコアと理由を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
高忠実度の例
この例では、レスポンスがコンテキストと密接に一致しています。出力内の各記述は検証可能で、提供されたコンテキスト項目によって裏付けられており、高いスコアにつながります。
import { openai } from "@ai-sdk/openai";
import { FaithfulnessMetric } from "@mastra/evals/llm";
const metric = new FaithfulnessMetric(openai("gpt-4o-mini"), {
context: [
"The Tesla Model 3 was launched in 2017.",
"It has a range of up to 358 miles.",
"The base model accelerates 0-60 mph in 5.8 seconds."
]
});
const query = "Tell me about the Tesla Model 3.";
const response = "The Tesla Model 3 was introduced in 2017. It can travel up to 358 miles on a single charge and the base version goes from 0 to 60 mph in 5.8 seconds.";
const result = await metric.measure(query, response);
console.log(result);
高忠実度の出力
この出力は、提供されたすべての情報がコンテキストに直接ひもづけられるため、スコアは1となります。欠落や矛盾する事実はありません。
{
score: 1,
info: {
reason: 'The score is 1 because all claims made in the output are supported by the provided context.'
}
}
忠実性が混在する例
この例では、根拠のある主張とない主張が混在しています。レスポンスの一部はコンテキストで裏付けられていますが、他の部分は元の資料にない新情報を含んでいます。
import { openai } from "@ai-sdk/openai";
import { FaithfulnessMetric } from "@mastra/evals/llm";
const metric = new FaithfulnessMetric(openai("gpt-4o-mini"), {
context: [
"Python was created by Guido van Rossum.",
"The first version was released in 1991.",
"Python emphasizes code readability."
]
});
const query = "What can you tell me about Python?";
const response = "Python was created by Guido van Rossum and released in 1991. It is the most popular programming language today and is used by millions of developers worldwide.";
const result = await metric.measure(query, response);
console.log(result);
忠実性が混在する場合の出力
スコアが低いのは、レスポンスの一部しか検証できないためです。いくつかの主張はコンテキストと一致しますが、他は未確認または範囲外であり、全体の忠実性を下げています。
{
score: 0.5,
info: {
reason: "The score is 0.5 because while two claims are supported by the context (Python was created by Guido van Rossum and Python was released in 1991), the other two claims regarding Python's popularity and usage cannot be verified as they are not mentioned in the context."
}
}
忠実性が低い例
この例では、回答が文脈と直接矛盾しています。どの主張にも裏付けがなく、いくつかは提示された事実と衝突しています。
import { openai } from "@ai-sdk/openai";
import { FaithfulnessMetric } from "@mastra/evals/llm";
const metric = new FaithfulnessMetric(openai("gpt-4o-mini"), {
context: [
"Mars is the fourth planet from the Sun.",
"It has a thin atmosphere of mostly carbon dioxide.",
"Two small moons orbit Mars: Phobos and Deimos."
]
});
const query = "What do we know about Mars?";
const response = "Mars is the third planet from the Sun. It has a thick atmosphere rich in oxygen and nitrogen, and is orbited by three large moons.";
const result = await metric.measure(query, response);
console.log(result);
忠実性が低い出力
各主張が不正確、または文脈と矛盾しているため、スコアは0となります。
{
score: 0,
info: {
reason: "The score is 0 because all claims made in the output contradict the provided context. The output states that Mars is the third planet from the Sun, while the context clearly states it is the fourth. Additionally, it claims that Mars has a thick atmosphere rich in oxygen and nitrogen, contradicting the context's description of a thin atmosphere mostly composed of carbon dioxide. Finally, the output mentions that Mars is orbited by three large moons, while the context specifies that it has only two small moons, Phobos and Deimos. Therefore, there are no supported claims, leading to a score of 0."
}
}
メトリックの構成
評価の事実ベースとなる情報源を定義する context
配列を指定して、FaithfulnessMetric
のインスタンスを作成できます。最大スコアを制御する scale
などの任意パラメータも設定できます。
const metric = new FaithfulnessMetric(openai("gpt-4o-mini"), {
context: [""],
scale: 1
});
設定可能なオプションの一覧は FaithfulnessMetric を参照してください。
結果の理解
FaithfulnessMetric
は次の形式で結果を返します:
{
score: number,
info: {
reason: string
}
}
Faithfulness スコア
Faithfulness スコアは 0 から 1 の範囲です:
- 1.0: すべての主張が正確で、文脈によって直接裏付けられている。
- 0.7–0.9: ほとんどの主張は正しいが、軽微な追加や省略がある。
- 0.4–0.6: 一部の主張は裏付けられるが、他は検証不能。
- 0.1–0.3: 内容の大半が不正確、または裏付けがない。
- 0.0: すべての主張が誤り、または文脈と矛盾している。
Faithfulness 情報
スコアの理由の説明。次の詳細を含む:
- どの主張が検証されたか、あるいは矛盾していたか
- 事実整合性の度合い
- 欠落や捏造の詳細に関する指摘
- 全体的な応答の信頼性の要約