Skip to content

Commit 2dca409

Browse files
author
Maryanne Gichohi
committed
Address PR comments
1 parent 96a9bdf commit 2dca409

File tree

2 files changed

+69
-77
lines changed

2 files changed

+69
-77
lines changed

articles/azure-app-configuration/howto-chat-completion-config.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@ ms.collection: ce-skilling-ai-copilot
1212

1313
# Chat Completion configuration in Azure App Configuration
1414

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 selection and 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.
1616

1717
## Prerequisites
1818
- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free)
1919
- An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store).
20-
- [Azure OpenAI access](/azure/ai-services/openai/overview#get-started-with-azure-openai-service)
2120

2221
> [!NOTE]
23-
> 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.
2423
>
2524
2625
## Create a chat completion configuration
2726

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.
2928

3029
1. In Azure portal, navigate to your App configuration store. From the **Operations** menu, select **Configuration explorer** > **Create**. Then select **AI configuration**.
3130

@@ -41,23 +40,28 @@ In this section, we will create a chat completion configuration in Azure Portal
4140

4241
## Create and deploy an Azure OpenAI service resource
4342

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:
4544

4645
| Field | value |
4746
|-----------------|---------|
4847
| Select a model | gpt-4o |
4948
| Deployment name | gpt-4o |
5049

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`.
5251

5352
1. Select **Operations** > **Configuration explorer** > **Create** > **Key-Value**
5453

55-
1. Specify the following values:
56-
- **Key**: Type **ChatLLM:Endpoint**.
57-
- **Label**: Leave this value blank.
58-
- **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).
55+
56+
| **Key** | **Value** |
57+
|---------------------|-------------------------------------------------------------|
58+
| _ChatLLM:Endpoint_ | _Paste the model endpoint you copied in the previous step_ |
5959

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).
6165
6266
1. Continue to the following instructions to implement the chat completion configuration into your application for the language or platform you are using.
6367

articles/azure-app-configuration/quickstart-chat-completion-dotnet.md

Lines changed: 54 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,75 +14,97 @@ ms.author: mgichohi
1414
ms.collection: ce-skilling-ai-copilot
1515
---
1616

17-
# Quickstart: Use chat completion configuration in a .NET console app
17+
# Use chat completion configuration in a .NET console app
1818

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.
2020

2121
## Prerequisites
2222

2323
- 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)
24+
- [The latest .NET SDK](https://dotnet.microsoft.com/download)
2525

2626
## Create a console app
2727

28-
1. Create a new folder for your project.
28+
1. Create a new folder for your project. In the new folder, run the following command to create a new .NET console app project:
2929

30-
1. In the new folder, run the following command to create a new .NET console app project:
31-
```bash
30+
```dotnetcli
3231
dotnet new console
3332
```
3433
35-
1. Install the required Nuget packages in your project:
34+
1. Install the following NuGet packages in your project:
3635
37-
```bash
36+
```dotnetcli
3837
dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
3938
dotnet add package Microsoft.Extensions.Configuration.Binder
4039
dotnet add package Azure.Identity
4140
dotnet add package Azure.AI.OpenAI --prerelease
4241
```
4342
44-
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:
5044
5145
```csharp
5246
using Microsoft.Extensions.Configuration;
5347
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
5448
using Azure.Identity;
5549
using Azure.AI.OpenAI;
5650
using OpenAI.Chat;
57-
5851
```
5952
6053
1. Connect to your App Configuration store by calling the `AddAzureAppConfiguration` method in the _Program.cs_ file.
6154
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.
6356
6457
```csharp
6558
var credential = new DefaultAzureCredential();
6659
6760
IConfigurationRefresher _refresher = null;
6861
62+
// Connect to Azure App Configuration
6963
IConfiguration configuration = new ConfigurationBuilder()
7064
.AddAzureAppConfiguration(options =>
7165
{
7266
string endpoint = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT");
7367
7468
options.Connect(new Uri(endpoint), credential)
75-
.Select("ChatLLM*")
69+
// 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.
7673
.ConfigureRefresh(refresh =>
7774
{
78-
refresh.RegisterAll()
79-
.SetRefreshInterval(TimeSpan.FromSeconds(10));
75+
refresh.RegisterAll();
8076
});
8177
8278
_refresher = options.GetRefresher();
8379
}).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.
8485
85-
string modelEndpoint = configuration.GetSection("ChatLLM:Endpoint").Value;
86+
```csharp
87+
// Initialize the AzureOpenAIClient
88+
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+
});
86108
```
87109
88110
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
110132
}
111133
```
112134
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 time for 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()
124-
.AddAzureAppConfiguration(options =>
125-
{
126-
string endpoint = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT");
127-
128-
options.Connect(new Uri(endpoint), credential)
129-
.Select("ChatLLM*")
130-
.ConfigureKeyVault(keyVaultOptions =>
131-
{
132-
keyVaultOptions.SetCredential(credential);
133-
})
134-
.ConfigureRefresh(refresh =>
135-
{
136-
refresh.RegisterAll()
137-
.SetRefreshInterval(TimeSpan.FromSeconds(10));
138-
});
139-
140-
_refresher = options.GetRefresher();
141-
}).Build();
142-
143-
string apiKey = configuration["ChatLLM:ApiKey"];
144-
145-
// Initialize the AzureOpenAIClient with API key
146-
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(modelEndpoint), new AzureKeyCredential(apiKey));
147-
148-
```
149-
150135
1. Next, update the existing code in _Program.cs_ file to configure the chat completion options:
151136
152137
```csharp
@@ -244,27 +229,30 @@ In this quickstart you will create a .NET console app with dynamic configuration
244229
245230
IConfigurationRefresher _refresher = null;
246231
232+
// Connect to Azure App Configuration
247233
IConfiguration configuration = new ConfigurationBuilder()
248234
.AddAzureAppConfiguration(options =>
249235
{
250236
string endpoint = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT");
251237
252238
options.Connect(new Uri(endpoint), credential)
253-
.Select("ChatLLM*")
254-
.ConfigureRefresh(refresh =>
255-
{
256-
refresh.RegisterAll()
257-
.SetRefreshInterval(TimeSpan.FromSeconds(10));
258-
});
239+
// Load all keys that start with `ChatLLM:` and have no label.
240+
.Select("ChatLLM:*")
241+
// Reload configuration if any selected key-values have changed.
242+
// Use the default refresh interval of 30 seconds. It can be overridden via AzureAppConfigurationRefreshOptions.SetRefreshInterval
243+
.ConfigureRefresh(refresh =>
244+
{
245+
refresh.RegisterAll();
246+
});
259247
260248
_refresher = options.GetRefresher();
261249
262250
}).Build();
263251
264-
string modelEndpoint = configuration.GetSection("ChatLLM:Endpoint").Value;
252+
var openAIEndpoint = configuration["ChatLLM:Endpoint"];
265253
266254
// Initialize the AzureOpenAIClient
267-
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(modelEndpoint), credential);
255+
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(openAIEndpoint), credential);
268256
269257
while (true)
270258
{
@@ -360,7 +348,7 @@ In this quickstart you will create a .NET console app with dynamic configuration
360348
```
361349
362350
1. After the environment variable is properly set, run the following command to run and build your app locally:
363-
``` bash
351+
```dotnetcli
364352
dotnet build
365353
dotnet run
366354
```

0 commit comments

Comments
 (0)