-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtealengine-basic.ts
More file actions
122 lines (103 loc) · 3.09 KB
/
Copy pathtealengine-basic.ts
File metadata and controls
122 lines (103 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* TealEngine Basic Usage Example
*
* This example demonstrates basic TealEngine usage with policy templates
* and simple policy evaluation.
*/
import { TealOpenAI, TealEngine } from 'tealtiger';
async function main() {
console.log('=== TealEngine Basic Usage Example ===\n');
// Example 1: Using a policy template
console.log('1. Using Policy Template (Customer Support)');
const customerSupportEngine = new TealEngine(
TealEngine.Templates.customerSupport()
);
const client1 = new TealOpenAI({
apiKey: process.env.OPENAI_API_KEY || 'your-api-key',
agentId: 'support-agent-001',
engine: customerSupportEngine
});
try {
const response1 = await client1.chat.create({
model: 'gpt-3.5-turbo',
messages: [
{ role: 'user', content: 'Hello! I need help with my account.' }
]
});
console.log('✅ Request allowed by policy');
console.log('Response:', response1.choices[0].message.content);
console.log('Metadata:', response1.metadata);
} catch (error: any) {
console.log('❌ Request blocked:', error.message);
}
console.log('\n---\n');
// Example 2: Custom policy definition
console.log('2. Custom Policy Definition');
const customEngine = new TealEngine({
tools: {
'chat': { allowed: true },
'file_read': { allowed: true },
'file_write': { allowed: false },
'file_delete': { allowed: false }
},
identity: {
agentId: 'custom-agent-001',
role: 'assistant',
permissions: ['read', 'chat'],
forbidden: ['write', 'delete']
},
behavioral: {
costLimit: {
daily: 10.00,
hourly: 2.00
},
rateLimit: {
requests: 100,
window: '1h'
}
}
});
const client2 = new TealOpenAI({
apiKey: process.env.OPENAI_API_KEY || 'your-api-key',
agentId: 'custom-agent-001',
engine: customEngine
});
try {
const response2 = await client2.chat.create({
model: 'gpt-3.5-turbo',
messages: [
{ role: 'user', content: 'What is the weather today?' }
]
});
console.log('✅ Request allowed by custom policy');
console.log('Response:', response2.choices[0].message.content?.substring(0, 100) + '...');
} catch (error: any) {
console.log('❌ Request blocked:', error.message);
}
console.log('\n---\n');
// Example 3: Policy validation
console.log('3. Policy Validation');
const validationResult = customEngine.validate();
if (validationResult.valid) {
console.log('✅ Policy is valid');
} else {
console.log('❌ Policy has errors:', validationResult.errors);
}
console.log('\n---\n');
// Example 4: Testing policies
console.log('4. Testing Policies');
const testResult = customEngine.test({
agentId: 'custom-agent-001',
action: 'chat.create',
tool: 'chat',
content: 'Test message'
});
console.log('Test result:', {
allowed: testResult.allowed,
reason: testResult.reason,
triggeredPolicies: testResult.triggeredPolicies
});
console.log('\n=== Example Complete ===');
}
// Run the example
main().catch(console.error);