Instructive Code Retriever: Learn from Large Language Model's Feedback for Code Intelligence Tasks

Jiawei Lu, Haoye Wang, Zhongxin Liu, Keyu Liang, Lingfeng Bao, Xiaohu Yang · DOI

Introduction

Large Language Models (LLMs) have revolutionized code intelligence tasks, ranging from automated code summarization to complex program synthesis and bug fixing. The primary mechanism for leveraging these models is In-Context Learning (ICL), where the model is provided with a few-shot prompt containing task-specific demonstrations. However, the efficacy of ICL is heavily dependent on the quality and relevance of these demonstrations. Traditional retrieval methods, such as BM25, rely on lexical keyword matching, which often fails to capture the deep structural and semantic nuances of programming languages.

To address this, we present the Instructive Code Retriever (ICR), a novel framework designed to optimize example selection by incorporating structural code analysis and direct feedback from LLMs. By aligning the retriever's selection process with the actual needs of the LLM, ICR significantly enhances performance across a variety of code intelligence benchmarks.

Methodology: The ICR Framework

The ICR framework moves beyond simple text-based retrieval by implementing a "model-aware" approach. Its architecture is defined by four key innovations:

1. Dual-Modality Encoding

Unlike standard retrievers that treat code as plain text, ICR utilizes a bi-encoder architecture (based on BERT) that processes two distinct modalities: the natural language description and the Abstract Syntax Tree (AST) of the code. By encoding the AST, the retriever can perceive the structural "skeleton" of a program, allowing it to distinguish between snippets that are lexically similar but logically distinct.

2. LLM-in-the-Loop Feedback

The "instructive" nature of ICR stems from its training signal. During the training phase, an LLM (e.g., GPT-Neo) evaluates a set of candidate demonstrations and provides a score based on how much each example improves its performance on a specific query. This feedback loop ensures that the retriever learns to prioritize examples that are empirically useful to the model, rather than just those that look similar to the query.

3. Tree-Based Contrastive Loss

To effectively learn from structural data, the authors introduced a specialized Ranking-Tree Loss function. This loss function incorporates structural similarity metrics, ensuring the retriever understands the relative quality of examples. It moves the training objective from simple binary similarity to a nuanced ranking based on the LLM's feedback.

4. Iterative Training Process

ICR employs an iterative refinement process. The retriever identifies potential examples, the LLM provides feedback on their utility, and the retriever subsequently updates its parameters to better align with the LLM’s preferences. This cycle continues until the retriever converges on an optimal selection strategy.

Experimental Evaluation

The researchers evaluated ICR across eight datasets covering three core domains:

  • Code Summarization: Utilizing CodeSearchNet (CSN) for Java and Python, with additional testing on JavaScript, PHP, and Ruby to assess generalization.
  • Program Synthesis: Using the CoNaLa dataset for Python.
  • Bug Fixing: Utilizing the Bug2Fix (B2F) dataset, categorized by code length (small and medium).

Key Results and Insights

ICR demonstrated substantial improvements over state-of-the-art (SOTA) baselines like BM25 and ASAP.

Performance Gains

  • Code Summarization: ICR achieved a 136% improvement in BLEU-4 scores over BM25 for Java and a 90% improvement over the ASAP method for Python.
  • Program Synthesis: On the CoNaLa dataset, ICR improved CodeBLEU scores by 74.6% compared to BM25.
  • Bug Fixing: The framework increased BLEU-4 scores by 3.6 and 3.2 points across the small and medium B2F datasets, respectively.

Cross-Language Generalization and Transferability

A significant finding was ICR’s ability to generalize across programming languages. Despite being trained primarily on Java and Python, the retriever performed exceptionally well on Ruby and PHP. This suggests that ICR learns universal structural patterns of "instructive" demonstrations that are not language-specific. Furthermore, a retriever trained on a smaller model (GPT-Neo-2.7B) successfully boosted the performance of a much larger model (Code Llama-13B), highlighting its potential as a general-purpose plug-in for various LLMs.

Discussion

The success of ICR underscores several critical points in code intelligence:

  • Structure as a Signal: ASTs are vital for tasks like bug fixing, where the difference between a bug and a fix is often syntactically minimal but structurally significant.
  • Model Alignment: "Model-agnostic" retrievers are limited. By becoming "model-aware," ICR accounts for the specific preferences and "blind spots" of the LLM it serves.
  • Robustness to Ordering: High-quality retrieval reduces the sensitivity of LLMs to the order of examples in a prompt, simplifying the prompt engineering process.

Limitations and Future Directions

While ICR represents a major step forward, it faces challenges regarding training overhead. Scoring thousands of examples with an LLM is computationally intensive. Current mitigations involve scoring only the "Top-K" candidates, but further optimization is needed for massive datasets. Additionally, future research will explore example diversity, aiming to retrieve sets of demonstrations that cover multiple facets of a problem rather than redundant variations of a single solution.

Summary updated

BibTeX
@inproceedings{lu2024instructive,
  author = {Jiawei Lu and Haoye Wang and Zhongxin Liu and Keyu Liang and Lingfeng Bao and Xiaohu Yang},
  title = {{Instructive Code Retriever: Learn from Large Language Model's Feedback for Code Intelligence Tasks}},
  booktitle = {ASE},
  year = {2024},
  doi = {10.1145/3691620.3694997},
}
Figure