Skip to main content
Mistral AI is a cloud based platform serving their own LLMs, like Mistral, Mixtral, and Codestral. Although AG2 can be used with Mistral AI’s API directly by changing the base_url to their url, it does not cater for some differences between messaging and, with their API being more strict than OpenAI’s, it is recommended to use the Mistral AI Client class as shown in this notebook. You will need a Mistral.AI account and create an API key. See their website for further details.

Features

When using this client class, messages are automatically tailored to accommodate the specific requirements of Mistral AI’s API (such as role orders), which have become more strict than OpenAI’s API. Additionally, this client class provides support for function/tool calling and will track token usage and cost correctly as per Mistral AI’s API costs (as of June 2024).

Getting started

First you need to install the AG2 package with the Mistral API extra.
If you have been using autogen or ag2, all you need to do is upgrade it using:
or
as autogen and ag2 are aliases for the same PyPI package.
Mistral provides a number of models to use, included below. See the list of models here. See the sample OAI_CONFIG_LIST below showing how the Mistral AI client class is used by specifying the api_type as mistral.
As an alternative to the api_key key and value in the config, you can set the environment variable MISTRAL_API_KEY to your Mistral AI key. Linux/Mac:
Windows:

API parameters

The following parameters can be added to your config for the Mistral.AI API. See this link for further information on them and their default values.
  • temperature (number 0..1)
  • top_p (number 0..1)
  • max_tokens (null, integer >= 0)
  • random_seed (null, integer)
  • safe_prompt (True or False)
Example:

Two-Agent Coding Example

In this example, we run a two-agent chat with an AssistantAgent (primarily a coding agent) to generate code to count the number of prime numbers between 1 and 10,000 and then it will be executed. We’ll use Mistral’s Mixtral 8x22B model which is suitable for coding.

Tool Call Example

In this example, instead of writing code, we will have two agents playing chess against each other using tool calling to make moves. We’ll change models to Mistral AI’s large model for this challenging task.
First install the chess package by running the following command: