@@ -5,7 +5,7 @@ author: travisw
5
5
ms.author : travisw
6
6
ms.service : azure-ai-openai
7
7
ms.topic : include
8
- ms.date : 03/07/2024
8
+ ms.date : 01/09/2025
9
9
---
10
10
11
11
[ !INCLUDE [ Set up required variables] ( ./use-your-data-common-variables.md )]
@@ -14,14 +14,12 @@ ms.date: 03/07/2024
14
14
15
15
From the project directory, open the * Program.cs* file and replace its contents with the following code:
16
16
17
- ### Without response streaming
18
-
19
17
``` csharp
20
- using Azure ;
18
+ using System ;
21
19
using Azure .AI .OpenAI ;
20
+ using System .ClientModel ;
22
21
using Azure .AI .OpenAI .Chat ;
23
22
using OpenAI .Chat ;
24
- using System .Text .Json ;
25
23
using static System .Environment ;
26
24
27
25
string azureOpenAIEndpoint = GetEnvironmentVariable (" AZURE_OPENAI_ENDPOINT" );
@@ -31,36 +29,38 @@ string searchEndpoint = GetEnvironmentVariable("AZURE_AI_SEARCH_ENDPOINT");
31
29
string searchKey = GetEnvironmentVariable (" AZURE_AI_SEARCH_API_KEY" );
32
30
string searchIndex = GetEnvironmentVariable (" AZURE_AI_SEARCH_INDEX" );
33
31
34
- #pragma warning disable AOAI001
35
32
AzureOpenAIClient azureClient = new (
36
- new Uri (azureOpenAIEndpoint ),
37
- new AzureKeyCredential (azureOpenAIKey ));
33
+ new Uri (azureOpenAIEndpoint ),
34
+ new ApiKeyCredential (azureOpenAIKey ));
38
35
ChatClient chatClient = azureClient .GetChatClient (deploymentName );
39
36
37
+ // Extension methods to use data sources with options are subject to SDK surface changes. Suppress the
38
+ // warning to acknowledge and this and use the subject-to-change AddDataSource method.
39
+ #pragma warning disable AOAI001
40
+
40
41
ChatCompletionOptions options = new ();
41
42
options .AddDataSource (new AzureSearchChatDataSource ()
42
43
{
43
- Endpoint = new Uri (searchEndpoint ),
44
- IndexName = searchIndex ,
45
- Authentication = DataSourceAuthentication .FromApiKey (searchKey ),
44
+ Endpoint = new Uri (searchEndpoint ),
45
+ IndexName = searchIndex ,
46
+ Authentication = DataSourceAuthentication .FromApiKey (searchKey ),
46
47
});
47
48
48
49
ChatCompletion completion = chatClient .CompleteChat (
49
- [
50
- new UserChatMessage (" What are my available health plans?" ),
51
- ], options );
52
-
53
- Console .WriteLine (completion .Content [0 ].Text );
50
+ [
51
+ new UserChatMessage (" What health plans are available?" ),
52
+ ],
53
+ options );
54
54
55
- AzureChatMessageContext onYourDataContext = completion .GetAzureMessageContext ();
55
+ ChatMessageContext onYourDataContext = completion .GetMessageContext ();
56
56
57
57
if (onYourDataContext ? .Intent is not null )
58
58
{
59
- Console .WriteLine ($" Intent: {onYourDataContext .Intent }" );
59
+ Console .WriteLine ($" Intent: {onYourDataContext .Intent }" );
60
60
}
61
- foreach (AzureChatCitation citation in onYourDataContext ? .Citations ?? [])
61
+ foreach (ChatCitation citation in onYourDataContext ? .Citations ?? [])
62
62
{
63
- Console .WriteLine ($" Citation: {citation .Content }" );
63
+ Console .WriteLine ($" Citation: {citation .Content }" );
64
64
}
65
65
```
66
66
@@ -86,72 +86,4 @@ Thank you for your interest in the Contoso electronics plan and benefit packages
86
86
learn more about the various options available to you...// Omitted for brevity
87
87
```
88
88
89
- This will wait until the model has generated its entire response before printing the results. Alternatively, if you want to asynchronously stream the response and print the results, you can replace the contents of * Program.cs* with the code in the next example.
90
-
91
- ### Async with streaming
92
-
93
- ``` csharp
94
- using Azure ;
95
- using Azure .AI .OpenAI ;
96
- using Azure .AI .OpenAI .Chat ;
97
- using OpenAI .Chat ;
98
- using static System .Environment ;
99
-
100
- string azureOpenAIEndpoint = GetEnvironmentVariable (" AZURE_OPENAI_ENDPOINT" );
101
- string azureOpenAIKey = GetEnvironmentVariable (" AZURE_OPENAI_API_KEY" );
102
- string deploymentName = GetEnvironmentVariable (" AZURE_OPENAI_DEPLOYMENT_ID" );
103
- string searchEndpoint = GetEnvironmentVariable (" AZURE_AI_SEARCH_ENDPOINT" );
104
- string searchKey = GetEnvironmentVariable (" AZURE_AI_SEARCH_API_KEY" );
105
- string searchIndex = GetEnvironmentVariable (" AZURE_AI_SEARCH_INDEX" );
106
-
107
- #pragma warning disable AOAI001
108
-
109
- AzureOpenAIClient azureClient = new (
110
- new Uri (azureOpenAIEndpoint ),
111
- new AzureKeyCredential (azureOpenAIKey ));
112
- ChatClient chatClient = azureClient .GetChatClient (deploymentName );
113
-
114
- ChatCompletionOptions options = new ();
115
- options .AddDataSource (new AzureSearchChatDataSource ()
116
- {
117
- Endpoint = new Uri (searchEndpoint ),
118
- IndexName = searchIndex ,
119
- Authentication = DataSourceAuthentication .FromApiKey (searchKey ),
120
- });
121
-
122
- var chatUpdates = chatClient .CompleteChatStreamingAsync (
123
- [
124
- new UserChatMessage (" What are my available health plans?" ),
125
- ], options );
126
-
127
- AzureChatMessageContext onYourDataContext = null ;
128
- await foreach (var chatUpdate in chatUpdates )
129
- {
130
- if (chatUpdate .Role .HasValue )
131
- {
132
- Console .WriteLine ($" {chatUpdate .Role }: " );
133
- }
134
-
135
- foreach (var contentPart in chatUpdate .ContentUpdate )
136
- {
137
- Console .Write (contentPart .Text );
138
- }
139
-
140
- if (onYourDataContext == null )
141
- {
142
- onYourDataContext = chatUpdate .GetAzureMessageContext ();
143
- }
144
- }
145
-
146
- Console .WriteLine ();
147
- if (onYourDataContext ? .Intent is not null )
148
- {
149
- Console .WriteLine ($" Intent: {onYourDataContext .Intent }" );
150
- }
151
- foreach (AzureChatCitation citation in onYourDataContext ? .Citations ?? [])
152
- {
153
- Console .Write ($" Citation: {citation .Content }" );
154
- }
155
- ```
156
-
157
-
89
+ This will wait until the model has generated its entire response before printing the results.
0 commit comments