|
| 1 | +--- |
| 2 | +ms.date: 2/21/2025 |
| 3 | +ms.topic: quickstart |
| 4 | +ms.custom: devx-track-dotnet, devx-track-dotnet-ai |
| 5 | +author: alexwolfmsft |
| 6 | +ms.author: alexwolf |
| 7 | +--- |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +* .NET 9.0 SDK - [Install the .NET 9.0 SDK](https://dotnet.microsoft.com/download) |
| 12 | +* Visual Studio 2022 - [Install Visual Studio 2022](https://visualstudio.microsoft.com/) (optional), or |
| 13 | +* Visual Studio Code - [Install Visual Studio Code](https://code.visualstudio.com) (optional) |
| 14 | + * With the C# DevKit - [Install C# Dev Kit extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit) |
| 15 | + |
| 16 | +## Install the .NET AI app template |
| 17 | + |
| 18 | +The **AI Chat Web App** template is available as a template package through NuGet. Use the [`dotnet new install`](../../../core/tools/dotnet-new-install.md) command to install the package: |
| 19 | + |
| 20 | +```dotnetcli |
| 21 | +dotnet new install Microsoft.Extensions.AI.Templates |
| 22 | +``` |
| 23 | + |
| 24 | +## Create the .NET AI app |
| 25 | + |
| 26 | +After you install the AI app templates, you can use them to create starter apps through Visual Studio UI, Visual Studio Code, or the .NET CLI. |
| 27 | + |
| 28 | +# [Visual Studio](#tab/visual-studio) |
| 29 | + |
| 30 | +1. Inside Visual Studio, navigate to **File > New > Project**. |
| 31 | +1. On the **Create a new project** screen, search for **AI Chat Web App**. Select the matching result and then choose **Next**. |
| 32 | +1. On the **Configure your new project** screen, enter the desired name and location for your project and then choose **Next**. |
| 33 | +1. On the **Additional information** screen: |
| 34 | + - For the **Framework** option, select **.NET 9.0**. |
| 35 | + - For the **AI service provider** option, select **Azure OpenAI**. |
| 36 | + - Make sure the **Use keyless authentication for Azure services** checkbox is checked. |
| 37 | + - For the **Vector store** option, select **Local on-disc (for prototyping)**. |
| 38 | +1. Select **Create** to complete the process. |
| 39 | + |
| 40 | +# [Visual Studio Code](#tab/visual-studio-code) |
| 41 | + |
| 42 | +1. Open the command palette in Visual Studio Code. |
| 43 | +1. Search for *New project* and select the result called **.NET: New Project**. |
| 44 | +1. Filter the project templates list by searching for *AI*. |
| 45 | +1. Select **AI Chat Web App** and press <kbd>Enter</kbd>. |
| 46 | + |
| 47 | +> [!NOTE] |
| 48 | +> The command palette experience currently only supports the default settings. To configure your AI platform and vector store during template creation, use the Visual Studio or .NET CLI workflows. |
| 49 | +
|
| 50 | +# [.NET CLI](#tab/dotnet-cli) |
| 51 | + |
| 52 | +1. In a terminal window, navigate to an empty directory on your device. |
| 53 | +1. Create a new app with the `dotnet new` command and the following parameters: |
| 54 | + |
| 55 | + ```dotnetcli |
| 56 | + dotnet new aichatweb --framework "net9.0" --AIServiceProvider "azureopenai" --VectorStore "local" |
| 57 | + ``` |
| 58 | +
|
| 59 | + The .NET CLI creates a new .NET 9.0 app with the configurations you specified. |
| 60 | +
|
| 61 | +1. Open the new app in your editor of choice, such as Visual Studio Code. |
| 62 | +
|
| 63 | + ```dotnetcli |
| 64 | + code . |
| 65 | + ``` |
| 66 | +
|
| 67 | +--- |
| 68 | +
|
| 69 | +### Explore the sample app |
| 70 | +
|
| 71 | +[!INCLUDE [ai-templates-explore-app](ai-templates-explore-app.md)] |
| 72 | +
|
| 73 | +## Create and configure the Azure OpenAI resource |
| 74 | +
|
| 75 | +To use the .NET AI templates, you'll need to create and authenticate to an Azure OpenAI service: |
| 76 | +
|
| 77 | +1. [Create an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource?pivots=web-portal) if you don't already have one available. |
| 78 | +
|
| 79 | +2. 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`). To learn how to deploy a model, see [Create a resource](/azure/ai-services/openai/how-to/create-resource?pivots=web-portal#deploy-a-model) in the Azure OpenAI docs. |
| 80 | +
|
| 81 | +3. The AI template is configured to use Microsoft Entra ID for keyless authentication. Configure the Azure OpenAI resource for keyless authentication: |
| 82 | +
|
| 83 | +- In the Azure Portal, navigate to the overview page of your Azure OpenAI resource. |
| 84 | +- Select **Access control (IAM)** from the left navigation. |
| 85 | +- [Add a role assignment](/azure/developer/ai/keyless-connections) for the `Azure AI Developer` role to your Azure account. |
| 86 | +
|
| 87 | +## Configure the app |
| 88 | +
|
| 89 | +The **AI Chat Web App** app is almost ready to go as soon as it's created. However, you'll need to provide the endpoint for your Azure OpenAI service for the app to connect to. By default, the app template searches for this value in the project's local .NET user secrets. You can manage user secrets using either the Visual Studio UI or the .NET CLI. |
| 90 | +
|
| 91 | +> [!NOTE] |
| 92 | +> To avoid authentication errors, make sure you're signed in to Visual Studio or the Azure CLI with a user that's assigned the **Azure AI Developer** role on your Azure OpenAI resource. |
| 93 | +
|
| 94 | +# [Visual Studio](#tab/configure-visual-studio) |
| 95 | +
|
| 96 | +1. In Visual Studio, right-click on your project in the Solution Explorer and select "Manage User Secrets". This opens a `secrets.json` file where you can store your API keys without them being tracked in source control. |
| 97 | +
|
| 98 | +2. Add the following key and value: |
| 99 | +
|
| 100 | + ```json |
| 101 | + { |
| 102 | + "AzureOpenAi:Endpoint": "<your-endpoint>" |
| 103 | + } |
| 104 | + ``` |
| 105 | +
|
| 106 | +# [.NET CLI](#tab/configure-dotnet-cli) |
| 107 | +
|
| 108 | +```dotnetcli |
| 109 | +dotnet user-secrets set AzureOpenAi:Endpoint <your-azure-openai-endpoint> |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +By default, the app template assumes your AI model deployment names are the same as the underlying models. If necessary, update the deployment name parameters to match your `gpt-4o-mini` and `text-embedding-3-small` deployment names: |
| 115 | + |
| 116 | +```csharp |
| 117 | +// Update these parameter values to match your Azure OpenAI model deployment names |
| 118 | +var chatClient = azureOpenAi.AsChatClient("gpt-4o-mini"); |
| 119 | +var embeddingGenerator = azureOpenAi.AsEmbeddingGenerator("text-embedding-3-small"); |
| 120 | +``` |
0 commit comments