Edit

Share via


Get started with Phi Silica in the Windows App SDK

Important

Available in the latest experimental channel release of the Windows App SDK.

The Windows App SDK experimental channel includes APIs and features in early stages of development. All APIs in the experimental channel are subject to extensive revisions and breaking changes and may be removed from subsequent releases at any time. Experimental features are not supported for use in production environments and apps that use them cannot be published to the Microsoft Store.

  • Phi Silica is not available in China.
  • Unpackaged apps are not supported.

Phi Silica is a local language model that you can integrate into your Windows apps using the Windows App SDK.

As Microsoft's most powerful NPU-tuned local language model, Phi Silica is optimized for efficiency and performance on Windows Copilot+ PCs devices while still offering many of the capabilities found in Large Language Models (LLMs).

This level of optimization is exclusive to the model within the Windows App SDK and is not available in other versions of Phi.

For API details, see API ref for Phi Silica in the Windows App SDK.

For content moderation details, see Content safety with generative AI APIs.

Tip

Provide feedback on these APIs and their functionality by creating a new Issue in the Windows App SDK GitHub repo (include Phi Silica in the title) or by responding to an existing issue.

Prerequisites

Use the Windows App SDK to integrate Phi Silica into your Windows app

With a local Phi Silica language model and the Windows App SDK you can generate text responses to user prompts.

Generate a complete response

This example shows how to generate a response to a Q&A prompt where the full response is generated before the result is returned.

  1. Ensure the language model is available by calling the IsAvailable method and waiting for the MakeAvailableAsync method to return successfully.

  2. Once the language model is available, create a LanguageModel object to reference it.

  3. Submit a string prompt to the model using the GenerateResponseAsync method, which returns the complete result.

using Microsoft.Windows.AI.Generative; 
 
 
if (!LanguageModel.IsAvailable()) 
{ 
   var op = await LanguageModel.MakeAvailableAsync(); 
} 
 
using LanguageModel languageModel = await LanguageModel.CreateAsync(); 
 
string prompt = "Provide the molecular formula for glucose."; 
 
var result = await languageModel.GenerateResponseAsync(prompt); 
 
Console.WriteLine(result.Response); 
using namespace winrt::Microsoft::Windows::AI::Generative;

if (!LanguageModel::IsAvailable()) 
{
    auto op = LanguageModel::MakeAvailableAsync().get();
}

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

std::string prompt = "Provide the molecular formula for glucose.";

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

std::cout << result.Response() << std::endl;

The response generated by this example is:

The molecular formula for glucose is C6H12O6.

Generate a complete response

Our API has built in content moderation which is customizable. This example shows how to specify your own thresholds for the internal content moderation. Learn more about Content Moderation with Windows Copilot Runtime.

  1. Create a LanguageModel object to reference the local language model. *A check has already been performed to ensure the Phi Silica language model is available on the user's device in the previous snippet.
  2. Create a ContentFilterOptions object and specify your preferred values.
  3. Submit a string prompt to the model using the GenerateResponseAsync method with the ContentFilterOptions as one of the parameters.
using Microsoft.Windows.AI.Generative; 

using LanguageModel languageModel = await LanguageModel.CreateAsync(); 
 
string prompt = "Provide the molecular formula for glucose."; 

ContentFilterOptions filterOptions = new ContentFilterOptions();
filterOptions.PromptMinSeverityLevelToBlock.ViolentContentSeverity = SeverityLevel.Medium;
filterOptions.ResponseMinSeverityLevelToBlock.ViolentContentSeverity = SeverityLevel.Medium;
 
// var result = await languageModel.GenerateResponseAsync(null, prompt, filterOptions); 
 
Console.WriteLine(result.Response);
using namespace winrt::Microsoft::Windows::AI::Generative;

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

std::string prompt = "Provide the molecular formula for glucose.";

ContentFilterOptions contentFilter = ContentFilterOptions(); 
contentFilter.PromptMinSeverityLevelToBlock().ViolentContentSeverity(SeverityLevel::Medium); 
contentFilter.ResponseMinSeverityLevelToBlock().ViolentContentSeverity(SeverityLevel::Medium); 

// auto result = languageModel.GenerateResponseAsync(nullptr, prompt, filterOptions).get();

std::cout << result.Response() << std::endl;

Generate a stream of partial responses

This example shows how to generate a response to a Q&A prompt where the response is returned as a stream of partial results.

  1. Create a LanguageModel object to reference the local language model. *A check has already been performed to ensure the Phi Silica language model is available on the user's device in the previous snippet.

  2. Asynchronously retrieve the LanguageModelResponse in a call to GenerateResponseWithProgressAsync. Write it to the console as the response is generated.

