Skip to content

Commit e8a3b22

Browse files
authored
Merge pull request #3198 from MicrosoftDocs/repo_sync_working_branch
Confirm merge from repo_sync_working_branch to main to sync with https://github.com/MicrosoftDocs/azure-ai-docs (branch main)
2 parents f458d77 + 6e38b25 commit e8a3b22

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

articles/ai-services/openai/includes/assistants-csharp.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,35 @@ Passwordless authentication is more secure than key-based alternatives and is th
7171
7272
### Create the assistant
7373
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+
7479
Update the `Program.cs` file with the following code to create an assistant:
7580
7681
```csharp
7782
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;
7989
8090
// Assistants is a beta API and subject to change
8191
// Acknowledge its experimental status by suppressing the matching warning.
8292
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
8393
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
94+
string deploymentName = "<Replace with Deployment Name>"
8495
8596
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
8697
8798
// Use for passwordless auth
8899
//var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
89100
90-
FileClient fileClient = openAIClient.GetFileClient();
91-
AssistantClient assistantClient = openAIClient.GetAssistantClient();
101+
OpenAIFileClient fileClient = azureClient.GetOpenAIFileClient();
102+
AssistantClient assistantClient = azureClient.GetAssistantClient();
92103
93104
// First, let's contrive a document we'll use retrieval with and upload it.
94105
using Stream document = BinaryData.FromString("""
@@ -120,13 +131,13 @@ using Stream document = BinaryData.FromString("""
120131
}
121132
""").ToStream();
122133
123-
OpenAIFileInfo salesFile = await fileClient.UploadFileAsync(
134+
OpenAI.Files.OpenAIFile salesFile = await fileClient.UploadFileAsync(
124135
document,
125136
"monthly_sales.json",
126137
FileUploadPurpose.Assistants);
127138
128139
// Now, we'll create a client intended to help with that data
129-
AssistantCreationOptions assistantOptions = new()
140+
OpenAI.Assistants.AssistantCreationOptions assistantOptions = new()
130141
{
131142
Name = "Example: Contoso sales RAG",
132143
Instructions =
@@ -136,7 +147,7 @@ AssistantCreationOptions assistantOptions = new()
136147
Tools =
137148
{
138149
new FileSearchToolDefinition(),
139-
new CodeInterpreterToolDefinition(),
150+
new OpenAI.Assistants.CodeInterpreterToolDefinition(),
140151
},
141152
ToolResources = new()
142153
{
@@ -158,7 +169,9 @@ ThreadCreationOptions threadOptions = new()
158169
InitialMessages = { "How well did product 113045 sell in February? Graph its trend over time." }
159170
};
160171
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);
162175
163176
// Check back to see when the run is done
164177
do
@@ -168,15 +181,15 @@ do
168181
} while (!threadRun.Status.IsTerminal);
169182
170183
// 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
172185
= assistantClient.GetMessagesAsync(
173186
threadRun.ThreadId,
174187
new MessageCollectionOptions() { Order = MessageCollectionOrder.Ascending });
175188
176-
await foreach (ThreadMessage message in messages)
189+
await foreach (OpenAI.Assistants.ThreadMessage message in messages)
177190
{
178191
Console.Write($"[{message.Role.ToString().ToUpper()}]: ");
179-
foreach (MessageContent contentItem in message.Content)
192+
foreach (OpenAI.Assistants.MessageContent contentItem in message.Content)
180193
{
181194
if (!string.IsNullOrEmpty(contentItem.Text))
182195
{
@@ -202,9 +215,9 @@ await foreach (ThreadMessage message in messages)
202215
}
203216
if (!string.IsNullOrEmpty(contentItem.ImageFileId))
204217
{
205-
OpenAIFileInfo imageInfo = await fileClient.GetFileAsync(contentItem.ImageFileId);
218+
OpenAI.Files.OpenAIFile imageFile = await fileClient.GetFileAsync(contentItem.ImageFileId);
206219
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");
208221
imageBytes.ToStream().CopyTo(stream);
209222
210223
Console.WriteLine($"<image: {imageInfo.Filename}.png>");
@@ -214,6 +227,22 @@ await foreach (ThreadMessage message in messages)
214227
}
215228
```
216229

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+
217246
Run the app using the [`dotnet run`](/dotnet/core/tools/dotnet-run) command:
218247

219248
```csharp

0 commit comments

Comments
 (0)