Instructions to use microsoft/mistral-7b-instruct-v0.2-ONNX with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/mistral-7b-instruct-v0.2-ONNX with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/mistral-7b-instruct-v0.2-ONNX", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("microsoft/mistral-7b-instruct-v0.2-ONNX", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/mistral-7b-instruct-v0.2-ONNX with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/mistral-7b-instruct-v0.2-ONNX" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/mistral-7b-instruct-v0.2-ONNX", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/microsoft/mistral-7b-instruct-v0.2-ONNX
- SGLang
How to use microsoft/mistral-7b-instruct-v0.2-ONNX with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "microsoft/mistral-7b-instruct-v0.2-ONNX" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/mistral-7b-instruct-v0.2-ONNX", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "microsoft/mistral-7b-instruct-v0.2-ONNX" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/mistral-7b-instruct-v0.2-ONNX", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use microsoft/mistral-7b-instruct-v0.2-ONNX with Docker Model Runner:
docker model run hf.co/microsoft/mistral-7b-instruct-v0.2-ONNX
it run colab cpu
from transformers import AutoTokenizer, pipeline
from optimum.onnxruntime import ORTModelForCausalLM
import os
Define the root local path where the full model repository was cloned
This should be the directory that contains the 'onnx' subfolder, config.json, etc.
local_repo_root = "/content/mistral-7b-instruct-v0.2-ONNX"
The specific subfolder within the local_repo_root where the ONNX model files are located
onnx_model_subfolder = "onnx/cpu_and_mobile/mistral-7b-instruct-v0.2-cpu-int4-rtn-block-32-acc-level-4"
The specific ONNX file name
onnx_file_name = "mistral-7b-instruct-v0.2-cpu-int4-rtn-block-32-acc-level-4.onnx"
Ensure the root directory exists
if not os.path.exists(local_repo_root):
print(f"Error: Local repository root not found at {local_repo_root}")
else:
print(f"Loading model and tokenizer from local repository: {local_repo_root}")
Manually download the tokenizer.model file to the local_repo_root if it's not already there
tokenizer_model_path = os.path.join(local_repo_root, "tokenizer.model")
if not os.path.exists(tokenizer_model_path):
print(f"Downloading missing tokenizer.model to {local_repo_root}...")
os.system(f"wget -O {tokenizer_model_path} https://huggingface.co/microsoft/mistral-7b-instruct-v0.2-ONNX/resolve/main/tokenizer.model")
print("tokenizer.model downloaded.")
Load the ONNX model from the local repository, pointing to the subfolder
model = ORTModelForCausalLM.from_pretrained(
local_repo_root, # Point to the root of the cloned repository
subfolder=onnx_model_subfolder, # Specify the subfolder for the ONNX model
file_name=onnx_file_name,
use_io_binding=True,
local_files_only=True # Crucial: tells the library to only look locally
)
Load the Tokenizer from the local repository root
tokenizer = AutoTokenizer.from_pretrained(
local_repo_root, # Point to the root of the cloned repository for tokenizer files
local_files_only=True # Crucial: tells the library to only look locally
)
Create pipeline
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
Test the model
result = pipe(
"Who is Napoleon Bonaparte?",
max_new_tokens=30,
do_sample=True,
temperature=0.7,
top_k=50,
top_p=0.9,
num_return_sequences=1
)
print(result)