-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlangchain.mjs
More file actions
48 lines (42 loc) · 1.32 KB
/
langchain.mjs
File metadata and controls
48 lines (42 loc) · 1.32 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
import { ChatOpenAI } from "@langchain/openai";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const processDocumentTool = tool(
async ({ inputPath, operation }) => {
return {
status: "queued",
inputPath,
operation,
nextStep: "Call your Nutrient endpoint or MCP server with this payload."
};
},
{
name: "process_document",
description: "Prepare a document-processing request for Nutrient workflows.",
schema: z.object({
inputPath: z.string(),
operation: z.enum(["ocr", "extract_text", "redact", "convert"])
})
}
);
async function main() {
const model = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4.1-mini"
}).bindTools([processDocumentTool]);
const response = await model.invoke(
"Queue OCR for ./assets/invoice-scan.pdf and explain what happens next."
);
const toolCalls = response.tool_calls ?? response.toolCalls ?? [];
if (toolCalls.length > 0) {
for (const call of toolCalls) {
if (call.name !== "process_document") continue;
const args = typeof call.args === "string" ? JSON.parse(call.args) : call.args;
const toolResult = await processDocumentTool.invoke(args);
console.log("Tool result:", toolResult);
}
return;
}
console.log(response.content);
}
void main();