This project is an AI chat application that demonstrates how to chat with custom data using an AI language model. Please note that this template is currently in an early preview stage. If you have feedback, please take a brief survey.
Note
Before running this project you need to configure the API keys or endpoints for the providers you have chosen. See below for details specific to your choices.
To use Azure OpenAI or Azure AI Search, you need an Azure account. If you don't already have one, create an Azure account.
A recent incompatibility was found between Ollama and Docker Desktop. This issue results in runtime errors when connecting to Ollama, and the workaround for that can lead to Docker not working for Aspire projects.
This incompatibility can be addressed by upgrading to Docker Desktop 4.41.1. See ollama/ollama#9509 for more information and a link to install the version of Docker Desktop with the fix.
To call the OpenAI REST API, you will need an API key. To obtain one, first create a new OpenAI account or log in. Next, navigate to the API key page and select "Create new secret key", optionally naming the key. Make sure to save your API key somewhere safe and do not share it with anyone.
Configure your API key for this project, using .NET User Secrets:
-
In Visual Studio, right-click on your project in the Solution Explorer and select "Manage User Secrets".
-
This will open a secrets.json file where you can store your API key without them being tracked in source control. Add the following key and value to the file:
{ "OpenAI:Key": "YOUR-API-KEY" }
From the command line, configure your API key for this project using .NET User Secrets by running the following commands:
cd <<your-project-directory>>
dotnet user-secrets set OpenAI:Key YOUR-API-KEYThis project is configured to use Ollama, an application that allows you to run AI models locally on your workstation. Note: Ollama is an excellent open source product, but it is not maintained by Microsoft.
First, download and install Ollama from their official website. Follow the installation instructions specific to your operating system.
This project uses the llama3.2 and all-minilm language models. To install these models, use the following commands in your terminal once Ollama has been installed:
ollama pull llama3.2
ollama pull all-minilmOnce the models are installed, you can start using them in your application. Refer to the Ollama documentation for detailed instructions on how to explore models locally.
This project is configured to use Foundry Local, which runs models on your workstation through a local OpenAI-compatible endpoint. It does not need an API key.
Install Foundry Local for your operating system by following the Foundry Local documentation.
The app starts the Foundry Local service for you. On first run, it downloads the configured models, then loads them into the local service.
The default chat model alias is qwen3-4b. The default embedding model alias is qwen3-embedding-0.6b.
You can change the defaults in appsettings.json, appsettings.Development.json, or user secrets:
{
"FoundryLocal": {
"ChatModel": "qwen3-4b",
"EmbeddingModel": "qwen3-embedding-0.6b",
"ServiceUrl": "http://127.0.0.1:5273"
}
}Use FoundryLocal:ServiceUrl if another local process already uses the default port.
To use Azure OpenAI, you will need an Azure account and an Azure OpenAI Service resource. For detailed instructions, see the Azure OpenAI Service documentation.
Create an Azure OpenAI Service resource.
Deploy the gpt-4o-mini and text-embedding-3-small models to your Azure OpenAI Service resource. When creating those deployments, give them the same names as the models (gpt-4o-mini and text-embedding-3-small). See the Azure OpenAI documentation to learn how to Deploy a model.
This template is configured to use keyless authentication (also known as Managed Identity, with Entra ID). In the Azure Portal, when viewing the Azure OpenAI resource you just created, view access control settings and assign yourself the Azure AI Developer role. Learn more about configuring authentication for local development.
Configure your Azure OpenAI endpoint for this project, using .NET User Secrets:
- In the Azure Portal, navigate to your Azure OpenAI resource.
- Copy the "Endpoint" URL from the "Keys and Endpoint" section.
-
In Visual Studio, right-click on your project in the Solution Explorer and select "Manage User Secrets".
-
This will open a
secrets.jsonfile where you can store your Azure OpenAI endpoint without it being tracked in source control. Add the following key and value to the file:{ "AzureOpenAI:Endpoint": "YOUR-AZURE-OPENAI-ENDPOINT" }
-
From the command line, configure your Azure OpenAI endpoint for this project using .NET User Secrets by running the following commands:
cd <<your-project-directory>> dotnet user-secrets set AzureOpenAI:Endpoint YOUR-AZURE-OPENAI-ENDPOINT
Make sure to replace YOUR-AZURE-OPENAI-ENDPOINT with your actual Azure OpenAI endpoint, formatted like https://YOUR-DEPLOYMENT-NAME.openai.azure.com/ (do not include any path after .openai.azure.com/).
Configure your Azure OpenAI API key and endpoint for this project, using .NET User Secrets:
- In the Azure Portal, navigate to your Azure OpenAI resource.
- Copy the "Endpoint" URL and "Key 1" from the "Keys and Endpoint" section.
-
In Visual Studio, right-click on your project in the Solution Explorer and select "Manage User Secrets".
-
This will open a secrets.json file where you can store your API key and endpoint without it being tracked in source control. Add the following keys & values to the file:
{ "AzureOpenAI:Key": "YOUR-AZURE-OPENAI-KEY", "AzureOpenAI:Endpoint": "YOUR-AZURE-OPENAI-ENDPOINT" }
-
From the command line, configure your API key and endpoint for this project using .NET User Secrets by running the following commands:
cd <<your-project-directory>> dotnet user-secrets set AzureOpenAI:Key YOUR-AZURE-OPENAI-KEY dotnet user-secrets set AzureOpenAI:Endpoint YOUR-AZURE-OPENAI-ENDPOINT
Make sure to replace YOUR-AZURE-OPENAI-KEY and YOUR-AZURE-OPENAI-ENDPOINT with your actual Azure OpenAI key and endpoint. Make sure your endpoint URL is formatted like https://YOUR-DEPLOYMENT-NAME.openai.azure.com/ (do not include any path after .openai.azure.com/).
To use Azure AI Search, you will need an Azure account and an Azure AI Search resource. For detailed instructions, see the Azure AI Search documentation.
Follow the instructions in the Azure portal to create an Azure AI Search resource. Note that there is a free tier for the service but it is not currently the default setting on the portal.
Note that if you previously used the same Azure AI Search resource with different model using this project name, you may need to delete your data-AIChatWeb-CSharp.Web-chunks and data-AIChatWeb-CSharp.Web-documents indexes using the Azure portal first before continuing; otherwise, data ingestion may fail due to a vector dimension mismatch.
This template is configured to use keyless authentication (also known as Managed Identity, with Entra ID). Before continuing, you'll need to configure your Azure AI Search resource to support this. Learn more. After creation, ensure that you have selected Role-Based Access Control (RBAC) under Settings > Keys, as this is not the default. Assign yourself the roles called out for local development. Learn more.
Configure your Azure AI Search endpoint for this project, using .NET User Secrets:
- In the Azure Portal, navigate to your Azure AI Search resource.
- Copy the "URL" from the "Overview" section.
-
In Visual Studio, right-click on your project in the Solution Explorer and select "Manage User Secrets".
-
This will open a
secrets.jsonfile where you can store your Azure AI Search endpoint securely. Add the following key & value to the file:{ "AzureAISearch:Endpoint": "YOUR-AZURE-AI-SEARCH-ENDPOINT" }
-
From the command line, configure your Azure AI Search endpoint for this project using .NET User Secrets by running the following commands:
cd <<your-project-directory>> dotnet user-secrets set AzureAISearch:Endpoint YOUR-AZURE-AI-SEARCH-ENDPOINT
Make sure to replace YOUR-AZURE-AI-SEARCH-ENDPOINT with your actual Azure AI Search endpoint.
Configure your Azure AI Search API key and endpoint for this project, using .NET User Secrets:
- In the Azure Portal, navigate to your Azure AI Search resource.
- Copy the "Endpoint" URL and "Primary admin key" from the "Keys" section.
-
In Visual Studio, right-click on your project in the Solution Explorer and select "Manage User Secrets".
-
This will open a
secrets.jsonfile where you can store your API key and endpoint without them being tracked in source control. Add the following keys and values to the file:{ "AzureAISearch:Key": "YOUR-AZURE-AI-SEARCH-KEY", "AzureAISearch:Endpoint": "YOUR-AZURE-AI-SEARCH-ENDPOINT" }
-
From the command line, configure your API key and endpoint for this project using .NET User Secrets by running the following commands:
cd <<your-project-directory>> dotnet user-secrets set AzureAISearch:Key YOUR-AZURE-AI-SEARCH-KEY dotnet user-secrets set AzureAISearch:Endpoint YOUR-AZURE-AI-SEARCH-ENDPOINT
Make sure to replace YOUR-AZURE-AI-SEARCH-KEY and YOUR-AZURE-AI-SEARCH-ENDPOINT with your actual Azure AI Search key and endpoint.
- Open the
.csprojfile in Visual Studio. - Press
Ctrl+F5or click the "Start" button in the toolbar to run the project.
- Open the project folder in Visual Studio Code.
- Install the C# Dev Kit extension for Visual Studio Code.
- Once installed, Open the
Program.csfile. - Run the project by clicking the "Run" button in the Debug view.
This template leverages JavaScript libraries to provide essential functionality. These libraries are located in the wwwroot/lib folder. For instructions on updating each dependency, please refer to the README.md file in each respective folder.
To learn more about development with .NET and AI, check out the following links: