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
- Phi Silica is not available in China.
Phi Silica is a local language model that you can integrate into your Windows apps using Windows Copilot Runtime.
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.
Integrate Phi Silica
With a local Phi Silica language model you can generate text responses to user prompts. First, ensure you have the pre-requisites and models available on your device as outlined in Getting Started with Windows Copilot Runtime APIs.
Using the right namespace
To use Phi Silica, make sure you are using the correct namespace:
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.
Ensure the language model is available by calling the GetReadyState method and waiting for the EnsureReadyAsync method to return successfully.
Once the language model is available, create a LanguageModel object to reference it.
Submit a string prompt to the model using the GenerateResponseAsync method, which returns the complete result.
using Microsoft.Windows.AI.Generative; if (!LanguageModel.GetReadyState()) { var op = await LanguageModel.EnsureReadyAsync(); } 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 Microsoft.Windows.AI; using namespace winrt::Microsoft::Windows::AI::Generative; if (LanguageModel::GetReadyState() == AIFeatureReadyState::EnsureNeeded) { auto op = LanguageModel::EnsureReadyAsync().get(); } auto languageModel = LanguageModel::CreateAsync().get(); std::string prompt = "Provide the molecular formula for glucose."; auto result = languageModel.GenerateResponseAsync(prompt).get(); std::cout << result.Text() << std::endl;
The response generated by this example 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.
- 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. - Create a
ContentFilterOptions
object and specify your preferred values. - Submit a string prompt to the model using the
GenerateResponseAsync
method with theContentFilterOptions
as one of the parameters.
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);
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.
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.
Asynchronously retrieve the LanguageModelResponse in a call to GenerateResponseWithProgressAsync. Write it to the console as the response is generated.
using LanguageModel languageModel = await LanguageModel.CreateAsync(); string prompt = "Introduce yourself."; 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);
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.
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.
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. 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);
Responsible AI
We have used a combination of the following steps to ensure these imaging APIs are 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.