Skip to content

Commit e319284

Browse files
author
Maryanne Gichohi
committed
Address PR comments
1 parent 7784af0 commit e319284

File tree

3 files changed

+90
-108
lines changed

3 files changed

+90
-108
lines changed

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,32 @@ Chat completion is an AI capability that enables models to generate conversation
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).
2020

21-
> [!NOTE]
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.
23-
>
24-
2521
## Create a chat completion configuration
2622

2723
In this section, you will create a chat completion configuration in Azure Portal using the GPT-4o model as an example.
2824

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

3127
1. Specify the following values:
32-
- **Key**: Type **ChatLLM:Model**.
28+
- **Key**: Type **ChatApp:Model**.
3329
- **Label**: Leave this value blank.
3430
- **Model**: Select **gpt-4o**.
31+
- **Message**: Add a new message.
32+
- **Role**: Select **user**
33+
- **Content**: Type "What is the capial of France?"
3534

3635
> [!div class="mx-imgBorder"]
3736
> ![Screen shot shows the create new AI configuration form](./media/create-ai-chat-completion-config.png)
3837
3938
1. Leave the rest of the values as default then select **Apply**.
4039

41-
## Create an Azure OpenAI resource
40+
## Add model connection configuration
41+
42+
You've added your chat completion configuration named **ChatApp:Model** in the previous section. In this section, you'll add your model connection details such as the model's API key and endpoint to App configuration. You can store the model API key as a Key Vault reference.
43+
44+
> [!NOTE]
45+
> 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.
46+
>
4247
4348
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:
4449

@@ -51,22 +56,12 @@ In this section, you will create a chat completion configuration in Azure Portal
5156

5257
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).
5358

54-
| **Key** | **Value** |
55-
|---------------------|----------------------------------------------------------------|
56-
| _ChatLLM:Endpoint_ | _Paste the resource endpoint you copied in the previous step_ |
57-
58-
1. Continue to the following instructions to implement the chat completion configuration into your application for the language or platform you are using.
59-
60-
- [.NET](./quickstart-chat-completion-dotnet.md)
59+
| **Key** | **Value** |
60+
|----------------------------------|----------------------------------------------------------------|
61+
| _ChatApp:AzureOpenAI:Endpoint_ | _Paste the resource endpoint you copied in the previous step_ |
62+
63+
1. To store your Azure OpenAI resource API key as a Key Vault reference, refer to [Add a Key Vault reference to App Configuration](./use-key-vault-references-dotnet-core.md#add-a-key-vault-reference-to-app-configuration) guide to add a Key Vault reference with the key _ChatApp:AzureOpenAI:ApiKey_.
6164

62-
> [!TIP]
63-
> To store your Azure OpenAI API key securely, consider storing it as a Key Vault reference.
64-
> - In your Azure OpenAI resource, from the **Resource Management** menu, select **Keys and Endpoint** and copy your API key.
65-
> - 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.
66-
> - 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).
67-
68-
69-
## Next steps
65+
1. Continue to the following instructions to implement the chat completion configuration into your application for the language or platform you are using.
7066

71-
> [!div class="nextstepaction"]
72-
> [AI configuration](./concept-ai-configuration.md)
67+
- [.NET](./quickstart-chat-completion-dotnet.md)
3.03 KB
Loading

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

