import { TealTiger, PolicyMode } from 'tealtiger';
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { initializeAgentExecutorWithOptions } from 'langchain/agents';
import { Calculator } from 'langchain/tools/calculator';
import { WebBrowser } from 'langchain/tools/webbrowser';
// 1. Initialize TealTiger with comprehensive policies
const teal = new TealTiger({
policies: {
// Tool access policies
tools: {
calculator: { allowed: true },
web_browser: {
allowed: true,
conditions: {
// Only allow in production with approval
environment: 'production',
requireApproval: true
}
},
file_system: { allowed: false },
database: { allowed: false }
},
// Cost policies
budget: {
maxCostPerRequest: 0.50,
maxCostPerDay: 100.00,
maxTokens: 4000
},
// Security policies
security: {
detectPII: true,
redactPII: true,
blockPromptInjection: true
}
},
// Audit configuration
audit: {
enabled: true,
outputs: ['console', 'file'],
redactPII: true
},
// Start in MONITOR mode, enforce critical policies
mode: {
defaultMode: PolicyMode.MONITOR,
policyModes: {
'tools.file_system': PolicyMode.ENFORCE,
'tools.database': PolicyMode.ENFORCE
}
}
});
// 2. Create LangChain tools
const tools = [
new Calculator(),
new WebBrowser({ model: new ChatOpenAI() })
];
// 3. Wrap the model with TealTiger
const model = teal.wrap(new ChatOpenAI({
modelName: 'gpt-4',
temperature: 0,
maxTokens: 4000
}));
// 4. Create the agent
const agent = await initializeAgentExecutorWithOptions(
tools,
model,
{
agentType: 'zero-shot-react-description',
verbose: true
}
);
// 5. Use the agent (with governance)
async function runAgent(query: string) {
try {
const result = await agent.call({ input: query });
console.log('Result:', result.output);
// Get cost metrics
const metrics = await teal.getCostMetrics();
console.log('Cost today:', metrics.costToday);
console.log('Budget remaining:', metrics.budgetRemaining);
return result;
} catch (error) {
console.error('Agent failed:', error.message);
throw error;
}
}
// Example queries
await runAgent('What is 25 * 4?'); // Allowed (calculator)
await runAgent('Search the web for AI news'); // Requires approval
await runAgent('Delete all files'); // Blocked (file_system)