-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlangchain.ts
More file actions
73 lines (58 loc) · 1.94 KB
/
langchain.ts
File metadata and controls
73 lines (58 loc) · 1.94 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
/**
* LangChain integration example
* Requires: npm install langchain @langchain/openai zod
*/
import { ChatOpenAI } from '@langchain/openai';
import { createAgent } from 'langchain';
import {
AgentDiff,
TypeScriptExecutorProxy,
BashExecutorProxy,
createLangChainTool,
} from 'agent-diff';
async function main() {
// Initialize Agent Diff client
const client = new AgentDiff();
// Create isolated environment
const env = await client.initEnv({
templateService: 'slack',
templateName: 'slack_default',
impersonateUserId: 'U01AGENBOT9',
});
console.log(`Environment: ${env.environmentId}`);
// Take before snapshot
const run = await client.startRun({ envId: env.environmentId });
// Create code executor tools
const tsExecutor = new TypeScriptExecutorProxy(env.environmentId, client.getBaseUrl(), env.token);
const bashExecutor = new BashExecutorProxy(env.environmentId, client.getBaseUrl(), env.token);
const tsTool = createLangChainTool(tsExecutor);
const bashTool = createLangChainTool(bashExecutor);
// Create agent
const agent = createAgent({
model: new ChatOpenAI({ model: 'gpt-4' }),
tools: [tsTool, bashTool],
});
// Run agent
console.log('\nRunning agent...');
const result = await agent.invoke({
messages: [
{
role: 'user',
content: `Use the Slack API to:
1. List all channels (https://slack.com/api/conversations.list)
2. Post "Hello from LangChain agent!" to channel C01ABCD1234 (https://slack.com/api/chat.postMessage)
Use execute_typescript or execute_bash tools.`,
},
],
});
console.log('\nAgent response:', result.messages[result.messages.length - 1]?.content);
// Get diff
const diff = await client.diffRun({
runId: run.runId,
});
console.log('\nChanges made:');
console.log('- Diff:', JSON.stringify(diff.diff, null, 2));
// Cleanup
await client.deleteEnv(env.environmentId);
}
main().catch(console.error);