lettucedetect-code-hallucination
收藏资源简介:
LettuceDetect Grounded Hallucination Dataset是一个用于幻觉检测的基准数据集,专注于评估大型语言模型在基于结构化上下文生成回答时的幻觉问题。该数据集是LettuceDetect数据收集项目的一部分,包含77,608个样本,涵盖五种不同的结构化上下文来源:源代码(来自SWE-bench代码库)、开发者工具输出(如文件转储、日志、git命令输出等)、学术论文(来自ACL论文的检索摘要)、GitHub README文件(通过GitHub API获取)以及维基百科文章。每种上下文对应不同的模态:代码、工具输出和Markdown文本。每个样本由一个基于上下文的提示和一个LLM生成的回答组成,回答分为正确回答和包含最小化扰动幻觉的回答。幻觉样本(占总数25%)在字符级别进行了精确标注,标注采用统一的幻觉分类法,包含三个顶级类别:矛盾(如数值、时间、实体错误)、无依据的添加(如额外声明)和虚构引用(如引用不存在的函数或章节)。数据以JSONL格式存储,包含提示、回答、幻觉标注列表、统一分类类别和子类别、数据源标识、上下文模态、语言及元数据等字段。数据集已划分为训练集(69,389个样本)、验证集(2,797个样本)和测试集(5,422个样本),各子数据集的样本数量分别为:代码(23,830)、工具输出(11,365)、ACL论文(5,355)、README(13,803)和维基百科(23,255)。数据生成过程涉及先生成正确的基于上下文的回答,然后使用Qwen/Qwen3.6-35B-A3B模型注入局部幻觉,并产生精确的字符级标注跨度。该数据集适用于多种自然语言处理任务,特别是令牌分类(用于幻觉跨度检测)和文本分类(用于幻觉样本识别),可用于训练和评估幻觉检测模型,尤其是在检索增强生成(RAG)场景中,支持按数据源或上下文模态进行过滤。
The LettuceDetect Grounded Hallucination Dataset is a benchmark dataset for hallucination detection, focusing on evaluating large language models hallucinations when generating responses based on structured contexts. It is part of the LettuceDetect data collection project. The dataset contains 77,608 samples, covering five different structured context sources: source code (from SWE-bench repositories), developer tool outputs (such as file dumps, logs, git command outputs, etc.), academic papers (retrieved summaries from ACL papers), GitHub README files (obtained via the GitHub API), and Wikipedia articles. Each context corresponds to different modalities: code, tool output, and Markdown text. Each sample consists of a context-based prompt and an LLM-generated response, categorized into correct responses and those containing minimally perturbed hallucinations. The hallucination samples (25% of the total) are precisely annotated at the character level, using a unified hallucination taxonomy with three top-level categories: contradiction (e.g., numerical, temporal, entity errors), unsupported addition (e.g., extra claims), and fabricated reference (e.g., referencing non-existent functions or sections). The data is stored in JSONL format, with fields including prompt (containing context and user request), answer, list of hallucination labels (empty for clean samples), unified classification categories and subcategories, dataset identifier, context modality, language (English), and metadata with source-specific information (e.g., document ID, hallucination status, injection model). The dataset is split into training (69,389 samples), validation (2,797 samples), and test (5,422 samples) sets. The sample counts for each subset are: code (23,830), tool output (11,365), ACL papers (5,355), README (13,803), and Wikipedia (23,255). The data generation process involves first generating correct context-based responses, then injecting localized hallucinations using the Qwen/Qwen3.6-35B-A3B model, and producing precise character-level annotation spans. This dataset is suitable for various natural language processing tasks, particularly token classification (for hallucination span detection) and text classification (for hallucination sample identification), and can be used to train and evaluate hallucination detection models, especially in retrieval-augmented generation (RAG) scenarios, supporting filtering by data source or context modality.
LettuceDetect Grounded Hallucination Dataset 概述
该数据集包含 LLM 响应中基于结构化上下文的 token 级幻觉标注,涵盖五种来源:源代码、开发者工具输出、学术论文、GitHub README 和维基百科。它是 LettuceDetect 数据收集项目的一部分。
每个样本将结构化上下文与 LLM 的回答配对,回答可以是正确的,也可以包含一个经过最小扰动、以字符跨度标注的幻觉。所有跨度使用统一的分类体系,因此不同来源共享同一个标签空间。
数据来源
dataset |
context_modality |
上下文 | 来源 |
|---|---|---|---|
lettucedetect-code |
code |
基于基准提交的仓库源文件 | SWE-bench |
lettucedetect-tool-output |
tool_output |
开发者工具输出(文件转储、日志、grep、git、构建/测试、curl 等) | tool-output-extraction-swebench |
lettucedetect-acl |
markdown |
从 ACL 论文中检索的前 k 个摘要(RAG) | acl-verbatim-spans |
lettucedetect-readme |
markdown |
GitHub README 章节 | 通过 GitHub API 获取的流行仓库 |
lettucedetect-wikipedia |
markdown |
维基百科文章章节 | open-wikipedia-markdown |
统计数据
整体数据集划分:
| 划分 | 总数 | 含幻觉 | 干净 |
|---|---|---|---|
| 训练集 | 69,389 | 17,466 | 51,923 |
| 验证集 | 2,797 | 708 | 2,089 |
| 测试集 | 5,422 | 1,411 | 4,011 |
| 总计 | 77,608 | 19,585 (25%) | 58,023 |
按来源统计:
| 来源 | 总数 | 含幻觉比例 |
|---|---|---|
lettucedetect-code |
23,830 | 28% |
lettucedetect-tool-output |
11,365 | 22% |
lettucedetect-acl |
5,355 | 27% |
lettucedetect-readme |
13,803 | 23% |
lettucedetect-wikipedia |
23,255 | 25% |
幻觉分类体系
跨度使用 LettuceDetect 统一分类体系标记,包含三个可注入的顶层类别,每个类别有子类型。
| 类别 | 定义 | 示例子类型 |
|---|---|---|
contradiction |
与上下文所述内容冲突(错误的值、数字、日期、实体、关系) | numerical, temporal, entity, relational, value |
unsupported_addition |
添加上下文未陈述或暗示的主张 | claim, behavior |
fabricated_reference |
引用上下文中不存在的命名元素(函数、文件、标识符、章节) | identifier, section, entity |
每个跨度带有 label(原始来源标签,例如 structural 或 NUMERICAL),以及统一的 category 和 subcategory。
数据格式
JSONL 文件中的每一行是一个样本,包含以下字段:
prompt— 结构化上下文和用户请求answer— LLM 响应(含幻觉或干净)labels— 标注的幻觉跨度(干净样本为空)category/subcategory— 统一分类字段dataset/context_modality— 来源区分器metadata— 一个 JSON 编码的字符串,包含来源特定的溯源信息(键因来源而异:代码的instance_id/repo/format_type,工具输出的tool_type,ACL 的paper_id,readme/wiki 的doc_id),使用json.loads解析
各来源说明
- 代码 — 多函数补丁被拆分为每个函数的子实例;答案调用的任何兄弟函数作为仅签名的 "Referenced definitions" 块包含,因此干净答案不会与结构性幻觉混淆。仓库划分不重叠(来自 SWE-bench)。
- 工具输出 — 基于工具输出的简短回答;幻觉误报输出内容。
- ACL — RAG 设置:上下文是前 k 个检索到的论文片段;注入是针对特定论文的(数值/实体/关系/方法论/引用类),可基于摘要检测到。按论文划分(无泄漏)。
- README / 维基百科 — 文档按标题分块;每块生成一个问题、回答,并用通用事实性注入进行破坏。按仓库/文章划分。
使用方式
python import json from datasets import load_dataset
ds = load_dataset("KRLabsOrg/lettucedetect-code-hallucination")
加载全部数据,或按来源/模态筛选
code = ds["train"].filter(lambda r: r["dataset"] == "lettucedetect-code") markdown = ds["train"].filter(lambda r: r["context_modality"] == "markdown")
for sample in ds["train"]: if sample["labels"]: meta = json.loads(sample["metadata"]) print(sample["category"], meta.get("is_hallucinated"))
与 LettuceDetect 结合使用
python from lettucedetect.models.inference import HallucinationDetector
detector = HallucinationDetector( method="transformer", model_path="KRLabsOrg/lettucedetect-large-modernbert-en-v1", ) spans = detector.predict( context=[sample["prompt"]], question="", answer=sample["answer"], output_format="spans", )
生成方式
每个来源首先生成一个正确、有依据的回答,然后注入一个局部幻觉,产生精确的字符级跨度(无需差异对齐)。文档来源(README、维基百科)还会从文档生成一个问题;ACL 使用检索到的块作为上下文。注入模型:Qwen/Qwen3.6-35B-A3B,通过 vLLM 提供服务。
局限
README 和维基百科是初步版本:对于规范主题,一个强大的模型可能偶尔根据世界知识而非上下文标记出一个幻觉;少数注入的编辑在没有上下文的情况下可以被检测到。预期的质量门控标准是仅回答 vs. 回答+上下文检测的差距;请将 markdown 来源视为比代码/工具输出/ACL 来源精度更低。
许可
CC-BY-4.0




