import { z } from "zod"; import { ChatOpenAI } from "langchain/chat_models/openai"; import { initializeAgentExecutorWithOptions } from "langchain/agents"; import { DynamicStructuredTool } from "langchain/tools"; const model = new ChatOpenAI({ temperature: 0.1 }); const tools = [ new DynamicStructuredTool({ name: "task-scheduler", description: "Schedules tasks", schema: z .object({ tasks: z .array( z.object({ title: z .string() .describe("The title of the tasks, reminders and alerts"), due_date: z .string() .describe("Due date. Must be a valid JavaScript date string"), task_type: z .enum([ "Call", "Message", "Todo", "In-Person Meeting", "Email", "Mail", "Text", "Open House", ]) .describe("The type of task"), }) ) .describe("The JSON for task, reminder or alert to create"), }) .describe("JSON definition for creating tasks, reminders and alerts"), func: async (input: { tasks: object }) => JSON.stringify(input), }), ]; const executor = await initializeAgentExecutorWithOptions(tools, model, { agentType: "openai-functions", verbose: true, handleParsingErrors: "Please try again, paying close attention to the allowed enum values", }); console.log("Loaded agent."); const input = `Set a reminder to renew our online property ads next week.`; console.log(`Executing with input "${input}"...`); const result = await executor.invoke({ input }); console.log({ result }); /* { result: { output: 'I have set a reminder for you to renew your online property ads on October 10th, 2022.' } } */