import { ChatMistralAI } from "@langchain/mistralai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import * as z from "zod";
import { tool } from "@langchain/core/tools";
const calculatorSchema = z.object({
operation: z
.enum(["add", "subtract", "multiply", "divide"])
.describe("The type of operation to execute."),
number1: z.number().describe("The first number to operate on."),
number2: z.number().describe("The second number to operate on."),
});
const calculatorTool = tool((input) => {
return JSON.stringify(input);
}, {
name: "calculator",
description: "A simple calculator tool",
schema: calculatorSchema,
});
// Bind the tool to the model
const modelWithTool = new ChatMistralAI({
model: "mistral-large-latest",
}).bindTools([calculatorTool]);
const calcToolPrompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant who always needs to use a calculator.",
],
["human", "{input}"],
]);
// Chain your prompt, model, and output parser together
const chainWithCalcTool = calcToolPrompt.pipe(modelWithTool);
const calcToolRes = await chainWithCalcTool.invoke({
input: "What is 2 + 2?",
});
console.log(calcToolRes.tool_calls);