Instructions to use mlx-community/c4ai-command-r-v01-4bit with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mlx-community/c4ai-command-r-v01-4bit with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="mlx-community/c4ai-command-r-v01-4bit", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("mlx-community/c4ai-command-r-v01-4bit", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("mlx-community/c4ai-command-r-v01-4bit", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - MLX
How to use mlx-community/c4ai-command-r-v01-4bit with MLX:
# Make sure mlx-lm is installed # pip install --upgrade mlx-lm # Generate text with mlx-lm from mlx_lm import load, generate model, tokenizer = load("mlx-community/c4ai-command-r-v01-4bit") prompt = "Write a story about Einstein" messages = [{"role": "user", "content": prompt}] prompt = tokenizer.apply_chat_template( messages, add_generation_prompt=True ) text = generate(model, tokenizer, prompt=prompt, verbose=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- LM Studio
- vLLM
How to use mlx-community/c4ai-command-r-v01-4bit with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "mlx-community/c4ai-command-r-v01-4bit" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mlx-community/c4ai-command-r-v01-4bit", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/mlx-community/c4ai-command-r-v01-4bit
- SGLang
How to use mlx-community/c4ai-command-r-v01-4bit 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 "mlx-community/c4ai-command-r-v01-4bit" \ --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": "mlx-community/c4ai-command-r-v01-4bit", "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 "mlx-community/c4ai-command-r-v01-4bit" \ --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": "mlx-community/c4ai-command-r-v01-4bit", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - MLX LM
How to use mlx-community/c4ai-command-r-v01-4bit with MLX LM:
Generate or start a chat session
# Install MLX LM uv tool install mlx-lm # Interactive chat REPL mlx_lm.chat --model "mlx-community/c4ai-command-r-v01-4bit"
Run an OpenAI-compatible server
# Install MLX LM uv tool install mlx-lm # Start the server mlx_lm.server --model "mlx-community/c4ai-command-r-v01-4bit" # Calling the OpenAI-compatible server with curl curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mlx-community/c4ai-command-r-v01-4bit", "messages": [ {"role": "user", "content": "Hello"} ] }' - Docker Model Runner
How to use mlx-community/c4ai-command-r-v01-4bit with Docker Model Runner:
docker model run hf.co/mlx-community/c4ai-command-r-v01-4bit
ValueError: Model type cohere not supported.
I get this error if I try to run it with these versions:
Package Version
accelerate 0.28.0
Brotli 1.0.9
certifi 2024.2.2
chardet 5.2.0
charset-normalizer 2.0.4
filelock 3.13.1
fsspec 2024.2.0
huggingface-hub 0.21.4
idna 3.4
Jinja2 3.1.3
MarkupSafe 2.1.3
mlx 0.6.0
mlx-lm 0.2.0
mpmath 1.3.0
networkx 3.1
numpy 1.26.4
packaging 24.0
pillow 10.2.0
pip 23.3.1
protobuf 4.25.3
psutil 5.9.8
PySocks 1.7.1
PyYAML 6.0.1
regex 2023.12.25
requests 2.31.0
safetensors 0.4.2
sentencepiece 0.2.0
setuptools 68.2.2
sympy 1.12
tokenizers 0.15.2
torch 2.2.1
torchaudio 2.2.1
torchvision 0.17.1
tqdm 4.66.2
transformers 4.38.1
typing_extensions 4.9.0
urllib3 2.1.0
wheel 0.41.2
Please update your mlx-lm library to the latest version 0.3.0.
Here is snippet:
pip install -U mlx-lm
Let me know if it works for you :)
yes, thank you very much :)
Most welcome!
Excuse me, how can I help you @DrEslamFouad ?