Introduction
Retrieval-Augmented Code Generation (RACG) has emerged as a powerful technique to enhance Large Language Models (LLMs) by providing them with relevant external context, such as code snippets or documentation. However, standard RACG approaches often struggle with two primary issues: information redundancy and the "preference gap." Retrievers frequently provide large, noisy blocks of code that can confuse the generator—a phenomenon known as being "lost in the middle." Furthermore, retrievers are typically optimized for lexical similarity, which may not align with the structural or informational preferences of a specific LLM generator.
To address these challenges, we introduce the Retrieve, Refactor, Generate (RRG) framework. RRG incorporates a dedicated "code refactorer" module designed to clean and optimize retrieved code before it reaches the generator. This approach bridges the gap between retrieval and generation, leading to significant improvements in code generation accuracy.
The RRG Framework
The core innovation of RRG is the Code Refactorer, a lightweight encoder-decoder model (based on CodeT5-60M) that acts as an intermediary between the retriever and the generator. The framework operates in a three-step pipeline:
- Retrieve: Relevant code snippets are fetched from an external database.
- Refactor: The refactorer processes the raw retrieved code to remove noise and compress it into a more useful format.
- Generate: The LLM generator produces the final code based on the refactored context.
Two-Stage Training Process
The refactorer is trained using a specialized two-stage scheme to ensure it aligns with the generator's needs:
- Stage 1: Refactored Fine-Tuning (SFT): The model is trained to compress raw retrieved code into a concise version that matches a "golden" reference. This stage focuses on summarization and noise reduction.
- Stage 2: Preference-aware Tuning (RL): Using Reinforcement Learning (PPO), the refactorer is fine-tuned with the generator's performance as a reward signal. If the refactored output helps the generator achieve a higher CodeBLEU score, the refactorer is rewarded. This aligns the refactorer's output with the specific "tastes" of the downstream LLM.
Evaluation and Results
The RRG framework was evaluated on the CodeSearchNet (Python and Java) and ConCode (Java) benchmarks. Experiments involved several generator models, including GPT-2, PolyCoder, CodeGEN, and CodeGPT.
Key Performance Gains
RRG consistently outperformed standard RAG across all metrics:
- Exact Match (EM): Improvements of up to 28%.
- BLEU Score: Increases of up to 13%.
- CodeBLEU: Gains of up to 6.8%.
Insights
- The Preference Gap is Real: The study confirms that lexical similarity (retriever's metric) is not always the best indicator of a generator's success. Different LLMs prefer different refactored versions of the same code.
- Efficiency and Conciseness: By refactoring code into a more concise format, RRG reduces input length, lowering inference costs and mitigating the "lost in the middle" effect.
- Model Synergy: A small 60M parameter refactorer can significantly boost the performance of much larger generators (up to 350M parameters), demonstrating high efficiency.
Conclusion and Future Work
The RRG framework demonstrates that a small, preference-aligned refactorer can significantly enhance retrieval-augmented code generation. While current experiments focused on models up to 350M parameters, future work will explore scaling to frontier models like GPT-4. Additionally, the team aims to improve the interpretability of generator preferences and develop adaptive refactoring modules that can dynamically adjust output length based on query complexity.
Summary updated