Why Cosine Similarity Dominates LLM Attack Detection
Cosine similarity excels in detecting known LLM attacks by focusing solely on the directional alignment of embedding vectors, not their magnitude. This is crucial for text embeddings where vector length often carries little semantic meaning, making it ideal for high-dimensional data spaces where Euclidean distance struggles.
The top 3
- Cosine vs. Euclidean: Why Direction Trumps Distance in LLMs: Cosine similarity measures the angle between vectors, focusing on direction, which is critical for semantic meaning in high-dimensional LLM embeddings; Euclidean distance, conversely, is heavily influenced by magnitude differences that may not convey semantic information.
- Semantic Alignment: Cosine Similarity's LLM Detection Prowess: LLMs encode semantic meaning into the direction of embedding vectors, making cosine similarity ideal because it directly quantifies the angular relationship, thereby capturing the closeness of meaning between prompts.
- Attack Robustness: Cosine Similarity's Invariance to Prompt Length: By normalizing vector magnitudes, cosine similarity effectively disregards variations in prompt length or minor rephrasing, which is crucial for robustly detecting diverse malicious prompt injection attempts.
Sources
Open the full topic