@@ -29,13 +29,14 @@ string searchKey = GetEnvironmentVariable("SearchKey");
2929string searchIndex = GetEnvironmentVariable (" SearchIndex" );
3030string deploymentName = GetEnvironmentVariable (" AOAIDeploymentId" );
3131
32+
3233var client = new OpenAIClient (new Uri (azureOpenAIEndpoint ), new AzureKeyCredential (azureOpenAIKey ));
3334
3435var 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
5860Console .WriteLine ($" Message from {responseMessage .Role }:" );
5961Console .WriteLine (" ===" );
@@ -62,7 +64,7 @@ Console.WriteLine("===");
6264
6365Console .WriteLine ($" Context information (e.g. citations) from chat extensions:" );
6466Console .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;
126128using System .Text .Json ;
127129using 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-
134131string azureOpenAIEndpoint = GetEnvironmentVariable (" AOAIEndpoint" );
135132string azureOpenAIKey = GetEnvironmentVariable (" AOAIKey" );
136133string searchEndpoint = GetEnvironmentVariable (" SearchEndpoint" );
137134string searchKey = GetEnvironmentVariable (" SearchKey" );
138135string searchIndex = GetEnvironmentVariable (" SearchIndex" );
139136string deploymentName = GetEnvironmentVariable (" AOAIDeploymentId" );
140137
138+
141139var client = new OpenAIClient (new Uri (azureOpenAIEndpoint ), new AzureKeyCredential (azureOpenAIKey ));
142140
143141var 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