Safe-Embed: Unveiling the Safety-Critical Knowledge of Sentence Encoders

Jinseok Kim, Jaewon Jung, Sangyeop Kim, Sohhyung Park, Sungzoon Cho

Introduction

In the rapidly evolving landscape of AI safety, much attention is focused on the "refusal" behaviors of Large Language Models (LLMs). However, production-grade safety stacks often rely on an earlier line of defense: sentence encoders. These models are used in embedding-based similarity searches to identify and filter harmful inputs before they reach the LLM. Despite their widespread use, it remains unclear whether these encoders possess a nuanced understanding of safety or merely rely on keyword matching. This paper, presented at KnowLLM 2024, introduces the Safe-Embed framework to evaluate this critical component.

Defining Safety-Critical Knowledge

The researchers define "Safety-Critical Knowledge" in sentence encoders through two distinct dimensions:

  1. Safety-Challenging Knowledge: The ability of an encoder to distinguish between an unsafe prompt and a lexically similar safe one (e.g., distinguishing "How to kill a person" from "How to kill a Python process").
  2. Safety-Taxonomy Knowledge: The ability to group diverse safety violations into coherent categories (e.g., recognizing that various linguistically different prompts all constitute "hate speech").
Conceptual overview of Safety-Challenging and Safety-Taxonomy Knowledge

Methodology and Metrics

To measure these capabilities, the authors developed several novel approaches and utilized diverse datasets:

  • Categorical Purity (CP): This metric assesses "Category Stickiness." It measures how many of the 10 nearest neighbors of an unsafe prompt belong to the same category of harm.
  • Safety-Boundary Similarity: Using GPT-4, the authors created "contrast sets" where unsafe prompts were minimally modified to become safe. This tests the model's precision in identifying the boundary between harm and utility.
  • Normalization: To ensure fair comparison across models with different similarity ranges, the authors established a baseline using "random" similarity from the Beavertails dataset.

The evaluation utilized benchmarks including XSTest (Augmented), Do-Not-Answer, and a newly created Safety-Contrast dataset containing five safe variations for every unsafe prompt in the Do-Not-Answer set.

Taxonomic Performance Across Harm Categories

The study reveals significant variance in how encoders handle different types of harm. Models generally excel at identifying Privacy Leaks and Stereotypes, likely due to the specific and consistent linguistic patterns associated with these categories. Conversely, broad and linguistically diverse categories like Disinformation and Hate Speech proved much more challenging, resulting in significantly lower Categorical Purity scores.

Categorical Purity (CP) scores across various safety categories for different sentence encoders

Key Insights and Results

  • The Context Blind Spot: A major finding was the failure of models in "Context" and "History" categories. Encoders often struggle to distinguish between real-world harm and fictional contexts (e.g., "How to steal a car" vs. "How to steal a car in GTA 4"). By focusing on the action rather than the context, these models risk high false-positive rates.
  • Size vs. Safety Awareness: Performance on general benchmarks like MTEB does not necessarily correlate with safety awareness. For instance, the 8B-parameter LLM2vec-Llama3 was outperformed on safety taxonomy tasks by the much smaller 110M-parameter ST5-Base.
  • Top Performers: ST5-XXL emerged as the leader in Categorical Purity (0.767), showing a strong ability to cluster similar harms. Meanwhile, LLM2vec-Mistral demonstrated the best safety separation, maintaining the greatest distance between harmful prompts and their safe counterparts.

Conclusion

While sentence encoders provide a valuable first line of defense, current models exhibit significant gaps, particularly regarding contextual nuance and broad harm categories. The authors suggest that future safety efforts should prioritize fine-tuning encoders specifically on safety taxonomies to bolster the robustness of AI filtering systems as a reliable "first-line-of-defense" detector.

Summary updated

BibTeX
@misc{kim2024safe,
  author = {Jinseok Kim and Jaewon Jung and Sangyeop Kim and Sohhyung Park and Sungzoon Cho},
  title = {{Safe-Embed: Unveiling the Safety-Critical Knowledge of Sentence Encoders}},
  year = {2024},
  eprint = {2407.06851},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.06851},
}
Figure