Skip to main content
QUICK REVIEW

[论文解读] DocPrompting: Generating Code by Retrieving the Docs

Shuyan Zhou, Uri Alon|arXiv (Cornell University)|Jul 13, 2022
Software Engineering ResearchComputer Science被引用 18
一句话总结

DocPrompting 是一种检索增强的代码生成框架,通过在推理过程中检索相关代码文档来提升零样本泛化能力。给定自然语言意图,它从动态文档池中检索相关文档,并利用这些文档来引导代码生成,从而提升对未见过的函数和库的性能。在 CoNaLa 和一个新的 Bash 基准测试上,相比 CodeT5,其 pass@1 提升了 2.85%,精确匹配最高提升 6.9%。

ABSTRACT

Publicly available source-code libraries are continuously growing and changing. This makes it impossible for models of code to keep current with all available APIs by simply training these models on existing code repositories. Thus, existing models inherently cannot generalize to using unseen functions and libraries, because these would never appear in the training data. In contrast, when human programmers use functions and libraries for the first time, they frequently refer to textual resources such as code manuals and documentation, to explore and understand the available functionality. Inspired by this observation, we introduce DocPrompting: a natural-language-to-code generation approach that explicitly leverages documentation by (1) retrieving the relevant documentation pieces given an NL intent, and (2) generating code based on the NL intent and the retrieved documentation. DocPrompting is general: it can be applied to any programming language and is agnostic to the underlying neural model. We demonstrate that DocPrompting consistently improves NL-to-code models: DocPrompting improves strong base models such as CodeT5 by 2.85% in pass@1 (52% relative gain) and 4.39% in pass@10 (30% relative gain) in execution-based evaluation on the popular Python CoNaLa benchmark; on a new Bash dataset tldr, DocPrompting improves CodeT5 and GPT-Neo1.3B by up to absolute 6.9% exact match.

研究动机与目标

  • 解决现有 NL-to-code 模型因训练数据静态而无法泛化到未见过的函数或库的问题。
  • 通过在测试时动态检索并阅读代码文档,使模型能够泛化到新 API。
  • 设计一种通用且与模型无关的框架,适用于多种编程语言和基础模型架构。
  • 证明在未见过的代码模式上,文档检索能显著提升代码生成性能。
  • 发布带有 oracle 文档标注的新基准,以支持未来检索增强型代码生成研究。

提出的方法

  • 两阶段流程:首先,检索器利用自然语言意图从预存在的文档池中检索相关文档。
  • 然后将检索到的文档与自然语言意图拼接,形成增强提示,供代码生成器使用。
  • 生成器基于组合后的自然语言意图和检索到的文档生成代码,从而理解此前未见过的函数。
  • 文档池为外部且可更新,支持在不重新训练生成器或检索器的情况下集成新库。
  • 该框架支持稀疏检索(如 BM25)和稠密检索(如 SimCSE)方法,并兼容多种基础模型,如 CodeT5、GPT-Neo 和 Codex。
  • 该方法通用且与底层神经网络架构无关,可无缝集成到现有 NL-to-code 模型中。

实验结果

研究问题

  • RQ1在推理时检索代码文档是否能显著提升 NL-to-code 生成对未见过的函数和库的泛化能力?
  • RQ2与标准少样本或零样本提示相比,检索增强提示在代码生成准确率上有何差异?
  • RQ3DocPrompting 的有效性是否因编程语言和模型架构的不同而有所变化?
  • RQ4当基准测试包含训练阶段未见的函数时,DocPrompting 是否能提升性能?
  • RQ5检索方法的选择(稀疏 vs. 稠密)对最终代码生成性能有何影响?

主要发现

  • 在 CoNaLa 基准上,DocPrompting 使 CodeT5 的 pass@1 提升 2.85%(相对提升 52%),在执行评估中表现显著提升。
  • 在新的 Bash 基准测试 tldr 上,DocPrompting 使 CodeT5 和 GPT-Neo-1.3B 的精确匹配最高提升 6.9% 绝对值,表明在不同语言中同样有效。
  • 在 tldr 数据集上,Codex 的 charBLEU 得分提升 6.78 分,表明生成代码在结构和语法上更加准确。
  • 性能提升在多种基础模型(包括 CodeT5、GPT-Neo 和 Codex)上保持一致,证实了该框架的泛化能力。
  • 即使测试集中包含训练阶段未见的函数,该框架仍优于基础模型,证明其具备泛化到未见 API 的能力。
  • 发布带有 oracle 文档标注的新基准和精心整理的文档池,为未来检索增强型代码生成研究提供了宝贵的测试平台。

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。