Mistral-Small-Instruct-2409 is an instruct fine-tuned version with the following characteristics:
22B parameters
Vocabulary to 32768
Supports function calling
128k sequence length
Usage Examples
vLLM (recommended)
We recommend using this model with the vLLM library
to implement production-ready inference pipelines.
Installation
Make sure you install vLLM >= v0.6.1.post1:
pip install --upgrade vllm
Also make sure you have mistral_common >= 1.4.1 installed:
pip install --upgrade mistral_common
You can also make use of a ready-to-go docker image.
Offline
py
1from vllm import LLM
2from vllm.sampling_params import SamplingParams
34model_name ="mistralai/Mistral-Small-Instruct-2409"56sampling_params = SamplingParams(max_tokens=8192)78# note that running Mistral-Small on a single GPU requires at least 44 GB of GPU RAM9# If you want to divide the GPU requirement over multiple devices, please add *e.g.* `tensor_parallel=2`10llm = LLM(model=model_name, tokenizer_mode="mistral", config_format="mistral", load_format="mistral")1112prompt ="How often does the letter r occur in Mistral?"1314messages =[15{16"role":"user",17"content": prompt
18},19]2021outputs = llm.chat(messages, sampling_params=sampling_params)2223print(outputs[0].outputs[0].text)
Server
You can also use Mistral Small in a server/client setting.
1from mistral_inference.transformer import Transformer
2from mistral_inference.generate import generate
34from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
5from mistral_common.protocol.instruct.messages import UserMessage
6from mistral_common.protocol.instruct.request import ChatCompletionRequest
789tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")10model = Transformer.from_folder(mistral_models_path)1112completion_request = ChatCompletionRequest(messages=[UserMessage(content="How often does the letter r occur in Mistral?")])1314tokens = tokenizer.encode_chat_completion(completion_request).tokens
1516out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)17result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])1819print(result)
Function calling
py
1from mistral_common.protocol.instruct.tool_calls import Function, Tool
2from mistral_inference.transformer import Transformer
3from mistral_inference.generate import generate
45from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
6from mistral_common.protocol.instruct.messages import UserMessage
7from mistral_common.protocol.instruct.request import ChatCompletionRequest
8910tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")11model = Transformer.from_folder(mistral_models_path)1213completion_request = ChatCompletionRequest(14 tools=[15 Tool(16 function=Function(17 name="get_current_weather",18 description="Get the current weather",19 parameters={20"type":"object",21"properties":{22"location":{23"type":"string",24"description":"The city and state, e.g. San Francisco, CA",25},26"format":{27"type":"string",28"enum":["celsius","fahrenheit"],29"description":"The temperature unit to use. Infer this from the users location.",30},31},32"required":["location","format"],33},34)35)36],37 messages=[38 UserMessage(content="What's the weather like today in Paris?"),39],40)4142tokens = tokenizer.encode_chat_completion(completion_request).tokens
4344out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)45result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])4647print(result)
Usage in Hugging Face Transformers
You can also use Hugging Face transformers library to run inference using various chat templates, or fine-tune the model.
Example for inference:
python
1from transformers import LlamaTokenizerFast, MistralForCausalLM
2import torch
34device ="cuda"5tokenizer = LlamaTokenizerFast.from_pretrained('mistralai/Mistral-Small-Instruct-2409')6tokenizer.pad_token = tokenizer.eos_token
78model = MistralForCausalLM.from_pretrained('mistralai/Mistral-Small-Instruct-2409', torch_dtype=torch.bfloat16)9model = model.to(device)1011prompt ="How often does the letter r occur in Mistral?"1213messages =[14{"role":"user","content": prompt},15]1617model_input = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(device)18gen = model.generate(model_input, max_new_tokens=150)19dec = tokenizer.batch_decode(gen)20print(dec)
And you should obtain
text
1<s>
2 [INST]
3 How often does the letter r occur in Mistral?
4 [/INST]
5 To determine how often the letter "r" occurs in the word "Mistral,"
6 we can simply count the instances of "r" in the word.
7 The word "Mistral" is broken down as follows:
8 - M
9 - i
10 - s
11 - t
12 - r
13 - a
14 - l
15 Counting the "r"s, we find that there is only one "r" in "Mistral."
16 Therefore, the letter "r" occurs once in the word "Mistral."
17</s>
The Mistral AI Team
Albert Jiang, Alexandre Sablayrolles, Alexis Tacnet, Alok Kothari, Antoine Roux, Arthur Mensch, Audrey Herblin-Stoop, Augustin Garreau, Austin Birky, Bam4d, Baptiste Bout, Baudouin de Monicault, Blanche Savary, Carole Rambaud, Caroline Feldman, Devendra Singh Chaplot, Diego de las Casas, Diogo Costa, Eleonore Arcelin, Emma Bou Hanna, Etienne Metzger, Gaspard Blanchet, Gianna Lengyel, Guillaume Bour, Guillaume Lample, Harizo Rajaona, Henri Roussez, Hichem Sattouf, Ian Mack, Jean-Malo Delignon, Jessica Chudnovsky, Justus Murke, Kartik Khandelwal, Lawrence Stewart, Louis Martin, Louis Ternon, Lucile Saulnier, Lélio Renard Lavaud, Margaret Jennings, Marie Pellat, Marie Torelli, Marie-Anne Lachaux, Marjorie Janiewicz, Mickaël Seznec, Nicolas Schuhl, Niklas Muhs, Olivier de Garrigues, Patrick von Platen, Paul Jacob, Pauline Buche, Pavan Kumar Reddy, Perry Savas, Pierre Stock, Romain Sauvestre, Sagar Vaze, Sandeep Subramanian, Saurabh Garg, Sophia Yang, Szymon Antoniak, Teven Le Scao, Thibault Schueller, Thibaut Lavril, Thomas Wang, Théophile Gervet, Timothée Lacroix, Valera Nemychnikova, Wendy Shang, William El Sayed, William Marshall