Language
Edit

Tutorial: Build a chat app with Phi Silica and .NET MAUI

Important

This tutorial uses Windows App SDK 2.6.4, which does not require a LanguageModel LAF token or unlock call. Apps using earlier SDK versions should retain their existing legacy unlock flow, or upgrade to Windows App SDK 2.6.4 or later. See legacy LAF setup in the WinUI tutorial and LAF compatibility.

This short tutorial walks through the Windows AI API sample for .NET MAUI.

Note

Phi Silica features are not available in China.

Prerequisites

  • Copilot+ PC with NPU — required for Phi Silica. See the Copilot+ PCs developer guide.
  • Windows 11 build 26100 or later (25H2) — check with winver.
  • Developer Mode enabled — Windows Settings → System → For developers → Developer Mode.
  • Visual Studio 2022 with the Windows application development workload.
  • .NET MAUI development tools for the sample's .NET target framework, with Windows support.
  • Windows App SDK 2.6.4 — use Microsoft.WindowsAppSDK version 2.6.4 as described below.

Complete the platform-specific steps for .NET MAUI described in the Get started building an app with Windows AI APIs.

Introduction

This sample shows how to use various Windows AI APIs, including LanguageModel for text generation and ImageScaler for scaling and sharpening images.

The sample includes the following four files:

  1. MauiWindowsAISample.csproj: Adds the required Windows App SDK package reference for the Windows AI APIs and sets the necessary TargetFramework for Windows.
  2. Platforms/Windows/MainPage.cs: Implements partial methods from the shared MainPage class that show and handle the text generation and image scaling functionality.
  3. MainPage.xaml: Defines controls for showing text generation and image scaling.
  4. MainPage.xaml.cs: Defines partial methods that MainPage.cs implements.

In the second file listed above, you'll find the following function, which demonstrates text summarization functionality.

  1. Create a LanguageModel instance (languageModel).
  2. Build a summarization prompt by prepending an instruction to the input text.
  3. Pass the prompt to the GenerateResponseAsync method and print the result.
using Microsoft.Windows.AI; 
using Microsoft.Windows.AI.Text;
 
using LanguageModel languageModel = await LanguageModel.CreateAsync(); 
 
string inputText = "This is a large amount of text I want to have summarized.";
string prompt = $"Summarize the following text concisely:\n\n{inputText}";
 
var result = await languageModel.GenerateResponseAsync(prompt); 
 
Console.WriteLine(result.Text); 
using namespace winrt::Microsoft::Windows::AI::Text;

auto languageModel = LanguageModel::CreateAsync().get();

std::wstring inputText = L"This is a large amount of text I want to have summarized.";
std::wstring prompt = L"Summarize the following text concisely:\n\n" + inputText;

auto result = languageModel.GenerateResponseAsync(prompt).get();

std::wcout << result.Text() << std::endl;

Note

The examples above use prompt engineering: they prepend a summarization instruction to the input text. For the dedicated text transformation APIs, see Text Intelligence Skills.

Build and run the sample

  1. Clone the WindowsAppSDK-Samples repo, use the main branch, and navigate to the Samples/WindowsAIFoundry/cs-maui folder.

  2. Open MauiWindowsAISample.sln in Visual Studio 2022.

  3. In MauiWindowsAISample.csproj, set the Windows-only Microsoft.WindowsAppSDK reference to version 2.6.4. The repository uses Central Package Management, so use VersionOverride to update this sample without changing other samples:

    <PackageReference Include="Microsoft.WindowsAppSDK" VersionOverride="2.6.4" />
    
  4. Restore NuGet packages. Keep the sample's package identity and systemAIModels capability; no LanguageModel token configuration or TryUnlockFeature call is needed.

  5. On an ARM64 Copilot+ PC, select ARM64 as the build architecture and ensure the debug toolbar has "Windows Machine" set as the target device.

  6. Press F5 or select "Start Debugging" from the Debug menu to run the sample (the sample can also be run without debugging by selecting "Start Without Debugging" from the Debug menu or Ctrl+F5).

  7. Click one of the "Scale" buttons to scale the image, or enter a text prompt and click the "Generate" button to generate a text response.

See also