Abstract
Large Language Models (LLMs) are prone to "toxic degeneration," where they mirror the toxicity or bias present in their input prompts. Current detoxification strategies often result in either overly cautious refusals or semantically incoherent outputs. We introduce CMD (Context-aware Model self-Detoxification), a framework that enables LLMs to self-detoxify by identifying and neutralizing toxic segments within a prompt before generating a response. By training on synthesized reasoning chains and employing a novel toxic contrastive loss, CMD achieves a state-of-the-art balance between safety and high-quality, coherent text generation.
Introduction
The safety of Large Language Models is a critical concern for real-world deployment. A primary challenge is the tendency of these models to follow the tone and content of their context, leading to toxic outputs when prompted with harmful text. Existing solutions typically involve response rejection or post-hoc output intervention. Response rejection, while safe, limits the model's utility in complex scenarios like mediation. Output intervention methods, such as Gedi or DExperts, steer the model away from toxic tokens during inference but often at the cost of semantic coherence, sometimes producing "gibberish" that ignores the prompt's intent.
CMD addresses this by making the model context-aware. Instead of fighting the model's natural tendency to follow context, CMD teaches the model to "clean" the context internally before proceeding with generation.
The CMD Framework
The CMD framework consists of two primary phases: dataset synthesis and model training.
Phase 1: Dataset Synthesis
To overcome the scarcity of high-quality human-labeled detoxification data, CMD uses a "Chain-of-Thought" (CoT) approach to synthesize training examples. This process involves three steps:
- Toxic Segment Detection: A Segment-CNN model identifies specific toxic spans within a prompt.
- Toxic Segment Detoxification: These spans are replaced with safe, synonymous text, preserving the original intent while removing harmful content.
- Context-Following Generation: The model generates multiple safe continuations based on the cleaned context.
Phase 2: Model Training
The target LLM is fine-tuned on this synthesized data. A critical component of this phase is the Toxic Contrastive Loss. Unlike standard fine-tuning, which only rewards "correct" safe responses, this loss explicitly penalizes the model for generating toxic candidates identified during synthesis. This pushes the model's internal representations away from toxic "negative samples," reinforcing the detoxification behavior.
Experimental Results
CMD was evaluated against several major baselines, including DExperts, Gedi, SGEAT, and ToxicReversal, across multiple architectures such as GPT2-XL and LLaMA 2.
Safety and Quality
CMD demonstrated a significant reduction in toxicity. For GPT2-XL, the toxicity probability dropped from 31.10% to 5.50%, representing an 82% relative reduction. Crucially, CMD maintained high text quality. While intervention-based baselines like Gedi saw perplexity (PPL) skyrocket to over 200, CMD achieved a PPL of 30.38—outperforming the base model's original PPL of 41.29. This indicates that CMD not only makes the model safer but also more fluent.
Human Evaluation
To validate these automated metrics, the authors conducted head-to-head human evaluations focusing on coherence and contextual consistency.
As shown in the evaluation results, CMD's outputs were consistently preferred or rated as equal to those of the baselines. It particularly excelled in maintaining consistency with the original prompt's intent while removing toxicity, a common failure point for existing methods.
Discussion and Conclusion
The success of CMD highlights that context is the primary catalyst for toxic generation. By training models to perform the entire pipeline—detect, clean, and generate—the need for external safety modules at inference time is eliminated, reducing latency. Furthermore, the use of contrastive learning proved essential; simply providing positive examples is insufficient for robust detoxification.
While CMD is highly effective, it is not a singular solution. Future work should explore more nuanced metrics that capture the balance of safety and relevance, as traditional semantic similarity metrics can sometimes reward toxic models for mirroring toxic prompts. CMD represents a significant step toward self-correcting, safe, and helpful AI systems.
Summary updated