ZS4C: Zero-Shot Synthesis of Compilable Code for Incomplete Code Snippets using LLMs

📄 arXiv: 2401.14279v3 📥 PDF

作者: Azmain Kabir, Shaowei Wang, Yuan Tian, Tse-Hsun Chen, Muhammad Asaduzzaman, Wenbin Zhang

分类: cs.SE, cs.AI

发布日期: 2024-01-25 (更新: 2024-12-09)

备注: This paper has been accepted and published in ACM Transactions on Software Engineering and Methodology (TOSEM), [2024], [https://dl.acm.org/doi/10.1145/3702979]

DOI: 10.1145/3702979


💡 一句话要点

提出ZS4C以解决代码片段编译问题

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 代码合成 大型语言模型 编译器 软件开发 自动化测试 技术问答 Python

📋 核心要点

  1. 现有方法未能有效解决从不完整代码片段生成可编译代码的问题,成功率较低。
  2. ZS4C通过两个阶段的处理,利用大型语言模型识别缺失的导入语句,并与编译器协作修复错误。
  3. ZS4C在StatType-SO和Python-SO数据集上表现优异,编译率提升50.1%,F1分数提高8.5%。

📝 摘要(中文)

技术问答网站为软件开发者提供了宝贵的知识,但其代码片段常因未解决的类型和缺失的库而无法编译,给用户带来了重用和分析的挑战。现有方法要么未关注生成可编译代码,要么成功率较低。为此,本文提出了ZS4C,一种基于大型语言模型(LLMs)从不完整代码片段中进行零-shot编译代码合成的轻量级方法。ZS4C分为两个阶段:首先,利用LLM(如GPT-3.5)识别代码片段中的缺失导入语句;其次,与验证器(如编译器)协作,修复因错误导入和语法问题引起的编译错误。ZS4C在StatType-SO基准和新数据集Python-SO上进行了评估,显著超越现有方法,将编译率从63%提升至95.1%。

🔬 方法详解

问题定义:本文旨在解决从不完整代码片段生成可编译代码的挑战。现有方法在处理缺失导入和语法错误时,成功率较低,无法满足开发者的需求。

核心思路:ZS4C的核心思路是利用大型语言模型(如GPT-3.5)进行零-shot学习,自动识别和补全代码片段中的缺失部分,并通过编译器验证修复后的代码。

技术框架:ZS4C的整体架构分为两个主要阶段:第一阶段,使用LLM识别缺失的导入语句;第二阶段,利用编译器验证并修复因导入错误和语法问题导致的编译错误。

关键创新:ZS4C的创新之处在于其轻量级的设计和高效的零-shot学习能力,显著提高了编译成功率,与现有方法相比,提供了更高的准确性和可靠性。

关键设计:在参数设置上,ZS4C优化了LLM的输入格式,并设计了有效的损失函数以提高导入语句的识别准确性。网络结构方面,ZS4C结合了语言模型和编译器的反馈机制,确保生成代码的可编译性。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

ZS4C在StatType-SO基准和Python-SO数据集上的实验结果显示,其编译率从63%提升至95.1%,相较于现有的SnR方法提高了50.1%。此外,ZS4C在导入语句的准确性上也表现优异,F1分数达到了0.98,比SnR提高了8.5%。

🎯 应用场景

ZS4C的研究成果在软件开发、代码重用和自动化测试等领域具有广泛的应用潜力。它能够帮助开发者快速修复和重用技术问答网站上的代码片段,从而提高开发效率和代码质量。未来,该方法还可以扩展到其他编程语言和开发环境中,进一步推动自动化编程的发展。

📄 摘要(原文)

Technical Q&A sites are valuable for software developers seeking knowledge, but the code snippets they provide are often uncompilable and incomplete due to unresolved types and missing libraries. This poses a challenge for users who wish to reuse or analyze these snippets. Existing methods either do not focus on creating compilable code or have low success rates. To address this, we propose ZS4C, a lightweight approach for zero-shot synthesis of compilable code from incomplete snippets using Large Language Models (LLMs). ZS4C operates in two stages: first, it uses an LLM, like GPT-3.5, to identify missing import statements in a snippet; second, it collaborates with a validator (e.g., compiler) to fix compilation errors caused by incorrect imports and syntax issues. We evaluated ZS4C on the StatType-SO benchmark and a new dataset, Python-SO, which includes 539 Python snippets from Stack Overflow across the 20 most popular Python libraries. ZS4C significantly outperforms existing methods, improving the compilation rate from 63% to 95.1% compared to the state-of-the-art SnR, marking a 50.1% improvement. On average, ZS4C can infer more accurate import statements (with an F1 score of 0.98) than SnR, with an improvement of 8.5% in the F1.