|
| 1 | +--- |
| 2 | +ms.date: 2/21/2025 |
| 3 | +ms.topic: include |
| 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 **GitHub Models**. |
| 36 | + - For the **Vector store** option, select **Local on-disc (for prototyping)**. |
| 37 | +1. Select **Create** to complete the process. |
| 38 | + |
| 39 | +# [Visual Studio Code](#tab/visual-studio-code) |
| 40 | + |
| 41 | +1. Open the command palette in Visual Studio Code. |
| 42 | +1. Search for *New project* and select the result called **.NET: New Project**. |
| 43 | +1. Filter the project templates list by searching for *AI*. |
| 44 | +1. Select **AI Chat Web App** and press <kbd>Enter</kbd>. |
| 45 | + |
| 46 | +> [!NOTE] |
| 47 | +> 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. |
| 48 | +
|
| 49 | +# [.NET CLI](#tab/dotnet-cli) |
| 50 | + |
| 51 | +1. In a terminal window, navigate to an empty directory on your device. |
| 52 | +1. Create a new app with the `dotnet new` command and the following parameters: |
| 53 | + |
| 54 | + ```dotnetcli |
| 55 | + dotnet new aichatweb --framework net9.0 --AiServiceProvider githubmodels --VectorStore local |
| 56 | + ``` |
| 57 | +
|
| 58 | + The .NET CLI creates a new .NET 9.0 app with the configurations you specified. |
| 59 | +
|
| 60 | +1. Open the new app in your editor of choice, such as Visual Studio Code. |
| 61 | +
|
| 62 | + ```dotnetcli |
| 63 | + code . |
| 64 | + ``` |
| 65 | +
|
| 66 | +--- |
| 67 | +
|
| 68 | +### Explore the sample app |
| 69 | +
|
| 70 | +The sample app you created is a Blazor Interactive Server web app preconfigured with common AI and data services. The app handles the following concerns for you: |
| 71 | +
|
| 72 | +- Includes essential `Microsoft.Extensions.AI` packages and other dependencies in the `csproj` file to help you get started working with AI. |
| 73 | +- Creates various AI services and registers them for dependency injection in the `Program.cs` file: |
| 74 | + - An `IChatClient` service to chat back and forth with the generative AI model |
| 75 | + - An `IEmbeddingGenerator` service that's used to generate embeddings, which are essential for vector search functionality |
| 76 | + - A `JsonVectorStore` to act as an in-memory vector store |
| 77 | +- Registers a SQLite database context service to handle ingesting documents. The app is preconfigured to ingest whatever documents you add to the `Data` folder of the project, including the provided example files. |
| 78 | +- Provides a complete chat UI using Blazor components. The UI handles rich formatting for the AI responses and provides features such as citations for response data. |
| 79 | +
|
| 80 | +## Configure access to GitHub Models |
| 81 | +
|
| 82 | +To authenticate to GitHub models from your code, you'll need to create a GitHub personal access token: |
| 83 | +
|
| 84 | +1. Navigate to the **Personal access tokens** page of your GitHub account settings. |
| 85 | +1. Select **Generate new token**. |
| 86 | +1. Enter a **Token name** and then select **Generate token** at the bottom of the page. |
| 87 | +1. Copy the token for use in the steps ahead. |
| 88 | +
|
| 89 | +## Configure the app |
| 90 | +
|
| 91 | +The **AI Chat Web App** app is almost ready to go as soon as it's created. However, you'll need to configure the app to use the personal access token you setup for GitHub Modelsfor. 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. |
| 92 | +
|
| 93 | +# [Visual Studio](#tab/configure-visual-studio) |
| 94 | +
|
| 95 | +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. |
| 96 | +
|
| 97 | +2. Add the following key and value: |
| 98 | +
|
| 99 | + ```json |
| 100 | + { |
| 101 | + "GitHubModels:Token": "<your-personal-access-token>" |
| 102 | + } |
| 103 | + ``` |
| 104 | +
|
| 105 | +# [.NET CLI](#tab/configure-dotnet-cli) |
| 106 | +
|
| 107 | +```dotnetcli |
| 108 | +dotnet user-secrets set AzureOpenAi:Endpoint <your-personal-access-token> |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +By default, the app template uses the `gpt-4o-mini` and `text-embedding-3-small` models. To try other models, update the name parameters in `Program.cs`: |
| 114 | + |
| 115 | + ```csharp |
| 116 | + var chatClient = ghModelsClient.AsChatClient("gpt-4o-mini"); |
| 117 | + var embeddingGenerator = ghModelsClient.AsEmbeddingGenerator("text-embedding-3-small"); |
| 118 | + ``` |
0 commit comments