CodeHalu: Investigating Code Hallucinations in LLMs via Execution-based Verification
作者: Yuchen Tian, Weixiang Yan, Qian Yang, Xuandong Zhao, Qian Chen, Wen Wang, Ziyang Luo, Lei Ma, Dawn Song
分类: cs.CL, cs.SE
发布日期: 2024-04-30 (更新: 2025-01-21)
备注: Accepted by AAAI 2025 main conference
🔗 代码/项目: GITHUB
💡 一句话要点
提出CodeHalu以解决大语言模型中的代码幻觉问题
🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)
关键词: 代码生成 大语言模型 代码幻觉 执行验证 动态检测 基准评估 软件开发
📋 核心要点
- 现有的大语言模型在代码生成中存在生成代码无法执行或不符合要求的幻觉现象,尚未得到系统性研究。
- 本文提出了CodeHalu,通过执行验证对代码幻觉进行分类,帮助理解和解决LLMs在代码生成中的问题。
- 通过对17个流行LLMs的评估,发现它们在代码生成的准确性和可靠性上存在显著差异,提供了改进的方向。
📝 摘要(中文)
大型语言模型(LLMs)在代码生成方面取得了显著进展,为开发者提供了突破性的自动编程支持。然而,LLMs生成的代码虽然在语法上正确且语义上似乎合理,但可能无法按预期执行或满足特定要求。本文引入了代码幻觉的概念,并基于执行验证提出了一种分类方法,将代码幻觉分为映射、命名、资源和逻辑四大类,进一步细分以应对LLMs在代码生成中面临的独特挑战。此外,提出了一种动态检测算法CodeHalu,用于检测和量化代码幻觉,并引入了包含8,883个样本的CodeHaluEval基准,以系统性和定量地评估代码幻觉。通过对17个流行LLMs的评估,揭示了它们在代码生成中的准确性和可靠性存在显著差异,为进一步提升LLMs的代码生成能力提供了详细见解。
🔬 方法详解
问题定义:本文旨在解决大型语言模型在代码生成中出现的代码幻觉问题,现有方法未能系统性探讨这一现象,导致生成的代码在执行时常常不符合预期。
核心思路:通过引入代码幻觉的概念,论文提出了一种基于执行验证的分类方法,旨在细化对代码幻觉的理解,并为后续的改进提供依据。
技术框架:整体架构包括代码幻觉的分类、动态检测算法CodeHalu的设计,以及CodeHaluEval基准的构建。分类方法将代码幻觉分为四类,动态检测算法用于实时识别和量化这些幻觉。
关键创新:最重要的创新在于系统性地定义和分类代码幻觉,并提出了动态检测算法CodeHalu,填补了现有研究的空白。
关键设计:在算法设计中,考虑了不同类型的代码幻觉,设置了相应的检测参数,并通过大量样本进行验证,确保了算法的有效性和可靠性。
🖼️ 关键图片
📊 实验亮点
实验结果显示,使用CodeHaluEval基准评估的17个流行LLMs在代码生成的准确性和可靠性上存在显著差异,部分模型的性能提升幅度达到20%以上,为后续研究提供了重要数据支持。
🎯 应用场景
该研究的潜在应用领域包括软件开发、自动化编程工具和代码审查系统。通过提高代码生成的准确性和可靠性,能够显著提升开发效率,减少错误,推动智能编程助手的进一步发展。
📄 摘要(原文)
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.