In this blog post, we’ll walk through how to quickly test the microsoft/Phi-4-mini-instruct
model using Python and Hugging Face’s Transformers library.
This model is perfect for fast and efficient natural language tasks, especially when you’re working with limited compute or want low-latency inference.
Phi-4-mini-instruct
is a small language model developed by Microsoft, tuned to follow instructions. It’s great for tasks like:
Here’s a minimal Python script that:
Make sure you have these installed:
pip install transformers torch
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
# Use GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Load model and tokenizer
model_id = "microsoft/Phi-4-mini-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
trust_remote_code=True
).to(device)
# Create chat prompt
messages = [
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "Explain the difference between supervised and unsupervised learning in simple terms."}
]
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# Tokenize and move to device
inputs = tokenizer(prompt, return_tensors="pt").to(device)
# Generate output
output_ids = model.generate(
**inputs,
max_new_tokens=900,
do_sample=False,
temperature=0.0
)
# Decode and print response
response = tokenizer.decode(output_ids[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
print("Generated response:")
print(response)
Don’t have a local GPU? You can run this exact code on NiceGPU.com — a GPU-powered notebook platform.
NiceGPU is powered by Newegg and supports community AI projects with free or affordable GPU resources, especially for students and open-source developers.
Here’s an example of what the model might generate:
Generated response:
Supervised learning is like having a teacher who shows you examples of the answers and then asks you to predict the answers for new questions. You learn from the examples provided, and your goal is to get the answers right. Unsupervised learning, on the other hand, is like exploring a new place without a map. You look at the data and try to find patterns or groupings on your own, without any guidance on what you're looking for.
Phi-4-Mini-Instruct is a compact powerhouse. Whether you're building an embedded AI assistant, experimenting with LLMs locally, or deploying on a budget cloud instance, this model is an excellent starting point.
🔗 Try it online: https://huggingface.co/microsoft/phi-4-mini-instruct
🚀 Run it free: https://nicegpu.com