一句话:提出MARIN,通过层次依赖挖掘与依赖约束解码缓解LLM代码生成中的API幻觉。
大型语言模型在自动代码生成中常出现API幻觉,即调用不存在的API或错误使用现有API。现有检索增强生成方法往往忽略实际项目中的结构依赖,且未真正验证生成的API是否可用。本文提出MARIN框架,包含两个阶段:层次依赖挖掘分析当前函数的局部与全局依赖,为LLM输入补充完整的项目上下文;依赖约束解码利用挖掘出的依赖自适应约束生成过程,确保生成的API符合项目规范。为评估API幻觉程度,作者构建了APIHul-Bench基准,并提出微幻觉数量MiHN与宏幻觉率MaHR两个新指标。在六个先进LLM上的实验表明,MARIN相比RAG平均降低MiHN 67.52%、MaHR 73.56%;应用于华为内部项目及两个专有LLM时,平均降低MiHN 57.33%、MaHR 59.41%。
Application Programming Interfaces (APIs) are crucial in modern software development. Large Language Models (LLMs) assist in automated code generation but often struggle with API hallucination, including invoking non-existent APIs and misusing existing ones in practical development scenarios. Existing studies resort to Retrieval-Augmented Generation (RAG) methods for mitigating the hallucination issue, but tend to fail since they generally ignore the structural dependencies in practical projects and do not indeed validate whether the generated APIs are available or not. To address these limitations, we propose MARIN, a framework for mitigating API hallucination in code generated by LLMs with hierarchical dependency aware. MARIN consists of two phases: Hierarchical Dependency Mining, which analyzes local and global dependencies of the current function, aiming to supplement comprehensive project context in LLMs' input, and Dependency Constrained Decoding, which utilizes mined dependencies to adaptively constrain the generation process, aiming to ensure the generated APIs align with the project's specifications. To facilitate the evaluation of the degree of API hallucination, we introduce a new benchmark APIHul-Bench and two new metrics including Micro Hallucination Number (MiHN) and Macro Hallucination Rate (MaHR). Experiments on six state-of-the-art LLMs demonstrate that MARIN effectively reduces API hallucinations, achieving an average decrease of 67.52% in MiHN and 73.56% in MaHR compared to the RAG approach. Applied to Huawei's internal projects and two proprietary LLMs, MARIN achieves average decreases of 57.33% in MiHN and 59.41% in MaHR.
@inproceedings{paperbot9009,
title = {Towards Mitigating API Hallucination in Code Generated by LLMs with Hierarchical Dependency Aware},
author = {Yujia Chen and Mingyu Chen and Cuiyun Gao and Zhihan Jiang and Zhongqi Li and Yuchi Ma},
booktitle = {FSE},
year = {2025},
doi = {10.1145/3696630.3728569}
}