首次揭示代码变更生成自然语言时的幻觉现象,超半数评论含幻觉。
Hallucinations in Code Change to Natural Language Generation: Prevalence and Evaluation of Detection Metrics
- 分析代码变更转自然语言任务中的幻觉生成机制
- 发现50%代码评审和20%提交消息存在幻觉
- 模型置信度与特征归因法可有效检测幻觉
语言模型在软件工程中表现出强大能力,如代码生成,但存在幻觉问题。尽管自然语言和代码生成中的幻觉已有研究,但涉及结构复杂、上下文依赖性强的代码变更任务中的幻觉仍鲜有探索。本文首次全面分析了两个关键任务——提交消息生成与代码评审评论生成中幻觉的出现情况。我们量化了近期语言模型中幻觉的普遍性,并评估多种基于指标的方法自动检测幻觉的效果。结果表明,约50%的生成代码评审和20%的生成提交消息包含幻觉。虽然单一常用指标检测效果较弱,但结合多个指标能显著提升性能。值得注意的是,模型置信度和特征归因指标在检测中表现优异,展现出推理时检测的潜力。所有代码与数据将在论文接受后公开。
原文摘要 · Abstract (English)
Language models have shown strong capabilities across a wide range of tasks in software engineering, such as code generation, yet they suffer from hallucinations. While hallucinations have been studied independently in natural language and code generation, their occurrence in tasks involving code changes which have a structurally complex and context-dependent format of code remains largely unexplored. This paper presents the first comprehensive analysis of hallucinations in two critical tasks involving code change to natural language generation: commit message generation and code review comment generation. We quantify the prevalence of hallucinations in recent language models and explore a range of metric-based approaches to automatically detect them. Our findings reveal that approximately 50\% of generated code reviews and 20\% of generated commit messages contain hallucinations. Whilst commonly used metrics are weak detectors on their own, combining multiple metrics substantially improves performance. Notably, model confidence and feature attribution metrics effectively contribute to hallucination detection, showing promise for inference-time detection.\footnote{All code and data will be released upon acceptance.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。