openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Images/ImageTestFixtureBase.cs
79lines · modeblame
65d5703fMaddy Heaps10 months ago | 1 | using Microsoft.ClientModel.TestFramework; |
| 2 | using Microsoft.ClientModel.TestFramework.TestProxy.Admin; | |
| 3 | using NUnit.Framework; | |
5dce104aJose Arriaga Maldonado1 years ago | 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; | |
65d5703fMaddy Heaps10 months ago | 10 | using System.Threading.Tasks; |
5dce104aJose Arriaga Maldonado1 years ago | 11 | using static OpenAI.Tests.TestHelpers; |
| 12 | | |
| 13 | namespace OpenAI.Tests.Images; | |
| 14 | | |
| 15 | [Category("Images")] | |
65d5703fMaddy Heaps10 months ago | 16 | public class ImageTestFixtureBase : OpenAIRecordedTestBase |
5dce104aJose Arriaga Maldonado1 years ago | 17 | { |
| 18 | public ImageTestFixtureBase(bool isAsync) : base(isAsync) | |
| 19 | { | |
65d5703fMaddy Heaps10 months ago | 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 | })); | |
5dce104aJose Arriaga Maldonado1 years ago | 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 | | |
65d5703fMaddy Heaps10 months ago | 46 | public async Task ValidateGeneratedImage(Uri imageUri, IEnumerable<string> possibleExpectedSubstrings, string descriptionHint = null) |
5dce104aJose Arriaga Maldonado1 years ago | 47 | { |
65d5703fMaddy Heaps10 months ago | 48 | ChatClient chatClient = GetProxiedOpenAIClient<ChatClient>(TestScenario.Chat); |
5dce104aJose Arriaga Maldonado1 years ago | 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 }; | |
65d5703fMaddy Heaps10 months ago | 55 | ClientResult<ChatCompletion> result = await chatClient.CompleteChatAsync(messages, chatOptions); |
5dce104aJose Arriaga Maldonado1 years ago | 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 | | |
65d5703fMaddy Heaps10 months ago | 63 | public async Task ValidateGeneratedImage(BinaryData imageBytes, IEnumerable<string> possibleExpectedSubstrings, string descriptionHint = null) |
5dce104aJose Arriaga Maldonado1 years ago | 64 | { |
65d5703fMaddy Heaps10 months ago | 65 | ChatClient chatClient = GetProxiedOpenAIClient<ChatClient>(TestScenario.Chat); |
5dce104aJose Arriaga Maldonado1 years ago | 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 }; | |
65d5703fMaddy Heaps10 months ago | 72 | ClientResult<ChatCompletion> result = await chatClient.CompleteChatAsync(messages, chatOptions); |
5dce104aJose Arriaga Maldonado1 years ago | 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 | } |