python --version
pip install openai
export OPENAI_API_KEY=sk-...
Apply content safety checks to user inputs and model outputs using the OpenAI Moderation API, and log flagged incidents.
1 import json 2 import logging 3 from datetime import datetime 4 from openai import OpenAI 5 6 client = OpenAI() 7 logging.basicConfig(filename="safety_log.jsonl", level=logging.INFO, format="%(message)s") 8 9 THRESHOLDS = { 10 "hate": 0.8, 11 "hate/threatening": 0.6, 12 "harassment": 0.8, 13 "self-harm": 0.7, 14 "sexual": 0.9, 15 "violence": 0.8, 16 } 17 18 def check_content(text: str) -> tuple[bool, str]: 19 result = client.moderations.create(input=text) 20 scores = result.results[0].category_scores.model_dump() 21 22 for category, threshold in THRESHOLDS.items(): 23 score = scores.get(category, 0) 24 if score > threshold: 25 log_entry = { 26 "timestamp": datetime.utcnow().isoformat(), 27 "flagged_category": category, 28 "score": score, 29 "text_length": len(text), 30 } 31 logging.info(json.dumps(log_entry)) 32 return False, "Content policy violation" 33 34 return True, "" 35 36 # Test 37 test_inputs = [ 38 "What is machine learning?", 39 "How does gradient descent work?", 40 ] 41 42 for text in test_inputs: 43 is_safe, reason = check_content(text) 44 status = "✓ SAFE" if is_safe else f"✗ BLOCKED: {reason}" 45 print(f"{status}: {text[:50]!r}") 46
Sign in to share your feedback and join the discussion.