Cosine Similarity: Key to Detecting Known Attacks
Rebuff's vector similarity layer employs cosine similarity to compare new user inputs against its database of known attack embeddings, flagging inputs that exceed a predefined similarity threshold.
The top 3
- 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.
- Top Alternatives: Euclidean Distance vs. Dot Product in LLM Security: While cosine similarity is preferred, Euclidean distance measures straight-line separation and is sensitive to vector magnitude, making it less ideal for semantic similarity unless embeddings are normalized. The dot product considers both direction and magnitude; for normalized embeddings, it becomes mathematically equivalent to cosine similarity and can be faster to compute, often used in vector databases for performance.
- Optimizing Embeddings for Enhanced Cosine Similarity Detection: The effectiveness of cosine similarity in detecting prompt attacks heavily relies on the quality of the LLM embeddings. Techniques like fine-tuning embedding models with adversarial examples, using specialized embedding models for security, and employing robust vectorization methods can significantly improve detection accuracy against malicious inputs.
Sources
Open the full topic