综合介绍
LettuceDetect 是 KRLabsOrg 开发的一个轻量级开源工具,专门用于检测检索增强生成(RAG)系统中生成的幻觉内容。它通过对比上下文、问题和回答,识别回答中不受上下文支持的部分,帮助开发者提升 RAG 系统的准确性。该工具基于 ModernBERT 技术,支持 4096 个 token 的长上下文处理,相比传统编码器模型效率更高,同时计算成本远低于大型语言模型(LLM)。LettuceDetect 在 RAGTruth 数据集上表现优异,大模型版本 F1 分数达 79.22%,超越多种现有方案。项目以 MIT 许可发布,代码和模型免费开放,适合需要优化 AI 生成内容可靠性的用户。
功能列表
- token级检测:逐词分析回答,精确标记幻觉部分。
- span级检测:识别回答中完整的幻觉片段,输出位置和置信度。
- 长上下文处理:支持 4096 个 token 的上下文,适用于复杂任务。
- 高效推理:提供 150M 和 396M 两种模型,单 GPU 上每秒处理 30-60 个样本。
- 开源集成:通过 pip 安装,提供简洁的 Python API,易于嵌入 RAG 系统。
- 多种输出格式:支持 token 级概率和 span 级预测结果,便于分析。
- 性能基准:在 RAGTruth 数据集上提供详细评估数据,方便对比。
使用帮助
LettuceDetect 是一个轻量高效的工具,用户只需简单安装即可快速上手。以下是详细的安装和使用指南,帮助你从零开始掌握其功能。
安装流程
- 准备 Python 环境
确保系统已安装 Python 3.8 或以上版本,并具备 pip 工具。建议使用虚拟环境:python -m venv venv source venv/bin/activate # Linux/Mac venv\Scripts\activate # Windows
- 安装 LettuceDetect
从 PyPI 安装最新版本:pip install lettucedetect
安装过程会自动下载核心依赖,包括 ModernBERT 模型。
- 验证安装
在 Python 终端运行以下代码,检查是否成功:from lettucedetect.models.inference import HallucinationDetector print("LettuceDetect 安装成功!")
基本使用方法
LettuceDetect 提供简洁的 Python API,只需几行代码即可检测幻觉。以下是一个基础示例:
示例代码
from lettucedetect.models.inference import HallucinationDetector
# 初始化检测器
detector = HallucinationDetector(
method="transformer",
model_path="KRLabsOrg/lettucedect-base-modernbert-en-v1"
)
# 输入数据
contexts = ["France is a country in Europe. The capital of France is Paris. The population of France is 67 million."]
question = "What is the capital of France? What is the population of France?"
answer = "The capital of France is Paris. The population of France is 69 million."
# 执行 span 级检测
predictions = detector.predict(
context=contexts,
question=question,
answer=answer,
output_format="spans"
)
# 输出结果
print("检测结果:", predictions)
输出示例:
检测结果: [{'start': 31, 'end': 71, 'confidence': 0.994, 'text': ' The population of France is 69 million.'}]
结果显示,“人口是6900万”被标记为幻觉,因为上下文表明人口为6700万。
主要功能操作流程
1. 初始化检测器
- 参数说明:
method
:目前仅支持 "transformer"。model_path
:可选KRLabsOrg/lettucedect-base-modernbert-en-v1
(150M)或KRLabsOrg/lettucedect-large-modernbert-en-v1
(396M)。
- 操作:根据任务需求选择模型,base 版轻量快速,large 版精度更高。
2. 准备输入
- 上下文(context):传入包含背景信息的字符串列表,需为英文。
- 问题(question):输入具体问题,需与上下文相关。
- 回答(answer):输入 RAG 系统生成的回答。
- 注意:确保上下文总长度不超过 4096 个 token。
3. 运行检测
- 调用方法:使用
detector.predict()
。 - 输出格式:
"spans"
:返回幻觉片段的起止位置、文本和置信度。"tokens"
:返回每个 token 的幻觉概率。
- 操作:选择适合的输出格式,span 级适合快速查看,token 级适合深入分析。
4. 分析结果
- span 级输出:检查每个幻觉片段的
text
和confidence
,置信度接近 1 表示高概率幻觉。 - token 级输出:逐词查看
prob
值,判断具体错误点。 - 后续处理:根据结果优化 RAG 系统或记录问题。
特色功能详解
token 级检测
LettuceDetect 支持逐词分析,提供精细的幻觉检测:
predictions = detector.predict(
context=contexts,
question=question,
answer=answer,
output_format="tokens"
)
print(predictions)
输出示例:
检测结果: [{'token': '69', 'pred': 1, 'prob': 0.95}, {'token': 'million', 'pred': 1, 'prob': 0.95}]
这表明“69 million”被标记为幻觉,适合需要精确调试的场景。
长上下文支持
对于长文本任务,LettuceDetect 可处理 4096 个 token:
contexts = ["A long context repeated many times..." * 50]
predictions = detector.predict(context=contexts, question="...", answer="...")
只需确保输入在限制范围内即可。
Streamlit 演示
LettuceDetect 提供交互式演示:
- 安装 Streamlit:
pip install streamlit
- 运行演示:
streamlit run demo/streamlit_demo.py
- 在浏览器中输入上下文、问题和回答,实时查看检测结果。
高级使用
训练自定义模型
- 下载 RAGTruth 数据集(链接),放入
data/ragtruth
文件夹。 - 预处理数据:
python lettucedetect/preprocess/preprocess_ragtruth.py --input_dir data/ragtruth --output_dir data/ragtruth
- 训练模型:
python scripts/train.py --data_path data/ragtruth/ragtruth_data.json --model_name answerdotai/ModernBERT-base --output_dir outputs/hallucination_detector --batch_size 4 --epochs 6 --learning_rate 1e-5
性能优化
- GPU 加速:安装 PyTorch CUDA 版本,提升推理速度。
- 批量处理:将多个样本放入
contexts
列表,一次性检测。
注意事项
- 输入必须为英文,暂不支持其他语言。
- 确保网络畅通,以便首次运行时下载模型。
通过以上步骤,用户可以轻松使用 LettuceDetect 检测 RAG 系统幻觉,提升生成内容的可靠性。