TL;DR
Large language models (LLMs) often struggle with ambiguous prompts, leading to either unsafe responses or unnecessary refusals. This paper introduces CONTEXTLENS, a novel framework that extracts contextual information from user prompts to guide LLMs towards safer and more accurate inferences. By disentangling context generation from response generation, CONTEXTLENS significantly reduces harmful outputs and over-refusals, making LLMs more reliable.
What Problem Does This Paper Address?
Modern large language models (LLMs) are powerful but can be easily misled by ambiguous or underspecified prompts. This ambiguity can lead to two major issues:
- Unsafe Outputs: The LLM might generate harmful, unethical, or illegal content if it misinterprets the user's intent or fails to recognize potential risks.
- Over-Refusal: Conversely, to err on the side of caution, LLMs might refuse to answer perfectly benign requests if they perceive even a slight risk or ambiguity, hindering their usefulness.
Existing approaches, like adding explicit reasoning steps, have shown limited success in reliably preventing these issues, especially with adversarial prompts. The core problem is that LLMs often generate responses without fully understanding the nuanced context of the user's request, which includes their intent, prior knowledge, and the potential implications of the query.
Methods
The paper proposes a context-aware inference framework that disentangles context generation from response generation. The core of this framework is CONTEXTLENS, a lightweight LLM trained to act as a "context generator."
Here's how it works:
- Context Generator (CONTEXTLENS): This model takes a user prompt and generates a "context snippet." This snippet is designed to encapsulate crucial information about the user's intent, potential risks, and relevant background knowledge.
- Context-Aware Inference: The generated context snippet is then fed along with the original prompt to a separate, potentially larger, "decoder" LLM. This enriched input allows the decoder model to make a more informed and safer decision.
Key innovations in CONTEXTLENS include:
- Autoencoder-like Training: The context generator is trained using an autoencoder-like approach. It learns to generate a compressed latent representation (the context snippet) of the prompt. A decoder then attempts to reconstruct the original prompt and generate a response based on this context. This encourages the context snippet to be information-rich.
- Reinforcement Learning (RL) with Specialized Rewards: CONTEXTLENS is trained using RL with a custom reward function. This reward function includes:
- Reconstruction Loss: Encourages the decoder to accurately reconstruct the original prompt from the context.
- Safety Rewards: Penalizes unsafe outputs and rewards compliance with safety guidelines.
- Similarity Rewards: Ensures the reconstructed prompt is similar to the original.
- Transferability: The context generator is designed to be independent of the decoder model, promoting transferability of generated contexts across different foundation models.
The paper explores three methods for generating context snippets:
- Zero-Shot Prompting: Using a fixed prompt template to elicit context from an LLM without further training.
- Thinking Traces: Leveraging intermediate reasoning steps generated by existing reasoning models.
- RL-trained Context Generator (CONTEXTLENS): The primary contribution, trained end-to-end for optimal context generation.
Datasets
The paper uses several datasets for training and evaluation:
- SafetyInstruct: Contains explicit harmful prompts.
- AdvBench: Also contains explicit harmful prompts.
- Wildjailbreak: Includes both adversarial benign and adversarial harmful prompts designed to confuse models about compliance vs. refusal.
- XSTest: Interleaves adversarial benign prompts (to test over-refusal) and explicit harmful prompts.
A subset of the WildJailbreak training set (5,000 samples) was used for training CONTEXTLENS, with an additional 500 samples for validation.
Key Insights and Ideas
- Context is King (for LLM Safety): The most significant insight is that providing explicit, extracted context dramatically improves LLM safety and reliability. It's not just about having a powerful LLM, but about feeding it the right information to make better decisions.
- Disentangling is Crucial: Separating the task of context generation from response generation is key. This modularity allows for specialized training of the context generator and promotes its reusability across different LLMs.
- Autoencoders for Rich Context: The autoencoder-like training objective is surprisingly effective at imbuing the context snippet with rich, latent information about the prompt, going beyond simple keyword matching.
- Reasoning Traces Aren't Always Transferable: While thinking traces from reasoning models can help, they are not as effective for cross-model transfer as dedicated context generation. This is likely because they are optimized for the specific model that generated them and might encode model-specific biases or reasoning errors.
- Small Models Can Generate Powerful Context: The CONTEXTLENS generator itself is a relatively small model (3B parameters), yet it can produce context that significantly boosts the performance of much larger foundation models.
Key Results
- Reduced Harmful Outputs: CONTEXTLENS achieves an average reduction of 5.6% in Attack Success Rate (ASR) on harmful prompts across various base models.
- Improved Compliance: It leads to an average improvement of 6.2% in the harmonic mean of ASR and Compliance on challenging datasets like XSTest and Wildjailbreak.
- Significant Gains on Specific Models: For instance, zero-shot context generated by a Llama-3-8B-Instruct model provided up to a 22.93% improvement in Wildjailbreak for GPT-40.
- Effective Transferability: CONTEXTLENS-generated contexts show strong generalization, improving performance by an average of 9.4% across different decoder models (including Llama and GPT variants).
- Qualitative Improvements: Examples show CONTEXTLENS successfully distinguishing benign requests (e.g., asking about cocaine) from harmful ones (e.g., asking how to dispose of a dead baby or where to find people to shoot), preventing unnecessary refusals and unsafe responses.
Limitations and Future Work
- Computational Cost: While CONTEXTLENS itself is lightweight, the overall inference process now involves an additional step for context generation, increasing latency.
- Context Quality vs. Model Size: While CONTEXTLENS improves performance across models, larger base models benefit more from the context, suggesting that the ability to effectively use the context is also important.
- Future Work: The authors suggest exploring more sophisticated context generation strategies and further investigating the transferability of contexts across even more diverse model architectures and tasks. They also highlight the potential for context-aware inference to become a standard paradigm for building safer AI systems.
Summary updated