Use this file to discover all available pages before exploring further.
Mistral AI is a platform that offers hosting for their powerful open source models.This will help you getting started with Mistral chat models. For detailed documentation of all ChatMistralAI features and configurations head to the API reference.
When sending chat messages to mistral, there are a few requirements to follow:
The first message can not be an assistant (ai) message.
Messages must alternate between user and assistant (ai) messages.
Messages can not end with an assistant (ai) or system message.
const aiMsg = await llm.invoke([ [ "system", "You are a helpful assistant that translates English to French. Translate the user sentence.", ], ["human", "I love programming."],])aiMsg
Mistral’s API supports tool calling for a subset of their models. You can see which models support tool calling on this page.The examples below demonstrates how to use it:
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 modelconst 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 togetherconst chainWithCalcTool = calcToolPrompt.pipe(modelWithTool);const calcToolRes = await chainWithCalcTool.invoke({ input: "What is 2 + 2?",});console.log(calcToolRes.tool_calls);
Mistral AI supports custom hooks for three events: beforeRequest, requestError, and response. Examples of the function signature for each hook type can be seen below:
const beforeRequestHook = (req: Request): Request | void | Promise<Request | void> => { // Code to run before a request is processed by Mistral};const requestErrorHook = (err: unknown, req: Request): void | Promise<void> => { // Code to run when an error occurs as Mistral is processing a request};const responseHook = (res: Response, req: Request): void | Promise<void> => { // Code to run before Mistral sends a successful response};
To add these hooks to the chat model, either pass them as arguments and they are automatically added:
The method addAllHooksToHttpClient clears all currently added hooks before assigning the entire updated hook lists to avoid hook duplication.Hooks can be removed one at a time, or all hooks can be cleared from the model at once.