Privacy-Preserving In-Context Learning for Large Language Models

Tong Wu, Ashwinee Panda, Jiachen T. Wang, Prateek Mittal

TL;DR

This paper introduces Differentially Private In-Context Learning (DP-ICL), a framework that prevents Large Language Models (LLMs) from leaking sensitive private data used as prompt exemplars. By applying a "sample-and-aggregate" ensemble approach at the inference stage, DP-ICL provides strong mathematical privacy guarantees for both text classification and language generation tasks without requiring access to or modification of model weights.

What Problem Does This Paper Address?

In-context learning (ICL) allows LLMs to adapt to domain-specific tasks simply by appending a few examples (exemplars) to the user's query. Because it requires only black-box API access, it has become a popular alternative to fine-tuning, especially for retrieval-augmented generation (RAG) systems handling private data (e.g., electronic health records).

However, ICL introduces severe privacy risks. Malicious users can craft adversarial prompts—known as prompt leaking attacks—to trick the LLM into outputting the private exemplars verbatim.

Figure 1: A demonstration of the privacy attack on in-context exemplars, also known as prompt leak- ing attack. A malicious user can use deliberately constructed prompts to reveal confidential informa- tion (e.g., health records) in exemplars.

Standard privacy techniques like Differentially Private Stochastic Gradient Descent (DP-SGD) are impractical here: they require modifying model weights (impossible for black-box APIs like GPT-4), demand massive compute for hyperparameter tuning, and violate the GDPR "right to be forgotten" (deleting a single data point requires completely retraining the model). DP-ICL addresses these gaps by shifting privacy preservation from the training phase to the prediction phase.

Methods

DP-ICL adapts the classic "sample-and-aggregate" differential privacy paradigm into a four-phase framework:

Figure 2: Our proposed DP-ICL framework has four phases: In Phase I, we partition the subsampled sensitive database into separate subsets, each comprising a collection of exemplars. Phase II involves constructing prompts by pairing each exemplar with the query. During Phase III, the model processes these exemplar-query pairs and produces corresponding outputs. Finally, in Phase IV, these outputs are aggregated through a differentially private mechanism before being returned to the user. More details are presented in Section 3.
  1. Subsample and Partition: The private dataset is randomly subsampled (using Poisson sampling to amplify privacy) and split into disjoint subsets of exemplars.
  2. Pairing with Query: Each subset is paired independently with the user's query to form multiple parallel prompts.
  3. Prompting the Model: The LLM processes these prompts in parallel, yielding an ensemble of independent responses.
  4. Private Aggregation: The individual responses are aggregated using a differentially private mechanism before returning a single answer to the user.

The core challenge lies in Phase 4, which varies significantly depending on the task type:

  • Text Classification: The authors use the Report-Noisy-Max (RNM) mechanism with Gaussian noise. They count the predicted labels across the ensemble, add independent Gaussian noise to the histogram bins, and return the class with the highest noisy count.
Figure 3: An overview of private aggregation method for text classification. We first count the output labels and put them in a histogram. Next, we add Gaussian noise to this histogram. Finally, we release the label with the highest noisy count.
  • Language Generation: Free-form text generation suffers from the "curse of dimensionality"—the probability of two independent LLM instances generating the exact same sentence is near zero, resulting in an intractably sparse histogram. To solve this, the authors propose two techniques:
  • Embedding Space Aggregation (ESA): Sentences are projected into a continuous semantic embedding space. The framework calculates a private mean embedding vector using the Gaussian mechanism and maps it back to text by selecting a zero-shot generated candidate sentence that maximizes cosine similarity.
  • Keyword Space Aggregation (KSA): Sentences are broken down into individual words to form a frequency histogram. Top-$K$ keywords are privately selected using either the Joint Exponential Mechanism (JEM) or Propose-Test-Release (PTR). The LLM is then re-queried with a new prompt instructing it to synthesize a final answer using these approved keywords.

Datasets

The authors evaluated DP-ICL across three distinct task types using standard benchmarks:

  • Text Classification:
  • SST-2 & Amazon (Sentiment analysis)
  • AGNews (Topic classification)
  • TREC (Question classification)
  • Document Question Answering:
  • PFL-DocVQA (A dataset containing extracted OCR tokens from document images, which often include sensitive information)
  • Dialog Summarization:
  • SAMSum (A dataset of abstractive dialogue summaries)

Key Insights and Ideas

  • Privacy via Consensus: Privacy is achieved because the final output relies on a noisy consensus across disjoint subsets. No single sensitive exemplar can drastically alter the aggregated output, satisfying the core definition of differential privacy.
  • Beating the Curse of Dimensionality: By mapping infinite sentence spaces into lower-dimensional keyword or embedding spaces, the authors made private aggregation possible for generative language tasks without tracking privacy loss token-by-token.
  • Keywords Outperform Embeddings: Interestingly, Keyword Space Aggregation (KSA) significantly outperformed Embedding Space Aggregation (ESA). ESA suffers from information loss during projection and struggles to reconstruct high-quality text from a raw average embedding vector using zero-shot generation.
  • Compliance by Design: DP-ICL inherently supports the "right to be forgotten." If a user requests their data be deleted, the data holder simply removes the record from the exemplar database—no retraining or fine-tuning required.

Key Results

  • Classification Performance: On the SST-2 dataset using GPT-3 Davinci, DP-ICL achieved 95.80% accuracy at a strict privacy budget of $\epsilon = 3$, showing zero performance degradation compared to non-private baselines. It also established a new state-of-the-art compared to traditional DP-SGD methods (e.g., RoBERTa-large fine-tuned via DP-SGD).
  • Generation Performance: For document question answering (DocVQA) using Keyword-PTR, the drop in ROUGE-1 score from non-private aggregation to a strict privacy setting ($\epsilon = 1$) was less than 3%. On the SAMSum summarization task, Keyword-JEM at $\epsilon = 1$ improved ROUGE-1 scores by 4.5% over zero-shot learning, landing within 1% of the non-private ensemble baseline.

Limitations and Future Work

  • Query Budget Constraints: Unlike DP-SGD models (which can be queried infinitely once trained privately), DP-ICL accumulates privacy loss with every user query. The current framework is optimized for a maximum of roughly 10,000 queries before the privacy budget is exhausted.
  • API Cost and Latency: Because the framework queries the LLM multiple times in parallel (once for each ensemble subset), it incurs higher monetary costs and API latency than standard single-prompt ICL.
  • Future Directions: The authors point out that developing better embedding-to-text reconstruction models could revitalize the ESA approach. Additionally, finding optimal ways to prompt LLMs with extracted keywords remains an open area for exploration.

Summary updated

BibTeX
@inproceedings{wu2024privacy,
  author = {Tong Wu and Ashwinee Panda and Jiachen T. Wang and Prateek Mittal},
  title = {{Privacy-Preserving In-Context Learning for Large Language Models}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=x4OPJ7lHVU},
}
Figure