Exploring and Evaluating Hallucinations in LLM-Powered Code Generation

Fang Liu,Yang Liu,Lin Shi,Houkun Huang,Ruifeng Wang,Zhen Yang,Li Zhang,Zhongqi Li,Yuchi Ma
2024-05-11
Abstract:The rise of Large Language Models (LLMs) has significantly advanced many 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 misalign with the factual knowledge, making the deployment of LLMs potentially risky in a wide range of applications. Existing work mainly focuses on investing the hallucination in the domain of natural language generation (NLG), leaving a gap in understanding the types and extent 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 present in it. Our study established a comprehensive taxonomy of hallucinations in LLM-generated code, encompassing 5 primary categories of hallucinations depending on the conflicting objectives and varying degrees of deviation observed in code generation. Furthermore, we systematically analyzed the distribution of hallucinations, exploring variations among different LLMs and their correlation with code correctness. Based on the results, we proposed HalluCode, a benchmark for evaluating the performance of code LLMs in recognizing hallucinations. Hallucination recognition and mitigation experiments with HalluCode and HumanEval show existing LLMs face great challenges in recognizing hallucinations, particularly in identifying their types, and are hardly able to mitigate hallucinations. We believe our findings will shed light on future research about hallucination evaluation, detection, and mitigation, ultimately paving the way for building more effective and reliable code LLMs in the future.
Software Engineering,Artificial Intelligence
What problem does this paper attempt to address?
### The Problem Addressed by This Paper This paper primarily explores the issue of hallucinations generated by large language models (LLMs) in code generation tasks. Specifically: 1. **Definition and Classification of Hallucinations**: - By analyzing code snippets generated by different LLMs, the paper summarizes and classifies the types of hallucinations in these codes. The paper establishes a hallucination classification system comprising 5 main categories and describes 19 specific types of hallucinations in detail. 2. **Analysis of Hallucination Distribution and Impact**: - The paper systematically analyzes the distribution of these hallucinations, explores the differences between different LLMs, and examines the correlation between hallucinations and code correctness. 3. **Development of Evaluation Benchmark**: - The paper proposes a benchmark specifically for evaluating hallucinations in LLMs' code generation—HALLU CODE, and uses this benchmark along with HumanEval to conduct hallucination identification experiments on several state-of-the-art LLMs. 4. **Revealing Challenges in Existing Models**: - Experimental results indicate that existing LLMs face significant challenges in identifying hallucinations, especially in distinguishing types of hallucinations, and struggle to effectively mitigate these issues. Through these studies, the paper aims to provide valuable insights for future methods of hallucination evaluation, detection, and mitigation, ultimately enhancing the accuracy and reliability of LLMs in the field of code generation.