openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Images/ImageTestFixtureBase.cs
79lines · modecode
| 1 | using Microsoft.ClientModel.TestFramework; |
| 2 | using Microsoft.ClientModel.TestFramework.TestProxy.Admin; |
| 3 | using NUnit.Framework; |
| 4 | using OpenAI.Chat; |
| 5 | using OpenAI.Tests.Utility; |
| 6 | using System; |
| 7 | using System.ClientModel; |
| 8 | using System.Collections.Generic; |
| 9 | using System.Linq; |
| 10 | using System.Threading.Tasks; |
| 11 | using static OpenAI.Tests.TestHelpers; |
| 12 | |
| 13 | namespace OpenAI.Tests.Images; |
| 14 | |
| 15 | [Category("Images")] |
| 16 | public class ImageTestFixtureBase : OpenAIRecordedTestBase |
| 17 | { |
| 18 | public ImageTestFixtureBase(bool isAsync) : base(isAsync) |
| 19 | { |
| 20 | // Replace large images with a small 1x1 PNG in recordings to save space |
| 21 | BodyKeySanitizers.Add(new BodyKeySanitizer( |
| 22 | new BodyKeySanitizerBody("$..b64_json") |
| 23 | { |
| 24 | Value = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=" |
| 25 | })); |
| 26 | |
| 27 | // Sanitize data URLs in chat messages to prevent huge base64 content |
| 28 | BodyRegexSanitizers.Add(new BodyRegexSanitizer( |
| 29 | new BodyRegexSanitizerBody() |
| 30 | { |
| 31 | Value = @"""url"": ""data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=""", |
| 32 | Regex = @"""url""\s*:\s*""data:image/[^""]+""" |
| 33 | })); |
| 34 | } |
| 35 | |
| 36 | public enum ImageSourceKind |
| 37 | { |
| 38 | UsingStream, |
| 39 | UsingFilePath |
| 40 | } |
| 41 | |
| 42 | internal const string CatPrompt = "A big cat with big round eyes and large cat ears, sitting in an empty room and looking at the camera."; |
| 43 | |
| 44 | internal static Array s_imageSourceKindSource = Enum.GetValues(typeof(ImageSourceKind)); |
| 45 | |
| 46 | public async Task ValidateGeneratedImage(Uri imageUri, IEnumerable<string> possibleExpectedSubstrings, string descriptionHint = null) |
| 47 | { |
| 48 | ChatClient chatClient = GetProxiedOpenAIClient<ChatClient>(TestScenario.Chat); |
| 49 | IEnumerable<ChatMessage> messages = [ |
| 50 | new UserChatMessage( |
| 51 | ChatMessageContentPart.CreateTextPart($"Describe this image for me. {descriptionHint}"), |
| 52 | ChatMessageContentPart.CreateImagePart(imageUri)), |
| 53 | ]; |
| 54 | ChatCompletionOptions chatOptions = new() { MaxOutputTokenCount = 2048 }; |
| 55 | ClientResult<ChatCompletion> result = await chatClient.CompleteChatAsync(messages, chatOptions); |
| 56 | |
| 57 | Assert.That(result.Value?.Content, Has.Count.EqualTo(1)); |
| 58 | string contentText = result.Value.Content[0].Text.ToLowerInvariant(); |
| 59 | |
| 60 | Assert.That(possibleExpectedSubstrings.Any(possibleExpectedSubstring => contentText.Contains(possibleExpectedSubstring))); |
| 61 | } |
| 62 | |
| 63 | public async Task ValidateGeneratedImage(BinaryData imageBytes, IEnumerable<string> possibleExpectedSubstrings, string descriptionHint = null) |
| 64 | { |
| 65 | ChatClient chatClient = GetProxiedOpenAIClient<ChatClient>(TestScenario.Chat); |
| 66 | IEnumerable<ChatMessage> messages = [ |
| 67 | new UserChatMessage( |
| 68 | ChatMessageContentPart.CreateTextPart($"Describe this image for me. {descriptionHint}"), |
| 69 | ChatMessageContentPart.CreateImagePart(imageBytes, "image/png")), |
| 70 | ]; |
| 71 | ChatCompletionOptions chatOptions = new() { MaxOutputTokenCount = 2048 }; |
| 72 | ClientResult<ChatCompletion> result = await chatClient.CompleteChatAsync(messages, chatOptions); |
| 73 | |
| 74 | Assert.That(result.Value?.Content, Has.Count.EqualTo(1)); |
| 75 | string contentText = result.Value.Content[0].Text.ToLowerInvariant(); |
| 76 | |
| 77 | Assert.That(possibleExpectedSubstrings.Any(possibleExpectedSubstring => contentText.Contains(possibleExpectedSubstring))); |
| 78 | } |
| 79 | } |
| 80 | |