title | description | ms.date | ms.topic | ms.custom | author | ms.author | zone_pivot_groups |
---|---|---|---|---|---|---|---|
Quickstart - Build an AI chat app with .NET |
Create a simple AI powered chat app using Semantic Kernel SDK for .NET and the OpenAI or Azure OpenAI SDKs |
04/09/2025 |
quickstart |
devx-track-dotnet, devx-track-dotnet-ai |
fboucher |
frbouche |
openai-library |
In this quickstart, you learn how to create a conversational .NET console chat app using an OpenAI or Azure OpenAI model. The app uses the xref:Microsoft.Extensions.AI library so you can write code using AI abstractions rather than a specific SDK. AI abstractions enable you to change the underlying AI model with minimal code changes.
Note
The Microsoft.Extensions.AI
library is currently in Preview.
:::zone target="docs" pivot="openai"
[!INCLUDE openai-prereqs]
:::zone-end
:::zone target="docs" pivot="azure-openai"
[!INCLUDE azure-openai-prereqs]
:::zone-end
[!INCLUDE semantic-kernel]
[!INCLUDE clone-sample-repo]
Complete the following steps to create a .NET console app to connect to an AI model.
-
In an empty directory on your computer, use the
dotnet new
command to create a new console app:dotnet new console -o ChatAppAI
-
Change directory into the app folder:
cd ChatAppAI
-
Install the required packages:
:::zone target="docs" pivot="azure-openai"
dotnet add package Azure.Identity dotnet add package Azure.AI.OpenAI dotnet add package Microsoft.Extensions.AI.OpenAI dotnet add package Microsoft.Extensions.Configuration dotnet add package Microsoft.Extensions.Configuration.UserSecrets
:::zone-end
:::zone target="docs" pivot="openai"
dotnet add package OpenAI dotnet add package Microsoft.Extensions.AI.OpenAI dotnet add package Microsoft.Extensions.Configuration dotnet add package Microsoft.Extensions.Configuration.UserSecrets
:::zone-end
-
Open the app in Visual Studio Code (or your editor of choice).
code .
:::zone target="docs" pivot="azure-openai"
[!INCLUDE create-ai-service]
:::zone-end
:::zone target="docs" pivot="openai"
-
Navigate to the root of your .NET project from a terminal or command prompt.
-
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> dotnet user-secrets set ModelName <your-openai-model-name>
:::zone-end
The app uses the Microsoft.Extensions.AI
package to send and receive requests to the AI model and is designed to provide users with information about hiking trails.
-
In the Program.cs file, add the following code to connect and authenticate to the AI model.
:::zone target="docs" pivot="azure-openai"
:::code language="csharp" source="snippets/build-chat-app/azure-openai/program.cs" range="1-12":::
[!NOTE] xref:Azure.Identity.DefaultAzureCredential searches for authentication credentials from your local tooling. If you aren't using the
azd
template to provision the Azure OpenAI resource, you'll need to assign theAzure AI Developer
role to the account you used to sign in to Visual Studio or the Azure CLI. For more information, see Authenticate to Azure AI services with .NET.:::zone-end
:::zone target="docs" pivot="openai"
:::code language="csharp" source="snippets/build-chat-app/openai/program.cs" range="1-11":::
:::zone-end
-
Create a system prompt to provide the AI model with initial role context and instructions about hiking recommendations:
:::code language="csharp" source="snippets/build-chat-app/openai/program.cs" range="13-30":::
-
Create a conversational loop that accepts an input prompt from the user, sends the prompt to the model, and prints the response completion:
:::code language="csharp" source="snippets/build-chat-app/openai/program.cs" range="32-51":::
-
Use the
dotnet run
command to run the app:dotnet run
The app prints out the completion response from the AI model. Send additional follow up prompts and ask other questions to experiment with the AI chat functionality.
:::zone target="docs" pivot="azure-openai"
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
azd down
:::zone-end