HuggingFace镜像/hallucination_evaluation_model
模型介绍文件和版本分析
下载使用量0

亮点:

  • HHEM-2.1-Open 相较于 HHEM-1.0 有显著改进。
  • HHEM-2.1-Open 性能优于 GPT-3.5-Turbo,甚至超过 GPT-4。
  • HHEM-2.1-Open 可在消费级硬件上运行,32 位精度下占用内存空间不足 600MB,在现代 x86 CPU 上处理 2k token 输入耗时约 1.5 秒。

HHEM-2.1-Open 在使用方法上引入了重大变更。请根据下方的新用法更新您的代码。我们正在努力使其与 HuggingFace 的推理端点兼容。对于由此带来的不便,我们深表歉意。

HHEM-2.1-Open 是 [HHEM-1.0-Open] 的重大升级版本,后者由 Vectara 于 2023 年 11 月推出。HHEM 模型系列专为检测大型语言模型(LLMs)中的幻觉现象而设计。它们在构建检索增强生成(RAG)应用方面特别有用,在这类应用中,LLM 会对一组事实进行总结,而 HHEM 可用于衡量该总结与事实的事实一致性程度。

[无需编码,直接在浏览器中试用 HHEM-2.1-Open]

幻觉检测基础

我们所说的“存在幻觉”或“事实不一致”,是指某一文本(待判断的假设)未得到另一文本(给定的证据/前提)的支持。要判断一个文本是否存在幻觉,始终需要两个文本。在 RAG(检索增强生成)应用中,LLM 会获得从某些数据集检索到的若干文本片段(通常称为事实或上下文),如果生成的总结(假设)未得到这些事实(证据)的支持,就表明存在幻觉。

RAG 中一种常见的幻觉类型是内容真实但属于幻觉。 例如,给定前提“法国的首都是柏林”,假设“法国的首都是巴黎”就属于幻觉——尽管这一说法符合世界知识。当 LLMs 生成内容时,若不是基于 RAG 检索过程提供的文本数据,而是基于其预训练知识,就会出现这种情况。

此外,幻觉检测具有“不对称性”,即不满足交换律。例如,给定前提“我访问了美国”,假设“我访问了爱荷华州”会被视为存在幻觉;但反过来,假设“我访问了美国”在给定前提“我访问了爱荷华州”时则被认为是一致的。

使用 HHEM-2.1-Open

HHEM-2.1 与 HHEM-1.0 相比有一些不兼容的更改。您原来适用于 HHEM-1(2023 年 11 月版本)的代码将不再适用。尽管我们正在努力实现向后兼容,但请您先按照以下新的使用说明进行操作。

这里我们提供了几种在 transformers 库中使用 HHEM-2.1-Open 的方法。

您可能会遇到一条警告消息:“Token indices sequence length is longer than the specified maximum sequence length”。请忽略此消息,这是继承自基础模型 T5-base 的问题。

与 AutoModel 配合使用

这是使用 HHEM-2.1-Open 最端到端且开箱即用的方式。它接受一个由(前提,假设)对组成的列表作为输入,并为每对返回一个 0 到 1 之间的分数,其中 0 表示假设完全没有得到前提的支持,1 表示假设完全得到前提的支持。

from transformers import AutoModelForSequenceClassification
import torch
import torch_npu

device = torch.device('npu:0')
pairs = [ # Test data, List[Tuple[str, str]]
    ("The capital of France is Berlin.", "The capital of France is Paris."), # factual but hallucinated
    ('I am in California', 'I am in United States.'), # Consistent
    ('I am in United States', 'I am in California.'), # Hallucinated
    ("A person on a horse jumps over a broken down airplane.", "A person is outdoors, on a horse."),
    ("A boy is jumping on skateboard in the middle of a red bridge.", "The boy skates down the sidewalk on a red bridge"),
    ("A man with blond-hair, and a brown shirt drinking out of a public water fountain.", "A blond man wearing a brown shirt is reading a book."),
    ("Mark Wahlberg was a fan of Manny.", "Manny was a fan of Mark Wahlberg.")
]

# Step 1: Load the model
model = AutoModelForSequenceClassification.from_pretrained(
    'hallucination_evaluation_model', trust_remote_code=True).to(device)

