Large Language Models (LLMs) have shown remarkable progress in automated code generation. Yet, LLM-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. We present CoCoGen, a new code generation approach that uses compiler feedback to improve the LLM-generated code. CoCoGen first leverages static analysis to identify mismatches 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 CoCoGen 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 CoCoGen significantly improves the vanilla LLMs by over 80% in generating code dependent on the project context and consistently outperforms the existing retrieval-based code generation baselines.
翻译:大型语言模型在自动化代码生成方面已展现出显著进展。然而,LLM生成的代码可能存在API使用、类、数据结构方面的错误,或缺失项目特定信息。由于大量项目特定上下文无法全部放入LLM的提示中,我们必须寻找允许模型探索项目级代码上下文的方法。本文提出CoCoGen——一种利用编译器反馈改进LLM生成代码的新型代码生成方法。CoCoGen首先通过静态分析识别生成代码与项目上下文之间的不匹配,随后利用从代码仓库提取的信息迭代地对齐并修复已识别的错误。我们将CoCoGen与两个代表性LLM(GPT-3.5-Turbo和Code Llama(13B))集成,并将其应用于Python代码生成。实验结果表明,在生成依赖项目上下文的代码时,CoCoGen较原始LLM提升超过80%,且持续优于现有基于检索的代码生成基线方法。