Skip to main content

幻觉评分器

🌐 Hallucination Scorer

createHallucinationScorer() 函数通过将大语言模型(LLM)的输出与提供的上下文进行比较,来评估其生成的信息是否符合事实。该评分器通过识别上下文与输出之间的直接矛盾来衡量幻觉(hallucination)。

🌐 The createHallucinationScorer() function evaluates whether an LLM generates factually correct information by comparing its output against the provided context. This scorer measures hallucination by identifying direct contradictions between the context and the output.

参数
Direct link to 参数

🌐 Parameters

createHallucinationScorer() 函数接受一个包含以下属性的单一选项对象:

🌐 The createHallucinationScorer() function accepts a single options object with the following properties:

model:

LanguageModel
Configuration for the model used to evaluate hallucination.

scale:

number
= 1
Maximum score value.

此函数返回 MastraScorer 类的一个实例。.run() 方法接受与其他评分器相同的输入(参见 MastraScorer 参考),但返回值包括如下所述的特定于 LLM 的字段。

🌐 This function returns an instance of the MastraScorer class. The .run() method accepts the same input as other scorers (see the MastraScorer reference), but the return value includes LLM-specific fields as documented below.

.run() 返回
Direct link to .run() 返回

🌐 .run() Returns

runId:

string
The id of the run (optional).

preprocessStepResult:

object
Object with extracted claims: { claims: string[] }

preprocessPrompt:

string
The prompt sent to the LLM for the preprocess step (optional).

analyzeStepResult:

object
Object with verdicts: { verdicts: Array<{ statement: string, verdict: 'yes' | 'no', reason: string }> }

analyzePrompt:

string
The prompt sent to the LLM for the analyze step (optional).

score:

number
Hallucination score (0 to scale, default 0-1).

reason:

string
Detailed explanation of the score and identified contradictions.

generateReasonPrompt:

string
The prompt sent to the LLM for the generateReason step (optional).

评分详情
Direct link to 评分详情

🌐 Scoring Details

评分者通过矛盾检测和无根据陈述分析来评估幻觉。

🌐 The scorer evaluates hallucination through contradiction detection and unsupported claim analysis.

评分流程
Direct link to 评分流程

🌐 Scoring Process

  1. 分析事实内容:
    • 从上下文中提取语句
    • 识别数值和日期
    • 映射语句关系
  2. 分析输出中的幻觉情况:
    • 与上下文语句进行比较
    • 将直接冲突标记为幻觉
    • 将不支持的主张识别为虚构
    • 评估数值准确性
    • 考虑近似情境
  3. 计算幻觉得分:
    • 统计幻觉陈述(矛盾和无支持的主张)
    • 除以总声明数
    • 按配置范围缩放

最终得分:(hallucinated_statements / total_statements) * scale

🌐 Final score: (hallucinated_statements / total_statements) * scale

重要考虑因素
Direct link to 重要考虑因素

🌐 Important Considerations

  • 在上下文中不存在的主张被视为虚构
  • 主观的说法都是幻想,除非有明确的支持
  • 在特定上下文中,对事实使用推测性语言(“可能”、“也许”)是允许的
  • 关于背景中不存在的事实的推测性语言被视为幻觉
  • 空输出会导致零幻觉
  • 数值评估考虑:
    • 适合尺度的精度
    • 上下文近似
    • 明确的精确度指标

分数解释
Direct link to 分数解释

🌐 Score interpretation

幻觉评分介于 0 到 1 之间:

🌐 A hallucination score between 0 and 1:

  • 0.0:无幻觉——所有陈述均与上下文匹配。
  • 0.3–0.4:低幻觉——少量矛盾。
  • 0.5–0.6:混合幻觉——存在多处矛盾。
  • 0.7–0.8:高度幻觉 — 许多矛盾。
  • 0.9–1.0:完全虚构——大部分或所有说法都与上下文相矛盾。

注意: 评分代表幻觉程度——分数越低表示与提供的上下文的事实一致性越高

示例
Direct link to 示例

🌐 Example

根据提供的上下文评估代理的回答是否存在幻觉:

🌐 Evaluate agent responses for hallucinations against provided context:

src/example-hallucination.ts
import { runEvals } from "@mastra/core/evals";
import { createHallucinationScorer } from "@mastra/evals/scorers/prebuilt";
import { myAgent } from "./agent";

// Context is typically populated from agent tool calls or RAG retrieval
const scorer = createHallucinationScorer({
model: "openai/gpt-4o",
});

const result = await runEvals({
data: [
{
input: "When was the first iPhone released?",
},
{
input: "Tell me about the original iPhone announcement.",
},
],
scorers: [scorer],
target: myAgent,
onItemComplete: ({ scorerResults }) => {
console.log({
score: scorerResults[scorer.id].score,
reason: scorerResults[scorer.id].reason,
});
},
});

console.log(result.scores);

有关 runEvals 的更多详细信息,请参阅 runEvals 参考

🌐 For more details on runEvals, see the runEvals reference.

要将此评分器添加到代理中,请参阅 评分器概览 指南。

🌐 To add this scorer to an agent, see the Scorers overview guide.

🌐 Related