Skip to content

Commit 5358b8c

Browse files
committed
feat(langchain/createAgent): Simple standalone data analysis experiment
1 parent 187ce5a commit 5358b8c

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { ChatAnthropic } from "@langchain/anthropic";
2+
import { HumanMessage } from "@langchain/core/messages";
3+
4+
import { MemorySaver } from "@langchain/langgraph-checkpoint";
5+
import { createAgent } from "./agents/middlewareAgent/index.js";
6+
import { anthropicCodeExecutionMiddleware } from "./agents/middlewareAgent/middleware/anthropicCodeExecution.js";
7+
import {
8+
downloadFileAnthropic,
9+
extractGeneratedFilesAnthropic,
10+
getFileMetadataAnthropic,
11+
uploadFileAnthropic,
12+
} from "./agents/middlewareAgent/middleware/anthropicHelpers.js";
13+
14+
// Initial setup
15+
const model = new ChatAnthropic({
16+
model: "claude-sonnet-4-20250514",
17+
});
18+
const client = model.createClient({});
19+
const thread = {
20+
configurable: {
21+
thread_id: "test-123",
22+
},
23+
};
24+
const agent = createAgent({
25+
model,
26+
middleware: [anthropicCodeExecutionMiddleware()],
27+
checkpointSaver: new MemorySaver(),
28+
});
29+
30+
// Upload the file to Anthropic
31+
const uploadedFile = await uploadFileAnthropic(client, "test_data.csv");
32+
33+
// First invocation - should create container and analyze uploaded data
34+
const response1 = await agent.invoke(
35+
{
36+
messages: new HumanMessage({
37+
content: [
38+
{
39+
type: "text",
40+
text: "Filter to just widget A",
41+
},
42+
{ type: "container_upload", file_id: uploadedFile.fileId },
43+
],
44+
}),
45+
},
46+
thread
47+
);
48+
console.log("Response 1:", response1.messages);
49+
50+
// Second invocation - should reuse container and previous analysis
51+
const response2 = await agent.invoke(
52+
{
53+
messages: new HumanMessage(
54+
"Turn that into a graph of sales and units over time."
55+
),
56+
},
57+
thread
58+
);
59+
console.log("Response 2:", response2.messages);
60+
61+
// Extract and download generated files
62+
for (const fileId of extractGeneratedFilesAnthropic(response2)) {
63+
const metadata = await getFileMetadataAnthropic(client, fileId);
64+
await downloadFileAnthropic(client, fileId, metadata.filename);
65+
console.log(`Downloaded generated file: ${metadata.filename}`);
66+
}

libs/langchain/test_data.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
date,region,product,sales,units
2+
2024-01-15,North,Widget A,1250.50,25
3+
2024-01-16,South,Widget B,890.25,18
4+
2024-01-17,East,Widget A,1456.75,29
5+
2024-01-18,West,Widget C,2100.00,42
6+
2024-01-19,North,Widget B,765.50,15
7+
2024-01-20,South,Widget A,1890.00,38
8+
2024-01-21,East,Widget C,2340.25,47
9+
2024-01-22,West,Widget B,1123.75,22
10+
2024-01-23,North,Widget C,1567.50,31
11+
2024-01-24,South,Widget B,934.25,19

0 commit comments

Comments
 (0)