openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Chat/Example05_ChatWithVisionAsync.cs
32lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Chat; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.IO; |
| 6 | using System.Threading.Tasks; |
| 7 | |
| 8 | namespace OpenAI.Examples; |
| 9 | |
| 10 | public partial class ChatExamples |
| 11 | { |
| 12 | [Test] |
| 13 | public async Task Example05_ChatWithVisionAsync() |
| 14 | { |
| 15 | ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 16 | |
| 17 | string imageFilePath = Path.Combine("Assets", "images_dog_and_cat.png"); |
| 18 | using Stream imageStream = File.OpenRead(imageFilePath); |
| 19 | BinaryData imageBytes = BinaryData.FromStream(imageStream); |
| 20 | |
| 21 | List<ChatMessage> messages = [ |
| 22 | new UserChatMessage( |
| 23 | ChatMessageContentPart.CreateTextMessageContentPart("Please describe the following image."), |
| 24 | ChatMessageContentPart.CreateImageMessageContentPart(imageBytes, "image/png")) |
| 25 | ]; |
| 26 | |
| 27 | ChatCompletion chatCompletion = await client.CompleteChatAsync(messages); |
| 28 | |
| 29 | Console.WriteLine($"[ASSISTANT]:"); |
| 30 | Console.WriteLine($"{chatCompletion.Content[0].Text}"); |
| 31 | } |
| 32 | } |