Abstract
Large language models (LLMs) leveraging in-context learning (ICL) have set new benchmarks in few-shot learning across various tasks without needing task-specific fine-tuning. However, extensive research has demonstrated that the effectiveness of ICL is significantly influenced by the selection and ordering of demonstrations. Considering the critical role of demonstration selection in ICL, we introduce DemoShapley which is inspired by the Data Shapley valuation theorem. This approach assesses the influence of individual demonstration instances, distinguishing between those that contribute positively and those that may hinder performance. Our findings reveal that DemoShapley not only enhances model performance in terms of accuracy and fairness but also generalizes queries from domains distinct from those of the in-context demonstrations, highlighting its versatility and effectiveness in optimizing ICL demonstration selection. Last but not least, DemoShapley demonstrates its ability to aid in identifying noisy data within the demonstration set.
Introduction
In-Context Learning (ICL) has revolutionized how we interact with Large Language Models (LLMs), enabling them to perform new tasks simply by providing a few examples within the prompt. Despite its power, ICL is notoriously sensitive; a model's performance can fluctuate dramatically based on the choice, order, and formatting of these demonstrations. Traditional selection methods often rely on semantic similarity proxies, which fail to capture the actual marginal utility of an example or the model's inherent sensitivity to sequence order.
Methodology: Quantifying Demonstration Value
To address these challenges, we propose DemoShapley, a framework grounded in cooperative game theory. By treating the selection of demonstrations as a game where each example is a player, we can use Shapley values to fairly distribute "credit" for the model's performance.
DemoShapley and Beta-DemoShapley
The core of our approach involves calculating a demonstration's marginal contribution—the change in model accuracy when a specific example is added to various subsets of other demonstrations.
- DemoShapley: Adapts the Data Shapley theorem to LLMs, averaging contributions across different permutations to account for order sensitivity.
- Beta-DemoShapley: Introduces a weighted scheme using a Beta distribution. This prioritizes performance gains observed in smaller prompt sizes (e.g., 2-shot or 4-shot), reflecting the practical constraints of LLM context windows where an example's impact is most pronounced.
Computational Efficiency
Since exact Shapley value calculation is exponentially complex, we employ Truncated Monte Carlo (TMC). This method approximates values by randomly sampling permutations and terminating once the scores converge, making the valuation process feasible for real-world applications.
Key Insights
Our evaluation across models like ChatGPT-3.5, Llama3, and Mistral revealed several critical properties of demonstration utility:
- The "Bad Apple" Effect: Low-value demonstrations are often actively detrimental, significantly degrading the performance of otherwise high-quality prompts.
- Order Awareness: By averaging over permutations, DemoShapley captures the intrinsic utility of an example, independent of its specific position in a sequence.
- Automated Data Cleaning: DemoShapley naturally identifies mislabeled or "noisy" data. Flipped-label examples consistently receive the lowest scores, allowing for automated dataset purification.
- Fairness Enhancement: Selecting high-value demonstrations can reduce demographic biases (e.g., gender bias in income prediction), suggesting that high-utility data often aligns with fairer data representations.
Experimental Results
DemoShapley consistently outperforms state-of-the-art baselines such as CondAcc and Influence methods.
- Out-of-Distribution (OOD) Generalization: In financial news sentiment tasks, Beta-DemoShapley achieved 0.663 accuracy (16-shot), far exceeding the random baseline of 0.532.
- Robustness to Noise: In toxicity detection tasks, the framework accurately ranked intentionally "poisoned" examples at the bottom of the set, outperforming Leave-One-Out (LOO) methods.
- Model Sensitivity: While larger models like ChatGPT exhibit higher sensitivity to label noise, DemoShapley remains a robust filter across various model scales.
Conclusion and Future Work
DemoShapley provides a principled framework for valuing demonstrations in ICL, leading to improved accuracy, fairness, and data quality. While the method introduces some computational overhead due to multiple LLM inferences, its benefits in optimizing prompt engineering are substantial. Future research will explore the application of this framework to complex reasoning tasks and the impact of varying prompt templates on demonstration valuation.
Summary updated