Run this model inference on single tenant GPU with unmatched speed and reliability at scale.
Run this model inference with full control and performance in your environment.
Get help setting up a custom Dedicated Endpoints.
Talk with our engineer to get a quote for reserved GPU instances with discounts.
README
License: apache-2.0Installation
It is recommended to use mistralai/Mistral-7B-Instruct-v0.3 with mistral-inference. For HF transformers code snippets, please keep scrolling.
markdown
pip install mistral_inference
Download
py
from huggingface_hub import snapshot_downloadfrom pathlib import Pathmistral_models_path = Path.home().joinpath('mistral_models', '7B-Instruct-v0.3')mistral_models_path.mkdir(parents=True, exist_ok=True)snapshot_download(repo_id="mistralai/Mistral-7B-Instruct-v0.3", allow_patterns=["params.json", "consolidated.safetensors", "tokenizer.model.v3"], local_dir=mistral_models_path)
Chat
After installing mistral_inference, a mistral-chat CLI command should be available in your environment. You can chat with the model using
markdown
mistral-chat $HOME/mistral_models/7B-Instruct-v0.3 --instruct --max_tokens 256
Instruct following
py
from mistral_inference.transformer import Transformerfrom mistral_inference.generate import generatefrom mistral_common.tokens.tokenizers.mistral import MistralTokenizerfrom mistral_common.protocol.instruct.messages import UserMessagefrom mistral_common.protocol.instruct.request import ChatCompletionRequesttokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")model = Transformer.from_folder(mistral_models_path)completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])tokens = tokenizer.encode_chat_completion(completion_request).tokensout_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])print(result)
Function calling
py
from mistral_common.protocol.instruct.tool_calls import Function, Toolfrom mistral_inference.transformer import Transformerfrom mistral_inference.generate import generatefrom mistral_common.tokens.tokenizers.mistral import MistralTokenizerfrom mistral_common.protocol.instruct.messages import UserMessagefrom mistral_common.protocol.instruct.request import ChatCompletionRequesttokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")model = Transformer.from_folder(mistral_models_path)completion_request = ChatCompletionRequest(tools=[Tool(function=Function(name="get_current_weather",description="Get the current weather",parameters={"type": "object","properties": {"location": {"type": "string","description": "The city and state, e.g. San Francisco, CA",},"format": {"type": "string","enum": ["celsius", "fahrenheit"],"description": "The temperature unit to use. Infer this from the users location.",},},"required": ["location", "format"],},))],messages=[UserMessage(content="What's the weather like today in Paris?"),],)tokens = tokenizer.encode_chat_completion(completion_request).tokensout_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])print(result)
Generate with transformers
If you want to use Hugging Face transformers to generate text, you can do something like this.
py
from transformers import pipelinemessages = [{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},{"role": "user", "content": "Who are you?"},]chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")chatbot(messages)
Function calling with transformers
To use this example, you'll need transformers version 4.42.0 or higher. Please see the
function calling guide
in the transformers docs for more information.
python
from transformers import AutoModelForCausalLM, AutoTokenizerimport torchmodel_id = "mistralai/Mistral-7B-Instruct-v0.3"tokenizer = AutoTokenizer.from_pretrained(model_id)def get_current_weather(location: str, format: str):"""Get the current weatherArgs:location: The city and state, e.g. San Francisco, CAformat: The temperature unit to use. Infer this from the users location. (choices: ["celsius", "fahrenheit"])"""passconversation = [{"role": "user", "content": "What's the weather like in Paris?"}]tools = [get_current_weather]# format and tokenize the tool use promptinputs = tokenizer.apply_chat_template(conversation,tools=tools,add_generation_prompt=True,return_dict=True,return_tensors="pt",)model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")inputs.to(model.device)outputs = model.generate(**inputs, max_new_tokens=1000)print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Note that, for reasons of space, this example does not show a complete cycle of calling a tool and adding the tool call and tool results to the chat history so that the model can use them in its next generation. For a full tool calling example, please see the function calling guide, and note that Mistral does use tool call IDs, so these must be included in your tool calls and tool results. They should be exactly 9 alphanumeric characters.
Limitations
The Mistral 7B Instruct model is a quick demonstration that the base model can be easily fine-tuned to achieve compelling performance. It does not have any moderation mechanisms. We're looking forward to engaging with the community on ways to make the model finely respect guardrails, allowing for deployment in environments requiring moderated outputs.
The Mistral AI Team
Albert Jiang, Alexandre Sablayrolles, Alexis Tacnet, Antoine Roux, Arthur Mensch, Audrey Herblin-Stoop, Baptiste Bout, Baudouin de Monicault, Blanche Savary, Bam4d, Caroline Feldman, Devendra Singh Chaplot, Diego de las Casas, Eleonore Arcelin, Emma Bou Hanna, Etienne Metzger, Gianna Lengyel, Guillaume Bour, Guillaume Lample, Harizo Rajaona, Jean-Malo Delignon, Jia Li, Justus Murke, Louis Martin, Louis Ternon, Lucile Saulnier, Lélio Renard Lavaud, Margaret Jennings, Marie Pellat, Marie Torelli, Marie-Anne Lachaux, Nicolas Schuhl, Patrick von Platen, Pierre Stock, Sandeep Subramanian, Sophia Yang, Szymon Antoniak, Teven Le Scao, Thibaut Lavril, Timothée Lacroix, Théophile Gervet, Thomas Wang, Valera Nemychnikova, William El Sayed, William Marshall
Model provider
mistralai
Model tree
Base
mistralai/Mistral-7B-v0.3
Fine-tuned
this model
Modalities
Input
Text
Output
Text
Pricing
Dedicated Endpoints
View detailsSupported Functionality
Model APIs
Dedicated Endpoints
Container
More information