Intermediate

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

Python - PII Detection and Redaction
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:

Python - Multi-Level Blocklist
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 TypeUse CaseLatency
Toxicity classifierDetect hate speech, harassment, threats10-50ms
NER modelIdentify named entities for PII detection20-100ms
Topic classifierFlag outputs on restricted topics10-30ms
Sentiment analyzerDetect extremely negative or manipulative tone5-20ms
Embedding similarityCompare output against known harmful examples5-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.

Practical advice: Start with regex PII detection and a basic blocklist. Measure false positive and false negative rates. Add ML classifiers where keyword filters are insufficient. Continuously update your filter rules based on observed outputs.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.