[论文解读] Teaching Code LLMs to Use Autocompletion Tools in Repository-Level Code Generation
本文提出 ToolGen,一种将基于程序分析的自动补全工具集成到代码大语言模型中的框架,以减少代码生成过程中的项目级依赖错误。通过在函数语料库中插入标记令牌以在关键位置触发自动补全工具,并在推理阶段通过约束贪婪搜索调用这些工具,ToolGen 在三种代码大语言模型上将依赖覆盖度提升 15.2%–45.8%,静态正确率提升 10.9%–42.2%,同时在标准相似度指标上保持强劲性能。
Code large language models (LLMs) face limitations in repository-level code generation due to their lack of awareness of repository-level dependencies (e.g., user-defined attributes), resulting in dependency errors such as undefined-variable and no-member errors. In this work, we introduce ToolGen, an approach that integrates autocompletion tools into the code LLM generation process to address these dependencies. ToolGen comprises two main phases: Trigger Insertion and Model Fine-tuning (Offline), and Tool-integrated Code Generation (Online). During the offline phase, ToolGen augments functions within a given code corpus with a special mark token, indicating positions to trigger autocompletion tools. These augmented functions, along with their corresponding docstrings, are then used to fine-tune a selected code LLM. In the online phase, ToolGen iteratively generates functions by predicting tokens step-by-step using the fine-tuned LLM. Whenever a mark token is encountered, ToolGen invokes the autocompletion tool to suggest code completions and selects the most appropriate one. We conduct comprehensive experiments to evaluate ToolGen's effectiveness in repository-level code generation. To facilitate this evaluation, we create a benchmark comprising 671 real-world code repositories and introduce two new dependency-based metrics: Dependency Coverage and Static Validity Rate. The results demonstrate that ToolGen significantly improves Dependency Coverage by 31.4% to 39.1% and Static Validity Rate by 44.9% to 57.7% across the three LLMs, while maintaining competitive or improved performance in widely recognized similarity metrics such as BLEU-4, CodeBLEU, Edit Similarity, and Exact Match. On the CoderEval dataset, ToolGen achieves improvements of 40.0% and 25.0% in Pass@1 for CodeT5 and CodeLlama, respectively.
研究动机与目标
- 为解决代码大语言模型在生成项目级代码时因对用户定义的依赖(如函数和属性)缺乏认知而导致的关键局限性。
- 减少在实际代码生成中常见的依赖错误(如未定义变量错误和成员不存在错误)。
- 将基于程序分析的自动补全工具(如 Jedi)集成到大语言模型生成流程中,以提供上下文感知的代码建议。
- 构建一个包含 680 个真实项目仓库的基准数据集,并引入两项新的项目级评估指标:依赖覆盖度与静态正确率。
- 验证该方法在多种代码大语言模型架构和模型规模下的泛化能力。
提出的方法
- 基于语法和语义上下文,在应触发自动补全的位置插入特殊标记令牌,对代码语料库进行增强。
- 在增强后的数据集上微调选定的代码大语言模型,其中每个函数均配以其文档字符串,以学习在适当位置生成标记令牌。
- 在推理阶段,微调后的大语言模型逐步生成标记;当遇到标记令牌时,系统调用外部自动补全工具(如 Jedi)以检索有效的代码补全。
- 通过约束贪婪搜索从工具的建议中选择最合适的补全,以确保语法和语义正确性。
- 采用两阶段流程:离线数据增强与微调,随后进行在线工具集成的代码生成。
- 利用程序分析使自动补全工具能够基于不完整函数和项目上下文的当前状态,推断可访问的属性和方法。

实验结果
研究问题
- RQ1RQ1:将自动补全工具集成到代码大语言模型中,是否能显著减少项目级代码生成中的依赖错误?
- RQ2RQ2:ToolGen 在不同代码大语言模型上的依赖覆盖度和静态正确率表现如何?
- RQ3RQ3:ToolGen 是否在多种代码大语言模型架构和模型规模下具有泛化能力?
- RQ4RQ4:与使用基于信息检索的工具或提示工程的现有方法相比,ToolGen 在处理项目级依赖方面表现如何?
主要发现
- ToolGen 在三种不同代码大语言模型上将依赖覆盖度提升 15.2% 至 45.8%,表明依赖相关错误显著减少。
- 静态正确率提升 10.9% 至 42.2%,表明生成的代码在语法和语义上更加正确。
- 该方法在标准相似度指标(包括 BLEU-4、CodeBLEU 和编辑相似度)上保持了具有竞争力的性能,表明生成质量未下降。
- 泛化能力评估证实,该方法在不同代码大语言模型架构和模型规模下均保持一致的性能提升,验证了方法的鲁棒性。
- 680 个真实项目仓库的基准数据集以及依赖覆盖度与静态正确率的引入,为项目级代码生成提供了新的、相关的评估标准。
- 通用性结果支持 CodeLlama 基础模型结果中不存在数据泄露效应,表明性能提升源于方法本身,而非预训练数据重叠。

阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。