Skip to main content
QUICK REVIEW

[论文解读] CodeHalu: Investigating Code Hallucinations in LLMs via Execution-based Verification

Yuchen Tian, Weixiang Yan|arXiv (Cornell University)|Apr 30, 2024
Security and Verification in Computing被引用 4
一句话总结

该论文提出了CodeHalu框架,通过执行验证方法定义并分类大语言模型(LLM)中的代码幻觉。该框架提出四类幻觉——映射、命名、资源和逻辑幻觉,并设计了一种动态检测算法,以及一个包含8,883个样本、覆盖699个任务的基准测试集。对16个大语言模型的评估揭示了显著的幻觉率,凸显了提升代码生成模型可靠性的紧迫需求。

ABSTRACT

Large Language Models (LLMs) have made significant progress in code generation, offering developers groundbreaking automated programming support. However, LLMs often generate code that is syntactically correct and even semantically plausible, but may not execute as expected or fulfill specified requirements. This phenomenon of hallucinations in the code domain has not been systematically explored. To advance the community's understanding and research on this issue, we introduce the concept of code hallucinations and propose a classification method for code hallucination based on execution verification. We categorize code hallucinations into four main types: mapping, naming, resource, and logic hallucinations, with each category further divided into different subcategories to understand and address the unique challenges faced by LLMs in code generation with finer granularity. Additionally, we present a dynamic detection algorithm called CodeHalu designed to detect and quantify code hallucinations. We also introduce the CodeHaluEval benchmark, which includes 8,883 samples from 699 tasks, to systematically and quantitatively evaluate code hallucinations. By evaluating 17 popular LLMs using this benchmark, we reveal significant differences in their accuracy and reliability in code generation, offering detailed insights for further improving the code generation capabilities of LLMs. The CodeHalu benchmark and code are publicly available at https://github.com/yuchen814/CodeHalu.

研究动机与目标

  • 基于执行正确性而非语义合理性,定义并系统性分类大语言模型中的代码幻觉。
  • 解决代码生成中幻觉缺乏标准化评估的问题,该问题可能导致运行时错误和安全漏洞。
  • 开发一种动态检测算法,通过实际代码执行识别幻觉。
  • 构建一个全面的基准测试集(CodeHalu),包含699个编程任务和8,883个样本,用于评估大语言模型中幻觉的发生频率和类型。
  • 提供实证证据,表明在16个主流大语言模型中幻觉普遍存在,为未来提升模型功能正确性提供依据。

提出的方法

  • 将代码幻觉定义为语法上正确但执行失败或违反功能需求的生成代码。
  • 将幻觉分为四类主要类型:映射、命名、资源和逻辑,每类根据执行失败模式进一步细分为子类。
  • 开发一种动态检测算法,通过在测试用例上执行生成代码,实现实时识别幻觉。
  • 构建CodeHalu基准测试集,包含699个编程任务和8,883个幻觉样本,每个样本均标注了预期行为和执行结果。
  • 以执行验证为核心机制,验证正确性,从而将幻觉与单纯的语法或语义错误区分开来。
  • 在基准测试集上评估16个主流大语言模型,以测量幻觉的发生频率及其在各类别中的分布情况。
Figure 1: The summary of CodeHalu Benchmark. CodeHalu includes 4 main categories and 8 sub-categories of hallucinations.
Figure 1: The summary of CodeHalu Benchmark. CodeHalu includes 4 main categories and 8 sub-categories of hallucinations.

实验结果

研究问题

  • RQ1在基于大语言模型的代码生成过程中,代码幻觉的主要类型和子类型是什么?
  • RQ2在执行正确性标准下,不同大语言模型生成幻觉代码的频率如何?
  • RQ3代码生成中的幻觉在多大程度上源于逻辑、命名、资源或映射错误?
  • RQ4基于执行的验证能否有效检测并分类那些语法正确但功能错误的幻觉?
  • RQ5在真实编程任务中,不同大语言模型架构和提示策略下的幻觉率如何变化?

主要发现

  • 在评估的16个大语言模型中,代码幻觉普遍存在,且在频率和类型分布上存在显著差异。
  • 最常见的幻觉类型为逻辑幻觉(如错误的循环或条件处理)和资源相关幻觉(如整数溢出、内存错误)。
  • 部分模型(如MagicCoder和StarCoder)因对大整数处理不当或生成器误用,产生了OverflowError和StopIteration异常。
  • 基于执行的验证成功暴露了语法正确但语义错误的幻觉,验证了基准测试集的有效性。
  • 基准测试揭示,即使是最先进的大语言模型,在通过执行评估时,仍有超过20%的案例无法生成正确代码。
  • 本研究表明,幻觉并非随机产生,而是遵循可识别的模式,为未来模型训练中的针对性缓解策略提供了可能。
Figure 2: Collection of CodeHalu benchmark based on a verification-identification-construction process. For a sample from the APPS test set, we use $n$ LLMs to generate code, which is then executed and validated. We select the top 5 most frequent hallucinations from the code execution results to rep
Figure 2: Collection of CodeHalu benchmark based on a verification-identification-construction process. For a sample from the APPS test set, we use $n$ LLMs to generate code, which is then executed and validated. We select the top 5 most frequent hallucinations from the code execution results to rep

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。