Skip to main content
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.

Overview

Integration details

Model features

See the links in the table headers below for guides on how to use specific features.

Setup

To access Mistral AI models you’ll need to create a Mistral AI account, get an API key, and install the @langchain/mistralai integration package.

Credentials

Visit the Mistral console to sign up and generate an API key. Once you’ve done this set the MISTRAL_API_KEY environment variable:
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Installation

The LangChain ChatMistralAI integration lives in the @langchain/mistralai package:

Instantiation

Now we can instantiate our model object and generate chat completions:

Invocation

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.

Tool calling

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:

Hooks

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:
To add these hooks to the chat model, either pass them as arguments and they are automatically added:
Or assign and add them manually after instantiation:
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.

API reference

For detailed documentation of all ChatMistralAI features and configurations head to the API reference.