openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/Images/ImageTestFixtureBase.cs

65lines · modecode

1using NUnit.Framework;
2using OpenAI.Chat;
3using OpenAI.Tests.Utility;
4using System;
5using System.ClientModel;
6using System.Collections.Generic;
7using System.Linq;
8using static OpenAI.Tests.TestHelpers;
9
10namespace OpenAI.Tests.Images;
11
12[TestFixture(true)]
13[TestFixture(false)]
14[Parallelizable(ParallelScope.All)]
15[Category("Images")]
16public class ImageTestFixtureBase : SyncAsyncTestBase
17{
18 public ImageTestFixtureBase(bool isAsync) : base(isAsync)
19 {
20 }
21
22 public enum ImageSourceKind
23 {
24 UsingStream,
25 UsingFilePath
26 }
27
28 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.";
29
30 internal static Array s_imageSourceKindSource = Enum.GetValues(typeof(ImageSourceKind));
31
32 public void ValidateGeneratedImage(Uri imageUri, IEnumerable<string> possibleExpectedSubstrings, string descriptionHint = null)
33 {
34 ChatClient chatClient = GetTestClient<ChatClient>(TestScenario.Chat);
35 IEnumerable<ChatMessage> messages = [
36 new UserChatMessage(
37 ChatMessageContentPart.CreateTextPart($"Describe this image for me. {descriptionHint}"),
38 ChatMessageContentPart.CreateImagePart(imageUri)),
39 ];
40 ChatCompletionOptions chatOptions = new() { MaxOutputTokenCount = 2048 };
41 ClientResult<ChatCompletion> result = chatClient.CompleteChat(messages, chatOptions);
42
43 Assert.That(result.Value?.Content, Has.Count.EqualTo(1));
44 string contentText = result.Value.Content[0].Text.ToLowerInvariant();
45
46 Assert.That(possibleExpectedSubstrings.Any(possibleExpectedSubstring => contentText.Contains(possibleExpectedSubstring)));
47 }
48
49 public void ValidateGeneratedImage(BinaryData imageBytes, IEnumerable<string> possibleExpectedSubstrings, string descriptionHint = null)
50 {
51 ChatClient chatClient = GetTestClient<ChatClient>(TestScenario.Chat);
52 IEnumerable<ChatMessage> messages = [
53 new UserChatMessage(
54 ChatMessageContentPart.CreateTextPart($"Describe this image for me. {descriptionHint}"),
55 ChatMessageContentPart.CreateImagePart(imageBytes, "image/png")),
56 ];
57 ChatCompletionOptions chatOptions = new() { MaxOutputTokenCount = 2048 };
58 ClientResult<ChatCompletion> result = chatClient.CompleteChat(messages, chatOptions);
59
60 Assert.That(result.Value?.Content, Has.Count.EqualTo(1));
61 string contentText = result.Value.Content[0].Text.ToLowerInvariant();
62
63 Assert.That(possibleExpectedSubstrings.Any(possibleExpectedSubstring => contentText.Contains(possibleExpectedSubstring)));
64 }
65}
66