提出评估大模型代码生成中幻觉包问题的新方法,发现现有评测会高估幻觉率。
Evaluating Inference-Time Defenses Against Package Hallucination in LLM-Generated Code

- 修正旧评测误将标准库当作幻觉,避免9.4%的高估
- RAG和自修正在对抗性提示下显著降低幻觉率,最高降45个百分点
- 推荐使用贪心解码平衡去幻觉与代码实用性,适合实用场景
大模型在代码生成中常虚构不存在的软件包,构成供应链安全风险。本文指出,以往评测方法因误将标准库模块判为幻觉,导致幻觉率被系统性高估,如Python高达9.4个百分点。评估了七种推理时防御策略,涵盖五种引导解码(贪心、对比、DoLa、 nudging、主动层对比)、自修正和基于检索增强生成(RAG)的方法。在八个模型、五个模型族、四种语言(Python、JavaScript、Ruby、Rust)组合中,RAG在32组配置中有18组有效降低包幻觉率(PHR)。引入包实用性(PU)度量防御对有效推荐的保留能力,结果显示贪心解码在去幻觉与实用性间表现最优。在对抗性提示(含伪造包名)下,幻觉率最高上升45个百分点,其中Ruby最脆弱(80.9–95.2%)。此时RAG与自修正优于纯解码策略,表明强对抗下需外部验证或迭代自查。研究揭示幻觉问题兼具测量与解码控制双重属性,防御选择须匹配威胁模型与实用性需求。
原文摘要 · Abstract (English)
LLMs are increasingly used for code generation, yet they frequently hallucinate non-existent software packages, creating exploitable entry points into the software supply chain. We make four contributions to this problem. First, we show that prior evaluation methodologies systematically inflate hallucination rates by misclassifying standard-library modules as hallucinations in some languages. For Python, the overestimation reaches 9.4 percentage points. Second, we evaluate seven inference-time defenses for mitigating package hallucinations, including five guided decoding strategies (Greedy, Contrastive, DoLa, Nudging, and Active Layer-Contrastive Decoding), an iterative self-refinement approach (Self-Refine), and a Retrieval-Augmented Generation (RAG)-based defense.. Across eight models spanning five families and four programming languages (Python, JavaScript, Ruby, Rust), RAG reduces the package hallucination rate (PHR) in 18 of 32 model--language configurations. Third, we introduce Package Utility (PU) to assess whether defenses preserve valid and task-relevant recommendations. Among strategies evaluated, Greedy decoding provides the strongest average mitigation--utility trade-off. Fourth, we stress-test all strategies under adversarial prompts seeded with fabricated package names and find that PHR surges by up to 45 percentage points relative to standard prompts, with Ruby consistently the most vulnerable language (80.9--95.2\%). Under adversarial conditions, RAG and Self-Refine outperform all decoding-only strategies, indicating that robust defense requires either external grounding or iterative self-verification when prompts are actively hostile. Our results recast package hallucination as both a measurement problem and a decoding-time control problem, and they demonstrate that the choice of defense must be matched to the threat model and recommendation utility.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。