RefChecker

RefChecker

针对大语言模型输出的精细化幻觉检测框架

RefChecker是一个标准化评估框架,用于检测大语言模型(LLM)输出中的细微幻觉。该框架将LLM响应分解为知识三元组,在三种不同背景下进行精细化幻觉检测。项目包括人工标注的基准数据集、模块化架构和自动化检查器,有助于评估和改进LLM输出的事实准确性。RefChecker为研究人员和开发者提供了评估和提高LLM生成内容可靠性的工具。

RefChecker大语言模型幻觉检测评估框架事实性Github开源项目

RefChecker for Fine-grained Hallucination Detection

| 🔥 News | 🤖️ Demo | 🚀 Quick Start | 💾 Benchmark | 📖 Docs |

RefChecker provides a standardized assessment framework to identify subtle hallucinations present in the outputs of large language models (LLMs).

<p align="center"> <img src="imgs/framework.png" alt="RefChecker Framework" style="width:800px"> <br> <b>Figure</b>: RefChecker Framework </p>

🌟 Highlighted Features

  • Finer granularity - RefChecker breakdowns the claims in the LLM’s response into knowledge triplets, as opposed to paragraph, sentence or sub-sentence. Detecting at knowledge triplets will test the truthfulness of facts. Importantly, this finer granularity subsumes other coarse granularity and is therefore more informative and precise. One can arbitrarily roll up the granularity ladder to derive coarse level metrics if needed.
  • Wider Coverage - RefChecker differentiates three distinctive settings based on the quality and quantity of context provided for LLM’s response:
    1. Zero Context: the prompt is a factual question without any context (eg. Open QA).
    2. Noisy Context: the prompt is a question as well as a list of retrieved document (eg. RAG).
    3. Accurate Context: the prompt is a question as well as one document (eg. Summarization).
  • Human Evaluation - RefChecker includes 2.1k human annotated LLM’s responses consist of 300 test samples, each responded by 7 popular LLMs: GPT4, GPT-3.5-Turbo, InstructGPT, Falcon (Falcon-40B-Instruct), Alpaca (Alpaca-7B), LLaMA2(70B-Chat) and Claude 2. We will release the data and results upon approval.
  • Modular Architecture — RefChecker is a 3-stage pipeline, consisting of a claim extractor $E$, a hallucination checker $C$, and aggregation rules $\tau$. They can be invoked and configured individually from command-line. Other than the 3 core stages, there are 3 auxiliary components:
    1. human labeling tool (coming soon) to label claims,
    2. call to search engine for Zero Context setting
    3. a localization model to map each knowledge triple back to the corresponding snippets of the reference.

You can explore RefChecker in the following ways:

  • Demo Website - Setup a website and check your responses with user interfaces.
  • Quick Start - Setup the environment and check your responses in a console.
  • Automatic Checker - Check our automatic hallucination checker with strong performance and efficiency.

<a id='news'></a>

🔥 News

  • [07/22/2024] Add support for joint checking the claims for better checking efficency.
  • [06/24/2024] RefChecker supports most of the LLMs by employing litellm and vllm.
  • [05/23/2024] RefChecker paper is on Arxiv: https://arxiv.org/pdf/2405.14486
  • [12/07/2023] RefChecker 0.1 release.

❤️ Citation

Please check out the paper here: https://arxiv.org/pdf/2405.14486

If you use RefChecker in your work, please cite us:

@article{hu2024refchecker, title={RefChecker: Reference-based Fine-grained Hallucination Checker and Benchmark for Large Language Models}, author={Xiangkun Hu and Dongyu Ru and Lin Qiu and Qipeng Guo and Tianhang Zhang and Yang Xu and Yun Luo and Pengfei Liu and Yue Zhang and Zheng Zhang}, year={2024}, eprint={2405.14486}, archivePrefix={arXiv}, primaryClass={cs.CL} }

<a id='demo_website'></a>

