Filtering Techniques
Build practical output filters using regex patterns for PII detection, keyword blocklists, ML-based classifiers, and semantic similarity analysis.
PII Detection with Regex
import re class PIIFilter: PATTERNS = { "email": r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", "phone_us": r"\b(\+?1[-.]?)?\(?\d{3}\)?[-.]?\d{3}[-.]?\d{4}\b", "ssn": r"\b\d{3}-\d{2}-\d{4}\b", "credit_card": r"\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b", "ip_address": r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", } def redact(self, text: str) -> dict: redacted = text findings = [] for pii_type, pattern in self.PATTERNS.items(): matches = re.findall(pattern, redacted) if matches: findings.append({"type": pii_type, "count": len(matches)}) redacted = re.sub(pattern, f"[{pii_type.upper()}_REDACTED]", redacted) return {"text": redacted, "findings": findings}
Keyword and Phrase Blocklists
Blocklists catch known dangerous patterns but must be used carefully to avoid excessive false positives:
class BlocklistFilter: def __init__(self): self.hard_block = [# Always block] self.soft_block = [# Flag for review] self.context_block = [# Block only in certain contexts] def check(self, text: str) -> dict: text_lower = text.lower() for phrase in self.hard_block: if phrase in text_lower: return {"action": "block", "reason": phrase} for phrase in self.soft_block: if phrase in text_lower: return {"action": "review", "reason": phrase} return {"action": "allow"}
ML-Based Classification
ML classifiers handle nuanced content that keyword matching cannot catch:
| Classifier Type | Use Case | Latency |
|---|---|---|
| Toxicity classifier | Detect hate speech, harassment, threats | 10-50ms |
| NER model | Identify named entities for PII detection | 20-100ms |
| Topic classifier | Flag outputs on restricted topics | 10-30ms |
| Sentiment analyzer | Detect extremely negative or manipulative tone | 5-20ms |
| Embedding similarity | Compare output against known harmful examples | 5-15ms |
Semantic Similarity Filtering
Compare outputs against a database of known harmful content using embedding similarity:
- Embed the LLM output using a sentence transformer
- Compare against a vector database of known harmful content embeddings
- Flag outputs with high similarity scores (cosine similarity > 0.85)
- This catches paraphrased harmful content that keyword filters miss
Choosing the Right Filter Strategy
Speed-First: Regex + Blocklist
Under 5ms latency. Good for high-throughput APIs. Catches known patterns but misses novel threats. Use as the first filter layer.
Accuracy-First: ML Classifiers
10-100ms latency. Better at catching nuanced content. Run in parallel after regex layer. Use for high-risk applications.
Comprehensive: Full Pipeline
50-200ms total. Regex + blocklist + ML + semantic similarity. Maximum coverage. Use for safety-critical applications.
Specialized: Domain Filters
Custom filters for specific domains (medical, financial, legal). Combine general safety with domain-specific rules and vocabulary.
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX