Large Language Models Know What Makes Exemplary Contexts

Quanyu Long, Jianda Chen, Wenya Wang, Sinno Jialin PanarXiv 2024

Abstract

In-context learning (ICL) has proven to be a significant capability with the advancement of Large Language models (LLMs). By instructing LLMs using few-shot demonstrative examples, ICL enables them to perform a wide range of tasks without needing to update millions of parameters. This paper presents a unified framework for LLMs that allows them to self-select influential in-context examples to compose their contexts; self-rank candidates with different demonstration compositions; self-optimize the demonstration selection and ordering through reinforcement learning. Specifically, our method designs a parameter-efficient retrieval head that generates the optimized demonstration after training with rewards from LLM's own preference. Experimental results validate the proposed method's effectiveness in enhancing ICL performance. Additionally, our approach effectively identifies and selects the most representative examples for the current task, and includes more diversity in retrieval.

Introduction

In-Context Learning (ICL) allows Large Language Models (LLMs) to solve new tasks by observing a few demonstrative examples provided in the prompt. However, ICL is notoriously brittle; model performance can fluctuate significantly based on the choice and ordering of these examples. Traditional solutions often rely on dense retrievers (like SBERT) to find examples semantically similar to the query. This approach suffers from three major flaws: a mismatch between small retrieval models and massive inference models, a failure to account for mutual influence between examples in a prompt, and a lack of optimization for example ordering.

The RL-ICL Framework

To address these challenges, we introduce RL-ICL, a unified framework that treats example selection as a sequential decision-making problem. The framework consists of three core stages:

  1. Self-Select: Instead of picking $k$ examples simultaneously, the model selects them one by one. After each selection, the context is updated, allowing the model to naturally account for the mutual influence and ordering of the demonstration set.
  2. Self-Rank: The model evaluates various candidate demonstration compositions, ranking them based on the LLM's internal log-probabilities for the correct answer.
  3. Self-Optimize: We add a lightweight, parameter-efficient retrieval head to a frozen LLM. This head is fine-tuned using Proximal Policy Optimization (PPO) based on a reward model trained on the LLM's own preferences.
The RL-ICL unified framework illustrating the (a) self-select, (b) self-rank, and (c) self-optimize stages.

Experimental Results

We evaluated RL-ICL across 11 diverse tasks, including sentiment analysis, commonsense reasoning, natural language inference, and text generation, using Llama-3-8B as the base model. RL-ICL consistently outperformed traditional baselines like BM25, SBERT, and SimCSE. For example, on the SST-2 sentiment task, RL-ICL achieved 93.34% accuracy, a significant improvement over the vanilla Llama-3's 89.56%.

Our analysis shows that the RL-optimized retriever prioritizes diversity and representativeness over simple semantic similarity. Furthermore, the framework effectively mitigates order sensitivity by optimizing the sequence of demonstrations. While the computational cost of sampling grows with the number of demonstrations ($k$), the performance gains stabilize as $k$ increases.

Performance scaling across different numbers of demonstrations (k).

Conclusion

RL-ICL demonstrates that LLMs can serve as their own best judges for selecting exemplary contexts. By aligning retrieval with the model's internal preferences through reinforcement learning, we achieve superior and more robust ICL performance. Future work will explore applying this framework to longer contexts and developing generative retrievers that synthesize optimized examples.

Summary updated

BibTeX
@misc{long2024large,
  author = {Quanyu Long and Jianda Chen and Wenya Wang and Sinno Jialin Pan},
  title = {{Large Language Models Know What Makes Exemplary Contexts}},
  year = {2024},
  eprint = {2408.07505},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.07505},
}
Figure