š Post-Installation Guide: Next Steps
So you've just run npm install safeguard-ai. What now? This guide walks you through the first 10 minutes of using the library to ensure your implementation is robust and safe.
šļø The 1-Minute Verification
The quickest way to verify everything is working is to run a small test script.
- Create a file named
safeguard-test.js. - Paste this code:
const SafeguardAI = require('safeguard-ai');
async function verify() {
console.log('š Initializing SafeguardAI...');
const moderator = new SafeguardAI();
// Test 1: PII Detection (Doesn't require API key)
console.log('Test 1: Local PII Detection');
const result = await moderator.checkText("Email me at test@example.com");
if (result.piiDetected.length > 0) {
console.log('ā
PII Detection working: Found', result.piiDetected[0].type);
}
console.log('\nš Installation verified! Next: Add your OpenAI API key to test full moderation.');
}
verify().catch(err => console.error('ā Error:', err.message));
- Run it:
node safeguard-test.js
š ļø Essential Setup Phase
1. Secure Your API Key
SafeguardAI uses OpenAI's high-performance moderation models by default. You MUST secure your key.
Don't do this:
const moderator = new SafeguardAI({ apiKey: 'sk-12345...' }); // ā UNSAFE
Do this:
- Install
dotenv:npm install dotenv - Create a
.envfile:OPENAI_API_KEY=your_key_here - Load it:
require('dotenv').config();
const moderator = new SafeguardAI({ apiKey: process.env.OPENAI_API_KEY }); // ā
SAFE
šļø Choose Your Implementation Pattern
Depending on your application type, follow one of these patterns:
A. The "Gatekeeper" (Input Validation)
Use this if you want to block messages BEFORE they hit your database or LLM.
async function handleUserInput(input) {
const check = await moderator.checkText(input);
if (!check.safe) {
throw new Error("Sorry, your message contains restricted content.");
}
return processMessage(check.cleanText); // Processes redacted version
}
B. The "Sanitizer" (Output Cleaning)
Use this to ensure your AI's responses don't leak sensitive info or generate toxic content.
const rawResponse = await openai.createChatCompletion(...);
const safeResponse = await moderator.checkText(rawResponse.choices[0].message.content);
displayToUser(safeResponse.cleanText); // Shows redacted/cleaned output
C. The "Compliance Officer" (Automated Audit)
Use this to scan batch data for GDPR/HIPAA compliance.
const logs = await db.getLogs();
for (const log of logs) {
const audit = await moderator.checkText(log.content);
if (audit.piiDetected.length > 0) {
await db.markForRedaction(log.id, audit.cleanText);
}
}
š”ļø Best Practices for Production
- Fail-Safe Strategy: Decide what happens if the API fails.
- Restrictive: Block the user if the safety check fails to run.
- Permissive: Allow the user but log the failure for manual review.
- Strictness Tuning: Start with
medium. Switch tohighif you're building a kid-friendly app or a corporate bot. - Custom Blocklists: The AI might miss your specific company's "no-no" words. Use
moderator.rules.addBlockedWords(['competitor_x', 'secret_project_y']).
ā Troubleshooting Common Errors
| Error | Cause | Solution |
|---|---|---|
API key is required |
Environment variable not loaded | Ensure require('dotenv').config() is at the top of your main entry file. |
Rate limit exceeded |
Too many requests to OpenAI | Implement a queue (like BullMQ) or simple throttling. |
PII not detected |
Pattern mismatch | The default detector handles standard formats. Use addPattern() for custom IDs. |
š Deep Dives
Need help? Open an issue on GitHub Issues.