|
| 1 | +import "dotenv/config"; |
| 2 | +import { NutrientClient } from "@nutrient-sdk/dws-client-typescript"; |
| 3 | +import { ChatOpenAI } from "@langchain/openai"; |
| 4 | +import { tool } from "@langchain/core/tools"; |
| 5 | +import { z } from "zod"; |
| 6 | + |
| 7 | +const nutrientApiKey = process.env.NUTRIENT_API_KEY; |
| 8 | +if (!nutrientApiKey) { |
| 9 | + throw new Error("Missing NUTRIENT_API_KEY. Add it to examples/.env before running."); |
| 10 | +} |
| 11 | + |
| 12 | +if (!process.env.OPENAI_API_KEY) { |
| 13 | + throw new Error("Missing OPENAI_API_KEY. Add it to examples/.env before running."); |
| 14 | +} |
| 15 | + |
| 16 | +const client = new NutrientClient({ |
| 17 | + apiKey: nutrientApiKey, |
| 18 | +}); |
| 19 | + |
| 20 | +const redactEmails = tool( |
| 21 | + async ({ path }) => { |
| 22 | + const response = await client.createRedactionsPreset(path, "email-address", "apply"); |
| 23 | + return JSON.stringify(response); |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "redact_emails", |
| 27 | + description: "Redact email addresses from a document via Nutrient DWS.", |
| 28 | + schema: z.object({ |
| 29 | + path: z.string().describe("Local path to the input PDF file."), |
| 30 | + }), |
| 31 | + } |
| 32 | +); |
| 33 | + |
| 34 | +async function main() { |
| 35 | + const model = new ChatOpenAI({ |
| 36 | + model: "gpt-4.1-mini", |
| 37 | + apiKey: process.env.OPENAI_API_KEY, |
| 38 | + }); |
| 39 | + |
| 40 | + const response = await model.bindTools([redactEmails]).invoke( |
| 41 | + "Redact email addresses from ./assets/sample.pdf and summarize next steps." |
| 42 | + ); |
| 43 | + |
| 44 | + if (response.tool_calls?.length) { |
| 45 | + for (const call of response.tool_calls) { |
| 46 | + if (call.name === "redact_emails") { |
| 47 | + const args = |
| 48 | + typeof call.args === "object" && call.args !== null ? call.args : {}; |
| 49 | + const toolResult = await redactEmails.invoke(args); |
| 50 | + console.log(toolResult); |
| 51 | + return; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + console.log(response.content); |
| 57 | +} |
| 58 | + |
| 59 | +void main(); |
0 commit comments