This repository includes a .NET/C# app that uses Azure OpenAI to generate responses to user messages.
The project includes all the infrastructure and configuration needed to provision Azure OpenAI resources and deploy the app to Azure Container Apps using the Azure Developer CLI. By default, the app will use managed identity to authenticate with Azure OpenAI.
We recommend first going through the deploying steps before running this app locally, since the local app needs credentials for Azure OpenAI to work properly.
- An ASP.NET Core that uses Semantic Kernel package to access language models to generate responses to user messages.
- A Blazor frontend that streams responses from the backend.
- .NET Aspire to orchestrate and build a cloud native application.
- Bicep files for provisioning Azure resources, including Azure OpenAI, Azure Container Apps, Azure Container Registry, Azure Log Analytics, and RBAC roles.
- Using the OpenAI gpt-4o model through Azure OpenAI.
You have a few options for getting started with this template.
You can run this template virtually by using GitHub Codespaces. The button will open a web-based VS Code instance in your browser:
-
Open the template (this may take several minutes):
-
Open a terminal window
-
Continue with the deploying steps
If you're not using one of the above options for opening the project, then you'll need to:
-
Make sure the following tools are installed:
- .NET 8
- Git
- Azure Developer CLI (azd)
- VS Code or Visual Studio
- If using VS Code, install the C# Dev Kit
-
Download the project code:
azd init -t ai-chat-aspire-sk-csharp
-
If you're using Visual Studio, open the src/ai-chat-aspire-sk.sln solution file. If you're using VS Code, open the src folder.
-
Continue with the deploying steps, or if using Visual Studio, you can deploy the application and dependencies to Azure.
Visual Stuido Code Dev Containers can also be used locally, which will open the project in your local VS Code using the Dev Containers extension:
-
Start Docker Desktop (install it if not already installed)
-
Open the project:
-
In the VS Code window that opens, once the project files show up (this may take several minutes), open a terminal window.
-
Continue with the deploying steps
Once you've opened the project locally or in Dev Containers, you can deploy it to Azure.
-
Sign up for a free Azure account and create an Azure Subscription.
-
Check that you have the necessary permissions:
- Your Azure account must have
Microsoft.Authorization/roleAssignments/write
permissions, such as Role Based Access Control Administrator, User Access Administrator, or Owner. If you don't have subscription-level permissions, you must be granted RBAC for an existing resource group and deploy to that existing group. - Your Azure account also needs
Microsoft.Resources/deployments/write
permissions on the subscription level.
- Your Azure account must have
From a Terminal window, open the folder with the clone of this repo and run the following commands.
-
Login to Azure:
azd auth login
-
Provision and deploy dependencies for the project:
azd env new <environment> azd provision
You'll need to replace with an environment name you want to use (like "chat-app"), which will be used as a prefix for resources in Azure. Select a subscription from your Azure account, and select a location where OpenAI is available (like "francecentral"). Then it will provision the resources in your account and deploy the latest code. If you get an error or timeout with deployment, changing the location can help, as there may be availability constraints for the OpenAI resource.
-
When
azd
has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the chat app! 🎉 -
When you've made any changes to the app code, you can just run:
azd deploy
Start the project by pressing the F5 key (or clicking the Run button in the Run & Debug sidebar).
If using the command line, run the following from the src
directory:
```shell
dotnet run
```
In the Debug Console (or Terminal window) that appears, you'll see status messages written as the .NET Aspire application starts up. When it's finished starting, look for the text that says something like Login to the dashboard at https://localhost:17099/login?t=8e08b4369732034c8d67dc80f54fa1db
. Copy the text after "t=" - in this example you'd copy the text "8e08b4369732034c8d67dc80f54fa1db" this is a token you'll use to login to the .NET Aspire Dashboard. Then, click on the https://localhost:17099 URL, paste the token you just copied, and login.
Finally, in the dashboard that appears you'll see the aichatapp-web resource listed. Click on the URL under the Endpoints column to launch the web application and try the chat experience.
In order to run this app, you need to have an Azure OpenAI account deployed (from the deploying steps). After deployment, Azure OpenAI is configured for you using User Secrets. If you could not run the deployment steps here, or you want to use an existing Azure OpenAI resource and deployment, open a terminal from the root of this repo and run the following
cd ./src/AIChatApp.AppHost
dotnet user-secrets set "ConnectionStrings:openai" "https://{account_name}.openai.azure.com/"
The value for the connection string can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in the Azure OpenAI Studio > Playground > Code View. An example endpoint is: https://docs-test-001.openai.azure.com/.
Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. The majority of the Azure resources used in this infrastructure are on usage-based pricing tiers. However, Azure Container Registry has a fixed cost per registry per day.
You can try the Azure pricing calculator for the resources:
- Azure OpenAI Service: S0 tier, gpt-4o model. Pricing is based on token count. Pricing
- Azure Container App: Consumption tier with 0.5 CPU, 1GiB memory/storage. Pricing is based on resource allocation, and each month allows for a certain amount of free usage. Pricing
- Azure Container Registry: Basic tier. Pricing
- Log analytics: Pay-as-you-go tier. Costs based on data ingested. Pricing
azd down
.
This template uses Managed Identity for authenticating to the Azure OpenAI service.
You may want to consider additional security measures, such as:
- Protecting the Azure Container Apps instance with a firewall and/or Virtual Network.
- eShopSupport .NET Aspire + AI sample: A full featured .NET Aspire application using AI
- Develop .NET Apps with AI Features
- .NET Aspire Overview