ハルシネーション評価
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.
HallucinationMetric
を使用して、応答が提供されたコンテキストのいずれかの部分と矛盾していないかを評価します。このメトリックは query
と response
を受け取り、スコアと理由を含む info
オブジェクトを返します。
インストール
npm install @mastra/evals
ハルシネーションなしの例
この例では、返答は提供されたコンテキストと完全に整合しています。すべての主張は事実に基づいており、出典によって直接裏付けられているため、ハルシネーションスコアは低くなります。
import { openai } from "@ai-sdk/openai";
import { HallucinationMetric } from "@mastra/evals/llm";
const metric = new HallucinationMetric(openai("gpt-4o-mini"), {
context: [
"The iPhone was first released in 2007.",
"Steve Jobs unveiled it at Macworld.",
"The original model had a 3.5-inch screen."
]
});
const query = "When was the first iPhone released?";
const response = "The iPhone was first released in 2007, when Steve Jobs unveiled it at Macworld. The original iPhone featured a 3.5-inch screen.";
const result = await metric.measure(query, response);
console.log(result);
ハルシネーションなしの出力
矛盾がないため、スコアは0になります。すべての記述がコンテキストと一致しており、新たな情報や捏造は含まれていません。
{
score: 0,
info: {
reason: 'The score is 0 because none of the statements from the context were contradicted by the output.'
}
}
混合ハルシネーションの例
この例では、レスポンスに正確な主張と不正確な主張の両方が含まれています。いくつかの詳細はコンテキストと一致しますが、誇張された数値や誤った場所など、他の点はそれと明確に矛盾しています。これらの矛盾により、ハルシネーションスコアが上昇します。
import { openai } from "@ai-sdk/openai";
import { HallucinationMetric } from "@mastra/evals/llm";
const metric = new HallucinationMetric(openai("gpt-4o-mini"), {
context: [
"The first Star Wars movie was released in 1977.",
"It was directed by George Lucas.",
"The film earned $775 million worldwide.",
"The movie was filmed in Tunisia and England."
]
});
const query = "Tell me about the first Star Wars movie.";
const response = "The first Star Wars movie came out in 1977 and was directed by George Lucas. It made over $1 billion at the box office and was filmed entirely in California.";
const result = await metric.measure(query, response);
console.log(result);
混合ハルシネーションの出力
レスポンスの一部がコンテキストと矛盾しているため、この指標は中程度のスコアを割り当てます。正しい事実もある一方で、不正確または作為的な内容も含まれており、全体的な信頼性が低下します。
{
score: 0.5,
info: {
reason: 'スコアが0.5であるのは、出力の4つの記述のうち2つがコンテキスト内の主張と矛盾しており、正確な情報と不正確な情報のバランスが取れていることを示しているためです。'
}
}
完全なハルシネーションの例
この例では、応答がコンテキスト内のすべての重要な事実と矛盾しています。いずれの主張も裏取りできず、示された詳細はすべて事実と異なります。
import { openai } from "@ai-sdk/openai";
import { HallucinationMetric } from "@mastra/evals/llm";
const metric = new HallucinationMetric(openai("gpt-4o-mini"), {
context: [
"The Wright brothers made their first flight in 1903.",
"The flight lasted 12 seconds.",
"It covered a distance of 120 feet."
]
});
const query = "When did the Wright brothers first fly?";
const response = "The Wright brothers achieved their historic first flight in 1908. The flight lasted about 2 minutes and covered nearly a mile.";
const result = await metric.measure(query, response);
console.log(result);
完全なハルシネーションの出力
このメトリクスは、応答内のすべての記述がコンテキストと食い違っているため、スコアを1と割り当てます。詳細は全体的に捏造または不正確です。
{
score: 1,
info: {
reason: 'スコアが1.0なのは、出力の3つの記述すべてがコンテキストと直接矛盾しているためです。初飛行は1908年ではなく1903年、飛行時間は約2分ではなく12秒、距離はほぼ1マイルではなく120フィートです。'
}
}
メトリックの設定
事実に基づくソース資料を表す context
配列を指定すると、HallucinationMetric
のインスタンスを作成できます。最大スコアを制御するための scale
など、オプションのパラメータを設定することも可能です。
const metric = new HallucinationMetric(openai("gpt-4o-mini"), {
context: [""],
scale: 1
});
すべての設定オプションについては、HallucinationMetric を参照してください。
結果の理解
HallucinationMetric
は次の形の結果を返します:
{
score: number,
info: {
reason: string
}
}
ハルシネーションスコア
0〜1 の範囲のスコアです:
- 0.0: ハルシネーションなし — すべての主張が文脈と一致。
- 0.3–0.4: 低レベルのハルシネーション — いくつかの矛盾。
- 0.5–0.6: 中程度/混在 — 複数の矛盾。
- 0.7–0.8: 高レベルのハルシネーション — 多くの矛盾。
- 0.9–1.0: ほぼ全面的なハルシネーション — ほとんどまたはすべての主張が文脈と矛盾。
ハルシネーションの詳細
スコアの理由付け。以下を含みます:
- どの記述が文脈と整合/不整合か
- 矛盾の深刻度と頻度
- 事実からの逸脱度合い
- 応答全体の正確性と信頼性