Embedding-Based Semantic Filtering
What you'll learn: How to catch harmful content that sneaks past keyword filters by understanding its actual meaning.
The Problem with Keywords
You've implemented keyword and regex-based filtering (lesson 1435), but clever users can evade these rules. If you block "kill," attackers write "k1ll" or "eliminate" or use euphemisms. Traditional filters play an endless game of whack-a-mole.
How Semantic Filtering Works
Embedding-based semantic filtering uses vector embeddings to detect harmful content by meaning rather than exact wording. Here's the approach:
- Create reference embeddings of known harmful content categories (violence, hate speech, self-harm, etc.)
- Embed incoming text (input or output) into the same vector space
- Calculate similarity (cosine similarity, typically) between the new text and your harmful reference clusters
- Flag content when similarity exceeds your threshold
The Analogy
Think of embeddings as capturing the "essence" of language in geometric space. Harmful messages cluster together based on their meaning, even if the exact words differ. "I want to hurt them" and "They deserve pain" use different words but live in the same dangerous neighborhood of semantic space.
Why This Matters
Semantic filtering catches:
- Misspellings and l33tspeak ("d1e" instead of "die")
- Synonyms and paraphrasing
- Euphemisms and coded language
- Multilingual variations with similar meaning
You combine this with keyword filters (defense in depth): keywords catch obvious violations quickly, while embeddings catch sophisticated evasions.
Key Takeaway: Embedding-based semantic filtering detects harmful content by comparing the meaning of text in vector space, catching evasions that slip past keyword-based rules through paraphrasing, misspellings, or creative wording.