using Microsoft.Windows.AI.Generative; 

using LanguageModel languageModel = await LanguageModel.CreateAsync(); 
 
string prompt = "Provide the molecular formula for glucose."; 
 
AsyncOperationProgressHandler<LanguageModelResponse, string> 
progressHandler = (asyncInfo, delta) => 
{ 
    Console.WriteLine($"Progress: {delta}"); 
    Console.WriteLine($"Response so far: {asyncInfo.GetResults().Response()}"); 
 }; 
 
var asyncOp = languageModel.GenerateResponseWithProgressAsync(prompt); 
 
asyncOp.Progress = progressHandler; 
 
var result = await asyncOp;  
 
Console.WriteLine(result.Response);
using namespace winrt::Microsoft::Windows::AI::Generative;

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

std::string prompt = "Provide the molecular formula for glucose.";

AsyncOperationProgressHandler<LanguageModelResponse, std::string> progressHandler = 
    [](const IAsyncOperationWithProgress<LanguageModelResponse, std::string>& asyncInfo, const std::string& delta) 
    { 
        std::cout << "Progress: " << delta << std::endl; 
        std::cout << "Response so far: " << asyncInfo.GetResults().Response() << std::endl; 
    };

auto asyncOp = languageModel.GenerateResponseWithProgressAsync(prompt);

asyncOp.Progress(progressHandler); 

auto result = asyncOp.get();

std::cout << result.Response() << std::endl;

Apply predefined text formats for more consistent responses in your app

Phi Silica includes the ability to predefine text response formats for use in your app. Predefining a text format can provide more consistent response results with the following options:

  • Text to Table: Convert the prompt response into a table format.
  • Summarize: Return a summary based on the prompt text.
  • Rewrite: Rephrase the prompt text to add clarity and express the response in a more easily understood way.
  1. Create a LanguageModel object to reference the local language model. *A check has already been performed to ensure the Phi Silica language model is available on the user's device in the previous snippet.

  2. Create a LanguageModelOptions object and specify the predefined text format to use by assigning a LanguageModelSkill enum to the Skill field of the LanguageModelOptions object. The following values are available for the LanguageModelSkill enum.

    Enum Description
    LanguageModelSkill.General Default value, no predefined formatting applied.
    LanguageModelSkill.TextToTable Convert prompt text into a table if applicable.
    LanguageModelSkill.Summarize Return a summary based on the prompt text.
    LanguageModelSkill.Rewrite Rewrite the prompt text response to improve clarity and comprehension.
  3. Then we asynchronously retrieve the LanguageModelResponse in a call to GenerateResponseWithProgressAsync and write it to the console as the response is generated.

using Microsoft.Windows.AI.Generative; 
 
using LanguageModel languageModel = await LanguageModel.CreateAsync(); 
 
string prompt = "This is a large amount of text I want to have summarized.";

LanguageModelOptions options = new LanguageModelOptions {
    Skill = LanguageModelSkill.Summarize
};
 
var result = await languageModel.GenerateResponseAsync(options, prompt); 
 
Console.WriteLine(result.Response); 
using namespace winrt::Microsoft::Windows::AI::Generative;

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

std::string prompt = "This is a large amount of text I want to have summarized.";

LanguageModelOptions options = LanguageModelOptions();
options.Skill = LanguageModelSkill.Summarize;

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

std::cout << result.Response() << std::endl;

Responsible AI

Phi Silica provides developers with a powerful, trustworthy model for building apps with safe, secure AI experiences. The following steps have been taken to ensure Phi Silica is trustworthy, secure, and built responsibly. We recommend reviewing the best practices described in Responsible Generative AI Development on Windows when implementing AI features in your app.

  • Thorough testing and evaluation of the model quality to identify and mitigate potential risks.
  • Incremental roll out of Phi Silica experimental releases. Following the final Phi Silica experimental release, the roll out will expand to signed apps to ensure that malware scans have been applied to applications with local model capabilities.
  • Phi Silica provides a localized AI model that includes a Text Content Moderation API. This API identifies and filters potentially harmful content in both the input and AI-generated output. The local text content moderation model is based on the Azure AI Content Safety model for content moderation and provides similar performance. See Text Content Moderation with Windows Copilot Runtime for a description of severity level filter options and a code sample demonstrating how to implement these options.

Important

No content safety system is infallible and occasional errors can occur, so we recommend integrating supplementary Responsible AI (RAI) tools and practices. For more details, see Responsible Generative AI Development on Windows.