|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * PII Masking Example: Interactive chat with GuardrailsOpenAI. |
| 4 | + * |
| 5 | + * Demonstrates how to mask PII in the pre-flight stage (block=false) so that |
| 6 | + * user inputs are sanitized before reaching the model, while also blocking |
| 7 | + * PII that appears in the model's output (block=true). |
| 8 | + * |
| 9 | + * Highlights: |
| 10 | + * - Pre-flight PII guardrail automatically replaces detected entities with tokens like <EMAIL_ADDRESS> |
| 11 | + * - Encoded PII detection (Base64/URL/hex) is enabled via detect_encoded_pii |
| 12 | + * - Output stage blocks responses when PII is detected in the model reply |
| 13 | + * - Console output shows what was masked and which entities were found |
| 14 | + * |
| 15 | + * Run with: npx tsx pii_mask_example.ts |
| 16 | + * |
| 17 | + * Prerequisites: |
| 18 | + * - Set OPENAI_API_KEY in your environment |
| 19 | + */ |
| 20 | + |
| 21 | +import * as readline from 'readline'; |
| 22 | +import { |
| 23 | + GuardrailResult, |
| 24 | + GuardrailTripwireTriggered, |
| 25 | + GuardrailsOpenAI, |
| 26 | + GuardrailsResponse, |
| 27 | +} from '../../src'; |
| 28 | + |
| 29 | +type ChatMessage = { role: 'system' | 'user' | 'assistant'; content: string }; |
| 30 | + |
| 31 | +const PIPELINE_CONFIG = { |
| 32 | + version: 1, |
| 33 | + pre_flight: { |
| 34 | + version: 1, |
| 35 | + guardrails: [ |
| 36 | + { |
| 37 | + name: 'Contains PII', |
| 38 | + config: { |
| 39 | + entities: ['EMAIL_ADDRESS', 'PHONE_NUMBER', 'US_SSN'], |
| 40 | + block: false, |
| 41 | + detect_encoded_pii: true, |
| 42 | + }, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + input: { |
| 47 | + version: 1, |
| 48 | + guardrails: [ |
| 49 | + { |
| 50 | + name: 'Moderation', |
| 51 | + config: { |
| 52 | + categories: ['hate', 'violence'], |
| 53 | + }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + output: { |
| 58 | + version: 1, |
| 59 | + guardrails: [ |
| 60 | + { |
| 61 | + name: 'Contains PII', |
| 62 | + config: { |
| 63 | + entities: ['EMAIL_ADDRESS', 'PHONE_NUMBER', 'US_SSN'], |
| 64 | + block: true, |
| 65 | + detect_encoded_pii: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | +}; |
| 71 | + |
| 72 | +function createInterface(): readline.Interface { |
| 73 | + return readline.createInterface({ |
| 74 | + input: process.stdin, |
| 75 | + output: process.stdout, |
| 76 | + prompt: '\nEnter a message (or type "exit"): ', |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +function formatEntitySummary(entities: Record<string, string[]> | undefined): string { |
| 81 | + if (!entities) { |
| 82 | + return 'None'; |
| 83 | + } |
| 84 | + const parts: string[] = []; |
| 85 | + for (const [entity, matches] of Object.entries(entities)) { |
| 86 | + parts.push(`${entity} (${matches.length})`); |
| 87 | + } |
| 88 | + return parts.length ? parts.join(', ') : 'None'; |
| 89 | +} |
| 90 | + |
| 91 | +function logPiiMasking(result: GuardrailResult, originalInput: string): void { |
| 92 | + const info = result.info ?? {}; |
| 93 | + const masked = typeof info.checked_text === 'string' ? info.checked_text : originalInput; |
| 94 | + const detected = info.detected_entities as Record<string, string[]> | undefined; |
| 95 | + const stage = info.stage_name ?? 'pre_flight'; |
| 96 | + |
| 97 | + console.log(`\n🪪 PII detected and masked (${stage} stage)`); |
| 98 | + console.log('Original :', originalInput); |
| 99 | + console.log('Sanitized:', masked); |
| 100 | + console.log('Entities :', formatEntitySummary(detected)); |
| 101 | +} |
| 102 | + |
| 103 | +function logPiiInOutput(result: GuardrailResult): void { |
| 104 | + const info = result.info ?? {}; |
| 105 | + const detected = info.detected_entities as Record<string, string[]> | undefined; |
| 106 | + const stage = info.stage_name ?? 'output'; |
| 107 | + console.log(`\n⚠️ PII detected – response blocked (${stage} stage).`); |
| 108 | + console.log('Entities :', formatEntitySummary(detected)); |
| 109 | +} |
| 110 | + |
| 111 | +function inspectGuardrailResults( |
| 112 | + response: GuardrailsResponse, |
| 113 | + originalInput: string |
| 114 | +): void { |
| 115 | + const results = response.guardrail_results; |
| 116 | + |
| 117 | + if (results.preflight.length > 0) { |
| 118 | + for (const result of results.preflight) { |
| 119 | + const info = result.info ?? {}; |
| 120 | + if (info.guardrail_name === 'Contains PII' && info.pii_detected) { |
| 121 | + logPiiMasking(result, originalInput); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (results.output.length > 0) { |
| 127 | + for (const result of results.output) { |
| 128 | + const info = result.info ?? {}; |
| 129 | + if (info.guardrail_name === 'Contains PII' && result.tripwireTriggered) { |
| 130 | + logPiiInOutput(result); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +async function processInput( |
| 137 | + client: GuardrailsOpenAI, |
| 138 | + userInput: string, |
| 139 | + conversation: ChatMessage[] |
| 140 | +): Promise<void> { |
| 141 | + const messages = [...conversation, { role: 'user' as const, content: userInput }]; |
| 142 | + |
| 143 | + const response = await client.chat.completions.create({ |
| 144 | + model: 'gpt-4.1-mini', |
| 145 | + messages, |
| 146 | + }); |
| 147 | + |
| 148 | + inspectGuardrailResults(response, userInput); |
| 149 | + |
| 150 | + const assistantMessage = response.choices[0]?.message?.content ?? ''; |
| 151 | + console.log('\n🤖 Assistant:', assistantMessage.trim()); |
| 152 | + |
| 153 | + conversation.push({ role: 'user', content: userInput }); |
| 154 | + conversation.push({ role: 'assistant', content: assistantMessage }); |
| 155 | +} |
| 156 | + |
| 157 | +async function main(): Promise<void> { |
| 158 | + console.log('🔐 Guardrails PII Masking Example'); |
| 159 | + console.log(' - Pre-flight guardrail masks PII before it hits the model'); |
| 160 | + console.log(' - Output guardrail blocks replies that contain PII'); |
| 161 | + |
| 162 | + const client = await GuardrailsOpenAI.create(PIPELINE_CONFIG); |
| 163 | + const conversation: ChatMessage[] = [ |
| 164 | + { |
| 165 | + role: 'system', |
| 166 | + content: 'You are a helpful assistant. Keep responses concise.', |
| 167 | + }, |
| 168 | + ]; |
| 169 | + |
| 170 | + const rl = createInterface(); |
| 171 | + rl.prompt(); |
| 172 | + |
| 173 | + rl.on('line', async (line) => { |
| 174 | + const input = line.trim(); |
| 175 | + |
| 176 | + if (!input) { |
| 177 | + rl.prompt(); |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + if (input.toLowerCase() === 'exit') { |
| 182 | + rl.close(); |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + try { |
| 187 | + await processInput(client, input, conversation); |
| 188 | + } catch (error) { |
| 189 | + if (error instanceof GuardrailTripwireTriggered) { |
| 190 | + const info = error.guardrailResult.info ?? {}; |
| 191 | + const stage = info.stage_name ?? 'unknown'; |
| 192 | + console.log( |
| 193 | + `\n🛑 Guardrail triggered in ${stage} stage: ${info.guardrail_name ?? 'Unknown guardrail'}` |
| 194 | + ); |
| 195 | + console.log(JSON.stringify(error.guardrailResult, null, 2)); |
| 196 | + } else { |
| 197 | + console.error('\n❌ Error processing request:', error instanceof Error ? error.message : error); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + rl.prompt(); |
| 202 | + }); |
| 203 | + |
| 204 | + rl.on('close', () => { |
| 205 | + console.log('\n👋 Exiting the program.'); |
| 206 | + process.exit(0); |
| 207 | + }); |
| 208 | +} |
| 209 | + |
| 210 | +main().catch((error) => { |
| 211 | + console.error('Fatal error:', error); |
| 212 | + process.exit(1); |
| 213 | +}); |
0 commit comments