openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.10

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/Images/ImageGenerationTests.cs

206lines · modecode

1using NUnit.Framework;
2using OpenAI.Chat;
3using OpenAI.Images;
4using OpenAI.Tests.Utility;
5using System;
6using System.ClientModel;
7using System.Collections.Generic;
8using System.IO;
9using System.Threading.Tasks;
10using static OpenAI.Tests.TestHelpers;
11
12namespace OpenAI.Tests.Images;
13
14[TestFixture(true)]
15[TestFixture(false)]
16[Parallelizable(ParallelScope.All)]
17[Category("Images")]
18public partial class ImageGenerationTests : SyncAsyncTestBase
19{
20 public ImageGenerationTests(bool isAsync) : base(isAsync)
21 {
22 }
23
24 [Test]
25 public async Task BasicGenerationWorks()
26 {
27 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images);
28
29 string prompt = "An isolated stop sign.";
30
31 GeneratedImage image = IsAsync
32 ? await client.GenerateImageAsync(prompt)
33 : client.GenerateImage(prompt);
34 Assert.That(image.ImageUri, Is.Not.Null);
35 Assert.That(image.ImageBytes, Is.Null);
36
37 Console.WriteLine(image.ImageUri.AbsoluteUri);
38 ValidateGeneratedImage(image.ImageUri, "stop");
39 }
40
41 [Test]
42 public async Task GenerationWithOptionsWorks()
43 {
44 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images);
45
46 string prompt = "An isolated stop sign.";
47
48 ImageGenerationOptions options = new()
49 {
50 Quality = GeneratedImageQuality.Standard,
51 Style = GeneratedImageStyle.Natural,
52 };
53
54 GeneratedImage image = IsAsync
55 ? await client.GenerateImageAsync(prompt, options)
56 : client.GenerateImage(prompt, options);
57 Assert.That(image.ImageUri, Is.Not.Null);
58 }
59
60 [Test]
61 public async Task GenerationWithBytesResponseWorks()
62 {
63 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images);
64
65 string prompt = "An isolated stop sign.";
66
67 ImageGenerationOptions options = new()
68 {
69 ResponseFormat = GeneratedImageFormat.Bytes
70 };
71
72 GeneratedImage image = IsAsync
73 ? await client.GenerateImageAsync(prompt, options)
74 : client.GenerateImage(prompt, options);
75 Assert.That(image.ImageUri, Is.Null);
76 Assert.That(image.ImageBytes, Is.Not.Null);
77
78 ValidateGeneratedImage(image.ImageBytes, "stop");
79 }
80
81 [Test]
82 public async Task GenerateImageEditWorks()
83 {
84 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
85
86 string prompt = "A big cat with big, round eyes sitting in an empty room and looking at the camera.";
87 string maskImagePath = Path.Combine("Assets", "images_empty_room_with_mask.png");
88
89 GeneratedImage image = IsAsync
90 ? await client.GenerateImageEditAsync(maskImagePath, prompt)
91 : client.GenerateImageEdit(maskImagePath, prompt);
92 Assert.That(image.ImageUri, Is.Not.Null);
93 Assert.That(image.ImageBytes, Is.Null);
94
95 Console.WriteLine(image.ImageUri.AbsoluteUri);
96
97 ValidateGeneratedImage(image.ImageUri, "cat");
98 }
99
100 [Test]
101 public async Task GenerateImageEditWithMaskFileWorks()
102 {
103 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
104
105 string prompt = "A big cat with big, round eyes sitting in an empty room and looking at the camera.";
106 string originalImagePath = Path.Combine("Assets", "images_empty_room.png");
107 string maskImagePath = Path.Combine("Assets", "images_empty_room_with_mask.png");
108
109 GeneratedImage image = IsAsync
110 ? await client.GenerateImageEditAsync(originalImagePath, prompt, maskImagePath)
111 : client.GenerateImageEdit(originalImagePath, prompt, maskImagePath);
112 Assert.That(image.ImageUri, Is.Not.Null);
113 Assert.That(image.ImageBytes, Is.Null);
114
115 Console.WriteLine(image.ImageUri.AbsoluteUri);
116
117 ValidateGeneratedImage(image.ImageUri, "cat");
118 }
119
120 [Test]
121 public async Task GenerateImageEditWithBytesResponseWorks()
122 {
123 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
124
125 string prompt = "A big cat with big, round eyes sitting in an empty room and looking at the camera.";
126 string maskImagePath = Path.Combine("Assets", "images_empty_room_with_mask.png");
127
128 ImageEditOptions options = new()
129 {
130 ResponseFormat = GeneratedImageFormat.Bytes
131 };
132
133 GeneratedImage image = IsAsync
134 ? await client.GenerateImageEditAsync(maskImagePath, prompt, options)
135 : client.GenerateImageEdit(maskImagePath, prompt, options);
136 Assert.That(image.ImageUri, Is.Null);
137 Assert.That(image.ImageBytes, Is.Not.Null);
138
139 ValidateGeneratedImage(image.ImageBytes, "cat", "Note that it likely depicts some sort of animal.");
140 }
141
142 [Test]
143 public async Task GenerateImageVariationWorks()
144 {
145 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
146 string imagePath = Path.Combine("Assets", "images_dog_and_cat.png");
147
148 GeneratedImage image = IsAsync
149 ? await client.GenerateImageVariationAsync(imagePath)
150 : client.GenerateImageVariation(imagePath);
151 Assert.That(image.ImageUri, Is.Not.Null);
152 Assert.That(image.ImageBytes, Is.Null);
153
154 Console.WriteLine(image.ImageUri.AbsoluteUri);
155
156 ValidateGeneratedImage(image.ImageUri, "cat", "Note that it likely depicts some sort of animal.");
157 }
158
159 [Test]
160 public async Task GenerateImageVariationWithBytesResponseWorks()
161 {
162 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
163 string imagePath = Path.Combine("Assets", "images_dog_and_cat.png");
164
165 ImageVariationOptions options = new()
166 {
167 ResponseFormat = GeneratedImageFormat.Bytes
168 };
169
170 GeneratedImage image = IsAsync
171 ? await client.GenerateImageVariationAsync(imagePath, options)
172 : client.GenerateImageVariation(imagePath, options);
173 Assert.That(image.ImageUri, Is.Null);
174 Assert.That(image.ImageBytes, Is.Not.Null);
175
176 ValidateGeneratedImage(image.ImageBytes, "cat", "Note that it likely depicts some sort of animal.");
177 }
178
179 private void ValidateGeneratedImage(Uri imageUri, string expectedSubstring, string descriptionHint = null)
180 {
181 ChatClient chatClient = GetTestClient<ChatClient>(TestScenario.Chat);
182 IEnumerable<ChatMessage> messages = [
183 new UserChatMessage(
184 ChatMessageContentPart.CreateTextMessageContentPart($"Describe this image for me. {descriptionHint}"),
185 ChatMessageContentPart.CreateImageMessageContentPart(imageUri)),
186 ];
187 ChatCompletionOptions chatOptions = new() { MaxTokens = 2048 };
188 ClientResult<ChatCompletion> result = chatClient.CompleteChat(messages, chatOptions);
189
190 Assert.That(result.Value.Content[0].Text.ToLowerInvariant(), Contains.Substring(expectedSubstring));
191 }
192
193 private void ValidateGeneratedImage(BinaryData imageBytes, string expectedSubstring, string descriptionHint = null)
194 {
195 ChatClient chatClient = GetTestClient<ChatClient>(TestScenario.Chat);
196 IEnumerable<ChatMessage> messages = [
197 new UserChatMessage(
198 ChatMessageContentPart.CreateTextMessageContentPart($"Describe this image for me. {descriptionHint}"),
199 ChatMessageContentPart.CreateImageMessageContentPart(imageBytes, "image/png")),
200 ];
201 ChatCompletionOptions chatOptions = new() { MaxTokens = 2048 };
202 ClientResult<ChatCompletion> result = chatClient.CompleteChat(messages, chatOptions);
203
204 Assert.That(result.Value.Content[0].Text.ToLowerInvariant(), Contains.Substring(expectedSubstring));
205 }
206}
207