🤖️ Demo Website

<img src="imgs/demo.gif" alter="Demo" style="width:800px">

You can first setup a demo website and then use the web UI to try RefChecker as the animation shows above. There are four steps to perform hallucination detection in it:

  1. Extract Triplets: You can start with typing what you want to check in the top-left box. Then click the Next Step button on the right side. The checker will extract triplets in your text and show them in the bottom-left area.
  2. Gather Reference: You can then add reference text in the top-right box and click the Next Step button. If you don’t have reference text, leave the box empty and click the button anyway. We will retrieve some references with the text to be checked using search engines.
  3. Fact Checking: With the text to be checked and the retrieved reference text, the checker will perform fact checking then. The checking results will be shown in the bottom-left area, with ✅/❌/❓ indicating factual/hallucinatory/neutral. An overall factuality score will be given aside.
  4. Localization: You can then click the Next Step button and the checker will perform triplet localization. You can click the button on the left of each triplet to see the localization result.

<a id='quick_start'></a>

🚀 Quick Start

Setup Environment

First create a python environment using conda or virtualenv. Clone this repo and change path into the root directory. Then install:

pip install -e . python -m spacy download en_core_web_sm

Install optional dependencies to use open source extractors (Mistral, Mixtral) or enable acceleration for RepCChecker.

pip install -e .[open-extractor,repcex]

Code Examples

Choose Models for the Extractor and Checker

We use litellm as to invoke the LLMs. Please check the document for how to setup the model for different LLM providers: https://docs.litellm.ai/docs/providers . We give some examples below:

  • Amazon Bedrock

Setup the enviroment variables if you are not using AWS EC2 instance

If you are using AWS EC2, make sure your region has the access to the model

export AWS_ACCESS_KEY_ID=<your_aws_access_key_id> export AWS_SECRET_ACCESS_KEY=<your_aws_secret_access_key> export AWS_REGION_NAME=<your_aws_region_name>
import os from refchecker import LLMExtractor, LLMChecker # Claude 3 Sonnet from Amazon Bedrock model = 'bedrock/anthropic.claude-3-sonnet-20240229-v1:0' extractor = LLMExtractor(model=model, batch_size=8) checker = LLMChecker(model=model, batch_size=8)

You can also setup the enviroment variables in terminal to avoid disclosing these information in the code:

export AWS_ACCESS_KEY_ID=<your_aws_access_key_id> export AWS_SECRET_ACCESS_KEY=<your_aws_secret_access_key> export AWS_REGION_NAME=<your_aws_region_name>
  • OpenAI
import os from refchecker import LLMExtractor, LLMChecker os.environ["OPENAI_API_KEY"] = "<your_openai_api_key>" # GPT-4o from OpenAI model = 'gpt-4o' extractor = LLMExtractor(model=model, batch_size=8) checker = LLMChecker(model=model, batch_size=8)
  • Open source LLMs

Please use vllm to setup the API server for open source LLMs. For example, use the following command to deploy a Llama 3 8B hosted on HuggingFace:

python -m vllm.entrypoints.openai.api_server \ --model meta-llama/Meta-Llama-3-8B-Instruct \ --tensor-parallel-size 8 \ --dtype auto \ --api-key sk-123456789 \ --gpu-memory-utilization 0.9 \ --port 5000

Setup the api key:

export OPENAI_API_KEY=sk-123456789

Then we can initilize the extractor and checker with api_base:

import os from refchecker import LLMExtractor, LLMChecker # Note the prefix "openai/" here model = "openai/meta-llama/Meta-Llama-3-8B-Instruct" api_base = "http://0.0.0.0:5000/v1" extractor = LLMExtractor(model=model, batch_size=8, api_base=api_base) checker = LLMChecker(model=model, batch_size=8, api_base=api_base)
  • Fine-tuned Mistral 7B Claim Extractor

We fine-tuned a Mistral 7B model for claim extraction. Deploy it with vllm:

