Text Generation
Transformers
Safetensors
MLX
English
llama
alignment-handbook
trl
sft
conversational
text-generation-inference
Instructions to use mlx-community/SmolLM-360M-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mlx-community/SmolLM-360M-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="mlx-community/SmolLM-360M-Instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("mlx-community/SmolLM-360M-Instruct") model = AutoModelForCausalLM.from_pretrained("mlx-community/SmolLM-360M-Instruct", device_map="auto") 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/SmolLM-360M-Instruct 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/SmolLM-360M-Instruct") 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 Settings
- LM Studio
- vLLM
How to use mlx-community/SmolLM-360M-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "mlx-community/SmolLM-360M-Instruct" # 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/SmolLM-360M-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/mlx-community/SmolLM-360M-Instruct
- SGLang
How to use mlx-community/SmolLM-360M-Instruct 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/SmolLM-360M-Instruct" \ --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/SmolLM-360M-Instruct", "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/SmolLM-360M-Instruct" \ --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/SmolLM-360M-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - MLX LM
How to use mlx-community/SmolLM-360M-Instruct 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/SmolLM-360M-Instruct"
Run an OpenAI-compatible server
# Install MLX LM uv tool install mlx-lm # Start the server mlx_lm.server --model "mlx-community/SmolLM-360M-Instruct" # 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/SmolLM-360M-Instruct", "messages": [ {"role": "user", "content": "Hello"} ] }' - Docker Model Runner
How to use mlx-community/SmolLM-360M-Instruct with Docker Model Runner:
docker model run hf.co/mlx-community/SmolLM-360M-Instruct
- Atomic Chat
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| BASE_PATH = "/fsx/loubna/projects/alignment-handbook/recipes/cosmo2/sft/data" | |
| TEMPERATURE = 0.2 | |
| TOP_P = 0.9 | |
| CHECKPOINT = "loubnabnl/smollm-350M-instruct-add-basics" | |
| print(f"💾 Loading the model and tokenizer: {CHECKPOINT}...") | |
| device = "cuda" | |
| tokenizer = AutoTokenizer.from_pretrained(CHECKPOINT) | |
| model_s = AutoModelForCausalLM.from_pretrained(CHECKPOINT).to(device) | |
| print("🧪 Testing single-turn conversations...") | |
| L = [ | |
| "Hi", | |
| "Hello", | |
| "Tell me a joke", | |
| "Who are you?", | |
| "What's your name?", | |
| "How do I make pancakes?", | |
| "Can you tell me what is gravity?", | |
| "What is the capital of Morocco?", | |
| "What's 2+2?", | |
| "Hi, what is 2+1?", | |
| "What's 3+5?", | |
| "Write a poem about Helium", | |
| "Hi, what are some popular dishes from Japan?", | |
| ] | |
| for i in range(len(L)): | |
| print(f"🔮 {L[i]}") | |
| messages = [{"role": "user", "content": L[i]}] | |
| input_text = tokenizer.apply_chat_template(messages, tokenize=False) | |
| inputs = tokenizer.encode(input_text, return_tensors="pt").to(device) | |
| outputs = model_s.generate( | |
| inputs, max_new_tokens=200, top_p=TOP_P, do_sample=True, temperature=TEMPERATURE | |
| ) | |
| with open( | |
| f"{BASE_PATH}/{CHECKPOINT.split('/')[-1]}_temp_{TEMPERATURE}_topp{TOP_P}.txt", | |
| "a", | |
| ) as f: | |
| f.write("=" * 50 + "\n") | |
| f.write(tokenizer.decode(outputs[0])) | |
| f.write("\n") | |
| print("🧪 Now testing multi-turn conversations...") | |
| # Multi-turn conversations | |
| messages_1 = [ | |
| {"role": "user", "content": "Hi"}, | |
| {"role": "assistant", "content": "Hello! How can I help you today?"}, | |
| {"role": "user", "content": "What's 2+2?"}, | |
| ] | |
| messages_2 = [ | |
| {"role": "user", "content": "Hi"}, | |
| {"role": "assistant", "content": "Hello! How can I help you today?"}, | |
| {"role": "user", "content": "What's 2+2?"}, | |
| {"role": "assistant", "content": "4"}, | |
| {"role": "user", "content": "Why?"}, | |
| ] | |
| messages_3 = [ | |
| {"role": "user", "content": "Who are you?"}, | |
| {"role": "assistant", "content": "I am an AI assistant. How can I help you today?"}, | |
| {"role": "user", "content": "What's your name?"}, | |
| ] | |
| messages_4 = [ | |
| {"role": "user", "content": "Tell me a joke"}, | |
| {"role": "assistant", "content": "Sure! Why did the tomato turn red?"}, | |
| {"role": "user", "content": "Why?"}, | |
| ] | |
| messages_5 = [ | |
| {"role": "user", "content": "Can you tell me what is gravity?"}, | |
| { | |
| "role": "assistant", | |
| "content": "Sure! Gravity is a force that attracts objects toward each other. It is what keeps us on the ground and what makes things fall.", | |
| }, | |
| {"role": "user", "content": "Who discovered it?"}, | |
| ] | |
| messages_6 = [ | |
| {"role": "user", "content": "How do I make pancakes?"}, | |
| { | |
| "role": "assistant", | |
| "content": "Sure! Here is a simple recipe for pancakes: Ingredients: 1 cup flour, 1 cup milk, 1 egg, 1 tbsp sugar, 1 tsp baking powder, 1/2 tsp salt. Instructions: 1. Mix all the dry ingredients together in a bowl. 2. Add the milk and egg and mix until smooth. 3. Heat a non-stick pan over medium heat. 4. Pour 1/4 cup of batter onto the pan. 5. Cook until bubbles form on the surface, then flip and cook for another minute. 6. Serve with your favorite toppings.", | |
| }, | |
| {"role": "user", "content": "What are some popular toppings?"}, | |
| ] | |
| L = [messages_1, messages_2, messages_3, messages_4, messages_5, messages_6] | |
| for i in range(len(L)): | |
| input_text = tokenizer.apply_chat_template(L[i], tokenize=False) | |
| inputs = tokenizer.encode(input_text, return_tensors="pt").to(device) | |
| outputs = model_s.generate( | |
| inputs, max_new_tokens=200, top_p=TOP_P, do_sample=True, temperature=TEMPERATURE | |
| ) | |
| with open( | |
| f"{BASE_PATH}/{CHECKPOINT.split('/')[-1]}_temp_{TEMPERATURE}_topp{TOP_P}_MT.txt", | |
| "a", | |
| ) as f: | |
| f.write("=" * 50 + "\n") | |
| f.write(tokenizer.decode(outputs[0])) | |
| f.write("\n") | |
| print("🔥 Done!") | |