Abstract
Modern code completion tools often rely solely on the local context of the current file, missing out on the vast wealth of knowledge available in external repositories. We present ReACC, a retrieval-augmented framework that enhances code completion by searching a massive external database for similar code snippets. By integrating a hybrid retriever with a generative language model, ReACC achieves state-of-the-art performance on benchmarks like CodeXGLUE, effectively mimicking the human developer's practice of referencing existing examples.
Introduction
Most contemporary AI-driven code completion tools function as "introverts," limited by the context of the active file or project. While Transformer-based models have significantly improved these tools, they remain constrained by this narrow internal view. In contrast, human programmers frequently leverage external resources—searching for examples, consulting documentation, or adapting logic from other projects. Research indicates that 7-23% of software repositories contain cloned code, suggesting that external context is a vital component of the development process. ReACC addresses this gap by providing AI models with access to an "external world" of code, enabling more accurate and contextually rich predictions.
Methods
The ReACC framework decomposes the code completion task into two primary stages: retrieval and generation.
The Hybrid Retriever
When a partial code snippet is provided as a query, ReACC employs a hybrid retrieval strategy to identify relevant external code:
- Sparse Retrieval (BM25): This component matches keywords and identifier names, effectively locating code that utilizes similar APIs or libraries.
- Dense Retrieval (Dual-Encoder): Utilizing a GraphCodeBERT-based model, this component captures the semantic intent of the code, allowing it to find relevant snippets even when variable names differ.
The Generator
The generation stage utilizes an auto-regressive language model—specifically a domain-adapted GPT-2 variant known as CodeGPT. The generator receives the retrieved external snippet concatenated with the user's current internal code, using both to predict the subsequent tokens.
Data Augmentation for Retriever Training
To overcome the scarcity of datasets containing "similar code pairs," the authors implemented semantic-preserving transformations. By automatically creating variants of existing code through identifier renaming and dead code insertion, the model learned to recognize the underlying logic of code regardless of surface-level variations.
Experimental Setup
ReACC was evaluated across several major benchmarks:
- CodeXGLUE: Evaluated on the PY150 (Python) and GitHub Java Corpus datasets for token- and line-level completion.
- CodeNet: A collection of programming contest solutions used to test domain-specific performance and clone detection.
- CodeSearchNet: Utilized for the initial pre-training of the retriever.
Key Results and Insights
ReACC established new state-of-the-art results on the CodeXGLUE benchmark. On the Python dataset, it achieved an Exact Match (EM) score of 46.26%, a significant improvement over the 42.37% achieved by the standard CodeGPT-adapted baseline.
Key findings include:
- Lexical Anchors: Simple keyword matching (BM25) proved remarkably effective, as reused API calls serve as strong anchors for finding relevant code.
- Domain Gains: The framework showed its most substantial improvements in specialized domains like competitive programming (CodeNet), where the external database contains highly relevant algorithmic logic.
- Fragment Alignment: Retrieving the logical "next fragment" from a similar file, rather than just the similar snippet itself, provides the generator with a better template for completion.
Limitations and Future Work
Despite its success, ReACC faces challenges regarding retrieval latency in real-time IDE environments. Scaling to billions of lines of code requires highly efficient indexing. Additionally, the model's output quality is dependent on the quality of the retrieval database; buggy or poor-quality code in the source can negatively impact predictions. Future research will focus on more sophisticated filtering mechanisms to ensure only high-quality examples are provided to the generator.
Summary updated