|
1 | 1 | #!/usr/bin/env node
|
2 | 2 | /**
|
3 | 3 | * Example: Basic async guardrail bundle using Agents SDK with GuardrailAgent.
|
4 |
| - * |
| 4 | + * |
5 | 5 | * Run with: npx tsx agents_sdk.ts
|
6 |
| - * |
| 6 | + * |
7 | 7 | * Prerequisites:
|
8 | 8 | * - Install @openai/agents: npm install @openai/agents
|
9 | 9 | * - Set OPENAI_API_KEY environment variable
|
10 | 10 | */
|
11 | 11 |
|
12 | 12 | import * as readline from 'readline';
|
13 | 13 | import { GuardrailAgent } from '../../dist/index.js';
|
14 |
| -import { |
15 |
| - InputGuardrailTripwireTriggered, |
16 |
| - OutputGuardrailTripwireTriggered |
17 |
| -} from '@openai/agents'; |
| 14 | +import { InputGuardrailTripwireTriggered, OutputGuardrailTripwireTriggered } from '@openai/agents'; |
18 | 15 |
|
19 | 16 | // Define your pipeline configuration
|
20 | 17 | const PIPELINE_CONFIG = {
|
| 18 | + version: 1, |
| 19 | + pre_flight: { |
21 | 20 | version: 1,
|
22 |
| - pre_flight: { |
23 |
| - version: 1, |
24 |
| - guardrails: [ |
25 |
| - { |
26 |
| - name: "Moderation", |
27 |
| - config: { |
28 |
| - categories: ["hate", "violence", "self-harm"], |
29 |
| - }, |
30 |
| - }, |
31 |
| - ], |
32 |
| - }, |
33 |
| - input: { |
34 |
| - version: 1, |
35 |
| - guardrails: [ |
36 |
| - { |
37 |
| - name: "Custom Prompt Check", |
38 |
| - config: { |
39 |
| - model: "gpt-4.1-nano-2025-04-14", |
40 |
| - confidence_threshold: 0.7, |
41 |
| - system_prompt_details: "Check if the text contains any math problems.", |
42 |
| - }, |
43 |
| - }, |
44 |
| - ], |
45 |
| - }, |
46 |
| - output: { |
47 |
| - version: 1, |
48 |
| - guardrails: [ |
49 |
| - { name: "URL Filter", config: { url_allow_list: ["example.com"] } }, |
50 |
| - ], |
51 |
| - }, |
| 21 | + guardrails: [ |
| 22 | + { |
| 23 | + name: 'Moderation', |
| 24 | + config: { |
| 25 | + categories: ['hate', 'violence', 'self-harm'], |
| 26 | + }, |
| 27 | + }, |
| 28 | + ], |
| 29 | + }, |
| 30 | + input: { |
| 31 | + version: 1, |
| 32 | + guardrails: [ |
| 33 | + { |
| 34 | + name: 'Custom Prompt Check', |
| 35 | + config: { |
| 36 | + model: 'gpt-4.1-nano-2025-04-14', |
| 37 | + confidence_threshold: 0.7, |
| 38 | + system_prompt_details: 'Check if the text contains any math problems.', |
| 39 | + }, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + output: { |
| 44 | + version: 1, |
| 45 | + guardrails: [{ name: 'URL Filter', config: { url_allow_list: ['example.com'] } }], |
| 46 | + }, |
52 | 47 | };
|
53 | 48 |
|
54 | 49 | /**
|
55 | 50 | * Create a readline interface for user input.
|
56 | 51 | */
|
57 | 52 | function createReadlineInterface(): readline.Interface {
|
58 |
| - return readline.createInterface({ |
59 |
| - input: process.stdin, |
60 |
| - output: process.stdout, |
61 |
| - }); |
| 53 | + return readline.createInterface({ |
| 54 | + input: process.stdin, |
| 55 | + output: process.stdout, |
| 56 | + }); |
62 | 57 | }
|
63 | 58 |
|
64 | 59 | /**
|
65 | 60 | * Main input loop for the customer support agent with input/output guardrails.
|
66 | 61 | */
|
67 | 62 | async function main(): Promise<void> {
|
68 |
| - console.log('🤖 Customer Support Agent with Guardrails'); |
69 |
| - console.log('=========================================='); |
70 |
| - console.log('This agent has the following guardrails configured:'); |
71 |
| - console.log('• Pre-flight: Moderation (hate, violence, self-harm)'); |
72 |
| - console.log('• Input: Custom Prompt Check (math problems)'); |
73 |
| - console.log('• Output: URL Filter (only example.com allowed)'); |
74 |
| - console.log('==========================================\n'); |
75 |
| - |
76 |
| - try { |
77 |
| - // Create agent with guardrails automatically configured from pipeline configuration |
78 |
| - // Set raiseGuardrailErrors to true for strict error handling |
79 |
| - const agent = await GuardrailAgent.create( |
80 |
| - PIPELINE_CONFIG, |
81 |
| - "Customer support agent", |
82 |
| - "You are a customer support agent. You help customers with their questions.", |
83 |
| - {}, |
84 |
| - true // raiseGuardrailErrors = true |
85 |
| - ); |
86 |
| - |
87 |
| - // Dynamic import to avoid bundling issues |
88 |
| - const { run } = await import('@openai/agents'); |
89 |
| - |
90 |
| - const rl = createReadlineInterface(); |
91 |
| - |
92 |
| - // Handle graceful shutdown |
93 |
| - const shutdown = () => { |
94 |
| - console.log('\n👋 Exiting the program.'); |
95 |
| - rl.close(); |
96 |
| - process.exit(0); |
97 |
| - }; |
98 |
| - |
99 |
| - process.on('SIGINT', shutdown); |
100 |
| - process.on('SIGTERM', shutdown); |
101 |
| - |
102 |
| - while (true) { |
103 |
| - try { |
104 |
| - const userInput = await new Promise<string>((resolve) => { |
105 |
| - rl.question('Enter a message: ', resolve); |
106 |
| - }); |
107 |
| - |
108 |
| - if (userInput.toLowerCase() === 'exit' || userInput.toLowerCase() === 'quit') { |
109 |
| - shutdown(); |
110 |
| - break; |
111 |
| - } |
112 |
| - |
113 |
| - console.log('🤔 Processing...\n'); |
114 |
| - |
115 |
| - const result = await run(agent, userInput); |
116 |
| - console.log(`Assistant: ${result.finalOutput}\n`); |
117 |
| - |
118 |
| - } catch (error: any) { |
119 |
| - // Handle guardrail tripwire exceptions |
120 |
| - if (error instanceof InputGuardrailTripwireTriggered) { |
121 |
| - console.log('🛑 Input guardrail triggered! Please try a different message.\n'); |
122 |
| - continue; |
123 |
| - } else if (error instanceof OutputGuardrailTripwireTriggered) { |
124 |
| - console.log('🛑 Output guardrail triggered! The response was blocked.\n'); |
125 |
| - continue; |
126 |
| - } else { |
127 |
| - console.error('❌ An error occurred:', error.message); |
128 |
| - console.log('Please try again.\n'); |
129 |
| - } |
130 |
| - } |
| 63 | + console.log('🤖 Customer Support Agent with Guardrails'); |
| 64 | + console.log('=========================================='); |
| 65 | + console.log('This agent has the following guardrails configured:'); |
| 66 | + console.log('• Pre-flight: Moderation (hate, violence, self-harm)'); |
| 67 | + console.log('• Input: Custom Prompt Check (math problems)'); |
| 68 | + console.log('• Output: URL Filter (only example.com allowed)'); |
| 69 | + console.log('==========================================\n'); |
| 70 | + |
| 71 | + try { |
| 72 | + // Create agent with guardrails automatically configured from pipeline configuration |
| 73 | + // Set raiseGuardrailErrors to true for strict error handling |
| 74 | + const agent = await GuardrailAgent.create( |
| 75 | + PIPELINE_CONFIG, |
| 76 | + 'Customer support agent', |
| 77 | + 'You are a customer support agent. You help customers with their questions.', |
| 78 | + {}, |
| 79 | + true // raiseGuardrailErrors = true |
| 80 | + ); |
| 81 | + |
| 82 | + // Dynamic import to avoid bundling issues |
| 83 | + const { run } = await import('@openai/agents'); |
| 84 | + |
| 85 | + const rl = createReadlineInterface(); |
| 86 | + |
| 87 | + // Handle graceful shutdown |
| 88 | + const shutdown = () => { |
| 89 | + console.log('\n👋 Exiting the program.'); |
| 90 | + rl.close(); |
| 91 | + process.exit(0); |
| 92 | + }; |
| 93 | + |
| 94 | + process.on('SIGINT', shutdown); |
| 95 | + process.on('SIGTERM', shutdown); |
| 96 | + |
| 97 | + while (true) { |
| 98 | + try { |
| 99 | + const userInput = await new Promise<string>((resolve) => { |
| 100 | + rl.question('Enter a message: ', resolve); |
| 101 | + }); |
| 102 | + |
| 103 | + if (userInput.toLowerCase() === 'exit' || userInput.toLowerCase() === 'quit') { |
| 104 | + shutdown(); |
| 105 | + break; |
131 | 106 | }
|
132 | 107 |
|
133 |
| - } catch (error: any) { |
134 |
| - if (error.message.includes('@openai/agents')) { |
135 |
| - console.error('❌ Error: The @openai/agents package is required.'); |
136 |
| - console.error('Please install it with: npm install @openai/agents'); |
137 |
| - } else if (error.message.includes('OPENAI_API_KEY')) { |
138 |
| - console.error('❌ Error: OPENAI_API_KEY environment variable is required.'); |
139 |
| - console.error('Please set it with: export OPENAI_API_KEY=sk-...'); |
| 108 | + console.log('🤔 Processing...\n'); |
| 109 | + |
| 110 | + const result = await run(agent, userInput); |
| 111 | + console.log(`Assistant: ${result.finalOutput}\n`); |
| 112 | + } catch (error: any) { |
| 113 | + // Handle guardrail tripwire exceptions |
| 114 | + if (error instanceof InputGuardrailTripwireTriggered) { |
| 115 | + console.log('🛑 Input guardrail triggered! Please try a different message.\n'); |
| 116 | + continue; |
| 117 | + } else if (error instanceof OutputGuardrailTripwireTriggered) { |
| 118 | + console.log('🛑 Output guardrail triggered! The response was blocked.\n'); |
| 119 | + continue; |
140 | 120 | } else {
|
141 |
| - console.error('❌ Unexpected error:', error.message); |
| 121 | + console.error('❌ An error occurred:', error.message); |
| 122 | + console.log('Please try again.\n'); |
142 | 123 | }
|
143 |
| - process.exit(1); |
| 124 | + } |
| 125 | + } |
| 126 | + } catch (error: any) { |
| 127 | + if (error.message.includes('@openai/agents')) { |
| 128 | + console.error('❌ Error: The @openai/agents package is required.'); |
| 129 | + console.error('Please install it with: npm install @openai/agents'); |
| 130 | + } else if (error.message.includes('OPENAI_API_KEY')) { |
| 131 | + console.error('❌ Error: OPENAI_API_KEY environment variable is required.'); |
| 132 | + console.error('Please set it with: export OPENAI_API_KEY=sk-...'); |
| 133 | + } else { |
| 134 | + console.error('❌ Unexpected error:', error.message); |
144 | 135 | }
|
| 136 | + process.exit(1); |
| 137 | + } |
145 | 138 | }
|
146 | 139 |
|
147 | 140 | // Run the main function
|
148 | 141 | if (import.meta.url === `file://${process.argv[1]}`) {
|
149 |
| - main().catch((error) => { |
150 |
| - console.error('❌ Fatal error:', error); |
151 |
| - process.exit(1); |
152 |
| - }); |
| 142 | + main().catch((error) => { |
| 143 | + console.error('❌ Fatal error:', error); |
| 144 | + process.exit(1); |
| 145 | + }); |
153 | 146 | }
|
0 commit comments