Lines changed: 72 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ In this guide, you build an AI chat application and iterate on the prompt using
6666
string endpoint = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT");
6767
6868
options.Connect(new Uri(endpoint), credential)
69-
// Load all keys that start with `ChatLLM:` and have no label.
70-
.Select("ChatLLM:*")
69+
// Load all keys that start with `ChatApp:` and have no label.
70+
.Select("ChatApp:*")
7171
// Reload configuration if any selected key-values have changed.
7272
// Use the default refresh interval of 30 seconds. It can be overridden via AzureAppConfigurationRefreshOptions.SetRefreshInterval.
7373
.ConfigureRefresh(refresh =>
@@ -85,19 +85,19 @@ In this guide, you build an AI chat application and iterate on the prompt using
8585
8686
```csharp
8787
// Initialize the AzureOpenAIClient
88-
var openAIEndpoint = configuration["ChatLLM:Endpoint"];
88+
var openAIEndpoint = configuration["ChatApp:AzureOpenAI:Endpoint"];
8989
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(openAIEndpoint), credential);
9090
```
9191
9292
To access your Azure OpenAI resource with an API key, add the following code:
9393
9494
```csharp
9595
// Initialize the AzureOpenAIClient
96-
var apiKey = configuration["ChatLLM:ApiKey"];
96+
var apiKey = configuration["ChatApp:AzureOpenAI:ApiKey"];
9797
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(openAIEndpoint), new AzureKeyCredential(apiKey));
9898
```
9999
100-
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).
100+
If the key _ChatApp:AzureOpenAI: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).
101101
102102
```cshrap
103103
options.ConfigureKeyVault(keyVaultOptions =>
@@ -141,41 +141,36 @@ In this guide, you build an AI chat application and iterate on the prompt using
141141
142142
```csharp
143143
...
144-
var modelConfig = configuration.GetSection("ChatLLM:Model").Get<ModelConfiguration>();
145-
146-
ChatClient chatClient = client.GetChatClient(modelConfig.Model);
144+
var modelConfiguration = configuration.GetSection("ChatApp:Model").Get<ModelConfiguration>();
145+
146+
ChatClient chatClient = client.GetChatClient(modelConfiguration.Model);
147147
148148
// Configure chat completion options
149149
ChatCompletionOptions options = new ChatCompletionOptions
150150
{
151-
Temperature = modelConfig.Temperature,
152-
MaxOutputTokenCount = modelConfig.MaxTokens,
153-
TopP = modelConfig.TopP
151+
Temperature = modelConfiguration.Temperature,
152+
MaxOutputTokenCount = modelConfiguration.MaxTokens,
153+
TopP = modelConfiguration.TopP
154154
};
155+
156+
foreach (var message in modelConfiguration.Messages)
157+
{
158+
Console.WriteLine($"{message.Role}: {message.Content}");
159+
}
155160
```
156161
157162
1. Update the _Program.cs_ file to add a helper method `GetChatMessages` to process chat messages:
158163
159164
```csharp
160165
// Helper method to convert configuration messages to chat API format
161-
IEnumerable<ChatMessage> GetChatMessages(ModelConfiguration model)
166+
IEnumerable<ChatMessage> GetChatMessages(ModelConfiguration modelConfiguration)
162167
{
163-
var chatMessages = new List<ChatMessage>();
164-
165-
foreach (Message message in model.Messages)
168+
return modelConfiguration.Messages.Select<Message, ChatMessage>(message => message.Role switch
166169
{
167-
switch (message.Role)
168-
{
169-
case "system":
170-
chatMessages.Add(ChatMessage.CreateSystemMessage(message.Content));
171-
break;
172-
case "user":
173-
chatMessages.Add(ChatMessage.CreateUserMessage(message.Content));
174-
break;
175-
}
176-
}
177-
178-
return chatMessages;
170+
"system" => ChatMessage.CreateSystemMessage(message.Content),
171+
"user" => ChatMessage.CreateUserMessage(message.Content),
172+
"assistant" => ChatMessage.CreateAssistantMessage(message.Content)
173+
});
179174
}
180175
```
181176
@@ -184,16 +179,14 @@ In this guide, you build an AI chat application and iterate on the prompt using
184179
...
185180
186181
// CompleteChatAsync method generates a completion for the given chat
187-
IEnumerable<ChatMessage> messages = GetChatMessages(modelConfig);
182+
IEnumerable<ChatMessage> messages = GetChatMessages(modelConfiguration);
188183
189184
ChatCompletion completion = await chatClient.CompleteChatAsync(messages, options);
190185
191-
Console.WriteLine("-------------------Model response--------------------------");
192-
Console.WriteLine(completion.Content[0].Text);
193-
Console.WriteLine("-----------------------------------------------------------");
186+
Console.WriteLine($"AI response: {completion.Content[0].Text}");
194187
188+
Console.WriteLine("Press Enter to continue...");
195189
Console.ReadLine();
196-
Console.Clear();
197190
...
198191
```
199192
@@ -208,17 +201,16 @@ In this guide, you build an AI chat application and iterate on the prompt using
208201
{
209202
if (_refresher != null)
210203
{
204+
// Refresh the configuration from Azure App Configuration
211205
await _refresher.RefreshAsync();
212206
213207
// Existing code
214208
//
215209
216-
Console.WriteLine("-------------------Model response--------------------------");
217-
Console.WriteLine(completion.Content[0].Text);
218-
Console.WriteLine("-----------------------------------------------------------");
210+
Console.WriteLine($"AI response: {completion.Content[0].Text}");
219211
212+
Console.WriteLine("Press Enter to continue...");
220213
Console.ReadLine();
221-
Console.Clear();
222214
}
223215
}
224216
```
@@ -243,73 +235,68 @@ In this guide, you build an AI chat application and iterate on the prompt using
243235
string endpoint = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT");
244236
245237
options.Connect(new Uri(endpoint), credential)
246-
// Load all keys that start with `ChatLLM:` and have no label.
247-
.Select("ChatLLM:*")
248-
// Reload configuration if any selected key-values have changed.
249-
// Use the default refresh interval of 30 seconds. It can be overridden via AzureAppConfigurationRefreshOptions.SetRefreshInterval
250-
.ConfigureRefresh(refresh =>
251-
{
252-
refresh.RegisterAll();
253-
});
238+
// Load all keys that start with `ChatApp:*` and have no label.
239+
.Select("ChatApp:*")
240+
// Reload configuration if any selected key-values have changed.
241+
// Use the default refresh interval of 30 seconds. It can be overridden via AzureAppConfigurationRefreshOptions.SetRefreshInterval.
242+
.ConfigureRefresh(refresh =>
243+
{
244+
refresh.RegisterAll();
245+
});
254246
255247
_refresher = options.GetRefresher();
256-
257248
}).Build();
258249
259-
var openAIEndpoint = configuration["ChatLLM:Endpoint"];
260-
261250
// Initialize the AzureOpenAIClient
251+
var openAIEndpoint = configuration["ChatApp:AzureOpenAI:Endpoint"];
252+
262253
AzureOpenAIClient client = new AzureOpenAIClient(new Uri(openAIEndpoint), credential);
263254
264255
while (true)
265256
{
266257
if (_refresher != null)
267258
{
259+
// Refresh the configuration from Azure App Configuration
268260
await _refresher.RefreshAsync();
269261
270-
var modelConfig = configuration.GetSection("ChatLLM:Model").Get<ModelConfiguration>();
271-
272-
ChatClient chatClient = client.GetChatClient(modelConfig.Model);
262+
var modelConfiguration = configuration.GetSection("ChatApp:Model").Get<ModelConfiguration>();
263+
264+
ChatClient chatClient = client.GetChatClient(modelConfiguration.Model);
273265
274266
// Configure chat completion options
275267
ChatCompletionOptions options = new ChatCompletionOptions
276268
{
277-
Temperature = modelConfig.Temperature,
278-
MaxOutputTokenCount = modelConfig.MaxTokens,
279-
TopP = modelConfig.TopP
269+
Temperature = modelConfiguration.Temperature,
270+
MaxOutputTokenCount = modelConfiguration.MaxTokens,
271+
TopP = modelConfiguration.TopP
280272
};
281273
282-
IEnumerable<ChatMessage> messages = GetChatMessages(modelConfig);
274+
foreach (var message in modelConfiguration.Messages)
275+
{
276+
Console.WriteLine($"{message.Role}: {message.Content}");
277+
}
278+
279+
// CompleteChatAsync method generates a completion for the given chat
280+
IEnumerable<ChatMessage> messages = GetChatMessages(modelConfiguration);
283281
284282
ChatCompletion completion = await chatClient.CompleteChatAsync(messages, options);
285283
286-
Console.WriteLine("-------------------Model response--------------------------");
287-
Console.WriteLine(completion.Content[0].Text);
288-
Console.WriteLine("-----------------------------------------------------------");
284+
Console.WriteLine($"AI response: {completion.Content[0].Text}");
289285
286+
Console.WriteLine("Press Enter to continue...");
290287
Console.ReadLine();
291-
Console.Clear();
292288
}
293289
}
294290
295-
IEnumerable<ChatMessage> GetChatMessages(ModelConfiguration model)
291+
// Helper method to convert configuration messages to chat API format
292+
IEnumerable<ChatMessage> GetChatMessages(ModelConfiguration modelConfiguration)
296293
{
297-
var chatMessages = new List<ChatMessage>();
298-
299-
foreach (Message message in model.Messages)
294+
return modelConfiguration.Messages.Select<Message, ChatMessage>(message => message.Role switch
300295
{
301-
switch (message.Role)
302-
{
303-
case "system":
304-
chatMessages.Add(ChatMessage.CreateSystemMessage(message.Content));
305-
break;
306-
case "user":
307-
chatMessages.Add(ChatMessage.CreateUserMessage(message.Content));
308-
break;
309-
}
310-
}
311-
312-
return chatMessages;
296+
"system" => ChatMessage.CreateSystemMessage(message.Content),
297+
"user" => ChatMessage.CreateUserMessage(message.Content),
298+
"assistant" => ChatMessage.CreateAssistantMessage(message.Content)
299+
});
313300
}
314301
315302
public class ModelConfiguration
@@ -369,24 +356,24 @@ In this guide, you build an AI chat application and iterate on the prompt using
369356
You should see the following output:
370357
371358
```Output
372-
-------------------Model response--------------------------
373-
Of course! How can I assist you today?
374-
-----------------------------------------------------------
359+
system: You are a helpful assistant.
360+
user: What is the capital of France ?
361+
AI response: The capital of France is **Paris**.
362+
Press Enter to continue...
375363
376364
```
377365
378366
1. In Azure portal, select the App Configuration store instance that you created. From the **Operations** menu, select **Configuration explorer** and select the **ChatLLM:Model** key. Update the value of the Messages property:
379-
- For the first message:
380367
- Role: **system**
381-
- Content: "You are a historian who always speaks with a pirate accent".
382-
- Add a second message:
383-
- Role: **user**
384-
- Content: "Tell me about the Roman empire in 20 words"
368+
- Content: "You are a cheerful tour guide".
385369
386370
1. Press the Enter key to trigger a refresh and you should see the updated value in the Command Prompt or Powershell window:
387371
388372
```Output
389-
-------------------Model response--------------------------
390-
Arrr, the Roman Empire be a mighty force, spanin' lands far 'n wide, with legions, emperors, roads, and grand gladiator games!
391-
-----------------------------------------------------------
373+
system: You are a cheerful tour guide
374+
user: What is the capital of France ?
375+
AI response: Oh là là! The capital of France is the magnificent **Paris**! Known as the "City of Light" (*La Ville Lumière*),
376+
it's famous for its romantic ambiance, iconic landmarks like the Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral,
377+
as well as its delicious pastries and charming cafés. Have you ever been, or is it on your travel bucket list? 😊✨
378+
Press Enter to continue...
392379
```

0 commit comments

Comments
 (0)