Contrastive Prompting Enhances Sentence Embeddings in LLMs through Inference-Time Steering

Zifeng Cheng, Zhonghui Wang, Yuchen Fu, Zhiwei Jiang, Yafeng Yin, Cong Wang, Qing Gu

TL;DR

Contrastive Prompting (CP) is a training-free, plug-and-play inference intervention that significantly improves the quality of LLM-generated sentence embeddings. By introducing an auxiliary prompt that captures non-essential information (like stop words) and subtracting its hidden state from a normal prompt, CP steers the LLM to focus purely on core semantics. This simple activation steering approach yields substantial performance gains across semantic similarity and classification tasks without the need for data or fine-tuning.

What Problem Does This Paper Address?

Extracting high-quality sentence embeddings from Large Language Models (LLMs) without expensive fine-tuning usually relies on prompt engineering. A common technique (like PromptEOL) appends a suffix such as: This sentence: "[TEXT]" means in one word: " and uses the hidden state of the final token as the embedding.

However, the authors reveal a fundamental flaw in this approach: the final token's representation remains heavily polluted with non-essential information, such as stop words ("a", "the") and syntactic fluff. Even when prompts explicitly instruct the model to focus on main subjects and actions, next-token prediction analysis shows that the model still assigns the highest probability to irrelevant stop words. This over-encoding of noise limits the representation capacity of the embedding.

Figure 1: The comparison of sentence embeddings ob- tained by the auxiliary prompt, normal prompt, and our proposed inference-time steering method. The decoding probabilities of Next Token Prediction are used to reflect the semantic information contained in the correspond- ing sentence embeddings.

Methods

The authors introduce Contrastive Prompting (CP), an inference-time activation steering method that acts as a semantic filter. The pipeline consists of three main steps:

  1. Auxiliary Prompting: The input text is wrapped in an auxiliary prompt designed to isolate noise, for example: The irrelevant information of this sentence: "[TEXT]" means in one word: ". This is fed into the LLM up to an intermediate multi-head attention layer $\ell$ to extract an "auxiliary" contextualized value vector ($v^{\text{aux}}$).
  2. Contrastive Activation Steering: The text is also processed using a standard embedding prompt (the "normal" prompt) to get $v^{\text{nor}}$. The semantic activation vector ($\Delta v^\ell$) is calculated by subtracting the auxiliary vector from the normal vector: $$\Delta v^\ell = v^{\text{nor}} - v^{\text{aux}}$$ This subtraction effectively cancels out the non-essential features.

  3. Norm Adjustment & Forward Propagation: Because vector subtraction alters the magnitude of the hidden states, the authors propose two adjustment strategies before completing the forward pass:

  4. Norm Scaling (NS): Multiplies $\Delta v^\ell$ by a tuned scaling factor $\alpha$.
  5. Norm Recovering (NR): Renormalizes $\Delta v^\ell$ to match the original $L^2$-norm of the normal prompt vector, avoiding new hyperparameters.

The adjusted vector is then fed back into the attention layer's output matrix, and forward propagation continues to an intermediate layer to extract the final sentence embedding.

Figure 2: Illustration of the contrastive prompting method.

Datasets

The method was evaluated using LLaMA2 (7B, 13B), LLaMA3.1-8B, and OPT-6.7B backbones across several benchmarks:

  • Semantic Textual Similarity (STS): STS 2012–2016, STS-Benchmark (STS-B), and SICK-Relatedness (SICK-R).
  • Transfer Learning Tasks (SentEval): MR, CR, SUBJ, MPQA, SST-2, TREC, and MRPC.
  • MTEB Tasks: Subsets covering classification (AmazonCounterfactual, Banking77, Emotion), pair classification, reranking, and clustering.

Key Insights and Ideas

  • Stop-Words as Semantic Noise: Standard prompt engineering is an indirect way to alter representations. CP proves that explicitly defining what not to look at, and mathematically subtracting it, is far more effective than simply telling the model what to look at.
  • Early-Layer Intervention is Key: The optimal layer for steering is quite early (layer 5 for PromptEOL, layer 7 for Pretended CoT/Knowledge in LLaMA2-7B). This aligns with the understanding that token interaction and contextualization primarily occur in the early-to-mid attention layers.
  • Norm Consistency is Optional: Surprisingly, the Norm Scaling strategy typically outperforms Norm Recovering. This suggests that preserving the exact original activation scale is less important for embedding quality than amplifying the contrastive semantic signal.

Key Results

  • STS Performance Leap: On LLaMA2-7B, applying CP to the baseline PromptEOL method boosted the average STS score from 70.03 to 75.27 (+5.24 points).
  • Outperforming Supervised Models: On SentEval transfer tasks, PromptEOL + CP-NS achieved an average accuracy of 91.73, outperforming ST5-Enc (91.63), a 4.8B parameter model explicitly trained on billions of supervised sentence pairs.
  • Negligible Overhead: Because the auxiliary prompt only needs to be processed up to the early intervention layer $\ell$ (e.g., layer 5 out of 32), the computational overhead is minimal, adding only a fraction of the time required for a full forward pass.
Figure 3: Effects of intervention layer, scaling factor, and output layer. (a) Effects of the intervention layer on norm scaling. (b) Effects of the intervention layer on norm recovering. (c) Effects of the scaling factor on norm scaling. (d) The effects of the output layer on Knowledge+CP-NS.

Limitations and Future Work

  • Hyperparameter Dependency: The optimal intervention layer ($\ell$) and scaling factor ($\alpha$) still require a grid search on a validation set (like the STS-B development set).
  • Manual Prompt Design: The auxiliary prompts were hand-crafted. The authors note that automatically generating the optimal auxiliary prompt for any given normal prompt remains an open challenge.
  • Language Scope: The evaluation was strictly limited to English datasets; its efficacy on multilingual embeddings has yet to be tested.

Summary updated

BibTeX
@inproceedings{cheng2025contrastive,
  author = {Zifeng Cheng and Zhonghui Wang and Yuchen Fu and Zhiwei Jiang and Yafeng Yin and Cong Wang and Qing Gu},
  title = {{Contrastive Prompting Enhances Sentence Embeddings in LLMs through Inference-Time Steering}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure