You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Chat Completion configuration in Azure App Configuration
14
14
15
-
Chat completion is an AI capability that enables models to generate conversational responses based on a series of messages. Unlike simple text completion, chat completion maintains context across multiple exchanges, simulating a natural conversation. Chat completion configuration allows you to define and manage how AI models generate responses in your application. A simple configuration includes the model selectionand basic prompts. Azure OpenAI chat models support several [configuration options](/azure/ai-services/openai/reference#request-body-2) that control how responses are generated.
15
+
Chat completion is an AI capability that enables models to generate conversational responses based on a series of messages. Unlike simple text completion, chat completion maintains context across multiple exchanges, simulating a natural conversation. With chat completion configuration, you can define and manage how AI models respond within your application. A typical configuration includes model selection, system and user prompts, and model-specific settings such as temperature or token limits.
16
16
17
17
## Prerequisites
18
18
- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free)
19
19
- An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store).
> This tutorial demonstrates chat completion configuration management using Azure OpenAI models. However the configuration management demonstrated in the tutorial can be applied to any AI model you choose to work with in your application.
22
+
> This tutorial demonstrates how to use chat completion configuration with an Azure OpenAI model. However, the chat completion configuration demonstrated in the tutorial can be applied to any AI model you choose to work with in your application.
24
23
>
25
24
26
25
## Create a chat completion configuration
27
26
28
-
In this section, we will create a chat completion configuration in Azure Portal using the GPT-4o model as our example.
27
+
In this section, you will create a chat completion configuration in Azure Portal using the GPT-4o model as an example.
29
28
30
29
1. In Azure portal, navigate to your App configuration store. From the **Operations** menu, select **Configuration explorer** > **Create**. Then select **AI configuration**.
31
30
@@ -41,23 +40,28 @@ In this section, we will create a chat completion configuration in Azure Portal
41
40
42
41
## Create and deploy an Azure OpenAI service resource
43
42
44
-
1.[Create and deploy an Azure OpenAI service resource](/azure/ai-services/openai/how-to/create-resource)and configure the following fields:
43
+
1.Follow the [Get started with Azure OpenAI Service](/azure/ai-services/openai/overview#get-started-with-azure-openai-service) to create and deploy an Azure OpenAI service resource with following settings:
45
44
46
45
| Field | value |
47
46
|-----------------|---------|
48
47
| Select a model | gpt-4o |
49
48
| Deployment name | gpt-4o |
50
49
51
-
1.Select the new model you just deployed in the deployment table, locate the **Endpoint** section and copy the model endpoint url in the format`"https://<open-ai-resource-name>.openai.azure.com"`.
50
+
1.In your Azure OpenAI resource, from the **Resource Management** menu, select **Keys and Endpoint** section and copy your Azure OpenAI resource endpoint. It should follow the format: `https://<open-ai-resource-name>.openai.azure.com`.
-**Value**: Paste the model endpoint we copied in the previous step.
54
+
1. Navigate to your App Configuration store and add the following key-value. Leave **Label** and **Content Type** with their default values. For more information about how to add key-values to a store using the Azure portal or the CLI, go to [Create a key-value](./quickstart-azure-app-configuration-create.md#create-a-key-value).
|_ChatLLM:Endpoint_|_Paste the model endpoint you copied in the previous step_|
59
59
60
-
Leave the **Label** and the **Content type** with their default values then select **Apply**.
60
+
> [!TIP]
61
+
> To store your model API key securely, consider storing it as a Key Vault reference.
62
+
> - In your Azure OpenAI resource, go to the **Keys and Endpoint** section and copy your API key.
63
+
> - In your App Configuration store, add a new Key Vault reference with the **Key** set to `ChatLLM:ApiKey` and the **Value** set as a Key Vault reference.
64
+
> - For step-by-step guidance, see [Add a Key Vault reference to App Configuration](./use-key-vault-references-dotnet-core.md#add-a-key-vault-reference-to-app-configuration).
61
65
62
66
1. Continue to the following instructions to implement the chat completion configuration into your application for the language or platform you are using.
Copy file name to clipboardExpand all lines: articles/azure-app-configuration/quickstart-chat-completion-dotnet.md
+54-66Lines changed: 54 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,75 +14,97 @@ ms.author: mgichohi
14
14
ms.collection: ce-skilling-ai-copilot
15
15
---
16
16
17
-
# Quickstart: Use chat completion configuration in a .NET console app
17
+
# Use chat completion configuration in a .NET console app
18
18
19
-
In this quickstart you will create a .NET console app with dynamic configuration enabled that retrieves and updates chat completion settings without requiring application restarts.
19
+
In this guide, you build an AI chat application and iterate on the prompt using chat completion configuration dynamically loaded from Azure App Configuration.
20
20
21
21
## Prerequisites
22
22
23
23
- Complete the tutorial to [Create a chat completion configuration](./howto-chat-completion-config.md#create-a-chat-completion-configuration).
24
-
-[.NET SDK 6.0 or later](https://dotnet.microsoft.com/download)
1. Run the following command to restore packages for your project:
45
-
```bash
46
-
dotnet restore
47
-
```
48
-
49
-
1. Open the _Program.cs_, and add the following namespaces at the top of the file:
43
+
1. Open the _Program.cs_ file, and add the following namespaces at the top of the file:
50
44
51
45
```csharp
52
46
using Microsoft.Extensions.Configuration;
53
47
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
54
48
using Azure.Identity;
55
49
using Azure.AI.OpenAI;
56
50
using OpenAI.Chat;
57
-
58
51
```
59
52
60
53
1. Connect to your App Configuration store by calling the `AddAzureAppConfiguration` method in the _Program.cs_ file.
61
54
62
-
You can connect to App Configuration using **Microsoft Entra ID (recommended)**, or a connection string. In this example, you use Microsoft Entra ID, the`DefaultAzureCredential` to authenticate to your App Configuration store. Follow these [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to enable your application to access App Configuration by assigning the **App Configuration Data Reader** role to the identity represented by `DefaultAzureCredential`.
55
+
You can connect to App Configuration using either Microsoft Entra ID (recommended) or a connection string. In this example, you use Microsoft Entra ID with `DefaultAzureCredential` to authenticate to your App Configuration store. Follow these [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign the **App Configuration Data Reader** role to the identity represented by `DefaultAzureCredential`. Be sure to allow sufficient time for the permission to propagate before running your application.
63
56
64
57
```csharp
65
58
var credential = new DefaultAzureCredential();
66
59
67
60
IConfigurationRefresher _refresher = null;
68
61
62
+
// Connect to Azure App Configuration
69
63
IConfiguration configuration = new ConfigurationBuilder()
// Load all keys that start with `ChatLLM:` and have no label.
70
+
.Select("ChatLLM:*")
71
+
// Reload configuration if any selected key-values have changed.
72
+
// Use the default refresh interval of 30 seconds. It can be overridden via AzureAppConfigurationRefreshOptions.SetRefreshInterval.
76
73
.ConfigureRefresh(refresh =>
77
74
{
78
-
refresh.RegisterAll()
79
-
.SetRefreshInterval(TimeSpan.FromSeconds(10));
75
+
refresh.RegisterAll();
80
76
});
81
77
82
78
_refresher = options.GetRefresher();
83
79
}).Build();
80
+
```
81
+
82
+
1. Create an instance of the `AzureOpenAIClient` to connect to your Azure OpenAI resource. You can use either Microsoft Entra ID or API key for authentication.
83
+
84
+
To access your Azure OpenAI resource with Microsoft Entra ID, you use `DefaultAzureCredential`. Assign the [Cognitive Services OpenAI User](../role-based-access-control/built-in-roles/ai-machine-learning.md#cognitive-services-openai-user) role to the identity represented by `DefaultAzureCredential`. For detailed steps, refer to the [Role-based access control for Azure OpenAI service](/azure/ai-services/openai/how-to/role-based-access-control) guide. Be sure to allow sufficient time for the permission to propagate before running your application.
var openAIEndpoint = configuration["ChatLLM:Endpoint"];
89
+
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(openAIEndpoint), credential);
90
+
```
91
+
92
+
To access your Azure OpenAI resource with an API key, add the following code:
93
+
94
+
```csharp
95
+
// Initialize the AzureOpenAIClient
96
+
var apiKey = configuration["ChatLLM:ApiKey"];
97
+
98
+
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(openAIEndpoint), new AzureKeyCredential(apiKey));
99
+
```
100
+
101
+
If the key _ChatLLM:ApiKey_ is a Key Vault reference in App Configuration, make sure to add the following code snippet to the `AddAzureAppConfiguration` call and [grant your app access to Key Vault](./use-key-vault-references-dotnet-core.md#grant-your-app-access-to-key-vault)
102
+
103
+
```cshrap
104
+
options.ConfigureKeyVault(keyVaultOptions =>
105
+
{
106
+
keyVaultOptions.SetCredential(credential);
107
+
});
86
108
```
87
109
88
110
1. Define the `ModelConfiguration` class in _Program.cs_ file:
@@ -110,43 +132,6 @@ In this quickstart you will create a .NET console app with dynamic configuration
110
132
}
111
133
```
112
134
113
-
1. Create an instance of the `AzureOpenAIClient`. Use the existing instance of `DefaultAzureCredential` you created in the previous step to authenticate to your Azure OpenAI resource. Assign your identity the [Cognitive Services OpenAI User](../role-based-access-control/built-in-roles/ai-machine-learning.md#cognitive-services-openai-user) role or [Cognitive Services OpenAI Contributor](../role-based-access-control/built-in-roles/ai-machine-learning.md#cognitive-services-openai-contributor) role. For detailed steps, see [Role-based access control for Azure OpenAI service](/azure/ai-services/openai/how-to/role-based-access-control). Be sure to allow sufficient timefor the permission to propagate before running your application.
114
-
115
-
```csharp
116
-
// Initialize the AzureOpenAIClient
117
-
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(modelEndpoint), credential);
118
-
```
119
-
120
-
1. Alternatively, you can authenticate to your Azure OpenAI resource using an API key stored as a Key Vault reference in App Configuration with the key name "ChatLLM:ApiKey". Ensure to [grant your app access to Key Vault](./use-key-vault-references-dotnet-core.md#grant-your-app-access-to-key-vault). To read the Key Vault reference, update your configuration builder code in _Program.cs_ as follows:
121
-
122
-
```csharp
123
-
IConfiguration configuration = new ConfigurationBuilder()
0 commit comments