Enforce brand tone and block PII by combining a deterministic system prompt, real‑time moderation, and a post‑generation guardrail pipeline.
1. System Prompt – Use a concise, immutable system message that encodes brand voice and bans informal language. Set temperature=0 and top_p=0.95 to keep output deterministic.
2. Logit Bias – Apply logit_bias to suppress tokens that violate style (e.g., slang, profanity) and to zero‑out the end‑of‑text token (50256:-100).
3. Live Moderation – Call the OpenAI Moderation endpoint on every LLM reply. Treat a PII confidence > 0.8 as a hard block.
4. Guardrails Layer – Run the response through the Guardrails SDK (v2) with a custom rail that includes regexes for SSN, credit‑card, email, and phone patterns. Action = mask.
5. Feedback Loop – Log any moderation hits to a Prometheus counter llm_pii_violations_total and trigger a retraining alert if the rate exceeds 0.5 % over 10 k messages.
| Layer | Tool | Key Config |
|-------|------|------------|
| Prompt | OpenAI system + few‑shot | temperature=0, top_p=0.95 |
| Generation | gpt-4o‑mini | logit_bias for prohibited tokens |
| Moderation | OpenAI Moderation API | pii_score>0.8 → reject |
| Post‑filter | Guardrails SDK | regex PII patterns, action=mask |
Checklist
- [ ] System prompt locked in version control.
- [ ] logit_bias map includes all banned token IDs.
- [ ] Moderation call returns flagged and categories.pii > 0.8.
- [ ] Guardrails rail compiled and tested against OWASP PII regex list.
- [ ] Monitoring alerts configured for >0.5 % violation rate.
import openai, guardrails
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
temperature=0,
top_p=0.95,
messages=[
{"role":"system","content":"You are a friendly, concise support agent. Use brand voice: upbeat, empathetic, no slang."},
{"role":"user","content":user_input}
],
logit_bias={50256:-100}
)
moderation = openai.Moderation.create(input=response["choices"][0]["message"]["content"])
if moderation["results"][0]["flagged"] and moderation["results"][0]["categories"]["pii"] > 0.8:
raise ValueError("PII detected")
clean = guardrails.Guard.from_rail("brand_and_pii.rail").run(response["choices"][0]["message"]["content"])