Large language models (LLMs) have shown remarkable progress in automated code generation. Yet, incorporating LLM-based code generation into real-life software projects poses challenges, as the generated code may contain errors in API usage, class, data structure, or missing project-specific information. As much of this project-specific context cannot fit into the prompts of LLMs, we must find ways to allow the model to explore the project-level code context. To this end, this paper puts forward a novel approach, termed ProCoder, which iteratively refines the project-level code context for precise code generation, guided by the compiler feedback. In particular, ProCoder first leverages compiler techniques to identify a mismatch between the generated code and the project's context. It then iteratively aligns and fixes the identified errors using information extracted from the code repository. We integrate ProCoder with two representative LLMs, i.e., GPT-3.5-Turbo and Code Llama (13B), and apply it to Python code generation. Experimental results show that ProCoder significantly improves the vanilla LLMs by over 80% in generating code dependent on project context, and consistently outperforms the existing retrieval-based code generation baselines.
翻译:大型语言模型(LLMs)在自动代码生成方面已展现出显著进展。然而,将基于LLM的代码生成融入实际软件项目仍面临挑战,因为生成的代码可能存在API使用、类、数据结构错误,或缺失项目特定信息。由于此类项目级上下文大多无法完整纳入LLM的提示(prompts)中,我们必须寻找方法让模型能够探索项目级代码上下文。为此,本文提出一种名为ProCoder的新方法,它通过编译器反馈引导,迭代精炼项目级代码上下文,以生成精准代码。具体而言,ProCoder首先利用编译器技术识别生成代码与项目上下文之间的不匹配,然后使用从代码仓库中提取的信息,迭代对齐并修复已识别的错误。我们将ProCoder集成到两个代表性LLM(即GPT-3.5-Turbo和Code Llama(13B))中,并将其应用于Python代码生成。实验结果表明,在生成依赖项目上下文的代码时,ProCoder相较于原始LLM实现了超过80%的性能提升,并持续优于现有的基于检索的代码生成基线方法。