[论文解读] Beyond Functional Correctness: Exploring Hallucinations in LLM-Generated Code
本研究分析大型语言模型生成代码中的幻觉,提出5类幻觉的分类法,创造 HalluCode 以用于评估,并显示当前模型在识别和缓解此类幻觉方面存在困难。
The rise of Large Language Models (LLMs) has significantly advanced various applications on software engineering tasks, particularly in code generation. Despite the promising performance, LLMs are prone to generate hallucinations, which means LLMs might produce outputs that deviate from users' intent, exhibit internal inconsistencies, or misaligned with the real-world knowledge, making the deployment of LLMs potentially risky in a wide range of applications. Existing work mainly focuses on investigating the hallucination in the domain of Natural Language Generation (NLG), leaving a gap in comprehensively understanding the types, causes, and impacts of hallucinations in the context of code generation. To bridge the gap, we conducted a thematic analysis of the LLM-generated code to summarize and categorize the hallucinations, as well as their causes and impacts. Our study established a comprehensive taxonomy of code hallucinations, encompassing 3 primary categories and 12 specific categories. Furthermore, we systematically analyzed the distribution of hallucinations, exploring variations among different LLMs and benchmarks. Moreover, we perform an in-depth analysis on the causes and impacts of various hallucinations, aiming to provide valuable insights into hallucination mitigation. Finally, to enhance the correctness and reliability of LLM-generated code in a lightweight manner, we explore training-free hallucination mitigation approaches by prompt enhancing techniques. We believe our findings will shed light on future research about code hallucination evaluation and mitigation, ultimately paving the way for building more effective and reliable code LLMs in the future. The replication package is available at https://github.com/Lorien1128/code_hallucination
研究动机与目标
- 建立在 LLM 生成代码中发生的幻觉的分类法,并将其与用户意图和上下文相关联。
- 量化幻觉在不同代码 LLMs 中的分布和共现及其与代码正确性的关系。
- 开发 HalluCode,一个基准,用于评估和改进代码生成中对幻觉的识别与缓解。
- 提供实证洞见,为未来对代码 LLM 的检测、评估和缓解策略提供指导。
提出的方法
- 从 CodeGen、CodeRL 和 ChatGPT 在 HumanEval 与 DS-1000 数据集中收集了 13,968 条代码片段;抽样 3,084 条用于分析。
- 采用多 annotator 的主题分析来推导幻觉分类法(5 类,19 种类型)。
- 将分类法映射到 NLP 幻觉类别,并分析分布、共现和模型差异。
- 基于 Code Alpaca 派生的 5,663 个 Python 任务构建 HalluCode;通过类型感知评分和规则基础方法设计幻觉注入;对幻觉识别与缓解进行了评估。
- 在 HalluCode 上评估最先进模型(ChatGPT-4、CodeLlama-7B、DeepSeek-Coder-7B)的幻觉识别与缓解能力。
实验结果
研究问题
- RQ1在 LLM 生成的代码中,哪些幻觉类型最为普遍且分布如何?
- RQ2幻觉类型在不同 LLMs 之间如何共现并存在差异?
- RQ3幻觉与所生成代码的功能正确性之间有什么关系?
- RQ4HalluCode 能否在跨 LLM 的检测与缓解幻觉方面提供有效基准?
主要发现
- 存在 5 类主要幻觉及 19 个叶型的分类法;在 3,084 条样本中识别出 2,119 条幻觉。
- Intent Conflicting(32.1%)和 Context Deviation(含子类型 Inconsistency 31.8%、Repetition 17.3%、Dead Code 3.2%)最常见;Knowledge Conflicting 占 15.1%。
- 幻觉可以在一个程序中共同出现;Context Repetition 与其他类型最共同出现的情况最高,约占此类情况的1/3。
- 只有很小一部分包含幻觉的代码通过所有测试(≤10%),某些错误并非直接由幻觉导致,而幻觉往往指示潜在问题。
- HalluCode 包含 5,663 个用于评估幻觉识别和缓解的 Python 任务;开源模型相比更强的提示,在缓解幻觉方面能力有限。
- 不同模型呈现不同的主导幻觉类型,反映模型能力与训练策略。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。