Microsoft’s Phi-4 mannequin is out there on Hugging Face, providing builders a robust device for superior textual content era and reasoning duties. On this article, we’ll stroll you thru the steps to entry and use Phi-4, from making a Hugging Face account to producing outputs with the mannequin. We’ll additionally discover key options, together with its optimized efficiency for reminiscence and compute-constrained environments, and how one can successfully use Phi-4 in varied functions.
Phi 4 and its Options
Phi-4, is a state-of-the-art language mannequin designed for superior reasoning and high-quality textual content era. On this Phi-4, we’re having about 14 billion parameters that align nicely in reminiscence and computationally restricted situations to make it extremely appropriate for builders in search of to include environment friendly synthetic intelligence of their functions.

The Phi-4 mannequin follows a decoder-only transformer structure with 14 billion parameters, designed to course of textual content by way of a classy pipeline. At its core, the enter textual content is first tokenized utilizing the Tiktoken tokenizer with a vocabulary dimension of 100,352, which then feeds into the token embedding layer. The primary transformer structure consists of a number of layers of self-attention mechanisms able to dealing with a 16K token context window (expanded from 4K throughout midtraining), adopted by feed-forward networks.
The mannequin was educated on roughly 10 trillion tokens with a various knowledge composition: 40% artificial knowledge, 15% internet rewrites, 15% filtered internet knowledge, 20% code knowledge, and 10% focused acquisitions. The coaching pipeline progressed by way of three major phases: pre-training (with 4K context), mid-training (expanded to 16K context), and fine-tuning. Submit-training enhancements included Supervised Wonderful-tuning (SFT), Direct Choice Optimization (DPO) with pivotal token search, and judge-guided knowledge, culminating in a language mannequin that outputs likelihood distributions over its vocabulary to generate responses.
You’ll be able to learn extra about Phi-4 right here.
Options of Phi-4
- Context Size: Phi-4 helps a context size of as much as 16,000 tokens, permitting for in depth conversations or detailed textual content era.
- Security Measures: The mannequin incorporates strong security options, together with supervised fine-tuning and choice optimization, to make sure protected and useful interactions.
Stipulations
Earlier than getting began into the method of accessing PHI 4, ensure you have the next conditions:
- Hugging Face Account: You will want a Hugging Face account to entry and use fashions from the Hub.
- Python Surroundings: Guarantee you might have Python 3.7 or later put in in your machine.
- Libraries: Set up the required libraries.
Use the next instructions to put in them:
pip set up transformers
pip set up torch
Entry Phi-4 Utilizing Hugging Face?
Beneath we’ll present you easy methods to simply entry and make the most of Microsoft’s Phi-4 mannequin on Hugging Face, enabling highly effective textual content era and reasoning capabilities to your functions. Comply with our step-by-step directions to get began shortly and effectively.
Step 1: Making a Hugging Face Account
To entry PHI 4 and different fashions, you first must create an account on Hugging Face. Go to Hugging Face’s web site and join. After creating an account, you’ll have the ability to entry non-public and public fashions hosted on the platform.
Step 2: Authenticate with Hugging Face
To entry non-public fashions like PHI 4, you’ll want to authenticate your Hugging Face account. You need to use the Hugging Face CLI device to take action:
Set up the CLI device:
pip set up huggingface_hub
Log in to your Hugging Face account by operating the next command:
huggingface-cli login
Enter your credentials or token when prompted.
Step 3: Set up Required Libraries
First, guarantee you might have the transformers library put in. You’ll be able to set up it utilizing pip:
pip set up transformers
Step 4: Load the Phi-4 Mannequin
As soon as the library is put in, you possibly can load the Phi-4 mannequin utilizing the pipeline API from Hugging Face. Right here’s how you are able to do it:
import transformers
# Load the Phi-4 mannequin
pipeline = transformers.pipeline(
"text-generation",
mannequin="microsoft/phi-4",
model_kwargs={"torch_dtype": "auto"},
device_map="auto",
)
Step 5: Put together Your Enter
Phi-4 is optimized for chat-style prompts. You’ll be able to construction your enter as follows:
messages = [
{"role": "system", "content": "You are a data scientist providing insights and explanations to a curious audience."},
{"role": "user", "content": "How should I explain machine learning to someone new to the field?"},
]
Step 6: Producing Output
Use the pipeline to generate responses based mostly in your enter:
outputs = pipeline(messages, max_new_tokens=128)
print(outputs[0]['generated_text'])
Output:

Conclusion
Phi-4 is now totally accessible on Hugging Face, making it simpler than ever for builders and researchers to leverage its capabilities for varied functions. Whether or not you’re constructing chatbots, instructional instruments, or any utility requiring superior language understanding, Phi-4 stands out as a robust possibility.
For additional particulars and updates, you possibly can discuss with the official Hugging Face documentation and discover the capabilities of this modern mannequin.
Incessantly Requested Questions
A. Microsoft developed Phi-4, a state-of-the-art language mannequin, to excel in superior reasoning and high-quality textual content era. That includes 14 billion parameters, it optimizes efficiency for reminiscence and compute-constrained environments.
A. You want Python 3.7 or later, and libraries resembling transformers, torch, and huggingface_hub. Guarantee your machine meets the required compute necessities, particularly for dealing with giant fashions.
A. Phi-4 is good for textual content era, superior reasoning, chatbot growth, instructional instruments, and any utility requiring in depth language understanding and era.
A. Microsoft optimized Phi-4 for chat-style prompts, structuring inputs as an inventory of messages, every with a task (e.g., system, person) and content material.
A. The important thing options of Phi-4 are:
14 Billion Parameters : For superior textual content era
Context Size : As much as 16,000 tokens.
Security Options: Supervised fine-tuning and choice optimization for protected interactions.
Effectivity: Optimized for reminiscence and compute-constrained environments.