Skip to content

Commit 0b5aeee

Browse files
committed
updating code
1 parent 8452662 commit 0b5aeee

File tree

1 file changed

+17
-47
lines changed

1 file changed

+17
-47
lines changed

articles/ai-services/openai/includes/use-your-data-dotnet.md

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ string searchKey = GetEnvironmentVariable("SearchKey");
2929
string searchIndex = GetEnvironmentVariable("SearchIndex");
3030
string deploymentName = GetEnvironmentVariable("AOAIDeploymentId");
3131

32+
3233
var client = new OpenAIClient(new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey));
3334

3435
var chatCompletionsOptions = new ChatCompletionsOptions()
3536
{
3637
Messages =
3738
{
38-
new ChatMessage(ChatRole.User, "What are the differences between Azure Machine Learning and Azure AI services?"),
39+
new ChatRequestUserMessage("What are the differences between Azure Machine Learning and Azure AI services?"),
3940
},
4041
AzureExtensionsOptions = new AzureChatExtensionsOptions()
4142
{
@@ -48,12 +49,13 @@ var chatCompletionsOptions = new ChatCompletionsOptions()
4849
IndexName = searchIndex,
4950
},
5051
}
51-
}
52+
},
53+
DeploymentName = deploymentName
5254
};
5355

54-
Response<ChatCompletions> response = client.GetChatCompletions(deploymentName, chatCompletionsOptions);
56+
Response<ChatCompletions> response = client.GetChatCompletions(chatCompletionsOptions);
5557

56-
ChatMessage responseMessage = response.Value.Choices[0].Message;
58+
ChatResponseMessage responseMessage = response.Value.Choices[0].Message;
5759

5860
Console.WriteLine($"Message from {responseMessage.Role}:");
5961
Console.WriteLine("===");
@@ -62,7 +64,7 @@ Console.WriteLine("===");
6264

6365
Console.WriteLine($"Context information (e.g. citations) from chat extensions:");
6466
Console.WriteLine("===");
65-
foreach (ChatMessage contextMessage in responseMessage.AzureExtensionsContext.Messages)
67+
foreach (ChatResponseMessage contextMessage in responseMessage.AzureExtensionsContext.Messages)
6668
{
6769
string contextContent = contextMessage.Content;
6870
try
@@ -126,25 +128,22 @@ using Azure.AI.OpenAI;
126128
using System.Text.Json;
127129
using static System.Environment;
128130

129-
string endpoint = GetEnvironmentVariable("AOAIEndpoint");
130-
string key = GetEnvironmentVariable("AOAIKey");
131-
132-
var client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
133-
134131
string azureOpenAIEndpoint = GetEnvironmentVariable("AOAIEndpoint");
135132
string azureOpenAIKey = GetEnvironmentVariable("AOAIKey");
136133
string searchEndpoint = GetEnvironmentVariable("SearchEndpoint");
137134
string searchKey = GetEnvironmentVariable("SearchKey");
138135
string searchIndex = GetEnvironmentVariable("SearchIndex");
139136
string deploymentName = GetEnvironmentVariable("AOAIDeploymentId");
140137

138+
141139
var client = new OpenAIClient(new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey));
142140

143141
var chatCompletionsOptions = new ChatCompletionsOptions()
144142
{
143+
DeploymentName = deploymentName,
145144
Messages =
146145
{
147-
new ChatMessage(ChatRole.User, "What are the differences between Azure Machine Learning and Azure AI services?"),
146+
new ChatRequestUserMessage("What are the differences between Azure Machine Learning and Azure AI services?"),
148147
},
149148
AzureExtensionsOptions = new AzureChatExtensionsOptions()
150149
{
@@ -159,44 +158,15 @@ var chatCompletionsOptions = new ChatCompletionsOptions()
159158
}
160159
}
161160
};
162-
163-
Response<StreamingChatCompletions> response = await client.GetChatCompletionsStreamingAsync(
164-
deploymentName,
165-
chatCompletionsOptions);
166-
167-
using StreamingChatCompletions streamingChatCompletions = response.Value;
168-
169-
await foreach (StreamingChatChoice streamingChatChoice in streamingChatCompletions.GetChoicesStreaming())
161+
await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions))
170162
{
171-
await foreach (ChatMessage chatMessage in streamingChatChoice.GetMessageStreaming())
163+
if (chatUpdate.Role.HasValue)
172164
{
173-
if (chatMessage.Role != default)
174-
{
175-
Console.WriteLine($"Message from {chatMessage.Role}: ");
176-
}
177-
if (chatMessage.Content != default)
178-
{
179-
Console.Write(chatMessage.Content);
180-
}
181-
if (chatMessage.AzureExtensionsContext != default)
182-
{
183-
Console.WriteLine($"Context information (e.g. citations) from chat extensions:");
184-
foreach (var contextMessage in chatMessage.AzureExtensionsContext.Messages)
185-
{
186-
string contextContent = contextMessage.Content;
187-
try
188-
{
189-
var contextMessageJson = JsonDocument.Parse(contextMessage.Content);
190-
contextContent = JsonSerializer.Serialize(contextMessageJson, new JsonSerializerOptions()
191-
{
192-
WriteIndented = true,
193-
});
194-
}
195-
catch (JsonException)
196-
{}
197-
Console.WriteLine($"{contextMessage.Role}: {contextContent}");
198-
}
199-
}
165+
Console.Write($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: ");
166+
}
167+
if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
168+
{
169+
Console.Write(chatUpdate.ContentUpdate);
200170
}
201171
}
202172
```

0 commit comments

Comments
 (0)