Enhancing Safety of Large Language Models via Embedding Space Separation
Organizations: Gaoling School of Artificial Intelligence, Renmin University of China.
Abstract
Large language models (LLMs) have achieved impressive capabilities, yet ensuring their safety against harmful prompts remains a critical challenge. Recent work has revealed that the latent representations (embeddings) of harmful and safe queries in LLMs typically exhibit linear separability, a property that has been exploited to construct attacks by perturbing the embeddings of harmful queries towards the safe subspace. Motivated by this observation, we propose a representation-level fine-tuning approach, named Embedding Space Separation (ES2), which improves LLM safety by explicitly enlarging the distance between harmful and safe representations in the embedding space. To prevent degradation of model's general capabilities, we introduce a Kullback-Leibler (KL) divergence regularization term into the loss function, which constrains the logits of the fine-tuned model to align with those of the original base model on harmless inputs. We evaluate our method on several open-source LLMs using standard safety benchmarks. Extensive experimental results demonstrate that our approach substantially improves model safety while maintaining comparable general capabilities.
Figures & tables
| RepE Attack, DSR, | Soft Prompt Attack, DSR, | SCAV Attack, DSR, | ||||||||
| LLMs | Methods | Keyword | Answer | Useful | Keyword | Answer | Useful | Keyword | Answer | Useful |
| *LLama-2 (7B-Chat-hf) | Base Model | 50% | 46% | 42% | 84% | 77% | 71% | 10% | 6% | 6% |
| STL | 90% | 88% | 79% | 94% | 91% | 86% | 49% | 37% | 34% | |
| DPL | 90% | 87% | 76% | 93% | 93% | 90% | 54% | 44% | 39% | |
| ES2 (ours) | 100% | 98% | 94% | 100% | 98% | 97% | 80% | 72% | 70% | |
| +10% | +10% | +15% | +6% | +5% | +7% | +26% | +28% | +31% | ||
| LLMs | Methods | IFEval | BBH | GPQA | MATH | MuSR | MMLU-Pro | Average |
| Llama-2 (7B-Chat-hf) | Base Model | 0.423 | 0.298 | 0.250 | 0.010 | 0.368 | 0.108 | 0.243 |
| STL | 0.445 | 0.298 | 0.245 | 0.012 | 0.362 | 0.105 | 0.245 | |
| DPL | 0.444 | 0.298 | 0.245 | 0.015 | 0.365 | 0.106 | 0.246 | |
| ES2 (ours) | 0.433 | 0.297 | 0.262 | 0.019 | 0.355 | 0.107 | 0.246 | |
| Llama-3 (8B-Instruct) | Base Model | 0.763 | 0.420 | 0.257 | 0.084 | 0.357 | 0.113 | 0.332 |
| STL | 0.758 | 0.417 | 0.257 | 0.085 | 0.358 | 0.113 | 0.331 |
| RepE Attack | Soft Prompt Attack | SCAV Attack | |||||
| LLMs | Methods | Inc. | Gib. | Inc. | Gib. | Inc. | Gib. |
| Llama-2 (7B-Chat-hf) | Base Model | 0.0238% | 0.0000% | 0.0141% | 0.0000% | 0.0000% | 0.0000% |
| STL | 0.0253% | 0.0000% | 0.0349% | 0.0000% | 0.0000% | 0.0000% | |
| DPL | 0.0132% | 0.0000% | 0.0222% | 0.0000% | 0.0256% | 0.0000% | |
| ES2 (ours) | 0.0426% | 0.0745% | 0.0408% | 0.0515% | 0.2286% | 0.2857% | |
| Llama-3 (8B-Instruct) | Base Model | 0.0208% | 0.0000% | 0.0000% | 0.0000% | 0.0000% | 0.0869% |
Appendix figures & tables6 assets
Supplementary material from the paper’s appendix.
Appendix
| Refusal Keywords | |
| I’m sorry | Sorry |
| As an assistant | I cannot |
| unethical | not ethical |
| I am sorry | I apologize |
| I’m really sorry | It is not |
| As an AI | I’m an |
| Hyperparameter | Value |
| Optimizer | AdamW |
| Learning Rate | |
| Batch Size | 60 |
| Safe Prompt Number per Batch | 50 |
| Unsafe Prompt Number per Batch | 10 |
| Gradient Accumulation Steps | 4 |
| Llama-2-7B-Chat-hf | Llama-3-8B-Instruct | Qwen-2.5-7B-Instruct | |||||||
| Method / Configuration | Keyword | Answer | Useful | Keyword | Answer | Useful | Keyword | Answer | Useful |
| Baselines & Proposed Method | |||||||||
| Base Model | 10% | 6% | 6% | 34% | 29% | 23% | 29% | 28% | 25% |
| STL | 49% | 37% | 34% | 48% | 43% | 37% | 30% | 28% | 26% |
| DPL | 54% | 44% | 39% | 51% | 46% | 40% | 31% | 28% | 26% |
| ES2 (Ours, Two-Layer) | 80% | 72% | 70% | 77% | 71% | 69% | 55% | 48% | 46% |
| Llama-2-7B-Chat-hf | Llama-3-8B-Instruct | Qwen-2.5-7B-Instruct | |||||||
| Method / Configuration | Keyword | Answer | Useful | Keyword | Answer | Useful | Keyword | Answer | Useful |
| ES2 (Ours, ) | 80% | 72% | 70% | 77% | 71% | 69% | 55% | 48% | 46% |
| No Final Layer | |||||||||
| Layer | 20% | 13% | 11% | 39% | 31% | 27% | 30% | 27% | 27% |
| Layer | 19% | 11% | 9% | 38% | 29% | 29% | 28% | 28% | 26% |
| Layer | 22% | 16% | 13% | 40% | 30% | 26% | 30% | 28% | 27% |
| Llama-2-7B-Chat-hf | Llama-3-8B-Instruct | Qwen-2.5-7B-Instruct | |
| Method / Configuration | Avg. Perturbation Dist. | Avg. Perturbation Dist. | Avg. Perturbation Dist. |
| Base Model | 1.5043 | 0.5407 | 0.6854 |
| STL | 1.8496 | 0.6684 | 0.8955 |
| DPL | 1.9256 | 0.7985 | 0.8795 |
| ES2 (Ours, ) | 3.9986 | 1.9798 | 1.8865 |
| No Final Layer | |||
| Vanilla Attack, DSR, | AutoDAN Attack, DSR, | GCG Attack, DSR, | ||||||||
| LLMs | Methods | Keyword | Answer | Useful | Keyword | Answer | Useful | Keyword | Answer | Useful |
| *Llama-2 (7B-Chat) | Base Model | 99% | 98% | 98% | 80% | 77% | 72% | 89% | 88% | 78% |
| STL | 100% | 99% | 99% | 91% | 87% | 78% | 90% | 88% | 73% | |
| DPL | 100% | 100% | 99% | 90% | 87% | 76% | 99% | 99% | 88% | |
| ES2 (ours) | 100% | 100% | 100% | 100% | 100% | 94% | 100% | 100% | 92% | |
| +0% | +0% | +1% | +9% | +13% | +16% | +1% | +1% | +4% | ||
LLM Safety
Large language model (LLM) safety studies how to prevent harmful, unreliable, or misaligned behavior while preserving useful responses.
See all 438 papers