Learning to Retrieve In-Context Examples for Large Language Models

Liang Wang, Nan Yang, Furu Wei

Introduction

In-Context Learning (ICL) has revolutionized how we interact with Large Language Models (LLMs), allowing them to perform new tasks by simply providing a few examples within the prompt. However, LLM performance is highly sensitive to the specific examples chosen and their ordering. Traditional retrieval methods like BM25 or generic sentence embeddings often fail to account for the "internal logic" of a specific LLM. To bridge this gap, researchers at Microsoft developed LLM-R (LLM Retriever), a framework that trains dense retrievers to select the most effective in-context examples by leveraging the LLM's own feedback.

Methodology: The LLM-R Pipeline

The LLM-R framework utilizes a three-stage iterative pipeline to align a retriever with the preferences of a target LLM. This process ensures that the retriever learns to identify examples that maximize the model's predictive accuracy.

The LLM-R training and inference pipeline, illustrating the iterative process of ranking, reward modeling, and distillation.
  1. Ranking with LLM Feedback: The process begins with a pool of candidate examples. For a given query, a frozen LLM (such as LLaMA) calculates the conditional log-likelihood of the correct answer given different candidate examples. This identifies which examples make the correct answer most apparent to the model.
  2. Reward Modeling: Because raw LLM log-probabilities can be noisy, a Cross-Encoder reward model is trained to predict these preferences. This model provides stable "soft labels" that are more informative than simple binary rankings.
  3. Knowledge Distillation: Finally, the knowledge from the reward model is distilled into a Bi-Encoder dense retriever. While the reward model is accurate but slow, the Bi-Encoder allows for lightning-fast retrieval during inference, making it practical for real-world applications.

Experimental Setup

The effectiveness of LLM-R was evaluated across a massive collection of 30 publicly available NLP datasets spanning nine categories, including Question Answering, Commonsense Reasoning, and Sentiment Analysis.

Overview of the 30 NLP datasets used for training and evaluation, with held-out tasks highlighted in yellow.

To test the framework's ability to generalize, four datasets—QNLI, PIQA, WSC273, and Yelp—were held out entirely during the training phase. The retrieval pool consisted of approximately 6.3 million examples, providing a robust testbed for the retriever's performance.

Key Insights and Results

The study yielded several critical insights into the nature of in-context retrieval:

  • The LLM as the Ultimate Judge: Using conditional log-probabilities as a training signal captures fine-grained signals that accuracy-based metrics miss, allowing the retriever to learn the model's specific needs.
  • Pattern Matching: Analysis showed that LLM-R often retrieves examples sharing similar syntactic patterns or label distributions with the query, rather than just keyword similarity.
  • Cross-Model Generalization: A retriever trained on LLaMA-7B feedback also improved the performance of other models like GPT-3.5-Turbo, suggesting universal qualities to "good" in-context examples.

Quantitatively, LLM-R achieved a 7.8% average performance boost across all 30 tasks. On held-out tasks, it outperformed the next best model (E5-base) by an average of 5.4 points, proving it learns a generalizable retrieval strategy rather than just memorizing tasks.

Conclusion and Future Work

LLM-R demonstrates that the most effective way to optimize in-context learning is to let the LLM itself guide the retrieval process. While the framework shows significant promise, current limitations include the assumption of independence between examples in a prompt. Future work may explore combinatorial optimization to pick the best set of examples. Additionally, while LLM-R excels at classification, its impact on knowledge-intensive tasks is more modest, suggesting that different strategies may be needed when the LLM relies heavily on internal factual memory.

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