Language
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.WindowsAppSDKversion2.6.4as 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:
- MauiWindowsAISample.csproj: Adds the required Windows App SDK package reference for the Windows AI APIs and sets the necessary TargetFramework for Windows.
- Platforms/Windows/MainPage.cs: Implements partial methods from the shared MainPage class that show and handle the text generation and image scaling functionality.
- MainPage.xaml: Defines controls for showing text generation and image scaling.
- 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.
- Create a LanguageModel instance (
languageModel). - Build a summarization prompt by prepending an instruction to the input text.
- 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
Clone the WindowsAppSDK-Samples repo, use the
mainbranch, and navigate to the Samples/WindowsAIFoundry/cs-maui folder.Open MauiWindowsAISample.sln in Visual Studio 2022.
In
MauiWindowsAISample.csproj, set the Windows-onlyMicrosoft.WindowsAppSDKreference to version2.6.4. The repository uses Central Package Management, so useVersionOverrideto update this sample without changing other samples:<PackageReference Include="Microsoft.WindowsAppSDK" VersionOverride="2.6.4" />Restore NuGet packages. Keep the sample's package identity and
systemAIModelscapability; no LanguageModel token configuration orTryUnlockFeaturecall is needed.On an ARM64 Copilot+ PC, select ARM64 as the build architecture and ensure the debug toolbar has "Windows Machine" set as the target device.
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).
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.