Microsoft’s Phi-4 mannequin is offered on Hugging Face, providing builders a strong software for superior textual content technology 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 technology. On this Phi-4, we’re having about 14 billion parameters that align effectively in reminiscence and computationally restricted eventualities 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 via 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 principle 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 skilled 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 via three principal phases: pre-training (with 4K context), mid-training (expanded to 16K context), and fine-tuning. Publish-training enhancements included Supervised Effective-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 possibly can 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 technology.
- Security Measures: The mannequin incorporates sturdy security options, together with supervised fine-tuning and desire optimization, to make sure protected and useful interactions.
Stipulations
Earlier than getting began into the method of accessing PHI 4, be sure you have the next conditions:
- Hugging Face Account: You’ll need 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 mandatory libraries.
Use the next instructions to put in them:
pip set up transformers
pip set up torchThe way to Entry Phi-4 Utilizing Hugging Face?
Beneath we’ll present you how one can simply entry and make the most of Microsoft’s Phi-4 mannequin on Hugging Face, enabling highly effective textual content technology and reasoning capabilities to your functions. Observe 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 enroll. After creating an account, you’ll be capable 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, it is advisable authenticate your Hugging Face account. You need to use the Hugging Face CLI software to take action:
Set up the CLI software:
pip set up huggingface_hubLog in to your Hugging Face account by working the next command:
huggingface-cli loginEnter your credentials or token when prompted.
Step 3: Set up Required Libraries
First, guarantee you might have the transformers library put in. You possibly can set up it utilizing pip:
pip set up transformersStep 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 possibly can 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 primarily based in your enter:
outputs = pipeline(messages, max_new_tokens=128)
print(outputs[0]['generated_text'])Output:

Conclusion
Phi-4 is now absolutely 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, academic instruments, or any utility requiring superior language understanding, Phi-4 stands out as a strong choice.
For additional particulars and updates, you possibly can seek advice from the official Hugging Face documentation and discover the capabilities of this revolutionary mannequin.
Continuously Requested Questions
A. Microsoft developed Phi-4, a state-of-the-art language mannequin, to excel in superior reasoning and high-quality textual content technology. That includes 14 billion parameters, it optimizes efficiency for reminiscence and compute-constrained environments.
A. You want Python 3.7 or later, and libraries akin to transformers, torch, and huggingface_hub. Guarantee your machine meets the mandatory compute necessities, particularly for dealing with giant fashions.
A. Phi-4 is right for textual content technology, superior reasoning, chatbot improvement, academic instruments, and any utility requiring in depth language understanding and technology.
A. Microsoft optimized Phi-4 for chat-style prompts, structuring inputs as a listing of messages, every with a job (e.g., system, person) and content material.
A. The important thing options of Phi-4 are:
14 Billion Parameters : For superior textual content technology
Context Size : As much as 16,000 tokens.
Security Options: Supervised fine-tuning and desire optimization for protected interactions.
Effectivity: Optimized for reminiscence and compute-constrained environments.
