Learn how to build robust content moderation systems using PromptGuard’s advanced filtering capabilities for both input prompts and AI-generated responses.
Content Moderation Overview
Content moderation is essential for maintaining safe, appropriate AI applications. PromptGuard provides multi-layered content filtering for:Input Moderation
- Inappropriate Content: Hate speech, harassment, explicit content
- Harmful Requests: Violence, self-harm, illegal activities
- Spam and Abuse: Repetitive content, promotional spam
- PII Protection: Personal information detection and redaction
Output Moderation
- Response Safety: Ensuring AI responses are appropriate
- Content Quality: Filtering low-quality or nonsensical outputs
- Bias Detection: Identifying potentially biased content
- Compliance: Meeting regulatory and platform requirements
Content Categories and Policies
Standard Content Categories
| Category | Description | Default Action |
|---|---|---|
| Hate Speech | Content targeting individuals/groups based on identity | Block |
| Harassment | Bullying, threats, targeted abuse | Block |
| Violence | Graphic violence, threats of violence | Block |
| Self-Harm | Suicide, self-injury content | Block |
| Sexual Content | Explicit sexual material, inappropriate content | Block |
| Illegal Activities | Drug use, fraud, criminal activities | Block |
| Spam | Repetitive, promotional, or low-quality content | Filter |
| PII | Personal information (SSN, credit cards, etc.) | Redact |
Configuring Content Policies
Implementation Examples
Social Media Platform Moderation
- Tab Title
- Tab Title
// Social media content moderation system
import { OpenAI } from 'openai';
const openai = new OpenAI({
apiKey: process.env.PROMPTGUARD_API_KEY,
baseURL: 'https://api.promptguard.co/api/v1'
});
class SocialMediaModerator {
constructor() {
this.moderationQueue = [];
this.bannedUsers = new Set();
this.userViolationCounts = new Map();
}
async moderatePost(userId, content, contentType = 'text') {
try {
// Check if user is banned
if (this.bannedUsers.has(userId)) {
return {
allowed: false,
reason: 'user_banned',
message: 'User is currently banned from posting'
};
}
// AI-powered content analysis
const analysis = await this.analyzeContent(content, contentType);
// Apply moderation decision
const decision = this.makeModerationDecision(analysis, userId);
// Update user violation tracking
if (!decision.allowed) {
this.trackViolation(userId, decision.category);
}
return decision;
} catch (error) {
console.error('Moderation error:', error);
// Fail safe - block content if moderation fails
return {
allowed: false,
reason: 'moderation_error',
message: 'Content could not be verified for safety'
};
}
}
async analyzeContent(content, contentType) {
// Use PromptGuard to analyze content safety
const prompt = `Analyze this ${contentType} content for safety and appropriateness:
Content: "${content}"
Evaluate for:
1. Hate speech or harassment
2. Violence or threats
3. Sexual or inappropriate content
4. Spam or low-quality content
5. Personal information exposure
Respond with a JSON analysis including risk scores (0-1) for each category.`;
const response = await openai.chat.completions.create({
model: "gpt-5-nano",
messages: [
{
role: 'system',
content: 'You are a content moderation AI. Analyze content objectively and provide detailed risk assessments.'
},
{
role: 'user',
content: prompt
}
],
temperature: 0.1 // Low temperature for consistent analysis
});
try {
return JSON.parse(response.choices[0].message.content);
} catch (parseError) {
// Fallback if JSON parsing fails
return {
overall_risk: 0.5,
hate_speech: 0.3,
violence: 0.2,
sexual_content: 0.2,
spam: 0.1,
pii: 0.1
};
}
}
makeModerationDecision(analysis, userId) {
const thresholds = {
hate_speech: 0.7,
violence: 0.8,
sexual_content: 0.6,
spam: 0.8,
pii: 0.9
};
// Check each category
for (const [category, threshold] of Object.entries(thresholds)) {
if (analysis[category] > threshold) {
return {
allowed: false,
category: category,
risk_score: analysis[category],
reason: 'content_violation',
message: this.getViolationMessage(category),
requires_review: analysis[category] > 0.95
};
}
}
// Check overall risk
if (analysis.overall_risk > 0.8) {
return {
allowed: false,
category: 'general_safety',
risk_score: analysis.overall_risk,
reason: 'safety_concern',
message: 'Content flagged for safety review',
requires_review: true
};
}
return {
allowed: true,
risk_score: analysis.overall_risk,
message: 'Content approved'
};
}
getViolationMessage(category) {
const messages = {
hate_speech: 'Content contains hate speech or discriminatory language',
violence: 'Content contains violent or threatening material',
sexual_content: 'Content contains inappropriate sexual material',
spam: 'Content appears to be spam or low-quality',
pii: 'Content contains personal information that should be private'
};
return messages[category] || 'Content violates community guidelines';
}
trackViolation(userId, category) {
const userViolations = this.userViolationCounts.get(userId) || {
total: 0,
categories: {}
};
userViolations.total++;
userViolations.categories[category] = (userViolations.categories[category] || 0) + 1;
this.userViolationCounts.set(userId, userViolations);
// Auto-ban logic
if (userViolations.total >= 5) {
this.bannedUsers.add(userId);
this.notifyUserBan(userId, userViolations);
} else if (userViolations.total >= 3) {
this.sendWarning(userId, userViolations);
}
}
async moderateComment(postId, userId, comment) {
// Enhanced moderation for comments (often more toxic)
const strictThresholds = {
hate_speech: 0.6,
violence: 0.7,
sexual_content: 0.5,
harassment: 0.6
};
const analysis = await this.analyzeContent(comment, 'comment');
// Apply stricter thresholds for comments
for (const [category, threshold] of Object.entries(strictThresholds)) {
if (analysis[category] > threshold) {
return {
allowed: false,
category,
risk_score: analysis[category],
message: 'Comment blocked for inappropriate content'
};
}
}
return { allowed: true, message: 'Comment approved' };
}
}
// Usage in API endpoint
app.post('/api/posts', async (req, res) => {
const { userId, content, contentType } = req.body;
const moderator = new SocialMediaModerator();
try {
const result = await moderator.moderatePost(userId, content, contentType);
if (result.allowed) {
// Save post to database
const post = await savePost(userId, content);
res.status(201).json({ success: true, post });
} else {
res.status(400).json({
success: false,
reason: result.reason,
message: result.message,
category: result.category
});
}
} catch (error) {
console.error('Post creation error:', error);
res.status(500).json({
success: false,
message: 'Unable to process post at this time'
});
}
});
E-commerce Review Moderation
Advanced Content Filtering
Custom Content Rules
Multi-Language Content Moderation
Real-Time Content Filtering
Stream Processing for Live Content
Content Moderation Analytics
Moderation Dashboard
Testing Content Moderation
Automated Testing Suite
Next Steps
Data Privacy
Implement comprehensive data privacy protection
Enterprise Setup
Configure PromptGuard for enterprise environments
Chatbot Protection
Secure conversational AI applications
Security Overview
Complete security configuration guide