In-context Learning with Retrieved Demonstrations for Language Models: A Survey

Man Luo, Xin Xu, Yue Liu, Panupong Pasupat, Mehran Kazemi

Abstract

Large language models have demonstrated remarkable few-shot in-context learning (ICL) capabilities, adapting to new tasks with few-shots demonstrations. However, the efficacy of ICL is highly dependent on the selection of these demonstrations. Recent developments have introduced retrieval-based in-context learning (RetICL), which dynamically retrieves demonstrations tailored to each input query. This approach leverages existing databases and retrieval systems, enhancing efficiency and scalability while mitigating biases inherent in manual example selection. Given the promising results and growing interest in RetICL, we present a comprehensive survey of this field. Our review encompasses: design choices for ICL demonstration retrieval models, retrieval training procedures, inference strategies and current applications of RetICL. In the end, we explore future directions for this emerging technology.

Introduction

Large Language Models (LLMs) have revolutionized natural language processing through their ability to perform tasks via "few-shot" in-context learning (ICL). By providing a few examples (demonstrations) within the prompt, an LLM can infer task requirements and generate appropriate responses. However, this process is notoriously "brittle." Performance can fluctuate wildly based on the choice of examples, their ordering, and even the formatting used. Traditionally, researchers relied on a fixed set of hand-picked or randomly selected examples for every query. This static approach is often sub-optimal, failing to capture the specific nuances of diverse inputs.

Retrieval-based In-context Learning (RetICL) addresses this by using a dedicated retriever to find query-specific demonstrations. This survey explores how RetICL makes LLMs more robust, efficient, and capable of handling complex tasks without requiring expensive fine-tuning.

Design Dimensions of RetICL

The RetICL framework can be categorized into several key design dimensions, ranging from retrieval objectives to training procedures.

Taxonomy of Retrieval-based In-context Learning (RetICL)

Retrieval Objectives and Strategies

Most RetICL systems prioritize Similarity, seeking examples that are semantically or structurally similar to the input query. However, recent research emphasizes Diversity to ensure the prompt covers a broader reasoning space and avoids redundancy. Common strategies include:

  • Top-k Retrieval: Selecting the $k$ most similar examples independently.
  • Clustering: Grouping the example pool and selecting representative samples from each cluster to ensure variety.
  • Iterative Selection: A dynamic approach where the system selects one example, then uses the combined query and selected example to find the next most complementary demonstration.

Retriever Types and Training

Retrievers generally fall into two categories:

  1. Off-the-shelf Retrievers: Utilizing standard tools like BM25 for keyword matching or SBERT for dense vector embeddings.
  2. Fine-tuned Retrievers: Training custom models specifically for demonstration selection. A popular technique involves using a large "Teacher" LLM to score example helpfulness, then training a smaller retriever to predict those scores using losses like InfoNCE (contrastive learning) or KL-Divergence.

Literature Review and Key Results

The field of RetICL has seen a surge of representative works, each exploring different combinations of LLMs, retrieval methods, and evaluation tasks.

Summary of representative works in Retrieval-based In-context Learning

Performance and Scaling

RetICL consistently outperforms random or manual selection across various benchmarks, including Natural Language Understanding (NLU), reasoning (GSM8K), and coding (Text-to-SQL). For instance, in open-domain QA, BM25-based retrieval has shown improvements of over 10%. In more specialized tasks like text-to-SQL, retrievers like EPR have achieved gains of up to 30% over random baselines. Furthermore, RetICL allows smaller models (e.g., 7B parameters) to achieve performance levels comparable to much larger models (e.g., 175B) by providing high-quality, relevant "hints."

Key Insights

  • Reduced Sensitivity to Order: RetICL significantly mitigates the "order problem" in ICL. Because retrieved examples are highly relevant, the model's performance is less dependent on the sequence in which they appear.
  • The Power of BM25: Despite its simplicity, the keyword-based BM25 algorithm remains highly effective, particularly in tasks like semantic parsing where exact terminology is critical.
  • Diversity over Pure Similarity: Simply picking the most similar examples can lead to homogenous prompts. Incorporating diversity often yields superior reasoning performance.
  • LLM-in-the-loop Training: Using LLM feedback as a training signal for retrievers eliminates the need for manual labels, allowing for the creation of state-of-the-art retrieval models.

Future Directions and Limitations

While RetICL offers significant advantages, several challenges remain:

  • Unlabelled Corpora: Most current methods rely on clean databases of input-output pairs. Retrieving demonstrations from raw, unlabelled text (e.g., the web) is a major frontier.
  • Inference Latency: The additional step of retrieval adds computational overhead. Developing lightweight, high-speed retrievers is essential for real-time applications.
  • Multi-modal Integration: Extending RetICL to vision-language models, where images or videos serve as demonstrations, is still in its early stages.
  • Theoretical Foundations: While empirical results are strong, a rigorous mathematical understanding of why specific retrieved examples improve transformer reasoning is still lacking.

Summary updated

BibTeX
@article{luo2024context,
  author = {Man Luo and Xin Xu and Yue Liu and Panupong Pasupat and Mehran Kazemi},
  title = {{In-context Learning with Retrieved Demonstrations for Language Models: A Survey}},
  journal = {Accepted by TMLR},
  year = {2024},
  url = {https://openreview.net/forum?id=NQPo8ZhQPa},
}
Figure