Abstract
Large Language Models (LLMs) have revolutionized code generation at the snippet level, yet they often struggle with repository-level understanding, leading to hallucinations regarding project-wide logic and inter-file dependencies. This paper presents a methodology to enhance repository-level code QA by combining self-alignment—fine-tuning on model-generated, self-curated data—with Retrieval-Augmented Generation (RAG). Our results show that a specialized Mistral 7B model using this combined approach significantly outperforms its base version and even GPT-3.5 Turbo on repository-specific tasks. Furthermore, we introduce SpyderCodeQA, a manually curated benchmark designed to evaluate code QA across semantics, dependencies, and meta-information.
Introduction
While tools like GitHub Copilot excel at boilerplate generation, they frequently fail when asked complex questions about an entire software repository. These "hallucinations" often stem from a lack of context regarding how disparate files interact or how project-wide documentation relates to the source code. Additionally, privacy and cost concerns often prevent developers from using commercial APIs for proprietary codebases. This research, presented at ACL 2024 by researchers from the University of Hamburg, investigates how open-source models can be specialized for local repositories to provide high-performing, private, and cost-effective assistance.
Methodology
The study evaluates three primary strategies for repository specialization:
- Self-Alignment (Fine-Tuning): A "teacher" model generates synthetic Q&A pairs from repository code chunks. These samples undergo a "self-curation" process where the model filters out low-quality data before fine-tuning the target model using QLoRA.
- Retrieval-Augmented Generation (RAG): Relevant code context is retrieved from a Chroma vector database at inference time to ground the model's responses.
- The Combined Approach: The self-aligned model is integrated with the RAG pipeline, leveraging both its specialized internal knowledge and real-time external context.
Performance is measured using an "LLM-as-a-judge" framework, where GPT-4 performs pairwise comparisons between model outputs to determine win rates.
The SpyderCodeQA Benchmark
To rigorously evaluate these methods, the authors developed SpyderCodeQA, a manually curated benchmark based on the Spyder IDE repository. It consists of 325 expert-verified Q&A pairs categorized into three distinct dimensions of repository knowledge.
As illustrated in the figure above, the benchmark tests the model's grasp of what the code does (Semantics), how files relate (Dependencies), and high-level project details found in READMEs or licenses (Meta-Information).
Key Results and Insights
The experimental results highlight the synergy between fine-tuning and retrieval:
- Superior Performance: The combined Fine-tuned + RAG model achieved a 64% win rate against the base Mistral 7B and a 72% win rate against GPT-3.5 Turbo on repository-specific queries.
- Complementary Strengths: Fine-tuning helps the model internalize the repository's structure and coding style, while RAG provides the specific facts necessary to minimize hallucinations.
- The Specialization Trade-off: While the model became highly proficient in the target repository, its performance on general coding benchmarks like HumanEval decreased, indicating "catastrophic forgetting." This suggests that production-ready assistants might require Mixture-of-Experts (MoE) architectures to maintain general capabilities.
- Training Optimization: Performance peaked after two training epochs on the synthetic dataset; further training led to overfitting and performance degradation.
Limitations and Future Directions
The current study focused on a single Python repository and "1-hop" dependency analysis. Future research should explore multi-hop dependency structures (e.g., A depends on B, which depends on C) and evaluate the approach across diverse programming languages and larger project scales. Additionally, incorporating human-in-the-loop evaluations will be crucial for validating the real-world utility of these specialized assistants for developers.
Summary updated