22const { Agent, Runner, Tool, Guardrail } = require ( '../src/index' ) ;
33
44// Create an input guardrail that checks if the input contains inappropriate content
5- const inappropriateContentGuardrail = new Guardrail . InputGuardrail ( {
5+ const inappropriateContentGuardrail = new Guardrail . Input ( {
66 name : "inappropriate_content_filter" ,
77 check : async ( context , agent , input ) => {
8+
89 // In a real implementation, you might use a content moderation API
910 // Here we're just doing a simple check for demonstration
10- const message = typeof input === 'string' ? input :
11- ( input [ 0 ] ?. content || '' ) ;
11+ const message = typeof input === 'string' ? input : ( input [ 0 ] ?. content || '' ) ;
1212
1313 const inappropriateWords = [ 'hate' , 'violence' , 'explicit' ] ;
1414 const containsInappropriate = inappropriateWords . some ( word =>
1515 message . toLowerCase ( ) . includes ( word )
1616 ) ;
1717
18- return {
19- output_info : {
20- checked_for : "inappropriate content" ,
21- found : containsInappropriate ? "yes" : "no"
22- } ,
23- tripwire_triggered : containsInappropriate
24- } ;
18+ return containsInappropriate ;
2519 }
2620} ) ;
2721
2822// Create an output guardrail that ensures the response isn't too short
29- const minimumLengthGuardrail = new Guardrail . OutputGuardrail ( {
23+ const minimumLengthGuardrail = new Guardrail . Output ( {
3024 name : "minimum_length_check" ,
3125 check : async ( context , agent , output ) => {
3226 const minimumLength = 20 ; // characters
3327
34- // Safely handle null/undefined output
35- if ( ! output ) {
36- return {
37- output_info : {
38- checked_for : "minimum length" ,
39- required_length : minimumLength ,
40- actual_length : 0 ,
41- is_too_short : true ,
42- error : "Output is null or undefined"
43- } ,
44- tripwire_triggered : true
45- } ;
46- }
47-
48- // Convert output to string if it isn't already
4928 const outputStr = String ( output ) ;
5029 const isTooShort = outputStr . length < minimumLength ;
5130
52- return {
53- output_info : {
54- checked_for : "minimum length" ,
55- required_length : minimumLength ,
56- actual_length : outputStr . length ,
57- is_too_short : isTooShort
58- } ,
59- tripwire_triggered : isTooShort
60- } ;
31+ return isTooShort ;
6132 }
6233} ) ;
6334
@@ -86,45 +57,32 @@ async function main() {
8657 name : "Guarded Assistant" ,
8758 instructions : "You are a helpful assistant that provides detailed and thoughtful responses." ,
8859 tools : [ getWeather ] ,
89- inputGuardrails : [ inappropriateContentGuardrail ] ,
90- outputGuardrails : [ minimumLengthGuardrail ]
60+ guardrails : { input : [ inappropriateContentGuardrail ] , output : [ minimumLengthGuardrail ] }
9161 } ) ;
9262
93- try {
94- // Example 1: Normal input (should work fine)
95- console . log ( "Example 1: Normal input" ) ;
96- const result1 = await Runner . run ( agent , "Tell me about the weather in Paris" ) ;
97- console . log ( "Final output:" , result1 . finalOutput ) ;
98- console . log ( "Input guardrail results:" , JSON . stringify ( result1 . inputGuardrailResults , null , 2 ) ) ;
99- console . log ( "Output guardrail results:" , JSON . stringify ( result1 . outputGuardrailResults , null , 2 ) ) ;
63+ const tooShortAgent = new Agent ( {
64+ name : "Brief Assistant" ,
65+ instructions : "You are an assistant that gives very brief, one-word answers whenever possible." ,
66+ guardrails : { output : [ minimumLengthGuardrail ] }
67+ } ) ;
10068
101- // Example 2: Input that should trigger the input guardrail
102- console . log ( "\nExample 2: Inappropriate input" ) ;
103- try {
104- const result2 = await Runner . run ( agent , "I hate the weather in London" ) ;
105- console . log ( "Final output:" , result2 . finalOutput ) ;
106- } catch ( error ) {
107- console . log ( "Error (expected):" , error . message ) ;
108- }
10969
110- // Example 3: Create an agent that might generate short outputs
111- const tooShortAgent = new Agent ( {
112- name : "Brief Assistant" ,
113- instructions : "You are an assistant that gives very brief, one-word answers whenever possible." ,
114- outputGuardrails : [ minimumLengthGuardrail ]
115- } ) ;
70+ // Example 1: Normal input (should work fine)
71+ console . log ( "Example 1: Normal input" ) ;
72+ const result1 = await Runner . run ( agent , "Tell me about the weather in Paris" ) ;
73+ console . log ( "Final output:" , result1 . finalOutput ) ;
74+ console . log ( "Guardrails results:" , JSON . stringify ( result1 . guardrails , null , 2 ) ) ;
11675
117- console . log ( "\nExample 3: Testing output guardrail" ) ;
118- try {
119- const result3 = await Runner . run ( tooShortAgent , "What color is the sky?" ) ;
120- console . log ( "Final output:" , result3 . finalOutput ) ;
121- } catch ( error ) {
122- console . log ( "Error (expected):" , error . message ) ;
123- }
76+ // Example 2: Input that should trigger the input guardrail
77+ console . log ( "\nExample 2: Inappropriate input" ) ;
78+ const result2 = await Runner . run ( agent , "I hate the weather in London" ) ;
79+ console . log ( "Final output:" , result2 . finalOutput ) ;
80+ console . log ( "Guardrails results:" , JSON . stringify ( result2 . guardrails , null , 2 ) ) ;
12481
125- } catch ( error ) {
126- console . error ( "Unexpected error:" , error ) ;
127- }
82+ // Example 3: Create an agent that might generate short outputs
83+ const result3 = await Runner . run ( tooShortAgent , "What color is the sky?" ) ;
84+ console . log ( "Final output:" , result3 . finalOutput ) ;
85+ console . log ( "Guardrails results:" , JSON . stringify ( result3 . guardrails , null , 2 ) ) ;
12886}
12987
13088main ( ) . catch ( console . error ) ;
0 commit comments