TL;DR
This paper introduces CODEFILTER, a novel framework that intelligently filters retrieved code snippets to improve repository-level code completion. By analyzing the actual impact of each retrieved chunk on the completion task, CODEFILTER significantly boosts accuracy and efficiency, while also reducing the computational burden.
What Problem Does This Paper Address?
Modern code completion tools aim to assist developers by suggesting code snippets. A promising approach is Retrieval-Augmented Generation (RAG), which retrieves relevant code from other files in a repository to provide context. However, a key challenge is that simply retrieving many code chunks doesn't guarantee better completion. In fact, many retrieved chunks can be irrelevant or even detrimental, introducing noise and degrading performance. This paper addresses the problem of effectively selecting which retrieved code snippets are truly beneficial for code completion, moving beyond simply retrieving a fixed number of top-ranked chunks.
Methods
The core of CODEFILTER lies in its ability to assess the impact of each retrieved code chunk. The authors propose a likelihood-based metric to quantify how much a specific code chunk influences the model's ability to generate the correct completion. This metric calculates the difference in the model's log-likelihood of the ground truth code when the chunk is included versus when it's not.
Based on this metric, each retrieved chunk is classified as positive, neutral, or negative in its contribution. CODEFILTER then uses this information to build a framework that:
- Selectively retrieves: It can dynamically decide if more context is needed.
- Filters aggressively: It prioritizes and retains only the positive chunks, discarding neutral and negative ones.
- Adapts context: It learns to determine when enough context is available, avoiding unnecessary retrieval and processing.
This "filter-then-generate" paradigm allows the model to focus on genuinely helpful information.
Datasets
- RepoEval: A popular benchmark for repository-level code completion.
- CrossCodeLongEval: Another benchmark used for evaluating code completion.
- Custom Dataset: The authors constructed a new dataset by sampling from the Stack dataset, containing approximately 43,000 instances with over 400,000 labeled cross-file chunks. Each chunk was labeled as positive, neutral, or negative using their proposed likelihood-based metric.
Key Insights and Ideas
- Not all retrieved context is good context: A significant portion of retrieved code chunks can be irrelevant or even harmful to code completion. This highlights the need for intelligent filtering rather than just quantity.
- Impact matters more than similarity: Traditional retrieval methods often rely on similarity scores (like Jaccard). This paper shows that direct impact on the completion task is a more crucial factor.
- Filtering negative chunks is key: Removing chunks that actively degrade performance leads to substantial accuracy improvements.
- CODEFILTER is a "plug-and-play" component: The framework's filtering logic can be applied on top of existing retrieval methods and even generalize to larger language models, enhancing their performance.
Key Results
- Accuracy Improvement: CODEFILTER consistently improves completion accuracy, achieving an average of 3% increase in exact match over baseline RAG approaches across various tasks and models.
- Mitigating Negative Impact: For instances with negatively impacting chunks, CODEFILTER provides a substantial improvement of over 10% in exact match.
- Efficiency Gains: The framework significantly reduces the length of cross-file contexts, shortening the prompt by over 80% in token count, leading to enhanced computational efficiency.
- Generalizability: CODEFILTER demonstrates strong generalizability across different LLMs (StarCoderBase, CodeLlama) and can be used as a filtering policy for larger models like GPT-3.5.
Limitations and Future Work
- Language Specificity: The current study and framework are primarily focused on Python. Future work could explore generalization to other programming languages.
- Metric Reliance: While reference-based metrics are used, execution-based metrics (like unit test pass rates) could offer a more robust quality assessment, especially for longer code targets.
- Inference Overhead: CODEFILTER introduces some inference-time overhead due to the additional signal token generation. While it reduces retrieval costs, further optimizations for overall latency are a direction for future research.
- Broader Applications: The likelihood-based polarity metric is model-agnostic and could be extended to other NLP tasks beyond code completion, such as question answering or code generation/repair.
Summary updated