@@ -71,24 +71,34 @@ Passwordless authentication is more secure than key-based alternatives and is th
71
71
72
72
### Create the assistant
73
73
74
+ Note: For this sample, the following libraries were used:
75
+ - Azure.AI.OpenAI(2.1.0-beta2)
76
+ - Azure.AI.OpenAI.Assistants(1.0.0-beta4)
77
+
74
78
Update the `Program.cs` file with the following code to create an assistant:
75
79
76
80
```csharp
77
81
using Azure;
78
- using Azure.AI.OpenAI.Assistants;
82
+ using Azure.AI.OpenAI;
83
+ using Azure.Identity;
84
+ using Azure.Security.KeyVault.Secrets;
85
+ using OpenAI.Assistants;
86
+ using OpenAI.Files;
87
+ using System.ClientModel;
79
88
80
89
// Assistants is a beta API and subject to change
81
90
// Acknowledge its experimental status by suppressing the matching warning.
82
91
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
83
92
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
93
+ string deploymentName = "<Replace with Deployment Name>"
84
94
85
95
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
86
96
87
97
// Use for passwordless auth
88
98
//var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
89
99
90
- FileClient fileClient = openAIClient.GetFileClient ();
91
- AssistantClient assistantClient = openAIClient .GetAssistantClient();
100
+ OpenAIFileClient fileClient = azureClient.GetOpenAIFileClient ();
101
+ AssistantClient assistantClient = azureClient .GetAssistantClient();
92
102
93
103
// First, let's contrive a document we'll use retrieval with and upload it.
94
104
using Stream document = BinaryData.FromString("""
@@ -120,13 +130,13 @@ using Stream document = BinaryData.FromString("""
120
130
}
121
131
""").ToStream();
122
132
123
- OpenAIFileInfo salesFile = await fileClient.UploadFileAsync(
133
+ OpenAI.Files.OpenAIFile salesFile = await fileClient.UploadFileAsync(
124
134
document,
125
135
"monthly_sales.json",
126
136
FileUploadPurpose.Assistants);
127
137
128
138
// Now, we'll create a client intended to help with that data
129
- AssistantCreationOptions assistantOptions = new()
139
+ OpenAI.Assistants. AssistantCreationOptions assistantOptions = new()
130
140
{
131
141
Name = "Example: Contoso sales RAG",
132
142
Instructions =
@@ -136,7 +146,7 @@ AssistantCreationOptions assistantOptions = new()
136
146
Tools =
137
147
{
138
148
new FileSearchToolDefinition(),
139
- new CodeInterpreterToolDefinition(),
149
+ new OpenAI.Assistants. CodeInterpreterToolDefinition(),
140
150
},
141
151
ToolResources = new()
142
152
{
@@ -158,7 +168,9 @@ ThreadCreationOptions threadOptions = new()
158
168
InitialMessages = { "How well did product 113045 sell in February? Graph its trend over time." }
159
169
};
160
170
161
- ThreadRun threadRun = await assistantClient.CreateThreadAndRunAsync(assistant.Id, threadOptions);
171
+ var initialMessage = new OpenAI.Assistants.ThreadInitializationMessage(OpenAI.Assistants.MessageRole.User, ["hi"]);
172
+
173
+ ThreadRun threadRun = await assistantClient.CreateThreadAndRunAsync(assistant.Value.Id, threadOptions);
162
174
163
175
// Check back to see when the run is done
164
176
do
168
180
} while (!threadRun.Status.IsTerminal);
169
181
170
182
// Finally, we'll print out the full history for the thread that includes the augmented generation
171
- AsyncCollectionResult<ThreadMessage> messages
183
+ AsyncCollectionResult<OpenAI.Assistants. ThreadMessage> messages
172
184
= assistantClient.GetMessagesAsync(
173
185
threadRun.ThreadId,
174
186
new MessageCollectionOptions() { Order = MessageCollectionOrder.Ascending });
175
187
176
- await foreach (ThreadMessage message in messages)
188
+ await foreach (OpenAI.Assistants. ThreadMessage message in messages)
177
189
{
178
190
Console.Write($"[{message.Role.ToString().ToUpper()}]: ");
179
- foreach (MessageContent contentItem in message.Content)
191
+ foreach (OpenAI.Assistants. MessageContent contentItem in message.Content)
180
192
{
181
193
if (!string.IsNullOrEmpty(contentItem.Text))
182
194
{
@@ -202,9 +214,9 @@ await foreach (ThreadMessage message in messages)
202
214
}
203
215
if (!string.IsNullOrEmpty(contentItem.ImageFileId))
204
216
{
205
- OpenAIFileInfo imageInfo = await fileClient.GetFileAsync(contentItem.ImageFileId);
217
+ OpenAI.Files.OpenAIFile imageFile = await fileClient.GetFileAsync(contentItem.ImageFileId);
206
218
BinaryData imageBytes = await fileClient.DownloadFileAsync(contentItem.ImageFileId);
207
- using FileStream stream = File.OpenWrite($"{imageInfo .Filename}.png");
219
+ using FileStream stream = File.OpenWrite($"{imageFile .Filename}.png");
208
220
imageBytes.ToStream().CopyTo(stream);
209
221
210
222
Console.WriteLine($"<image: {imageInfo.Filename}.png>");
@@ -214,6 +226,22 @@ await foreach (ThreadMessage message in messages)
214
226
}
215
227
```
216
228
229
+ It is recommended that you store the API Key in a secure location, such as a Key Vault. The following code snippet can replace the
230
+ ``` GetEnvironmentVariable ``` lines to retrieve the Azure OpenAI API Key from your Key Vault instance:
231
+
232
+ ``` csharp
233
+ string keyVaultName = " <Replace with Key Vault Name>" ;
234
+ var kvUri = $" https://{keyVaultName }.vault.azure.net/" ;
235
+
236
+ var client = new SecretClient (new Uri (kvUri ), new DefaultAzureCredential ());
237
+
238
+ KeyVaultSecret endpointSecret = await client .GetSecretAsync (" AZURE-OPENAI-ENDPOINT" );
239
+ KeyVaultSecret apiKeySecret = await client .GetSecretAsync (" AZURE-OPENAI-API-KEY" );
240
+
241
+ string endpoint = endpointSecret .Value ;
242
+ string key = apiKeySecret .Value ;
243
+ ```
244
+
217
245
Run the app using the [ ` dotnet run ` ] ( /dotnet/core/tools/dotnet-run ) command:
218
246
219
247
``` csharp
0 commit comments