AI on Windows: Chatting with Phi Silica

Last year Microsoft introduced Phi Silica and recently made an experimental release of the Windows App Sdk that can be used by Windows application to interact with Phi Silica. In this post we’ll connect an Uno Platform application to Phi Silica.

We’ll keep things simple and start with the Blank preset in the Uno Platform template wizard and add Mvvm support under the Presentation section.

In order to access the Phi Silica apis we’ll first need to update to the experimental release of the Windows App Sdk. With Uno, this is done by setting the WinAppSdkVersion property to the experimental version, 1.7.250127003-experimental3.

<PropertyGroup Condition="'$(TargetFramework)'=='net9.0-windows10.0.26100'">
    <WinAppSdkVersion>1.7.250127003-experimental3</WinAppSdkVersion>
</PropertyGroup>

The Phi Silica apis won’t appear whilst the Platform of the project is set to AnyCPU. To resolve this, we need to set both the Platforms and RuntimeIdentifiers properties, when the application is targeting Windows.

<PropertyGroup Condition="'$(TargetFramework)'=='net9.0-windows10.0.26100'">
    <Platforms>x86;x64;ARM64</Platforms>
    <RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
    <WinAppSdkVersion>1.7.250127003-experimental3</WinAppSdkVersion>
</PropertyGroup>

After making this adjustment, open Configuration Manager and make sure the Platform value is set to the correct architecture.

Let’s add a MainViewModel that will be used to send a message to Phi Silica and get a response.

using Microsoft.UI.Xaml.Data;
#if WINDOWS
using Microsoft.Windows.AI.Generative;
#endif

namespace PhiMessagingApp;

[Bindable]
public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private string _response = string.Empty;

    [RelayCommand]
    public async Task SendMessage(string message)
    {
#if WINDOWS

        if (!LanguageModel.IsAvailable())
        {
            var op = await LanguageModel.MakeAvailableAsync();
        }

        using LanguageModel languageModel = await LanguageModel.CreateAsync();
        var result = await languageModel.GenerateResponseAsync(message);
        Response = result.Response;
#else
        Response = "Design-time response....";
#endif
    }
}

Next we’ll set an instance of the MainViewModel as the DataContext on the MainPage.

public MainPage()
{
    this.InitializeComponent();
    DataContext = new MainViewModel();
}

Whilst the Phi Silica APIs only work on Windows, for the purpose of designing the interface we’ll switch target to Desktop.

We’ll run the application and should see both Hot Design and Hot Reload buttons in the floating toolbar.

Clicking the Hot Design (left) button will enter the design experience. We’ll keep things simple by adding a TextBox (message input), Button (to invoke the SendMessage method) and TextBlock (response output). The following video shows adding these elements along with databinding the elements.

Now, with everything wired up we can switch back to the PhiMessagingApp (WinAppSDK Packaged) target.

Unfortunately, at this point you’ll likely run into the following exception:

System.Runtime.InteropServices.COMException: 'Interface not registered
Failed to find proxy registration for IID: {EBF3748D-1EF9-4B89-BA13-65D2B9EC04F0}.'

This exception is a result of the application running as self-contained, which is not supported by the experimental release, as noted in the docs.

Luckily, there’s a quick fix. We can adjust the WindowsAppSDKSelfContained property to false, which will resolve this issue.

<PropertyGroup Condition="'$(TargetFramework)'=='net9.0-windows10.0.26100'">
    <Platforms>x86;x64;ARM64</Platforms>
    <RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
    <WinAppSdkVersion>1.7.250127003-experimental3</WinAppSdkVersion>
    <WindowsAppSDKSelfContained>false</WindowsAppSDKSelfContained>
</PropertyGroup>

Let’s go ahead and run the application and enter a prompt, “Why is the sky blue”, and see the response.

As you can see from this post, it’s easy to reference Phi Silca from an Uno Platform application. Whilst the APIs are for Windows only, this can be used to light up your cross platform application with locally executing AI when running on Windows.

Leave a comment