In April 2024, Microsoft unveiled a new family of small language models (SLMs) known as Phi-3. These models are designed to deliver high performance in a compact and cost-effective package, catering to a wide range of applications from language understanding and coding to mathematical reasoning. Phi-3 models stand out by offering better performance compared to other models of similar size and even larger ones. This article will explore what Phi-3 models are, how developers can use them, and their potential applications on iPhone and Android platforms.
What are Phi-3 Small Language Models?
Phi-3 models are advanced AI models developed by Microsoft, focusing on providing superior performance in a smaller footprint. These models are designed to be cost-effective and efficient, making them accessible for various applications. The Phi-3 family includes:
- Phi-3-Mini: A 3.8 billion parameter model.
- Phi-3-Small: A 7 billion parameter model.
- Phi-3-Medium: A 14 billion parameter model.
These models support context lengths up to 128,000 tokens, enabling them to handle complex tasks that require understanding and processing large amounts of information. They are instruction-tuned, which means they are trained to follow different types of instructions, making them versatile and easy to use out of the box (Microsoft Azure).
How Developers Can Use Phi-3 Models
Phi-3 models are available on multiple platforms, including Microsoft Azure AI, Hugging Face, and Ollama. This availability provides flexibility for developers to deploy and utilize these models in various environments, including cloud and local setups.
Using Phi-3 Models on Azure AI
To get started with Phi-3 models on Azure AI, developers need to follow these steps:
Step 1: Set Up Azure Account:
pythonCopy code# Install Azure AI SDK !pip install azure-ai
Step 2: Authenticate and Initialize:
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
key = "your_key_here"
endpoint = "https://your_endpoint_here.cognitiveservices.azure.com/"
client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
Step 3: Use the Model:
documents = ["Hello, how can I help you today?"]
response = client.analyze_sentiment(documents=documents)
for doc in response:
print(f"Sentiment: {doc.sentiment}, Confidence Scores: {doc.confidence_scores}")
Using Phi-3 Models on Hugging Face
Hugging Face provides an intuitive interface for using Phi-3 models. Here’s how to use it:
- Install Transformers Library:
!pip install transformers
- Load and Use the Model:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-3-mini")
model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/phi-3-mini")
inputs = tokenizer("Translate English to French: Hello, how are you?", return_tensors="pt")
outputs = model.generate(**inputs)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Potential Applications and Best Use Cases
Phi-3 models excel in various applications due to their high performance and efficiency. Some of the best use cases include:
- Natural Language Processing (NLP): Tasks like sentiment analysis, text generation, translation, and summarization can benefit from Phi-3 models.
- Coding Assistance: These models can assist in code completion, bug detection, and automated documentation.
- Math and Reasoning: Phi-3 models can handle complex mathematical problems and logical reasoning tasks, making them useful in educational tools and research.
Using Phi-3 Models on iPhone and Android
To leverage Phi-3 models on mobile platforms, developers can use frameworks like TensorFlow Lite or ONNX Runtime, which support running models on both iOS and Android devices.
iPhone
Step 1: Convert Model to Core ML:
# Convert the model using coremltools
import coremltools as ct
model = ct.convert("path_to_phi-3_model.onnx", source="onnx")
model.save("phi-3-mini.mlmodel"
Step2: Integrate with iOS App:
import CoreML
import Vision
let model = try! VNCoreMLModel(for: phi_3_mini().model)
let request = VNCoreMLRequest(model: model) { (request, error) in
guard let results = request.results as? [VNClassificationObservation],
let topResult = results.first else {
return
}
print("Classification: \(topResult.identifier), Confidence: \(topResult.confidence)")
}
let handler = VNImageRequestHandler(cgImage: image, options: [:])
try? handler.perform([request])
Android
Step1: Convert Model to TensorFlow Lite:
# Convert the model to TensorFlow Lite format
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model("path_to_phi-3_model")
tflite_model = converter.convert()
with open("phi-3-mini.tflite", "wb") as f:
f.write(tflite_model)
Step2: Integrate with Android App:
import org.tensorflow.lite.Interpreter;
Interpreter tflite = new Interpreter(loadModelFile());
// Define input and output tensors
float[][] input = new float[1][128];
float[][] output = new float[1][128];
tflite.run(input, output);
Conclusion
The Phi-3 small language models by Microsoft represent a significant advancement in AI technology, offering high performance in a compact and cost-effective form. By leveraging these models, developers can enhance their applications with advanced language understanding, coding assistance, and reasoning capabilities. With availability on multiple platforms and support for mobile integration, Phi-3 models provide a versatile solution for a wide range of applications in both the cloud and on-device environments.