[Paper Review] DocPrompting: Generating Code by Retrieving the Docs
DocPrompting is a retrieval-augmented code generation framework that improves zero-shot generalization by retrieving relevant code documentation during inference. Given a natural language intent, it retrieves pertinent documentation from a dynamic pool and uses it to condition code generation, boosting performance on unseen functions and libraries. On CoNaLa and a new Bash benchmark, it improves CodeT5 by 2.85% pass@1 and up to 6.9% exact match.
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.
Motivation & Objective
- To address the limitation of existing NL-to-code models that cannot generalize to unseen functions or libraries due to static training data.
- To enable models to generalize to new APIs by dynamically retrieving and reading code documentation at test time.
- To design a general, model-agnostic framework that works across programming languages and base architectures.
- To demonstrate that retrieval of documentation significantly improves code generation performance on unseen code patterns.
- To release new benchmarks with oracle document annotations to support future research in retrieval-augmented code generation.
Proposed method
- A two-stage pipeline: first, a retriever uses the natural language intent to retrieve relevant documentation from a pre-existing documentation pool.
- The retrieved documentation is then concatenated with the natural language intent to form an augmented prompt for the code generator.
- The generator produces code based on the combined NL intent and retrieved documentation, enabling understanding of previously unseen functions.
- The documentation pool is external and updatable, allowing integration of new libraries without retraining the generator or retriever.
- The framework supports both sparse (e.g., BM25) and dense (e.g., SimCSE) retrieval methods, and is compatible with various base models like CodeT5, GPT-Neo, and Codex.
- The approach is general and agnostic to the underlying neural architecture, enabling plug-and-play integration with existing NL-to-code models.
Experimental results
Research questions
- RQ1Can retrieving code documentation at inference time significantly improve generalization to unseen functions and libraries in NL-to-code generation?
- RQ2How does retrieval-augmented prompting compare to standard few-shot or zero-shot prompting in terms of code generation accuracy?
- RQ3Does the effectiveness of DocPrompting vary across different programming languages and model architectures?
- RQ4Can DocPrompting improve performance on benchmarks with distribution shifts, such as unseen functions in test sets?
- RQ5How does the choice of retrieval method (sparse vs. dense) impact the final code generation performance?
Key findings
- DocPrompting improves CodeT5 by 2.85% in pass@1 (52% relative gain) on the CoNaLa benchmark, demonstrating strong gains in execution-based evaluation.
- On the new Bash benchmark tldr, DocPrompting improves CodeT5 and GPT-Neo-1.3B by up to 6.9% absolute exact match, showing effectiveness in a different language.
- The method achieves a 6.78 charBLEU score improvement for Codex on the tldr dataset, indicating better structural and syntactic accuracy.
- The performance gains are consistent across multiple base models, including CodeT5, GPT-Neo, and Codex, confirming the framework's generalizability.
- The framework outperforms base models even when the test set contains functions not seen during training, proving its ability to generalize to unseen APIs.
- The release of new benchmarks with oracle document annotations and curated documentation pools provides a valuable testbed for future retrieval-augmented code generation research.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.