Microsoft’s Phi-4 model is available on Hugging Face, offering developers a powerful tool for advanced text generation and reasoning tasks. In this article, we’ll walk you through the steps to access and use Phi-4, from creating a Hugging Face account to generating outputs with the model. We’ll also explore key features, including its optimized performance for memory and compute-constrained environments, and how you can effectively use Phi-4 in various applications.
Phi-4, is a state-of-the-art language model designed for advanced reasoning and high-quality text generation. In this Phi-4, we are having about 14 billion parameters that align well in memory and computationally limited scenarios to make it highly suitable for developers seeking to incorporate efficient artificial intelligence in their applications.
The Phi-4 model follows a decoder-only transformer architecture with 14 billion parameters, designed to process text through a sophisticated pipeline. At its core, the input text is first tokenized using the Tiktoken tokenizer with a vocabulary size of 100,352, which then feeds into the token embedding layer. The main transformer architecture consists of multiple layers of self-attention mechanisms capable of handling a 16K token context window (expanded from 4K during midtraining), followed by feed-forward networks.
The model was trained on approximately 10 trillion tokens with a diverse data composition: 40% synthetic data, 15% web rewrites, 15% filtered web data, 20% code data, and 10% targeted acquisitions. The training pipeline progressed through three main phases: pre-training (with 4K context), mid-training (expanded to 16K context), and fine-tuning. Post-training enhancements included Supervised Fine-tuning (SFT), Direct Preference Optimization (DPO) with pivotal token search, and judge-guided data, culminating in a language model that outputs probability distributions over its vocabulary to generate responses.
You can read more about Phi-4 here.
Before getting started into the process of accessing PHI 4, make sure you have the following prerequisites:
Use the following commands to install them:
pip install transformers
pip install torch
Below we’ll show you how to easily access and utilize Microsoft’s Phi-4 model on Hugging Face, enabling powerful text generation and reasoning capabilities for your applications. Follow our step-by-step instructions to get started quickly and efficiently.
To access PHI 4 and other models, you first need to create an account on Hugging Face. Visit Hugging Face’s website and sign up. After creating an account, you’ll be able to access private and public models hosted on the platform.
To access private models like PHI 4, you need to authenticate your Hugging Face account. You can use the Hugging Face CLI tool to do so:
Install the CLI tool:
pip install huggingface_hub
Log in to your Hugging Face account by running the following command:
huggingface-cli login
Enter your credentials or token when prompted.
First, ensure you have the transformers library installed. You can install it using pip:
pip install transformers
Once the library is installed, you can load the Phi-4 model using the pipeline API from Hugging Face. Here’s how you can do it:
import transformers
# Load the Phi-4 model
pipeline = transformers.pipeline(
"text-generation",
model="microsoft/phi-4",
model_kwargs={"torch_dtype": "auto"},
device_map="auto",
)
Phi-4 is optimized for chat-style prompts. You can structure your input 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?"},
]
Use the pipeline to generate responses based on your input:
outputs = pipeline(messages, max_new_tokens=128)
print(outputs[0]['generated_text'])
Output:
Phi-4 is now fully accessible on Hugging Face, making it easier than ever for developers and researchers to leverage its capabilities for various applications. Whether you’re building chatbots, educational tools, or any application requiring advanced language understanding, Phi-4 stands out as a powerful option.
For further details and updates, you can refer to the official Hugging Face documentation and explore the capabilities of this innovative model.
A. Microsoft developed Phi-4, a state-of-the-art language model, to excel in advanced reasoning and high-quality text generation. Featuring 14 billion parameters, it optimizes performance for memory and compute-constrained environments.
A. You need Python 3.7 or later, and libraries such as transformers, torch, and huggingface_hub. Ensure your machine meets the necessary compute requirements, especially for handling large models.
A. Phi-4 is ideal for text generation, advanced reasoning, chatbot development, educational tools, and any application requiring extensive language understanding and generation.
A. Microsoft optimized Phi-4 for chat-style prompts, structuring inputs as a list of messages, each with a role (e.g., system, user) and content.
A. The key features of Phi-4 are:
14 Billion Parameters : For advanced text generation
Context Length : Up to 16,000 tokens.
Safety Features: Supervised fine-tuning and preference optimization for safe interactions.
Efficiency: Optimized for memory and compute-constrained environments.