Use a layered guardrail architecture that combines a system prompt with a tone‑enforcement classifier and a pre‑response PII redaction filter, then passes the sanitized text to the LLM.
Implementation checklist
1. System prompt – set role: system with explicit tone rules (e.g., tone: friendly, concise, brand‑aligned). Include a short style guide snippet.
2. Tone classifier – deploy a lightweight model (e.g., distilbert-base-uncased-finetuned-tone) via Azure AI Language text/classify. Threshold ≥ 0.85 for “on‑brand”; otherwise rewrite or abort.
3. PII detector – call OpenAI Moderation endpoint before generation:
import openai
response = openai.Moderation.create(
input=user_input,
model="text-moderation-latest"
)
if any(flag for flag in response.results[0].categories.values() if flag):
sanitized = custom_redact(user_input) # regex + spaCy NER
else:
sanitized = user_input- Use categories flags (email, phone_number, ssn) with a severity cutoff of 0.7.
4. Redaction layer – apply regex patterns and spaCy en_core_web_lg NER to replace detected entities with [REDACTED].
5. LLM call – send sanitized text to the production model (e.g., gpt‑4o‑enterprise) with temperature=0.2 and max_tokens=512.
Quick comparison
| Layer | Tool | Latency (ms) | False‑positive rate |
|-------|------|--------------|----------------------|
| Tone | DistilBERT fine‑tuned | 45 | ~3% |
| PII | OpenAI Moderation + spaCy | 70 | ~1.5% |
| Guardrail | System prompt | 5 | N/A |
Gotcha: When the user submits code blocks, the regex‑based redactor can strip characters like : or #, breaking syntax; whitelist common programming symbols before applying PII patterns.