@@ -29,13 +29,14 @@ string searchKey = GetEnvironmentVariable("SearchKey");
29
29
string searchIndex = GetEnvironmentVariable (" SearchIndex" );
30
30
string deploymentName = GetEnvironmentVariable (" AOAIDeploymentId" );
31
31
32
+
32
33
var client = new OpenAIClient (new Uri (azureOpenAIEndpoint ), new AzureKeyCredential (azureOpenAIKey ));
33
34
34
35
var chatCompletionsOptions = new ChatCompletionsOptions ()
35
36
{
36
37
Messages =
37
38
{
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?" ),
39
40
},
40
41
AzureExtensionsOptions = new AzureChatExtensionsOptions ()
41
42
{
@@ -48,12 +49,13 @@ var chatCompletionsOptions = new ChatCompletionsOptions()
48
49
IndexName = searchIndex ,
49
50
},
50
51
}
51
- }
52
+ },
53
+ DeploymentName = deploymentName
52
54
};
53
55
54
- Response < ChatCompletions > response = client .GetChatCompletions (deploymentName , chatCompletionsOptions );
56
+ Response < ChatCompletions > response = client .GetChatCompletions (chatCompletionsOptions );
55
57
56
- ChatMessage responseMessage = response .Value .Choices [0 ].Message ;
58
+ ChatResponseMessage responseMessage = response .Value .Choices [0 ].Message ;
57
59
58
60
Console .WriteLine ($" Message from {responseMessage .Role }:" );
59
61
Console .WriteLine (" ===" );
@@ -62,7 +64,7 @@ Console.WriteLine("===");
62
64
63
65
Console .WriteLine ($" Context information (e.g. citations) from chat extensions:" );
64
66
Console .WriteLine (" ===" );
65
- foreach (ChatMessage contextMessage in responseMessage .AzureExtensionsContext .Messages )
67
+ foreach (ChatResponseMessage contextMessage in responseMessage .AzureExtensionsContext .Messages )
66
68
{
67
69
string contextContent = contextMessage .Content ;
68
70
try
@@ -126,25 +128,22 @@ using Azure.AI.OpenAI;
126
128
using System .Text .Json ;
127
129
using static System .Environment ;
128
130
129
- string endpoint = GetEnvironmentVariable (" AOAIEndpoint" );
130
- string key = GetEnvironmentVariable (" AOAIKey" );
131
-
132
- var client = new OpenAIClient (new Uri (endpoint ), new AzureKeyCredential (key ));
133
-
134
131
string azureOpenAIEndpoint = GetEnvironmentVariable (" AOAIEndpoint" );
135
132
string azureOpenAIKey = GetEnvironmentVariable (" AOAIKey" );
136
133
string searchEndpoint = GetEnvironmentVariable (" SearchEndpoint" );
137
134
string searchKey = GetEnvironmentVariable (" SearchKey" );
138
135
string searchIndex = GetEnvironmentVariable (" SearchIndex" );
139
136
string deploymentName = GetEnvironmentVariable (" AOAIDeploymentId" );
140
137
138
+
141
139
var client = new OpenAIClient (new Uri (azureOpenAIEndpoint ), new AzureKeyCredential (azureOpenAIKey ));
142
140
143
141
var chatCompletionsOptions = new ChatCompletionsOptions ()
144
142
{
143
+ DeploymentName = deploymentName ,
145
144
Messages =
146
145
{
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?" ),
148
147
},
149
148
AzureExtensionsOptions = new AzureChatExtensionsOptions ()
150
149
{
@@ -159,44 +158,15 @@ var chatCompletionsOptions = new ChatCompletionsOptions()
159
158
}
160
159
}
161
160
};
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 ))
170
162
{
171
- await foreach ( ChatMessage chatMessage in streamingChatChoice . GetMessageStreaming () )
163
+ if ( chatUpdate . Role . HasValue )
172
164
{
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 );
200
170
}
201
171
}
202
172
```
0 commit comments