Abstract
Pre-trained language models have achieved promising success in code retrieval tasks, where a natural language documentation query is given to find the most relevant existing code snippet. However, existing models focus only on optimizing the documentation code pairs by embedding them into latent space, without the association of external knowledge. In this paper, we propose a generation-augmented query expansion framework. Inspired by the human retrieval process - sketching an answer before searching, in this work, we utilize the powerful code generation model to benefit the code retrieval task. Specifically, we demonstrate that rather than merely retrieving the target code snippet according to the documentation query, it would be helpful to augment the documentation query with its generation counterpart - generated code snippets from the code generation model. To the best of our knowledge, this is the first attempt that leverages the code generation model to enhance the code retrieval task. We achieve new state-of-the-art results on the CodeSearchNet benchmark and surpass the baselines significantly.
Introduction
Code retrieval—finding a specific code snippet based on a natural language (NL) description—is a cornerstone of modern software engineering. However, it suffers from a fundamental "semantic gap." Natural language is descriptive and often vague, while programming language (PL) is formal and structural.
Current state-of-the-art models try to solve this by embedding both NL and PL into a shared "latent space." However, these models often rely on simple keyword matching rather than a deep understanding of logic. For instance, a search for "inverse keys and values in a dictionary" might return a matrix inversion function simply because of the keyword "inverse," missing the structural intent of the query.
Methodology: The GACR Framework
The authors propose GACR (Generation-Augmented Code Retrieval), a two-stage framework that bridges the semantic gap by "sketching" an answer before searching. This approach mimics how human experts often have a mental draft of a code structure before performing a search.
The framework operates in two main stages:
- Generation Stage: When a user enters an NL query, the system feeds it into a powerful code generation model (such as OpenAI’s Codex). The model generates "hallucinated" code snippets representing a potential implementation.
- Expansion & Retrieval Stage: The original NL query is augmented with these generated snippets. This expanded query is processed by a dual-representation encoder. Using a specialized attention mechanism (the "Type-A" mask), the model fuses information from the documentation and the generated code into a single vector used to search the codebase for the best match.
The study explored two variants: GACR-S (using a single generated snippet) and GACR-M (using multiple snippets to capture a broader range of possible implementations).
Key Insights and Results
GACR achieved new state-of-the-art (SOTA) results on the CodeSearchNet benchmark across six major programming languages: Python, Java, JavaScript, Ruby, Go, and PHP. Notably, on the PHP dataset, the GACR-M model showed a staggering 27.8% improvement over the GraphCodeBERT baseline.
The Power of Semantic Bridging
A key finding of the research is that generated code acts as a translation layer, converting user intent into the structural language of the target codebase. Interestingly, the component analysis reveals that the function name of the generated code often provides more useful retrieval information than the function body itself.
As shown in the ablation study (Figure 6), while the documentation alone is a strong baseline, the inclusion of the generated function—and specifically its name—consistently provides more useful retrieval information than the function body. This suggests that well-named "hallucinated" functions serve as high-quality semantic bridges between natural language and implementation.
Conclusion and Future Work
GACR demonstrates that leveraging code generation models to expand queries can significantly enhance retrieval tasks by bridging the domain gap between documentation and code. While effective, the approach faces challenges regarding computational overhead and token limits in Transformers. Future research may explore using smaller, distilled models for the generation phase or accessing the internal hidden representations of generators to further refine the retrieval process.
Summary updated