Learning to Retrieve In-Context Examples for Large Language Models

Liang Wang, Nan Yang, Furu Wei · DOI

Abstract

In-context learning (ICL) allows Large Language Models (LLMs) to perform new tasks using a few demonstrations. However, performance is highly sensitive to the choice of examples. This paper introduces LLM-R (LLM Retriever), a framework that iteratively distills fine-grained LLM preferences into a dense bi-encoder retriever. By using the LLM itself as an oracle to rank candidates, LLM-R learns to select examples that maximize performance. Evaluation across 30 tasks shows that LLM-R significantly outperforms standard retrieval baselines and generalizes across different model architectures and unseen tasks.

Introduction

Large Language Models have demonstrated remarkable few-shot capabilities through in-context learning. Despite this, the selection of in-context examples remains a critical bottleneck; minor changes in the prompt examples can lead to drastic fluctuations in accuracy. Traditional retrieval methods like BM25 or off-the-shelf sentence embeddings (e.g., SBERT) often fail to capture the specific nuances that make an example "helpful" for a particular LLM's internal logic. LLM-R addresses this by aligning the retriever directly with the LLM's preferences.

Methodology

The authors propose LLM-R, a multi-stage pipeline designed to bridge the gap between retrieval and LLM performance through iterative distillation.

The LLM-R framework: (Top) Iterative training involving candidate mining, LLM ranking, reward modeling, and distillation. (Bottom) Inference using the trained bi-encoder retriever.

The training process consists of four primary steps:

  1. Candidate Mining: An initial pool of potential examples is identified using a basic retriever like BM25.
  2. LLM as the Oracle: A frozen LLM ranks these candidates based on the log-probability it assigns to the correct answer when that example is included in the prompt. This provides a direct signal of "helpfulness."
  3. Reward Modeling: To avoid the high cost of querying the LLM for every retrieval, a cross-encoder reward model is trained to mimic the LLM's ranking preferences.
  4. Knowledge Distillation: Finally, a bi-encoder dense retriever is trained by distilling knowledge from the reward model. This bi-encoder is efficient enough for large-scale, real-time search.

This process is iterative. The newly trained bi-encoder finds "harder" candidates, which are then re-ranked by the LLM to further refine the retriever's accuracy.

Experimental Setup

The framework was evaluated using a massive collection of 30 NLP tasks spanning nine distinct categories, including Question Answering, Sentiment Analysis, Natural Language Inference (NLI), and Text Generation.

Overview of the 30 evaluation tasks across 9 categories. Orange boxes indicate held-out datasets used to test generalization.

To rigorously test generalization, four datasets—QNLI, PIQA, WSC273, and Yelp—were held out during the training phase. This allowed the researchers to observe how well the retriever performs on entirely unseen task types.

Key Results and Insights

The experimental results highlight several critical advantages of the LLM-R approach:

  • Significant Performance Gains: LLM-R improved LLaMA-7B's performance by an average of 7.8% compared to random example selection, consistently outperforming strong baselines like BM25, SBERT, and E5.
  • Cross-Model Generalization: A retriever trained on feedback from LLaMA-7B was found to improve the performance of other models, including GPT-3.5-Turbo and GPT-Neo. This suggests that "high-quality" examples possess universal characteristics across different architectures.
  • Pattern Matching over Semantic Proximity: Analysis revealed that LLM-R often prioritizes examples with similar input patterns or label distributions rather than simple semantic similarity.
  • Impact on Model Size: Smaller models (e.g., GPT-Neo-2.7B) showed the most significant improvements. High-quality examples act as a "force multiplier" for models with less inherent reasoning capacity.

Discussion and Limitations

While LLM-R represents a significant step forward, several limitations remain. The current model retrieves the "top-k" examples independently, ignoring potential interactions or the need for diversity within a single prompt. Additionally, the initial training phase is computationally expensive due to the LLM ranking step, though this is a one-time cost. Future research may explore combinatorial optimization to select sets of examples that work synergistically.

Conclusion

LLM-R demonstrates that directly incorporating LLM feedback into the retrieval training loop creates a more effective and generalizable system for in-context learning. By distilling complex model preferences into an efficient bi-encoder, the framework provides a scalable path toward optimizing LLM prompts for a wide variety of downstream tasks.

Summary updated

BibTeX
@inproceedings{wang2024learning,
  author = {Liang Wang and Nan Yang and Furu Wei},
  title = {{Learning to Retrieve In-Context Examples for Large Language Models}},
  booktitle = {EACL},
  year = {2024},
  url = {https://openreview.net/forum?id=UI5iLaBDJu},
}
Figure