python -m vllm.entrypoints.openai.api_server \ --model dongyru/Mistral-7B-Claim-Extractor \ --tensor-parallel-size 8 \ --dtype auto \ --api-key sk-123456789 \ --gpu-memory-utilization 0.9 \ --port 5000

Then we can initilize the extractor as follows:

extractor = LLMExtractor( model="openai/dongyru/Mistral-7B-Claim-Extractor", batch_size=8, api_base="http://0.0.0.0:5000/v1" )
  • Non-LLM based Checkers

We also offer non-LLM checker for efficent checking:

from refchecker import AlignScoreChecker, NLIChecker # Details see paper: https://arxiv.org/abs/2305.16739 checker = AlignScoreChecker(device=0, batch_size=128) # See https://huggingface.co/ynie/roberta-large-snli_mnli_fever_anli_R1_R2_R3-nli checker = NLIChecker(device=0, batch_size=128)

Run Extraction and Checking

Both the extractor and checker takes a batch of inputs:

# Batch of questions (optional) questions = ['question 1', 'question 2'] # Batch of model responses responses = ['response 1', 'response 2'] extraction_results = extractor.extract( batch_responses=responses, batch_questions=questions, max_new_tokens=1000 ) batch_claims = [[c.content for c in res.claims] for res in extraction_results] references = ['reference 1', 'reference 2'] batch_labels = checker.check( batch_claims=batch_claims, batch_references=references, max_reference_segment_length=0 )

The extraction_results is a list of RCClaim objects defined in refchecker/base.py.

Command Line Interface

We provide a command-line interface to run RefChecker in a console:

usage: refchecker-cli [-h] --input_path INPUT_PATH --output_path OUTPUT_PATH
                     [--cache_dir CACHE_DIR]
                     [--extractor_name EXTRACTOR_NAME]
                     [--extractor_max_new_tokens EXTRACTOR_MAX_NEW_TOKENS]
                     [--claim_format {triplet, subsentence}]
                     [--checker_name CHECKER_NAME]
                     [--extractor_api_base EXTRACTOR_API_BASE]
                     [--checker_api_base CHECKER_API_BASE]
                     [--repc_classifier_name {svm,svm_ensemble,nn,nn_ensemble}]
                     [--retriever_name {google}]
                     [--aggregator_name {strict,soft,major}]
                     [--use_retrieval]
                     [--batch_size_extractor BATCH_SIZE_EXTRACTOR]
                     [--batch_size_checker BATCH_SIZE_CHECKER]
                     [{extract,check,extract-check}]

positional arguments:
  {extract,check,extract-check}
     extract:       Extract claims from provided responses.
     check:         Check whether the provided claims are factual.
     extract-check: Extract claims and check whether they are factual.

options:
  -h, --help  show this help message and exit
  --input_path INPUT_PATH
              Input path to the json file.
  --output_path OUTPUT_PATH
              Output path to the result json file.
  --cache_dir CACHE_DIR
              Path to the cache directory. Default: ./.cache.
  --extractor_name EXTRACTOR_NAME
              Model used for extracting claims. Default: bedrock/anthropic.claude-3-sonnet-20240229-v1:0
  --extractor_max_new_tokens EXTRACTOR_MAX_NEW_TOKENS
              Max generated tokens of the extractor, set a larger value for longer documents. Default: 500
  --claim_format {triplet, subsentence}
              The format of the extracted claims. Default: triplet
  --checker_name CHECKER_NAME
              Model used for checking whether the claims are factual. Default: bedrock/anthropic.claude-3-sonnet-20240229-v1:0
  --extractor_api_base EXTRACTOR_API_BASE
              API base URL if using vllm for deploying the extractor.
  --checker_api_base CHECKER_API_BASE
              API base URL if using vllm for deploying the checker
  --repc_classifier_name {svm,svm_ensemble,nn,nn_ensemble}
              Classifier Model used for RepC checker, only valid when RepC checker is used.
              Default: nn_ensemble, neural network classifier with layer ensemble.
  --retriever_name {google}
              Model used for retrieving reference (currently only google is supported).
              Default: google.
  --aggregator_name {strict,soft,major}
              Aggregator used for aggregating the results from multiple triplets.
              Default: soft.
              *  strict: If any of the triplets is Contradiction, the response is
              Contradiction. If all of the triplets are Entailment, the response is
              Entailment. Otherwise, the response is Neutral.
              *  soft:   The ratio of each category is calculated.
              *  major:  The category with the most votes is selected.
  --use_retriever  
              Whether to use retrieval to find the reference for checking. Required
              if the reference field in input data is not provided.
  --serper_api_key SERPER_API_KEY
              Path to the serper api key file. Required if the google retriever is
              used.
  --batch_size_extractor BATCH_SIZE_EXTRACTOR
              Batch size for batching inference of eatractor. Default: 16.
  --batch_size_checker BATCH_SIZE_CHECKER
              Batch size for batching inference of checker. Default: 16.

