Summarize text using AI chat app with .NET
Get started with AI by creating a simple .NET 8.0 console chat application to summarize text. The application runs locally and uses the OpenAI gpt-3.5-turbo
model. Follow these steps to get access to OpenAI and learn how to use Semantic Kernel.
Prerequisites
- .NET 8.0 SDK - Install the .NET 8.0 SDK.
- An API key from OpenAI so you can run this sample.
- On Windows, PowerShell
v7+
is required. To validate your version, runpwsh
in a terminal. It should return the current version. If it returns an error, execute the following command:dotnet tool update --global PowerShell
.
Get started with AI by creating a simple .NET 8.0 console chat application to summarize text. The app runs locally and connects to the OpenAI gpt-35-turbo
model deployed into Azure OpenAI. Follow these steps to provision the Azure OpenAI service and learn how to use Semantic Kernel.
Prerequisites
- .NET 8 SDK - Install the .NET 8 SDK.
- An Azure subscription - Create one for free.
- Azure Developer CLI - Install or update the Azure Developer CLI.
- Access to Azure OpenAI service.
- On Windows, PowerShell
v7+
is required. To validate your version, runpwsh
in a terminal. It should return the current version. If it returns an error, execute the following command:dotnet tool update --global PowerShell
.
Get the sample project
Clone the GitHub repository that contains the sample apps for all of the quickstarts:
git clone https://github.com/dotnet/ai-samples.git
Create the Azure OpenAI service
The sample GitHub repository is structured as an Azure Developer CLI (azd
) template, which azd
can use to provision the Azure OpenAI service and model for you.
From a terminal or command prompt, navigate to the src\quickstarts\azure-openai directory of the sample repo.
Run the
azd up
command to provision the Azure OpenAI resources. It might take several minutes to create the Azure OpenAI service and deploy the model.azd up
azd
also configures the required user secrets for the sample app, such as the OpenAI access key.Note
If you encounter an error during the
azd up
deployment, visit the troubleshooting section.
Try the hiking benefits sample
From a terminal or command prompt, navigate to the
openai\01-HikeBenefitsSummary
directory.Run the following commands to configure your OpenAI API key as a secret for the sample app:
dotnet user-secrets init dotnet user-secrets set OpenAIKey <your-openai-key>
Use the
dotnet run
command to run the app:dotnet run
From a terminal or command prompt, navigate to the
azure-openai\01-HikeBenefitsSummary
directory.Use the
dotnet run
command to run the app:dotnet run
Tip
If you get an error message, the Azure OpenAI resources may not have finished deploying. Wait a couple of minutes and try again.
Explore the code
The app uses the Microsoft.SemanticKernel
package to send and receive requests to the OpenAI service.
The Program.cs file contains all of the app code. The first several lines of code set configuration values and get the OpenAI Key that was previously set using the dotnet user-secrets
command.
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string model = "gpt-3.5-turbo";
string key = config["OpenAIKey"];
The Kernel
class facilitates the requests and responses and registers an OpenAIChatCompletion
service.
// Create a Kernel containing the OpenAI Chat Completion Service
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(model, key)
.Build();
The application uses the Microsoft.SemanticKernel
package to send and receive requests to the Azure OpenAI service.
The Program.cs file contains all of the app code. The first several lines of code load secrets and configuration values that were set in the dotnet user-secrets
for you during the application provisioning.
// Retrieve the local secrets saved during the Azure deployment
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
string deployment = config["AZURE_OPENAI_GPT_NAME"];
string key = config["AZURE_OPENAI_KEY"];
The Kernel
class facilitates the requests and responses and registers an OpenAIChatCompletion
service.
// Create a Kernel containing the Azure OpenAI Chat Completion Service
Kernel kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(deployment, endpoint, key)
.Build();
Once the Kernel
is created, the app code reads the benefits.md
file content and uses it to create a prompt
for model. The prompt instructs the model to summarize the file text content.
// Create and print out the prompt
string prompt = $"""
Please summarize the the following text in 20 words or less:
{File.ReadAllText("benefits.md")}
""";
Console.WriteLine($"user >>> {prompt}");
The InvokePromptAsync
function sends the prompt
to the model to generate a response.
// Submit the prompt and print out the response
string response = await kernel.InvokePromptAsync<string>(
prompt,
new(new OpenAIPromptExecutionSettings()
{
MaxTokens = 400
})
);
Console.WriteLine($"assistant >>> {response}");
Customize the text content of the file or the length of the summary to see the differences in the responses.
Clean up resources
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
azd down
Troubleshoot
On Windows, you might get the following error messages after running azd up
:
postprovision.ps1 is not digitally signed. The script will not execute on the system
The script postprovision.ps1 is executed to set the .NET user secrets used in the application. To avoid this error, run the following PowerShell command:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Then re-run the azd up
command.
Another possible error:
'pwsh' is not recognized as an internal or external command, operable program or batch file. WARNING: 'postprovision' hook failed with exit code: '1', Path: '.\infra\post-script\postprovision.ps1'. : exit code: 1 Execution will continue since ContinueOnError has been set to true.
The script postprovision.ps1 is executed to set the .NET user secrets used in the application. To avoid this error, manually run the script using the following PowerShell command:
.\infra\post-script\postprovision.ps1
The .NET AI apps now have the user secrets configured and they can be tested.