Domain Adaptive Code Completion via Language Models and Decoupled Domain Databases

Ze Tang, Jidong Ge, Shangqing Liu, Tingwei Zhu, Tongtong Xu, Liguo Huang, Bin Luo

Large Language Models (LLMs) like GPT-4 and CodeGPT have revolutionized how we write code, but they often stumble when they hit the "domain wall." If you’re working on a niche internal project or using a brand-new library, these models—trained on general internet data—simply don't have the context.

A new paper, "Domain Adaptive Code Completion via Language Models and Decoupled Domain Databases," introduces a clever way to bridge this gap without the massive overhead of fine-tuning.

TL;DR

The authors propose $k$NM-LM (k-nearest mistakes language model), a retrieval-augmented system that helps LLMs adapt to specific projects by maintaining a "database of mistakes." By dynamically combining a base model's predictions with a decoupled datastore of domain-specific tokens using Bayesian inference, they achieve significant accuracy gains on private APIs and libraries without ever touching the model's internal weights.

What Problem Does This Paper Address?

Standard LLMs are "frozen" after training. To make them better at a specific task, researchers usually turn to fine-tuning, but this is problematic for two reasons:

  1. Accessibility: Many state-of-the-art models are "black boxes" (APIs only), making weight updates impossible.
  2. Volatility: Codebases change daily. Fine-tuning a model every time a developer pushes a new internal library is computationally ruinous.

Existing retrieval-augmented models ($k$NN-LMs) try to help by searching a database of code at test time. However, they usually store every token in the project, which is redundant and requires developers to manually tune "interpolation weights" (how much to trust the database vs. the model) for every new project.

Methods

The authors introduce two major innovations to make retrieval-augmented code completion practical:

  1. The Decoupled Database: Instead of storing every token from the domain code, $k$NM-LM only stores tokens that the base language model failed to predict correctly. This "collection of mistakes" acts as a specialized patch, reducing storage requirements and search noise.
  2. Bayesian Inference for Dynamic Weighting: In traditional systems, you might say "trust the database 20% of the time." $k$NM-LM is smarter. It uses Bayesian inference to look at the recent context. If the base model has been struggling to predict the last few tokens in a file, the system automatically increases the weight of the retrieval module. If the model is on a roll, it trusts the model more.

Because this approach only requires the model's output probabilities and embeddings, it is "plug-and-play" for black-box models like GPT-4.

Datasets

The researchers evaluated their tool across two main dimensions:

  • Intra-project Completion: They used 20 Java projects (10 large, 10 small) from the Java-mid dataset, using Git history to ensure the model was tested on code written after the database was built.
  • Intra-scenario Completion (API-Bench): This dataset covers four domains (Android, Machine Learning, Security, and Testing) across both Java and Python, testing how well the model handles specific third-party libraries.

Key Insights and Ideas

  • Learning from Failure: The most elegant idea here is the "mistakes" datastore. By decoupling the database from what the model already knows, the system focuses its resources exactly where the LLM is weakest—domain-specific identifiers and custom API calls.
  • Context-Aware Trust: The use of the Beta distribution to model the probability of an error ($p(E')$) allows the system to be statistically rigorous. It treats the recent code context as "new evidence" to update its belief in whether the model or the database is more likely to be right.
  • Robustness: Unlike previous methods that are highly sensitive to hyper-parameters, $k$NM-LM showed stable performance across various settings, making it much more viable for real-world IDE plugins where developers won't want to fiddle with settings.

Key Results

  • Significant Accuracy Boosts: On the "hover" project, $k$NM-LM improved token completion accuracy by 26.67% over CodeGPT and 35.99% over UniXcoder.
  • Superior API Handling: For complex Android API calls, the system nearly doubled the "Exact Match" (EM) rate compared to base models (e.g., CodeGPT went from 5.99% to 13.74%).
  • Efficiency: The decoupled database was significantly smaller than standard $k$NN-LM datastores (saving up to 389MB in large projects) while actually improving search speed and accuracy.
  • Outperforming Fine-tuning: While fine-tuning is generally powerful, the authors found that $k$NM-LM could be combined with fine-tuned models to push performance even higher, suggesting the two approaches are complementary.

Limitations and Future Work

  • Latency: While faster than previous retrieval methods, $k$NM-LM still adds a small delay compared to a raw LLM. The authors note it stays under the 1-second threshold for developer patience, but further optimization is needed for real-time "as-you-type" suggestions.
  • Cold Starts: The system relies on having some "in-domain" code to build the database. Its utility is lower at the very start of a brand-new project with no existing files.
  • Future Directions: The authors suggest applying this "decoupled mistake" logic to other software engineering tasks, such as code summarization or automated bug fixing, where domain-specific context is equally critical.

Summary updated

BibTeX
@inproceedings{tang2023domain,
  author = {Ze Tang and Jidong Ge and Shangqing Liu and Tingwei Zhu and Tongtong Xu and Liguo Huang and Bin Luo},
  title = {{Domain Adaptive Code Completion via Language Models and Decoupled Domain Databases}},
  booktitle = {ASE},
  year = {2023},
  url = {https://openreview.net/forum?id=dAHdL54jPP3},
}

Loading related content...

Figure