To extract claim triplets from LLM-generated responses, do:

refchecker-cli extract \ --input_path {INPUT_PATH} \ --output_path {OUTPUT_PATH} \ --extractor_name {EXTRACTOR_NAME} \ --extractor_api_base {EXTRACTOR_API_BASE}

The input json file contains a list of

{ "response": "", # required, the response to be checked "question": "", # optional if the question is not important (e.g., in summarization) "reference": "", # required, the reference for checking ... }

In the output json file, each item is added with a claims field, containing a list of [head, relation, tail].

To check hallucinations at triplet level, do:

refchecker-cli check \ --input_path {INPUT_PATH} \ --output_path {OUTPUT_PATH} \ --checker_name {CHECKER_NAME} \ --checker_api_base {CHECKER_API_BASE} \ --aggregator_name {strict,soft,major}

The input json file contains a list of

{ "response": "", # required, the response to be checked "claims": [ ["head1", "relation1", "tail1"], ["head2", "relation2", "tail2"], ... ] # required, the corresponding triplets of the response "reference": "", # optional if a retriever is used to get reference ... }

In the output json file, each item is added with the following fields:

{ "Y": Union[str, dict], # aggregated predictions on the whole response "ys": [ "Entailment", "Neutral", "Contradiction", ... ] # checker predictions on each triplet "reference": "", # added if a retriever is used to get reference ... }

The format of aggregated predictions Y depends on the selected aggregator. It is a str as “Entailment”, “Neutral”, or “Contradiction” if strict or major aggregators are used. It is a dict containing ratios of each category if the soft aggregator is used. We additionally include a special category “Abstain” introduced in Evaluation Metric.

Note that the retriever is required in the zero-context setting, where no reference is provided by users. You can activate it by adding the --use_retriever flag and specifying --retriever_name. Currently we only support a google-based retriever. Feel free to try your own retrieval system and welcome to contribute.

For using the google retriever and/or the OpenAI models, you should provide corresponding API keys by specifying --serper_api_key and/or --openai_key.

Finally, you can use the whole extraction and checking pipeline by:

refchecker-cli extract-check \ --input_path {INPUT_PATH} \ --output_path {OUTPUT_PATH} \ --extractor_name {EXTRACTOR_NAME} \ --checker_name {CHECKER_NAME} \

编辑推荐精选

问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

openai-agents-python

openai-agents-python

OpenAI Agents SDK,助力开发者便捷使用 OpenAI 相关功能。

openai-agents-python 是 OpenAI 推出的一款强大 Python SDK,它为开发者提供了与 OpenAI 模型交互的高效工具,支持工具调用、结果处理、追踪等功能,涵盖多种应用场景,如研究助手、财务研究等,能显著提升开发效率,让开发者更轻松地利用 OpenAI 的技术优势。

下拉加载更多