openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Chat/ChatWithVision.cs
44lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Chat; |
| 3 | using OpenAI.Tests.Utility; |
| 4 | using System; |
| 5 | using System.ClientModel; |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | using System.Threading.Tasks; |
| 9 | using static OpenAI.Tests.TestHelpers; |
| 10 | |
| 11 | namespace OpenAI.Tests.Chat; |
| 12 | |
| 13 | [TestFixture(true)] |
| 14 | [TestFixture(false)] |
| 15 | public partial class ChatWithVision : SyncAsyncTestBase |
| 16 | { |
| 17 | public ChatWithVision(bool isAsync) |
| 18 | : base(isAsync) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | [Test] |
| 23 | public async Task DescribeAnImage() |
| 24 | { |
| 25 | string mediaType = "image/png"; |
| 26 | string filePath = Path.Combine("Assets", "images_dog_and_cat.png"); |
| 27 | using Stream stream = File.OpenRead(filePath); |
| 28 | BinaryData imageData = BinaryData.FromStream(stream); |
| 29 | |
| 30 | ChatClient client = GetTestClient<ChatClient>(TestScenario.VisionChat); |
| 31 | IEnumerable<ChatMessage> messages = [ |
| 32 | new UserChatMessage( |
| 33 | ChatMessageContentPart.CreateTextMessageContentPart("Describe this image for me."), |
| 34 | ChatMessageContentPart.CreateImageMessageContentPart(imageData, mediaType)), |
| 35 | ]; |
| 36 | ChatCompletionOptions options = new() { MaxTokens = 2048 }; |
| 37 | |
| 38 | ClientResult<ChatCompletion> result = IsAsync |
| 39 | ? await client.CompleteChatAsync(messages, options) |
| 40 | : client.CompleteChat(messages, options); |
| 41 | Console.WriteLine(result.Value.Content[0].Text); |
| 42 | Assert.That(result.Value.Content[0].Text.ToLowerInvariant(), Contains.Substring("dog")); |
| 43 | } |
| 44 | } |
| 45 | |