Phi Cookbook

repository·main·Indexed 25 days ago

https://github.com/microsoft/phicookbook

A collection of hands-on examples and resources for working with Microsoft's Phi series of Small Language Models (SLMs). It provides guides for fine-tuning Phi-3 and Phi-4-mini using Microsoft Olive, optimizing models for CPU, GPU, and NPU via ONNX Runtime, and implementing generative AI applications such as RAG chatbots with WebGPU, Prompt Flow, and Azure AI Search.

Tokens
124.8K
Snippets
284
Records
478
Agent score
83%

What's inside Phi Cookbook

  1. Overview of Phi-3 Fine-tuning and Prompt flow Integration

    main

    This end-to-end (E2E) workflow demonstrates how to fine-tune, deploy, and integrate custom Phi-3 models using Azure Machine Learning and Prompt flow. The process is divided into three main scenarios:

    1. Scenario 1: Set up Azure resources and Prepare for fine-tuning: Includes creating an Azure Machine Learning Workspace, requesting GPU quotas, configuring role assignments, and preparing datasets.
    2. Scenario 2: Fine-tune the Phi-3 model and Deploy in Azure Machine Learning Studio: Involves setting up the Azure CLI, executing the fine-tuning process, and deploying the resulting model.
    3. Scenario 3: Integrate with Prompt flow and Chat with your custom model: Focuses on connecting the deployed model to Prompt flow for interactive chat capabilities.
  2. Overview of Phi-3 WebGPU RAG Chatbot

    main
    The Phi-3-mini WebGPU RAG Chatbot demonstrates the Retrieval-Augmented Generation (RAG) pattern using Phi-3 models hosted via ONNX. This pattern combines the capabilities of Phi-3 models (such as Phi-3-mini, Phi-3-small, and Phi-3-medium) with ONNX hosting to create efficient AI deployments. This approach is particularly useful for fine-tuning models for domain-specific tasks, balancing quality, cost-effectiveness, and long-context understanding. These models are available in the Azure AI Model Catalog and can be fine-tuned and deployed via platforms like HuggingFace and ONNX.
  3. Overview of Phi .NET Laboratory Projects

    main

    The Phi cookbook for .NET includes several laboratory projects demonstrating different model capabilities and library integrations.

    Phi-3 / Phi-3.5 Projects

    • LabsPhi301: Console chat using Microsoft.ML.OnnxRuntime.
    • LabsPhi302: Console chat using Microsoft.Semantic.Kernel.
    • LabPhi303: Image analysis using Phi-3 Vision and Microsoft.ML.OnnxRuntime.
    • LabPhi304: Image analysis with a menu for user interaction using Microsoft.ML.OnnxRuntime.

    Phi-4 Projects

    • LabPhi4-Chat: Console chat using Microsoft.ML.OnnxRuntime.
    • LabPhi-4-SK: Console chat using Semantic Kernel.
    • LabsPhi4-Chat-03GenAIChatClient: Console chat using Microsoft.ML.OnnxRuntimeGenAI and implementing IChatClient from Microsoft.Extensions.AI.
    • LabsPhi4-Chat-04-ChatMode: Console chat implementing chat memory.
    • Phi-4multimodal-vision: Image analysis using Phi-4 multimodal and Microsoft.ML.OnnxRuntime.
    • LabPhi4-MM-Audio: Audio file analysis and transcription using Phi-4 multimodal and Microsoft.ML.OnnxRuntime.
  4. Overview of Phi Labs for .NET

    main

    The Phi labs provide sample projects demonstrating how to integrate Phi models into .NET applications using various libraries.

    Available Labs

    ProjectModelDescription
    LabsPhi301Phi-3/3.5Console chat using Microsoft.ML.OnnxRuntime
    LabsPhi302Phi-3/3.5Console chat using Microsoft.Semantic.Kernel
    LabPhi303Phi-3/3.5Image analysis using Microsoft.ML.OnnxRuntime
    LabPhi304Phi-3/3.5Image analysis with interactive menu using Microsoft.ML.OnnxRuntime
    LabPhi4-ChatPhi-4Console chat using Microsoft.ML.OnnxRuntime
    LabPhi-4-SKPhi-4Console chat using Semantic Kernel
    LabsPhi4-Chat-03GenAIChatClientPhi-4Console chat using Microsoft.ML.OnnxRuntimeGenAI and Microsoft.Extensions.AI.IChatClient
    LabsPhi4-Chat-04-ChatModePhi-4Console chat with memory capabilities
    Phi-4multimodal-visionPhi-4Image analysis using Microsoft.ML.OnnxRuntime
    LabPhi4-MM-AudioPhi-4Audio transcription and analysis using Microsoft.ML.OnnxRuntime
  5. Overview of Olive Model Optimization

    main

    Olive (ONNX live) is a model optimization toolkit with a CLI designed to deliver models for the ONNX runtime with improved quality and performance. It optimizes models (typically PyTorch or Hugging Face) for specific deployment targets like NPUs, GPUs, or CPUs (e.g., Qualcomm, AMD, Nvidia, or Intel).

    Key Features:

    • Automated Workflows: Uses a sequence of passes (compression, quantization, graph optimization) and an auto-tuning search strategy to find the best model based on accuracy and latency constraints.
    • Built-in Components: Over 40 optimization components for quantization, compression, and fine-tuning.
    • CLI Commands: Supports olive quantize, olive auto-opt, and olive finetune.
    • Deployment Support: Supports Multi LoRA serving, model packaging, and integration with Hugging Face and Azure AI.
  6. Overview of Phi Labs Projects

    main

    The solution contains several sample labs demonstrating Phi model capabilities in C# using different libraries:

    ProjectModelDescription
    LabsPhi301Phi-3/3.5Console chat using Microsoft.ML.OnnxRuntime
    LabsPhi302Phi-3/3.5Console chat using Microsoft.Semantic.Kernel
    LabPhi303Phi-3/3.5Image analysis using Microsoft.ML.OnnxRuntime
    LabPhi304Phi-3/3.5Image analysis with interactive menu using Microsoft.ML.OnnxRuntime
    LabPhi4-ChatPhi-4Console chat using Microsoft.ML.OnnxRuntime
    LabPhi-4-SKPhi-4Console chat using Semantic Kernel
    LabsPhi4-Chat-03GenAIChatClientPhi-4Console chat using Microsoft.ML.OnnxRuntimeGenAI and IChatClient from Microsoft.Extensions.AI
    LabsPhi4-Chat-04-ChatModePhi-4Console chat with memory implementation
    Phi-4multimodal-visionPhi-4Image analysis using Microsoft.ML.OnnxRuntime
    LabPhi4-MM-AudioPhi-4Audio transcription and analysis using Microsoft.ML.OnnxRuntime
  7. Overview of Microsoft Olive for Phi-3 Fine-tuning

    main

    Microsoft Olive is a hardware-aware model optimization tool designed to simplify the optimization of machine learning models for specific hardware architectures (cloud or edge). It supports both fine-tuning and inference for generative AI models like Phi-3.

    Key capabilities include:

    • Automated Optimization: Integrates and automates techniques tailored for specific hardware targets.
    • End-to-End Solution: Considers constraints such as accuracy and latency while optimizing models.
    • Extensibility: Allows users to plug in custom optimization innovations.
    • Multi-Runtime Support: Works with AzureML, local GPU, CPU, and DirectML.
  8. Overview of Phi Labs .NET projects

    main

    The Phi Labs collection contains several C# projects demonstrating Phi model capabilities using different libraries:

    ProjectModelDescription
    LabsPhi301Phi-3/3.5Console chat using Microsoft.ML.OnnxRuntime
    LabsPhi302Phi-3/3.5Console chat using Microsoft.Semantic.Kernel
    LabPhi303Phi-3/3.5Image analysis using Microsoft.ML.OnnxRuntime
    LabPhi304Phi-3/3.5Image analysis with menu options using Microsoft.ML.OnnxRuntime
    LabPhi4-ChatPhi-4Console chat using Microsoft.ML.OnnxRuntime
    LabPhi-4-SKPhi-4Console chat using Semantic Kernel
    LabsPhi4-Chat-03GenAIChatClientPhi-4Console chat using Microsoft.ML.OnnxRuntimeGenAI and IChatClient from Microsoft.Extensions.AI
    LabsPhi4-Chat-04-ChatModePhi-4Console chat with memory implementation
    Phi-4multimodal-visionPhi-4Image analysis using Microsoft.ML.OnnxRuntime
    LabPhi4-MM-AudioPhi-4Audio analysis and transcription using Microsoft.ML.OnnxRuntime
  9. Overview of Microsoft Olive

    main

    Olive (ONNX live) is a model optimization toolkit and CLI designed to ensure quality and efficiency when deploying models for ONNX Runtime.

    Key Capabilities

    • Input/Output: Accepts PyTorch or Hugging Face models as input and outputs optimized ONNX models tailored for specific deployment targets (NPU, GPU, CPU) from vendors like Qualcomm, AMD, Nvidia, or Intel.
    • Workflow Engine: Executes a sequence of optimization tasks called passes (e.g., model compression, graph capture, quantization, graph optimization).
    • Auto-tuning: Uses a search strategy and algorithms to automatically tune pass parameters to achieve optimal metrics for accuracy and latency.

    Core Benefits

    • Automated Optimization: Reduces manual effort by automatically finding the best model based on your defined quality and efficiency constraints.
    • Extensive Components: Includes 40+ built-in model optimization components for quantization, compression, and fine-tuning.
    • Simplified CLI: Provides easy commands for common tasks like olive quantize, olive auto-opt, and olive finetune.
    • Deployment Ready: Supports model packaging, deployment, and Multi-LoRA serving.
    • Integrations: Native support for Hugging Face, Azure AI, and YAML/JSON-based workflow orchestration.
    • Efficiency: Built-in caching mechanism to reduce costs.
  10. Overview of Olive Model Optimization Toolkit

    main

    Olive (ONNX live) is a model optimization toolkit with a CLI designed to prepare models for the ONNX Runtime. It optimizes models for various deployment targets (NPU, GPU, CPU) provided by hardware vendors like Qualcomm, AMD, Nvidia, or Intel.

    Key Features:

    • Workflows: Executes an ordered sequence of optimization passes (e.g., compression, quantization, graph optimization).
    • Auto-tuning: Uses search algorithms to automatically find the best parameters for accuracy and latency.
    • Components: Over 40 built-in components for quantization, compression, and fine-tuning.
    • Integrations: Supports Hugging Face, Azure AI, and Multi-LoRA serving.
    • Efficiency: Includes a built-in caching mechanism to reduce costs.
  11. Overview of Olive (ONNX live) optimization toolkit

    main

    Olive is a model optimization toolkit with a CLI designed to deliver models for the ONNX runtime with improved quality and performance. It optimizes models (typically PyTorch or Hugging Face) for specific deployment targets like NPUs, GPUs, or CPUs (e.g., Qualcomm, AMD, Nvidia, or Intel).

    Key features include:

    • Automated Workflows: Uses a sequence of passes (compression, quantization, graph optimization) and a search strategy to auto-tune parameters for accuracy and latency.
    • Built-in Components: Over 40 optimization components for quantization, compression, and fine-tuning.
    • CLI Commands: Provides olive quantize, olive auto-opt, and olive finetune.
    • Integration: Supports Hugging Face, Azure AI, and Multi LoRA serving.
    • Efficiency: Includes a caching mechanism to reduce costs.
  12. Overview of Olive (ONNX live) model optimization

    main

    Olive is a model optimization toolkit with a CLI designed to help ship models for the ONNX Runtime with high quality and performance. It takes PyTorch or Hugging Face models as input and produces optimized ONNX models for deployment on various AI accelerators (NPU, GPU, CPU) provided by vendors like Qualcomm, AMD, Nvidia, or Intel.

    Key Features:

    • Automated Workflows: Executes ordered sequences of optimization tasks called passes (e.g., model compression, graph capture, quantization, graph optimization).
    • Auto-tuning: Uses search algorithms to automatically find the best parameters for each pass based on accuracy and latency metrics.
    • Built-in Components: Over 40 components for quantization, compression, and fine-tuning.
    • Integration: Supports Hugging Face, Azure AI, and Multi-LoRA serving.
    • Efficiency: Includes a caching mechanism to save costs during optimization.