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