Skip to main content
This will help you get started with MistralAIEmbeddings embedding models using LangChain. For detailed documentation on MistralAIEmbeddings features and configuration options, please refer to the API reference.

Overview

Integration details

Setup

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

Credentials

Head to console.mistral.ai to sign up to MistralAI 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 MistralAIEmbeddings integration lives in the @langchain/mistralai package:

Instantiation

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

Indexing and retrieval

Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials under the Learn tab. Below, see how to index and retrieve data using the embeddings object we initialized above. In this example, we will index and retrieve a sample document using the demo MemoryVectorStore.

Direct usage

Under the hood, the vectorstore and retriever implementations are calling embeddings.embedDocument(...) and embeddings.embedQuery(...) to create embeddings for the text(s) used in fromDocuments and the retriever’s invoke operations, respectively. You can directly call these methods to get embeddings for your own use cases.

Embed single texts

You can embed queries for search with embedQuery. This generates a vector representation specific to the query:

Embed multiple texts

You can embed multiple texts for indexing with embedDocuments. The internals used for this method may (but do not have to) differ from embedding queries:

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 embeddings 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 MistralAIEmbeddings features and configurations head to the API reference.