By default, vLLM sources the model weights from Hugging Face. To access Mistral model repositories, you need to be authenticated on Hugging Face, so an access token HF_TOKEN with the READ permission will be required. You should also ensure that you have accepted the conditions of access on each model card page.
If you already have the model artifacts on your infrastructure, you can use them directly by pointing vLLM to their local path instead of a Hugging Face model ID. In this scenario, you will be able to skip all Hugging Face-related setup steps.
Getting Started
The following sections will guide you through the process of deploying and querying Mistral models on vLLM.
Installing vLLM
Create a Python virtual environment and install the vllm package (version >=0.6.1.post1 to ensure maximum compatibility with all Mistral models).
Authenticate on the HuggingFace Hub using your access token $HF_TOKEN:
huggingface-cli login --token $HF_TOKEN
Offline Mode Inference
When using vLLM in offline mode, the model is loaded and used for one-off batch inference workloads.
from vllm import LLMfrom vllm.sampling_params import SamplingParamsmodel_name = "mistralai/Mistral-NeMo-Instruct-2407"sampling_params = SamplingParams(max_tokens=8192)llm = LLM( model=model_name, tokenizer_mode="mistral", load_format="mistral", config_format="mistral",)messages = [ { "role": "user", "content": "Who is the best French painter? Answer with detailed explanations.", }]res = llm.chat(messages=messages, sampling_params=sampling_params)print(res[0].outputs[0].text)
from vllm import LLMfrom vllm.sampling_params import SamplingParamsmodel_name = "mistralai/Mistral-Small-Instruct-2409"sampling_params = SamplingParams(max_tokens=8192)llm = LLM( model=model_name, tokenizer_mode="mistral", load_format="mistral", config_format="mistral",)messages = [ { "role": "user", "content": "Who is the best French painter? Answer with detailed explanations.", }]res = llm.chat(messages=messages, sampling_params=sampling_params)print(res[0].outputs[0].text)
Suppose you want to caption the following images:
You can do so by running the following code:
from vllm import LLMfrom vllm.sampling_params import SamplingParamsmodel_name = "mistralai/Pixtral-12B-2409"max_img_per_msg = 3sampling_params = SamplingParams(max_tokens=8192)llm = LLM( model=model_name, tokenizer_mode="mistral", load_format="mistral", config_format="mistral", limit_mm_per_prompt={"image": max_img_per_msg},)urls = [f"https://picsum.photos/id/{id}/512/512" for id in ["1", "11", "111"]]messages = [ { "role": "user", "content": [ {"type": "text", "text": "Describe this image"} ] + [{"type": "image_url", "image_url": {"url": f"{u}"}} for u in urls], },]res = llm.chat(messages=messages, sampling_params=sampling_params)print(res[0].outputs[0].text)
Server Mode Inference
In server mode, vLLM spawns an HTTP server that continuously waits for clients to connect and send requests concurrently. The server exposes a REST API that implements the OpenAI protocol, allowing you to directly reuse existing code relying on the OpenAI API.
Start the inference server to deploy your model, e.g., for Mistral NeMo:
The default number of image inputs per prompt is set to 1. To increase it, set the --limit_mm_per_prompt option (e.g., --limit_mm_per_prompt 'image=4').
If you encounter memory issues, set the --max_model_len option to reduce the memory requirements of vLLM (e.g., --max_model_len 16384). More troubleshooting details can be found in the vLLM documentation.
You can now run inference requests with images and text inputs. Suppose you want to caption the following image:
You can prompt the model and retrieve its response like so:
If you are looking to deploy vLLM as a containerized inference server, you can leverage the project's official Docker image (see more details in the vLLM Docker documentation).
Set the HuggingFace access token environment variable in your shell: