openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Assistants/Example05_AssistantsWithVisionAsync.cs
74lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Assistants; |
| 3 | using OpenAI.Files; |
| 4 | using System; |
| 5 | using System.ClientModel; |
| 6 | using System.IO; |
| 7 | using System.Threading.Tasks; |
| 8 | |
| 9 | namespace OpenAI.Examples; |
| 10 | |
| 11 | public partial class AssistantExamples |
| 12 | { |
| 13 | [Test] |
| 14 | public async Task Example05_AssistantsWithVisionAsync() |
| 15 | { |
| 16 | // Assistants is a beta API and subject to change; acknowledge its experimental status by suppressing the matching warning. |
| 17 | #pragma warning disable OPENAI001 |
| 18 | OpenAIClient openAIClient = new(Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 19 | OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient(); |
| 20 | AssistantClient assistantClient = openAIClient.GetAssistantClient(); |
| 21 | |
| 22 | OpenAIFile pictureOfAppleFile = await fileClient.UploadFileAsync( |
| 23 | Path.Combine("Assets", "images_apple.png"), |
| 24 | FileUploadPurpose.Vision); |
| 25 | |
| 26 | Uri linkToPictureOfOrange = new("https://raw.githubusercontent.com/openai/openai-dotnet/refs/heads/main/examples/Assets/images_orange.png"); |
| 27 | |
| 28 | Assistant assistant = await assistantClient.CreateAssistantAsync( |
| 29 | "gpt-4o", |
| 30 | new AssistantCreationOptions() |
| 31 | { |
| 32 | Instructions = "When asked a question, attempt to answer very concisely. " |
| 33 | + "Prefer one-sentence answers whenever feasible." |
| 34 | }); |
| 35 | |
| 36 | AssistantThread thread = await assistantClient.CreateThreadAsync(new ThreadCreationOptions() |
| 37 | { |
| 38 | InitialMessages = |
| 39 | { |
| 40 | new ThreadInitializationMessage( |
| 41 | MessageRole.User, |
| 42 | [ |
| 43 | "Hello, assistant! Please compare these two images for me:", |
| 44 | MessageContent.FromImageFileId(pictureOfAppleFile.Id), |
| 45 | MessageContent.FromImageUri(linkToPictureOfOrange), |
| 46 | ]), |
| 47 | } |
| 48 | }); |
| 49 | |
| 50 | AsyncCollectionResult<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreamingAsync( |
| 51 | thread.Id, |
| 52 | assistant.Id, |
| 53 | new RunCreationOptions() |
| 54 | { |
| 55 | AdditionalInstructions = "When possible, try to sneak in puns if you're asked to compare things.", |
| 56 | }); |
| 57 | |
| 58 | await foreach (StreamingUpdate streamingUpdate in streamingUpdates) |
| 59 | { |
| 60 | if (streamingUpdate.UpdateKind == StreamingUpdateReason.RunCreated) |
| 61 | { |
| 62 | Console.WriteLine($"--- Run started! ---"); |
| 63 | } |
| 64 | if (streamingUpdate is MessageContentUpdate contentUpdate) |
| 65 | { |
| 66 | Console.Write(contentUpdate.Text); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | _ = await fileClient.DeleteFileAsync(pictureOfAppleFile.Id); |
| 71 | _ = await assistantClient.DeleteThreadAsync(thread.Id); |
| 72 | _ = await assistantClient.DeleteAssistantAsync(assistant.Id); |
| 73 | } |
| 74 | } |
| 75 | |