What Makes Good In-Context Demonstrations for Code Intelligence Tasks with LLMs?

Shuzheng Gao, Xin-Cheng Wen, Cuiyun Gao, Wenxuan Wang, Hongyu Zhang, Michael R. Lyu · DOI

TL;DR

Large Language Models (LLMs) can learn tasks through "in-context learning" (ICL) by being shown a few examples. This paper systematically investigates how the selection, order, and number of these examples significantly impact ICL performance on code intelligence tasks like summarization, bug fixing, and synthesis. The findings offer practical guidance for crafting better demonstrations, leading to substantial performance gains.

What Problem Does This Paper Address?

The rise of LLMs has opened up new possibilities for code intelligence tasks, with in-context learning (ICL) being a particularly exciting development. ICL allows LLMs to adapt to new tasks with just a few examples, bypassing the need for costly fine-tuning. However, the effectiveness of ICL is highly sensitive to the quality of these "demonstration" examples. Researchers and practitioners currently lack a deep understanding of what constitutes a "good" demonstration for code-related tasks. This paper aims to fill this gap by empirically dissecting the impact of key demonstration design choices on ICL performance across several core code intelligence tasks.

Methods

The authors conduct a comprehensive experimental study to understand the impact of three primary factors on ICL performance:

  1. Demonstration Selection: They explore various methods for choosing demonstration examples, ranging from simple random selection to more sophisticated approaches like K-means clustering (for diversity) and retrieval-based methods (BM-25, SBERT, UniXcoder, CoCoSoDa) for instance-level selection.
  2. Demonstration Ordering: They investigate how the sequence of demonstration examples influences performance, comparing random ordering with similarity-based and reverse similarity-based ordering relative to the test sample.
  3. Number of Demonstration Examples: They systematically vary the number of examples provided in the prompt, from one up to 64, to observe its effect on performance.

These experiments are performed across three code intelligence tasks: code summarization, bug fixing, and program synthesis, using LLMs like OpenAI's Codex, GPT-3.5, and ChatGPT.

Datasets

The study utilizes established datasets for its evaluation:

  • Code Summarization: CodeSearchNet (CSN) (Java portion) and TLCodeSum (TLC).
  • Bug Fixing: B2F (B2Fmedium and B2Fsmall subsets).
  • Program Synthesis: CoNaLa.

The paper notes the statistics and characteristics of these datasets, including their sizes and typical use in prior research.

Key Insights and Ideas

  • Diversity Matters for Task-Level Demonstrations: For demonstrations that are the same for all test samples (task-level), improving the diversity of selected examples (e.g., using K-means clustering) not only boosts average performance but also leads to more stable predictions, reducing the variability caused by different example sets.
  • Instance-Level is King: Demonstrations tailored to each specific test sample (instance-level) consistently and significantly outperform task-level demonstrations across all evaluated tasks. This highlights the power of providing highly relevant examples.
  • BM-25: A Simple Yet Effective Baseline: For instance-level demonstration selection, the classic BM-25 retrieval method proves to be a surprisingly effective and simple approach, often matching or exceeding more complex dense retrieval methods.
  • Order Can Be Crucial, Especially for Task-Level: While instance-level demonstrations are generally robust to ordering, task-level demonstrations are more sensitive. Placing examples in ascending order of similarity to the test sample often yields better results.
  • More Examples Aren't Always Better (Due to Truncation): While increasing the number of demonstration examples generally improves performance initially, this effect plateaus and can even degrade due to LLM input length limitations. Code, being longer than natural language, exacerbates this "truncation problem," making careful selection of the number of examples critical.

Key Results

The paper reports substantial improvements achieved by carefully designed demonstrations:

  • Code Summarization: Improvements of at least 9.90% in BLEU-4.
  • Bug Fixing: Improvements of at least 175.96% in Exact Match (EM).
  • Program Synthesis: Improvements of at least 50.81% in Exact Match (EM).

These gains are compared against widely-used demonstration construction methods and zero-shot ICL, demonstrating the significant impact of thoughtful demonstration design. For instance, on GPT-3.5 and ChatGPT, a carefully designed demonstration outperformed baseline demonstrations by up to 294.57% (EM for bug fixing).

Limitations and Future Work

The authors acknowledge several limitations and suggest future research directions:

  • Potential Data Leakage: As they use closed-source LLMs (Codex, GPT-3.5, ChatGPT), there's a theoretical risk of models memorizing test data. However, they argue that the poor performance in zero-shot settings suggests this is unlikely to be the primary driver of results.
  • Task and Model Selection: The study focuses on three representative tasks and three LLMs. Future work could explore other task types (e.g., vulnerability detection, code translation) and a broader range of LLMs (e.g., CodeGen, CodeGeeX).
  • Language Diversity: Experiments were primarily conducted on Java and Python. Extending to other programming languages is a natural next step.
  • Code Representation: Current retrieval models still have a significant gap compared to an "oracle" (ideal selection), indicating room for improvement in code representation learning for semantic similarity.
  • Ordering Robustness: While similarity-based ordering shows promise, its improvement isn't always consistent, suggesting further research into optimal ordering methods.
  • Truncation Mitigation: Given the length of code, developing techniques to incorporate program slicing or reduction into ICL to manage prompt length and costs is crucial.
  • Quality of Code: Future work could also explore how the inherent quality or complexity of the source code itself impacts ICL performance.

Summary updated

BibTeX
@inproceedings{gao2023what,
  author = {Shuzheng Gao and Xin-Cheng Wen and Cuiyun Gao and Wenxuan Wang and Hongyu Zhang and Michael R. Lyu},
  title = {{What Makes Good In-Context Demonstrations for Code Intelligence Tasks with LLMs?}},
  booktitle = {ASE},
  year = {2023},
  doi = {10.1109/ase56229.2023.00109},
}
Figure