Skip to content

Commit 0618832

Browse files
authored
Fix for C# example as original did not run
1 parent c1d9c42 commit 0618832

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,34 @@ Passwordless authentication is more secure than key-based alternatives and is th
7171
7272
### Create the assistant
7373
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+
7478
Update the `Program.cs` file with the following code to create an assistant:
7579
7680
```csharp
7781
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;
7988
8089
// Assistants is a beta API and subject to change
8190
// Acknowledge its experimental status by suppressing the matching warning.
8291
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
8392
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
93+
string deploymentName = "<Replace with Deployment Name>"
8494
8595
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
8696
8797
// Use for passwordless auth
8898
//var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
8999
90-
FileClient fileClient = openAIClient.GetFileClient();
91-
AssistantClient assistantClient = openAIClient.GetAssistantClient();
100+
OpenAIFileClient fileClient = azureClient.GetOpenAIFileClient();
101+
AssistantClient assistantClient = azureClient.GetAssistantClient();
92102
93103
// First, let's contrive a document we'll use retrieval with and upload it.
94104
using Stream document = BinaryData.FromString("""
@@ -120,13 +130,13 @@ using Stream document = BinaryData.FromString("""
120130
}
121131
""").ToStream();
122132
123-
OpenAIFileInfo salesFile = await fileClient.UploadFileAsync(
133+
OpenAI.Files.OpenAIFile salesFile = await fileClient.UploadFileAsync(
124134
document,
125135
"monthly_sales.json",
126136
FileUploadPurpose.Assistants);
127137
128138
// Now, we'll create a client intended to help with that data
129-
AssistantCreationOptions assistantOptions = new()
139+
OpenAI.Assistants.AssistantCreationOptions assistantOptions = new()
130140
{
131141
Name = "Example: Contoso sales RAG",
132142
Instructions =
@@ -136,7 +146,7 @@ AssistantCreationOptions assistantOptions = new()
136146
Tools =
137147
{
138148
new FileSearchToolDefinition(),
139-
new CodeInterpreterToolDefinition(),
149+
new OpenAI.Assistants.CodeInterpreterToolDefinition(),
140150
},
141151
ToolResources = new()
142152
{
@@ -158,7 +168,9 @@ ThreadCreationOptions threadOptions = new()
158168
InitialMessages = { "How well did product 113045 sell in February? Graph its trend over time." }
159169
};
160170
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);
162174
163175
// Check back to see when the run is done
164176
do
@@ -168,15 +180,15 @@ do
168180
} while (!threadRun.Status.IsTerminal);
169181
170182
// 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
172184
= assistantClient.GetMessagesAsync(
173185
threadRun.ThreadId,
174186
new MessageCollectionOptions() { Order = MessageCollectionOrder.Ascending });
175187
176-
await foreach (ThreadMessage message in messages)
188+
await foreach (OpenAI.Assistants.ThreadMessage message in messages)
177189
{
178190
Console.Write($"[{message.Role.ToString().ToUpper()}]: ");
179-
foreach (MessageContent contentItem in message.Content)
191+
foreach (OpenAI.Assistants.MessageContent contentItem in message.Content)
180192
{
181193
if (!string.IsNullOrEmpty(contentItem.Text))
182194
{
@@ -202,9 +214,9 @@ await foreach (ThreadMessage message in messages)
202214
}
203215
if (!string.IsNullOrEmpty(contentItem.ImageFileId))
204216
{
205-
OpenAIFileInfo imageInfo = await fileClient.GetFileAsync(contentItem.ImageFileId);
217+
OpenAI.Files.OpenAIFile imageFile = await fileClient.GetFileAsync(contentItem.ImageFileId);
206218
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");
208220
imageBytes.ToStream().CopyTo(stream);
209221
210222
Console.WriteLine($"<image: {imageInfo.Filename}.png>");
@@ -214,6 +226,22 @@ await foreach (ThreadMessage message in messages)
214226
}
215227
```
216228

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

219247
```csharp

0 commit comments

Comments
 (0)