# Step 2: Use the model to predict
model.predict(pairs) # note the predict() method. Do not do model(pairs). 
# tensor([0.0111, 0.6474, 0.1290, 0.8969, 0.1846, 0.0050, 0.0543])

与 pipeline 结合使用

在 transformers 库中常用的 pipeline 类中,您需要使用我们训练模型时采用的提示模板手动准备数据。HHEM-2.1-Open 具有两个输出神经元,分别对应标签 hallucinated 和 consistent。在以下示例中,我们将要求 pipeline 返回两个标签的分数(通过设置 top_k=None,即以前的 return_all_scores=True),然后提取 consistent 标签的分数。

from transformers import pipeline, AutoTokenizer
import torch
import torch_npu

device = torch.device('npu:0')
pairs = [ # Test data, List[Tuple[str, str]]
    ("The capital of France is Berlin.", "The capital of France is Paris."),
    ('I am in California', 'I am in United States.'),
    ('I am in United States', 'I am in California.'),
    ("A person on a horse jumps over a broken down airplane.", "A person is outdoors, on a horse."),
    ("A boy is jumping on skateboard in the middle of a red bridge.", "The boy skates down the sidewalk on a red bridge"),
    ("A man with blond-hair, and a brown shirt drinking out of a public water fountain.", "A blond man wearing a brown shirt is reading a book."),
    ("Mark Wahlberg was a fan of Manny.", "Manny was a fan of Mark Wahlberg.")
]

# Prompt the pairs
prompt = "<pad> Determine if the hypothesis is true given the premise?\n\nPremise: {text1}\n\nHypothesis: {text2}"
input_pairs = [prompt.format(text1=pair[0], text2=pair[1]) for pair in pairs]

# Use text-classification pipeline to predict
classifier = pipeline(
            "text-classification",
            model='hallucination_evaluation_model',
            tokenizer=AutoTokenizer.from_pretrained('google/flan-t5-base'),
            trust_remote_code=True
        ).to(device)
full_scores = classifier(input_pairs, top_k=None) # List[List[Dict[str, float]]]

# Optional: Extract the scores for the 'consistent' label
simple_scores = [score_dict['score'] for score_for_both_labels in full_scores for score_dict in score_for_both_labels if score_dict['label'] == 'consistent']

print(simple_scores)
# Expected output: [0.011061512865126133, 0.6473632454872131, 0.1290171593427658, 0.8969419002532959, 0.18462494015693665, 0.005031010136008263, 0.05432349815964699]

当然,借助 pipeline,您还可以通过设置 top_k=1 来获取最可能的标签,即得分最高的标签。

HHEM-2.1-Open 与 HHEM-1.0 对比

HHEM-2.1-Open 与原始 HHEM-1.0 的主要区别在于,HHEM-2.1-Open 拥有无限制的上下文长度,而 HHEM-1.0 的上下文长度上限为 512 个 tokens。更长的上下文长度使得 HHEM-2.1-Open 能够为通常需要超过 512 个 tokens 的 RAG 提供更准确的幻觉检测。

以下表格对比了这两个模型在 AggreFact 和 RAGTruth 基准测试以及 GPT-3.5-Turbo 和 GPT-4 上的表现。具体而言,在 AggreFact 上,我们重点关注其 SOTA 子集(记为 AggreFact-SOTA),该子集包含由 Google 的 T5、Meta 的 BART 和 Google 的 Pegasus 生成的摘要,这三者是 AggreFact 基准测试中最新的三个模型。RAGTruth 的摘要(记为 RAGTruth-Summ)和问答(记为 RAGTruth-QA)子集的结果将分别报告。GPT-3.5-Turbo 和 GPT-4 的版本分别为 01-25 和 06-13。这两个 GPT 模型的零样本结果是使用 本文 中的提示模板获得的。

表 1:在 AggreFact-SOTA 上的性能

modelBalanced AccuracyF1RecallPrecision
HHEM-1.078.87%90.47%70.81%67.27%
HHEM-2.1-Open76.55%66.77%68.48%65.13%
GPT-3.5-Turbo zero-shot72.19%60.88%58.48%63.49%
GPT-4 06-13 zero-shot73.78%63.87%53.03%80.28%

