Edit

Share via


Get started building an app with Windows AI APIs

Learn about the Windows AI API hardware requirements and how to configure your device to successfully build apps using the Windows AI APIs.

Dependencies

Ensure that your PC supports Windows AI APIs and that all dependencies are installed. You can choose to do this automatically (recommended) or manually.

  1. Confirm that your device is a Copilot+ PC (we recommend the devices listed in the Copilot+ PCs developer guide).

  2. Run the following command in Windows Terminal.

    winget configure https://raw.githubusercontent.com/microsoft/winget-dsc/refs/heads/main/samples/Configuration%20files/Learn%20tutorials/Windows%20AI/learn_wcr.winget
    

    This runs a WinGet Configuration file that performs the following tasks:

    • Checks for minimum OS version.
    • Enables Developer Mode.
    • Installs Visual Studio Community Edition with WinUI and other required workloads.
    • Installs the Windows App SDK.

Build a new app

The following steps describe how to build an app that uses Windows AI APIs (select the tab for your preferred UI framework).

  1. In Visual Studio, create a new WinUI project by selecting the Blank App, Packaged (WinUI 3 in Desktop) template.

    A screenshot of the Visual Studio new Project UI with the WinUI template selected.

  2. In Solution Explorer, right-click the project node, select Properties > Application > General, and ensure that the target framework is set to .NET 8.0, and the target OS is set to 10.0.22621 or later.

    A screenshot of the Visual Studio project properties pane

  3. Edit the Package.appxmanifest file (right click and select View code) and add the following snippets.

    • The systemAIModels capability to the <Capabilities> node:

      <Capabilities>
         <systemai:Capability Name="systemAIModels"/>
      </Capabilities>
      
    • The systemai namespace specifier to "IgnorableNamespaces" in the <Package> node:

      xmlns:systemai="http://schemas.microsoft.com/appx/manifest/systemai/windows10"
      IgnorableNamespaces="uap rescap systemai"
      
    • The max version tested in the TargetDeviceFamily element of the <Dependencies> node needs to be at least 10.0.26226.0:

      <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.26226.0" />
      
  4. Add the following to your .waproj, .csproj, or .vcxproj file. This step is necessary to ensure visual studio doesn't override the max version tested.

    <AppxOSMinVersionReplaceManifestVersion>false</AppxOSMinVersionReplaceManifestVersion>
    <AppxOSMaxVersionTestedReplaceManifestVersion>false</AppxOSMaxVersionTestedReplaceManifestVersion>
    
  5. Right-click the project node and select Manage NuGet Packages....

  6. In NuGet Package Manager, check the Include prerelease checkbox, and select Windows App SDK version 1.8.250410001-experimental1. Click Install or Update.

    A screenshot of the Visual Studio nuget package manager with Microsoft.WindowsAppSDK 1.8.250410001-experimental1 selected.

  7. Ensure that your build configuration is set to ARM64.

    A screenshot of the Visual Studio build config set to ARM64.

  8. Build and run your app.

  9. If the app launches succesfully, then continue to Add your first AI API. Otherwise, see Troubleshooting.

Add your first AI API

When implementing a feature using Windows AI APIs, your app should first check for the availability of the AI model that supports that feature.

The following snippet shows how to check for model availability and generate a response.

  1. In MainWindow.xaml, add a TextBlock to display the LanguageModel response.

    <TextBlock x:Name="OutputText" HorizontalAlignment="Center" VerticalAlignment="Center" />
    
  2. At the top of MainWindow.xaml.cs, add the following using Microsoft.Windows.AI directive.

    using Microsoft.Windows.AI; 
    
  3. In MainWindow.xaml.cs, replace the MainWindow class with the following code, which confirms the LanguageModel is available and then submits a prompt asking for the model to respond with the molecular formula of glucose.

    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            InitAI();
        }
    
        private async void InitAI()
        {
            OutputText.Text = "Loading..";
    
            if (LanguageModel.GetReadyState() == AIFeatureReadyState.EnsureNeeded)
            {
                var result = await LanguageModel.EnsureReadyAsync();
                if (result.Status != PackageDeploymentStatus.CompletedSuccess)
                {
                    throw new Exception(result.ExtendedError().Message);
                }
            }            
    
            using LanguageModel languageModel = 
               await LanguageModel.CreateAsync();
    
            string prompt = "Provide the molecular formula of glucose.";
            var result = await languageModel.GenerateResponseAsync(prompt);
            OutputText.Text = result.Response;
        }
    }
    
  4. Build and run the app.

  5. The formula for glucose should appear in the text block.

Advanced tutorials and APIs

Now that you've successfully checked for model availability, explore the APIs further in the various Windows AI API tutorials.

Troubleshooting

If you encounter any errors, it's typically because of your hardware or the absence of a required model.

  • The GetReadyState method checks whether the model required by an AI feature is available on the user's device. You must call this method before any call to the model.
  • If the model isn't available on the user's device, then you can call the method EnsureReadyAsync to install the required model. Model installation runs in the background, and the user can check the install progress on the Windows Settings > Windows Update Settings page.
  • The EnsureReadyAsync method has a status option that can show a loading UI. If the user has unsupported hardware, then EnsureReadyAsync will fail with an error.

See Windows AI API troubleshooting and FAQ for more assistance.

See also