AI Engineering & LLMs (50 Qs)•Part 4: LLM Security, Safety & OWASP Top 10 for LLMs
PII Redaction & Data Sanitization: Presidio, Token Replacement & Differential Privacy
1. The PII Data Leakage Hazard
Sending customer data to foundation model APIs (OpenAI, Anthropic, Google) or fine-tuning models on raw customer service tickets risks violating GDPR, HIPAA, and CCPA:
- Personally Identifiable Information (PII): Social Security Numbers, Credit Card Numbers, email addresses, phone numbers, medical diagnoses.
- Parametric memorization: Models can memorize and inadvertently regurgitate sensitive PII during subsequent user queries.
2. Pre-Inference Sanitization Pipeline with Microsoft Presidio
less
Raw User Prompt:
"Customer John Doe (SSN: 123-45-6789, email: john@example.com) requested refund."
│
▼
[ PII Anonymizer Engine ]
• Named Entity Recognition (NER) + Regex Pattern Matching
• Replace PII with reversible surrogate tokens
│
▼
Sanitized Prompt Sent to LLM:
"Customer <PERSON_1> (SSN: <US_SSN_1>, email: <EMAIL_1>) requested refund."
│
▼
LLM Response:
"Processed refund request for <PERSON_1>."
│
▼
[ PII De-Anonymizer / Re-Hydration ]
"Processed refund request for John Doe."
3. Production Python Implementation using Presidio
python
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine
analyzer = AnalyzerEngine()
anonymizer = AnonymizerEngine()
def sanitize_user_input(text: str) -> str:
# 1. Analyze text for PII entities
results = analyzer.analyze(
text=text,
entities=["PHONE_NUMBER", "EMAIL_ADDRESS", "US_SSN", "CREDIT_CARD"],
language="en"
)
# 2. Anonymize entities with synthetic placeholder tokens
anonymized_result = anonymizer.anonymize(
text=text,
analyzer_results=results
)
return anonymized_result.text
cleaned = sanitize_user_input("Contact me at alice@company.com or 555-0199.")
# Output: "Contact me at <EMAIL_ADDRESS> or <PHONE_NUMBER>."