表 2:在 RAGTruth-Summ 上的性能

modelBalanced AccuracyF1RecallPrecision
HHEM-1.053.36%15.77%9.31%51.35%
HHEM-2.1-Open64.42%44.83%31.86%75.58%
GPT-3.5-Turbo zero-shot58.49%29.72%18.14%82.22%
GPT-4 06-13 zero-shot62.62%40.59%26.96%82.09%

表 3:在 RAGTruth-QA 上的性能

modelBalanced AccuracyF1RecallPrecision
HHEM-1.052.58%19.40%16.25%24.07%
HHEM-2.1-Open74.28%60.00%54.38%66.92%
GPT-3.5-Turbo zero-shot56.16%25.00%18.13%40.28%
GPT-4 06-13 zero-shot74.11%57.78%56.88%58.71%

上述表格显示,HHEM-2.1-Open 在 RAGTruth-Summ 和 RAGTruth-QA 基准测试中相较于 HHEM-1.0 有显著提升,而在 AggreFact-SOTA 基准测试中略有下降。然而,在解读这些结果时,请注意 AggreFact-SOTA 是在相对较旧类型的 LLMs 上进行评估的:

  • AggreFact-SOTA 中的 LLMs:T5、BART 和 Pegasus;
  • RAGTruth 中的 LLMs:GPT-4-0613、GPT-3.5-turbo-0613、Llama-2-7B/13B/70B-chat 以及 Mistral-7B-instruct。

HHEM-2.1-Open 与 GPT-3.5-Turbo 及 GPT-4 的对比

从上述表格中我们还可以得出结论,HHEM-2.1-Open 在所有三个基准测试中均优于 GPT-3.5-Turbo 和 GPT-4。HHEM-2.1-Open 相较于 GPT-3.5-Turbo 和 GPT-4 的量化优势总结如下表 4 所示。

表 4:HHEM-2.1-Open 相较于 GPT-3.5-Turbo 和 GPT-4 的平衡准确率百分点

AggreFact-SOTARAGTruth-SummRAGTruth-QA
HHEM-2.1-Open 优于 GPT-3.5-Turbo4.36%5.93%18.12%
HHEM-2.1-Open 优于 GPT-42.64%1.80%0.17%

HHEM-2.1-Open 的另一优势在于其效率。它可在消费级硬件上运行,在 32 位精度下占用不到 600MB 的 RAM 空间,在现代 x86 CPU 上处理 2k token 的输入耗时约 1.5 秒。

HHEM-2.1:HHEM-2.1-Open 更强大的专有对应版本

从名称中您可能已经有所察觉,HHEM-2.1-Open 是高级版 HHEM-2.1 的开源版本。HHEM-2.1(不带“-Open”)仅通过 Vectara 的 RAG-as-a-service 平台提供。HHEM-2.1 与 HHEM-2.1-Open 的主要区别在于,HHEM-2.1 支持三种语言的跨语言能力:英语、德语和法语,而 HHEM-2.1-Open 仅支持英语。“跨语言”意味着这三种语言的任意组合,例如,文档为德语,查询为英语,结果为法语。

为何选择 Vectara 中的 RAG?

Vectara 提供值得信赖的生成式 AI 平台。该平台使组织能够快速创建以其拥有的数据、文档和知识为基础的 AI 助手体验。Vectara 的无服务器 RAG-as-a-Service 还解决了企业采用所需的关键问题,即:减少幻觉、提供可解释性/溯源性、实施访问控制、允许知识的实时更新,以及减轻大型语言模型带来的知识产权/偏见担忧。

要开始体验 HHEM-2.1 的优势,您可以注册免费的 Vectara 账户,系统将在每次查询时自动返回 HHEM-2.1 分数。

以下是一些额外资源:

  1. Vectara API 文档。
  2. 使用 Forrest 的 vektara 包快速入门。
  3. 了解更多关于 Vectara 的 Boomerang 嵌入模型、Slingshot 重排序器 和 Mockingbird LLM

LLM 幻觉排行榜

如果您想随时了解使用此模型评估顶级 LLM 模型的最新测试结果,我们提供了一个[公开排行榜],该排行榜会定期更新,相关结果也可在GitHub 仓库中查看。