TL;DR
This paper introduces LAIL, a novel approach to improve code generation with Large Language Models (LLMs) by intelligently selecting demonstration examples for in-context learning. LAIL leverages the LLMs themselves to assess which examples are most helpful, leading to significantly better code generation performance and more human-preferred code.
What Problem Does This Paper Address?
Large Language Models (LLMs) have shown remarkable ability in code generation through "in-context learning" (ICL). This means they can generate code based on a prompt that includes a few examples of requirements and their corresponding code solutions. However, the performance of ICL is highly sensitive to the quality of these examples. Current methods for selecting examples are often heuristic (e.g., random selection or simple text similarity), which can lead to suboptimal and inconsistent results. This paper addresses the critical need for a more intelligent, LLM-aware method to select demonstration examples that truly align with the LLM's internal "preferences" and knowledge, thereby boosting code generation accuracy.
Methods
The core of LAIL is its "LLM-aware" approach to example selection. Instead of relying on external heuristics, LAIL uses the LLM itself to evaluate candidate examples. For a given requirement, LAIL feeds candidate examples to the LLM and observes the prediction probability of the ground-truth code. This probability serves as a measure of how helpful the example is. Based on these probabilities, candidate examples are labeled as "positive" (helpful) or "negative" (less helpful).
These labeled examples are then used to train a "neural retriever." This retriever is optimized using a contrastive learning objective, aiming to learn the LLM's preferences for good examples. Essentially, it learns to pull requirements and their truly helpful examples closer together in an embedding space, while pushing away unhelpful ones. During inference, this trained retriever selects the most relevant examples for a new requirement, which are then fed into the LLM for code generation.
Datasets
The paper evaluates LAIL on three code generation datasets:
- MBJP (Java)
- MBPP (Python)
- MBCPP (C++)
These datasets are derived from existing benchmarks and contain programming problems with descriptions, solutions, and test cases. The authors also mention experiments on a repository-level dataset called DevEval.
Key Insights and Ideas
- LLMs know best: The most significant insight is that LLMs themselves possess the best understanding of what makes a good demonstration example for code generation. Relying on LLM feedback directly is more effective than traditional similarity metrics.
- Beyond text similarity: Simple textual similarity between requirements is insufficient. LAIL demonstrates that semantically relevant examples, even if not textually identical, can be far more beneficial.
- Transferability is key: A trained LAIL retriever shows surprising transferability across different LLMs and even different programming languages and datasets, suggesting a generalizable mechanism for understanding LLM preferences.
- Contrastive learning for preference: Using contrastive learning to train a retriever effectively captures and leverages the LLM's learned preferences for demonstration examples.
Key Results
LAIL significantly outperforms state-of-the-art baselines across multiple LLMs (CodeGen, GPT-3.5, ChatGPT) and datasets.
- CodeGen: LAIL achieved improvements of 11.58%, 6.89%, and 5.07% in Pass@1 on MBJP, MBPP, and MBCPP, respectively.
- GPT-3.5: Improvements of 4.38%, 2.85%, and 2.74% in Pass@1 were observed on the same datasets.
- Human Evaluation: Developers preferred programs generated by LAIL, rating them higher in correctness, code quality, and maintainability, with improvements of up to 11.5% in correctness over the SOTA baseline.
- Transferability: Retrievers trained on one LLM or dataset showed significant performance gains when applied to others, indicating robustness.
Limitations and Future Work
- Computational Cost: While LAIL is efficient during inference, the initial training of the retriever and the LLM-based example estimation can be computationally intensive.
- Candidate Pool Size: The performance can be influenced by the size and quality of the candidate pool from which examples are selected.
- Future Work: The authors suggest exploring more sophisticated methods for LLM-based example estimation and investigating the transferability of retrievers to a wider range of LLMs and datasets. They also aim to further optimize the contrastive learning parameters.
Summary updated