openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/Images/ImagesTests.cs

1008lines · 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.Linq;
10using System.Threading.Tasks;
11using static OpenAI.Tests.TestHelpers;
12
13namespace OpenAI.Tests.Images;
14
15[TestFixture(true)]
16[TestFixture(false)]
17[Parallelizable(ParallelScope.All)]
18[Category("Images")]
19public class ImagesTests : SyncAsyncTestBase
20{
21 private const string CatPrompt = "A big cat with big round eyes and large cat ears, sitting in an empty room and looking at the camera.";
22
23 public ImagesTests(bool isAsync) : base(isAsync)
24 {
25 }
26
27 public enum ImageSourceKind
28 {
29 UsingStream,
30 UsingFilePath
31 }
32
33 private static Array s_imageSourceKindSource = Enum.GetValues(typeof(ImageSourceKind));
34
35 #region GenerateImages
36
37 [Test]
38 public async Task BasicGenerationWorks()
39 {
40 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images);
41
42 string prompt = "An isolated stop sign.";
43
44 GeneratedImage image = IsAsync
45 ? await client.GenerateImageAsync(prompt)
46 : client.GenerateImage(prompt);
47 Assert.That(image.ImageUri, Is.Not.Null);
48 Assert.That(image.ImageBytes, Is.Null);
49
50 Console.WriteLine(image.ImageUri.AbsoluteUri);
51 ValidateGeneratedImage(image.ImageUri, ["stop"]);
52 }
53
54 [Test]
55 public async Task GenerationWithOptionsWorks()
56 {
57 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images);
58
59 string prompt = "An isolated stop sign.";
60
61 ImageGenerationOptions options = new()
62 {
63 Quality = GeneratedImageQuality.Standard,
64 Style = GeneratedImageStyle.Natural,
65 };
66
67 GeneratedImage image = IsAsync
68 ? await client.GenerateImageAsync(prompt, options)
69 : client.GenerateImage(prompt, options);
70 Assert.That(image.ImageUri, Is.Not.Null);
71 Assert.That(image.ImageBytes, Is.Null);
72
73 ValidateGeneratedImage(image.ImageUri, ["stop"]);
74 }
75
76 [Test]
77 public async Task GenerationWithBytesResponseWorks()
78 {
79 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images);
80
81 string prompt = "An isolated stop sign.";
82
83 ImageGenerationOptions options = new()
84 {
85 ResponseFormat = GeneratedImageFormat.Bytes
86 };
87
88 GeneratedImage image = IsAsync
89 ? await client.GenerateImageAsync(prompt, options)
90 : client.GenerateImage(prompt, options);
91 Assert.That(image.ImageUri, Is.Null);
92 Assert.That(image.ImageBytes, Is.Not.Null);
93
94 ValidateGeneratedImage(image.ImageBytes, ["stop"]);
95 }
96
97 [Test]
98 public void GenerateImageCanParseServiceError()
99 {
100 ImageClient client = new("dall-e-3", new ApiKeyCredential("fake_key"));
101 string prompt = "An isolated stop sign.";
102 ClientResultException ex = null;
103
104 if (IsAsync)
105 {
106 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageAsync(prompt));
107 }
108 else
109 {
110 ex = Assert.Throws<ClientResultException>(() => client.GenerateImage(prompt));
111 }
112
113 Assert.That(ex.Status, Is.EqualTo(401));
114 }
115
116 [Test]
117 public async Task GenerationOfMultipleImagesWorks()
118 {
119 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
120
121 string prompt = "An isolated stop sign.";
122
123 GeneratedImageCollection images = IsAsync
124 ? await client.GenerateImagesAsync(prompt, 2)
125 : client.GenerateImages(prompt, 2);
126
127 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
128
129 Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
130 Assert.That(images.Count, Is.EqualTo(2));
131
132 foreach (GeneratedImage image in images)
133 {
134 Assert.That(image.ImageUri, Is.Not.Null);
135 Assert.That(image.ImageBytes, Is.Null);
136 ValidateGeneratedImage(image.ImageUri, ["stop"]);
137 }
138 }
139
140 [Test]
141 public async Task GenerationOfMultipleImagesWithBytesResponseWorks()
142 {
143 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
144
145 string prompt = "An isolated stop sign.";
146
147 ImageGenerationOptions options = new()
148 {
149 ResponseFormat = GeneratedImageFormat.Bytes
150 };
151
152 GeneratedImageCollection images = IsAsync
153 ? await client.GenerateImagesAsync(prompt, 2, options)
154 : client.GenerateImages(prompt, 2, options);
155
156 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
157
158 Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
159 Assert.That(images.Count, Is.EqualTo(2));
160
161 foreach (GeneratedImage image in images)
162 {
163 Assert.That(image.ImageUri, Is.Null);
164 Assert.That(image.ImageBytes, Is.Not.Null);
165 ValidateGeneratedImage(image.ImageBytes, ["stop"]);
166 }
167 }
168
169 [Test]
170 public void GenerateImagesCanParseServiceError()
171 {
172 ImageClient client = new("dall-e-3", new ApiKeyCredential("fake_key"));
173 string prompt = "An isolated stop sign.";
174 ClientResultException ex = null;
175
176 if (IsAsync)
177 {
178 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImagesAsync(prompt, 2));
179 }
180 else
181 {
182 ex = Assert.Throws<ClientResultException>(() => client.GenerateImages(prompt, 2));
183 }
184
185 Assert.That(ex.Status, Is.EqualTo(401));
186 }
187
188 #endregion
189
190 #region GenerateImageEdits
191
192 [Test]
193 [TestCaseSource(nameof(s_imageSourceKindSource))]
194 public async Task GenerateImageEditWorks(ImageSourceKind imageSourceKind)
195 {
196 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
197
198 string maskFilename = "images_empty_room_with_mask.png";
199 string maskImagePath = Path.Combine("Assets", maskFilename);
200 GeneratedImage image = null;
201
202 if (imageSourceKind == ImageSourceKind.UsingStream)
203 {
204 using FileStream mask = File.OpenRead(maskImagePath);
205
206 image = IsAsync
207 ? await client.GenerateImageEditAsync(mask, maskFilename, CatPrompt)
208 : client.GenerateImageEdit(mask, maskFilename, CatPrompt);
209 }
210 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
211 {
212 image = IsAsync
213 ? await client.GenerateImageEditAsync(maskImagePath, CatPrompt)
214 : client.GenerateImageEdit(maskImagePath, CatPrompt);
215 }
216 else
217 {
218 Assert.Fail("Invalid source kind.");
219 }
220
221 Assert.That(image.ImageUri, Is.Not.Null);
222 Assert.That(image.ImageBytes, Is.Null);
223
224 Console.WriteLine(image.ImageUri.AbsoluteUri);
225
226 ValidateGeneratedImage(image.ImageUri, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
227 }
228
229 [Test]
230 [TestCaseSource(nameof(s_imageSourceKindSource))]
231 public async Task GenerateImageEditWithBytesResponseWorks(ImageSourceKind imageSourceKind)
232 {
233 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
234
235 string maskFilename = "images_empty_room_with_mask.png";
236 string maskImagePath = Path.Combine("Assets", maskFilename);
237 GeneratedImage image = null;
238
239 ImageEditOptions options = new()
240 {
241 ResponseFormat = GeneratedImageFormat.Bytes
242 };
243
244 if (imageSourceKind == ImageSourceKind.UsingStream)
245 {
246 using FileStream mask = File.OpenRead(maskImagePath);
247
248 image = IsAsync
249 ? await client.GenerateImageEditAsync(mask, maskFilename, CatPrompt, options)
250 : client.GenerateImageEdit(mask, maskFilename, CatPrompt, options);
251 }
252 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
253 {
254 image = IsAsync
255 ? await client.GenerateImageEditAsync(maskImagePath, CatPrompt, options)
256 : client.GenerateImageEdit(maskImagePath, CatPrompt, options);
257 }
258 else
259 {
260 Assert.Fail("Invalid source kind.");
261 }
262
263 Assert.That(image.ImageUri, Is.Null);
264 Assert.That(image.ImageBytes, Is.Not.Null);
265
266 ValidateGeneratedImage(image.ImageBytes, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
267 }
268
269 [Test]
270 public void GenerateImageEditFromStreamCanParseServiceError()
271 {
272 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
273 string maskFilename = "images_empty_room_with_mask.png";
274 string maskImagePath = Path.Combine("Assets", maskFilename);
275 using FileStream mask = File.OpenRead(maskImagePath);
276
277 ClientResultException ex = null;
278
279 if (IsAsync)
280 {
281 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageEditAsync(mask, maskFilename, CatPrompt));
282 }
283 else
284 {
285 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageEdit(mask, maskFilename, CatPrompt));
286 }
287
288 Assert.That(ex.Status, Is.EqualTo(401));
289 }
290
291 [Test]
292 public void GenerateImageEditFromPathCanParseServiceError()
293 {
294 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
295 string maskFilename = "images_empty_room_with_mask.png";
296 string maskImagePath = Path.Combine("Assets", maskFilename);
297
298 ClientResultException ex = null;
299
300 if (IsAsync)
301 {
302 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageEditAsync(maskImagePath, CatPrompt));
303 }
304 else
305 {
306 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageEdit(maskImagePath, CatPrompt));
307 }
308
309 Assert.That(ex.Status, Is.EqualTo(401));
310 }
311
312 [Test]
313 [TestCaseSource(nameof(s_imageSourceKindSource))]
314 public async Task GenerateImageEditWithMaskFileWorks(ImageSourceKind imageSourceKind)
315 {
316 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
317
318 string originalImageFilename = "images_empty_room.png";
319 string maskFilename = "images_empty_room_with_mask.png";
320 string originalImagePath = Path.Combine("Assets", originalImageFilename);
321 string maskImagePath = Path.Combine("Assets", maskFilename);
322 GeneratedImage image = null;
323
324 if (imageSourceKind == ImageSourceKind.UsingStream)
325 {
326 using FileStream originalImage = File.OpenRead(originalImagePath);
327 using FileStream mask = File.OpenRead(maskImagePath);
328
329 image = IsAsync
330 ? await client.GenerateImageEditAsync(originalImage, originalImageFilename, CatPrompt, mask, maskFilename)
331 : client.GenerateImageEdit(mask, maskFilename, CatPrompt);
332 }
333 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
334 {
335 image = IsAsync
336 ? await client.GenerateImageEditAsync(originalImagePath, CatPrompt, maskImagePath)
337 : client.GenerateImageEdit(maskImagePath, CatPrompt, maskImagePath);
338 }
339 else
340 {
341 Assert.Fail("Invalid source kind.");
342 }
343
344 Assert.That(image.ImageUri, Is.Not.Null);
345 Assert.That(image.ImageBytes, Is.Null);
346
347 Console.WriteLine(image.ImageUri.AbsoluteUri);
348
349 ValidateGeneratedImage(image.ImageUri, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
350 }
351
352 [Test]
353 [TestCaseSource(nameof(s_imageSourceKindSource))]
354 public async Task GenerateImageEditWithMaskFileWithBytesResponseWorks(ImageSourceKind imageSourceKind)
355 {
356 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
357
358 string originalImageFilename = "images_empty_room.png";
359 string maskFilename = "images_empty_room_with_mask.png";
360 string originalImagePath = Path.Combine("Assets", originalImageFilename);
361 string maskImagePath = Path.Combine("Assets", maskFilename);
362 GeneratedImage image = null;
363
364 ImageEditOptions options = new()
365 {
366 ResponseFormat = GeneratedImageFormat.Bytes
367 };
368
369 if (imageSourceKind == ImageSourceKind.UsingStream)
370 {
371 using FileStream originalImage = File.OpenRead(originalImagePath);
372 using FileStream mask = File.OpenRead(maskImagePath);
373
374 image = IsAsync
375 ? await client.GenerateImageEditAsync(originalImage, originalImageFilename, CatPrompt, mask, maskFilename, options)
376 : client.GenerateImageEdit(mask, maskFilename, CatPrompt, options);
377 }
378 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
379 {
380 image = IsAsync
381 ? await client.GenerateImageEditAsync(originalImagePath, CatPrompt, maskImagePath, options)
382 : client.GenerateImageEdit(maskImagePath, CatPrompt, maskImagePath, options);
383 }
384 else
385 {
386 Assert.Fail("Invalid source kind.");
387 }
388
389 Assert.That(image.ImageUri, Is.Null);
390 Assert.That(image.ImageBytes, Is.Not.Null);
391
392 ValidateGeneratedImage(image.ImageBytes, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
393 }
394
395 [Test]
396 public void GenerateImageEditWithMaskFileFromStreamCanParseServiceError()
397 {
398 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
399 string originalImageFilename = "images_empty_room.png";
400 string maskFilename = "images_empty_room_with_mask.png";
401 string originalImagePath = Path.Combine("Assets", originalImageFilename);
402 string maskImagePath = Path.Combine("Assets", maskFilename);
403 using FileStream originalImage = File.OpenRead(originalImagePath);
404 using FileStream mask = File.OpenRead(maskImagePath);
405
406 ClientResultException ex = null;
407
408 if (IsAsync)
409 {
410 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageEditAsync(originalImage, originalImageFilename, CatPrompt, mask, maskFilename));
411 }
412 else
413 {
414 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageEdit(originalImage, originalImageFilename, CatPrompt, mask, maskFilename));
415 }
416
417 Assert.That(ex.Status, Is.EqualTo(401));
418 }
419
420 [Test]
421 public void GenerateImageEditWithMaskFileFromPathCanParseServiceError()
422 {
423 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
424 string originalImageFilename = "images_empty_room.png";
425 string maskFilename = "images_empty_room_with_mask.png";
426 string originalImagePath = Path.Combine("Assets", originalImageFilename);
427 string maskImagePath = Path.Combine("Assets", maskFilename);
428
429 ClientResultException ex = null;
430
431 if (IsAsync)
432 {
433 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageEditAsync(originalImagePath, CatPrompt, maskImagePath));
434 }
435 else
436 {
437 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageEdit(originalImagePath, CatPrompt, maskImagePath));
438 }
439
440 Assert.That(ex.Status, Is.EqualTo(401));
441 }
442
443 [Test]
444 [TestCaseSource(nameof(s_imageSourceKindSource))]
445 public async Task GenerateMultipleImageEditsWorks(ImageSourceKind imageSourceKind)
446 {
447 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
448
449 string maskFilename = "images_empty_room_with_mask.png";
450 string maskImagePath = Path.Combine("Assets", maskFilename);
451 GeneratedImageCollection images = null;
452
453 if (imageSourceKind == ImageSourceKind.UsingStream)
454 {
455 using FileStream mask = File.OpenRead(maskImagePath);
456
457 images = IsAsync
458 ? await client.GenerateImageEditsAsync(mask, maskFilename, CatPrompt, 2)
459 : client.GenerateImageEdits(mask, maskFilename, CatPrompt, 2);
460 }
461 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
462 {
463 images = IsAsync
464 ? await client.GenerateImageEditsAsync(maskImagePath, CatPrompt, 2)
465 : client.GenerateImageEdits(maskImagePath, CatPrompt, 2);
466 }
467 else
468 {
469 Assert.Fail("Invalid source kind.");
470 }
471
472 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
473
474 Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
475 Assert.That(images.Count, Is.EqualTo(2));
476
477 foreach (GeneratedImage image in images)
478 {
479 Assert.That(image.ImageUri, Is.Not.Null);
480 Assert.That(image.ImageBytes, Is.Null);
481 Console.WriteLine(image.ImageUri.AbsoluteUri);
482 ValidateGeneratedImage(image.ImageUri, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
483 }
484 }
485
486 [Test]
487 [TestCaseSource(nameof(s_imageSourceKindSource))]
488 public async Task GenerateMultipleImageEditsWithBytesResponseWorks(ImageSourceKind imageSourceKind)
489 {
490 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
491
492 string maskFilename = "images_empty_room_with_mask.png";
493 string maskImagePath = Path.Combine("Assets", maskFilename);
494 GeneratedImageCollection images = null;
495
496 ImageEditOptions options = new()
497 {
498 ResponseFormat = GeneratedImageFormat.Bytes
499 };
500
501 if (imageSourceKind == ImageSourceKind.UsingStream)
502 {
503 using FileStream mask = File.OpenRead(maskImagePath);
504
505 images = IsAsync
506 ? await client.GenerateImageEditsAsync(mask, maskFilename, CatPrompt, 2, options)
507 : client.GenerateImageEdits(mask, maskFilename, CatPrompt, 2, options);
508 }
509 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
510 {
511 images = IsAsync
512 ? await client.GenerateImageEditsAsync(maskImagePath, CatPrompt, 2, options)
513 : client.GenerateImageEdits(maskImagePath, CatPrompt, 2, options);
514 }
515 else
516 {
517 Assert.Fail("Invalid source kind.");
518 }
519
520 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
521
522 Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
523 Assert.That(images.Count, Is.EqualTo(2));
524
525 foreach (GeneratedImage image in images)
526 {
527 Assert.That(image.ImageUri, Is.Null);
528 Assert.That(image.ImageBytes, Is.Not.Null);
529 ValidateGeneratedImage(image.ImageBytes, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
530 }
531 }
532
533 [Test]
534 public void GenerateMultipleImageEditsFromStreamCanParseServiceError()
535 {
536 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
537 string maskFilename = "images_empty_room_with_mask.png";
538 string maskImagePath = Path.Combine("Assets", maskFilename);
539 using FileStream mask = File.OpenRead(maskImagePath);
540
541 ClientResultException ex = null;
542
543 if (IsAsync)
544 {
545 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageEditsAsync(mask, maskFilename, CatPrompt, 2));
546 }
547 else
548 {
549 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageEdits(mask, maskFilename, CatPrompt, 2));
550 }
551
552 Assert.That(ex.Status, Is.EqualTo(401));
553 }
554
555 [Test]
556 public void GenerateMultipleImageEditsFromPathCanParseServiceError()
557 {
558 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
559 string maskFilename = "images_empty_room_with_mask.png";
560 string maskImagePath = Path.Combine("Assets", maskFilename);
561
562 ClientResultException ex = null;
563
564 if (IsAsync)
565 {
566 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageEditsAsync(maskImagePath, CatPrompt, 2));
567 }
568 else
569 {
570 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageEdits(maskImagePath, CatPrompt, 2));
571 }
572
573 Assert.That(ex.Status, Is.EqualTo(401));
574 }
575
576 [Test]
577 [TestCaseSource(nameof(s_imageSourceKindSource))]
578 public async Task GenerateMultipleImageEditsWithMaskFileWorks(ImageSourceKind imageSourceKind)
579 {
580 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
581
582 string originalImageFilename = "images_empty_room.png";
583 string maskFilename = "images_empty_room_with_mask.png";
584 string originalImagePath = Path.Combine("Assets", originalImageFilename);
585 string maskImagePath = Path.Combine("Assets", maskFilename);
586 GeneratedImageCollection images = null;
587
588 if (imageSourceKind == ImageSourceKind.UsingStream)
589 {
590 using FileStream originalImage = File.OpenRead(originalImagePath);
591 using FileStream mask = File.OpenRead(maskImagePath);
592
593 images = IsAsync
594 ? await client.GenerateImageEditsAsync(originalImage, originalImageFilename, CatPrompt, mask, maskFilename, 2)
595 : client.GenerateImageEdits(mask, maskFilename, CatPrompt, 2);
596 }
597 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
598 {
599 images = IsAsync
600 ? await client.GenerateImageEditsAsync(originalImagePath, CatPrompt, maskImagePath, 2)
601 : client.GenerateImageEdits(maskImagePath, CatPrompt, maskImagePath, 2);
602 }
603 else
604 {
605 Assert.Fail("Invalid source kind.");
606 }
607
608 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
609
610 Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
611 Assert.That(images.Count, Is.EqualTo(2));
612
613 foreach (GeneratedImage image in images)
614 {
615 Assert.That(image.ImageUri, Is.Not.Null);
616 Assert.That(image.ImageBytes, Is.Null);
617 Console.WriteLine(image.ImageUri.AbsoluteUri);
618 ValidateGeneratedImage(image.ImageUri, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
619 }
620 }
621
622 [Test]
623 [TestCaseSource(nameof(s_imageSourceKindSource))]
624 public async Task GenerateMultipleImageEditsWithMaskFileWithBytesResponseWorks(ImageSourceKind imageSourceKind)
625 {
626 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
627
628 string originalImageFilename = "images_empty_room.png";
629 string maskFilename = "images_empty_room_with_mask.png";
630 string originalImagePath = Path.Combine("Assets", originalImageFilename);
631 string maskImagePath = Path.Combine("Assets", maskFilename);
632 GeneratedImageCollection images = null;
633
634 ImageEditOptions options = new()
635 {
636 ResponseFormat = GeneratedImageFormat.Bytes
637 };
638
639 if (imageSourceKind == ImageSourceKind.UsingStream)
640 {
641 using FileStream originalImage = File.OpenRead(originalImagePath);
642 using FileStream mask = File.OpenRead(maskImagePath);
643
644 images = IsAsync
645 ? await client.GenerateImageEditsAsync(originalImage, originalImageFilename, CatPrompt, mask, maskFilename, 2, options)
646 : client.GenerateImageEdits(mask, maskFilename, CatPrompt, 2, options);
647 }
648 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
649 {
650 images = IsAsync
651 ? await client.GenerateImageEditsAsync(originalImagePath, CatPrompt, maskImagePath, 2, options)
652 : client.GenerateImageEdits(maskImagePath, CatPrompt, maskImagePath, 2, options);
653 }
654 else
655 {
656 Assert.Fail("Invalid source kind.");
657 }
658
659 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
660
661 Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
662 Assert.That(images.Count, Is.EqualTo(2));
663
664 foreach (GeneratedImage image in images)
665 {
666 Assert.That(image.ImageUri, Is.Null);
667 Assert.That(image.ImageBytes, Is.Not.Null);
668 ValidateGeneratedImage(image.ImageBytes, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
669 }
670 }
671
672 [Test]
673 public void GenerateMultipleImageEditsWithMaskFileFromStreamCanParseServiceError()
674 {
675 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
676 string originalImageFilename = "images_empty_room.png";
677 string maskFilename = "images_empty_room_with_mask.png";
678 string originalImagePath = Path.Combine("Assets", originalImageFilename);
679 string maskImagePath = Path.Combine("Assets", maskFilename);
680 using FileStream originalImage = File.OpenRead(originalImagePath);
681 using FileStream mask = File.OpenRead(maskImagePath);
682
683 ClientResultException ex = null;
684
685 if (IsAsync)
686 {
687 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageEditsAsync(originalImage, originalImageFilename, CatPrompt, mask, maskFilename, 2));
688 }
689 else
690 {
691 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageEdits(originalImage, originalImageFilename, CatPrompt, mask, maskFilename, 2));
692 }
693
694 Assert.That(ex.Status, Is.EqualTo(401));
695 }
696
697 [Test]
698 public void GenerateMultipleImageEditsWithMaskFileFromPathCanParseServiceError()
699 {
700 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
701 string originalImageFilename = "images_empty_room.png";
702 string maskFilename = "images_empty_room_with_mask.png";
703 string originalImagePath = Path.Combine("Assets", originalImageFilename);
704 string maskImagePath = Path.Combine("Assets", maskFilename);
705
706 ClientResultException ex = null;
707
708 if (IsAsync)
709 {
710 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageEditsAsync(originalImagePath, CatPrompt, maskImagePath, 2));
711 }
712 else
713 {
714 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageEdits(originalImagePath, CatPrompt, maskImagePath, 2));
715 }
716
717 Assert.That(ex.Status, Is.EqualTo(401));
718 }
719
720 #endregion
721
722 #region GenerateImageVariations
723
724 [Test]
725 [TestCaseSource(nameof(s_imageSourceKindSource))]
726 public async Task GenerateImageVariationWorks(ImageSourceKind imageSourceKind)
727 {
728 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
729 string imageFilename = "images_dog_and_cat.png";
730 string imagePath = Path.Combine("Assets", imageFilename);
731 GeneratedImage image = null;
732
733 if (imageSourceKind == ImageSourceKind.UsingStream)
734 {
735 using FileStream imageFile = File.OpenRead(imagePath);
736
737 image = IsAsync
738 ? await client.GenerateImageVariationAsync(imageFile, imageFilename)
739 : client.GenerateImageVariation(imageFile, imageFilename);
740 }
741 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
742 {
743 image = IsAsync
744 ? await client.GenerateImageVariationAsync(imagePath)
745 : client.GenerateImageVariation(imagePath);
746 }
747 else
748 {
749 Assert.Fail("Invalid source kind.");
750 }
751
752 Assert.That(image.ImageUri, Is.Not.Null);
753 Assert.That(image.ImageBytes, Is.Null);
754
755 Console.WriteLine(image.ImageUri.AbsoluteUri);
756
757 ValidateGeneratedImage(image.ImageUri, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
758 }
759
760 [Test]
761 [TestCaseSource(nameof(s_imageSourceKindSource))]
762 public async Task GenerateImageVariationWithBytesResponseWorks(ImageSourceKind imageSourceKind)
763 {
764 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
765 string imageFilename = "images_dog_and_cat.png";
766 string imagePath = Path.Combine("Assets", imageFilename);
767 GeneratedImage image = null;
768
769 ImageVariationOptions options = new()
770 {
771 ResponseFormat = GeneratedImageFormat.Bytes
772 };
773
774 if (imageSourceKind == ImageSourceKind.UsingStream)
775 {
776 using FileStream imageFile = File.OpenRead(imagePath);
777
778 image = IsAsync
779 ? await client.GenerateImageVariationAsync(imageFile, imageFilename, options)
780 : client.GenerateImageVariation(imageFile, imageFilename, options);
781 }
782 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
783 {
784 image = IsAsync
785 ? await client.GenerateImageVariationAsync(imagePath, options)
786 : client.GenerateImageVariation(imagePath, options);
787 }
788 else
789 {
790 Assert.Fail("Invalid source kind.");
791 }
792
793 Assert.That(image.ImageUri, Is.Null);
794 Assert.That(image.ImageBytes, Is.Not.Null);
795
796 ValidateGeneratedImage(image.ImageBytes, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
797 }
798
799 [Test]
800 public void GenerateImageVariationFromStreamCanParseServiceError()
801 {
802 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
803 string imageFilename = "images_dog_and_cat.png";
804 string imagePath = Path.Combine("Assets", imageFilename);
805 using FileStream imageFile = File.OpenRead(imagePath);
806
807 ClientResultException ex = null;
808
809 if (IsAsync)
810 {
811 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageVariationAsync(imageFile, imageFilename));
812 }
813 else
814 {
815 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageVariation(imageFile, imageFilename));
816 }
817
818 Assert.That(ex.Status, Is.EqualTo(401));
819 }
820
821 [Test]
822 public void GenerateImageVariationFromPathCanParseServiceError()
823 {
824 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
825 string imageFilename = "images_dog_and_cat.png";
826 string imagePath = Path.Combine("Assets", imageFilename);
827
828 ClientResultException ex = null;
829
830 if (IsAsync)
831 {
832 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageVariationAsync(imagePath));
833 }
834 else
835 {
836 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageVariation(imagePath));
837 }
838
839 Assert.That(ex.Status, Is.EqualTo(401));
840 }
841
842 [Test]
843 [TestCaseSource(nameof(s_imageSourceKindSource))]
844 public async Task GenerateMultipleImageVariationsWorks(ImageSourceKind imageSourceKind)
845 {
846 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
847 string imageFilename = "images_dog_and_cat.png";
848 string imagePath = Path.Combine("Assets", imageFilename);
849 GeneratedImageCollection images = null;
850
851 if (imageSourceKind == ImageSourceKind.UsingStream)
852 {
853 using FileStream imageFile = File.OpenRead(imagePath);
854
855 images = IsAsync
856 ? await client.GenerateImageVariationsAsync(imageFile, imageFilename, 2)
857 : client.GenerateImageVariations(imageFile, imageFilename, 2);
858 }
859 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
860 {
861 images = IsAsync
862 ? await client.GenerateImageVariationsAsync(imagePath, 2)
863 : client.GenerateImageVariations(imagePath, 2);
864 }
865 else
866 {
867 Assert.Fail("Invalid source kind.");
868 }
869
870 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
871
872 Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
873 Assert.That(images.Count, Is.EqualTo(2));
874
875 foreach (GeneratedImage image in images)
876 {
877 Assert.That(image.ImageUri, Is.Not.Null);
878 Assert.That(image.ImageBytes, Is.Null);
879 Console.WriteLine(image.ImageUri.AbsoluteUri);
880 ValidateGeneratedImage(image.ImageUri, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
881 }
882 }
883
884 [Test]
885 [TestCaseSource(nameof(s_imageSourceKindSource))]
886 public async Task GenerateMultipleImageVariationsWithBytesResponseWorks(ImageSourceKind imageSourceKind)
887 {
888 ImageClient client = GetTestClient<ImageClient>(TestScenario.Images, "dall-e-2");
889 string imageFilename = "images_dog_and_cat.png";
890 string imagePath = Path.Combine("Assets", imageFilename);
891 GeneratedImageCollection images = null;
892
893 ImageVariationOptions options = new()
894 {
895 ResponseFormat = GeneratedImageFormat.Bytes
896 };
897
898 if (imageSourceKind == ImageSourceKind.UsingStream)
899 {
900 using FileStream imageFile = File.OpenRead(imagePath);
901
902 images = IsAsync
903 ? await client.GenerateImageVariationsAsync(imageFile, imageFilename, 2, options)
904 : client.GenerateImageVariations(imageFile, imageFilename, 2, options);
905 }
906 else if (imageSourceKind == ImageSourceKind.UsingFilePath)
907 {
908 images = IsAsync
909 ? await client.GenerateImageVariationsAsync(imagePath, 2, options)
910 : client.GenerateImageVariations(imagePath, 2, options);
911 }
912 else
913 {
914 Assert.Fail("Invalid source kind.");
915 }
916
917 long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds();
918
919 Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024));
920 Assert.That(images.Count, Is.EqualTo(2));
921
922 foreach (GeneratedImage image in images)
923 {
924 Assert.That(image.ImageUri, Is.Null);
925 Assert.That(image.ImageBytes, Is.Not.Null);
926 ValidateGeneratedImage(image.ImageBytes, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal.");
927 }
928 }
929
930 [Test]
931 public void GenerateMultipleImageVariationsFromStreamCanParseServiceError()
932 {
933 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
934 string imageFilename = "images_dog_and_cat.png";
935 string imagePath = Path.Combine("Assets", imageFilename);
936 using FileStream imageFile = File.OpenRead(imagePath);
937
938 ClientResultException ex = null;
939
940 if (IsAsync)
941 {
942 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageVariationsAsync(imageFile, imageFilename, 2));
943 }
944 else
945 {
946 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageVariations(imageFile, imageFilename, 2));
947 }
948
949 Assert.That(ex.Status, Is.EqualTo(401));
950 }
951
952 [Test]
953 public void GenerateMultipleImageVariationsFromPathCanParseServiceError()
954 {
955 ImageClient client = new("dall-e-2", new ApiKeyCredential("fake_key"));
956 string imageFilename = "images_dog_and_cat.png";
957 string imagePath = Path.Combine("Assets", imageFilename);
958
959 ClientResultException ex = null;
960
961 if (IsAsync)
962 {
963 ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageVariationsAsync(imagePath, 2));
964 }
965 else
966 {
967 ex = Assert.Throws<ClientResultException>(() => client.GenerateImageVariations(imagePath, 2));
968 }
969
970 Assert.That(ex.Status, Is.EqualTo(401));
971 }
972
973 #endregion
974
975 private void ValidateGeneratedImage(Uri imageUri, IEnumerable<string> possibleExpectedSubstrings, string descriptionHint = null)
976 {
977 ChatClient chatClient = GetTestClient<ChatClient>(TestScenario.Chat);
978 IEnumerable<ChatMessage> messages = [
979 new UserChatMessage(
980 ChatMessageContentPart.CreateTextPart($"Describe this image for me. {descriptionHint}"),
981 ChatMessageContentPart.CreateImagePart(imageUri)),
982 ];
983 ChatCompletionOptions chatOptions = new() { MaxOutputTokenCount = 2048 };
984 ClientResult<ChatCompletion> result = chatClient.CompleteChat(messages, chatOptions);
985
986 Assert.That(result.Value?.Content, Has.Count.EqualTo(1));
987 string contentText = result.Value.Content[0].Text.ToLowerInvariant();
988
989 Assert.That(possibleExpectedSubstrings.Any(possibleExpectedSubstring => contentText.Contains(possibleExpectedSubstring)));
990 }
991
992 private void ValidateGeneratedImage(BinaryData imageBytes, IEnumerable<string> possibleExpectedSubstrings, string descriptionHint = null)
993 {
994 ChatClient chatClient = GetTestClient<ChatClient>(TestScenario.Chat);
995 IEnumerable<ChatMessage> messages = [
996 new UserChatMessage(
997 ChatMessageContentPart.CreateTextPart($"Describe this image for me. {descriptionHint}"),
998 ChatMessageContentPart.CreateImagePart(imageBytes, "image/png")),
999 ];
1000 ChatCompletionOptions chatOptions = new() { MaxOutputTokenCount = 2048 };
1001 ClientResult<ChatCompletion> result = chatClient.CompleteChat(messages, chatOptions);
1002
1003 Assert.That(result.Value?.Content, Has.Count.EqualTo(1));
1004 string contentText = result.Value.Content[0].Text.ToLowerInvariant();
1005
1006 Assert.That(possibleExpectedSubstrings.Any(possibleExpectedSubstring => contentText.Contains(possibleExpectedSubstring)));
1007 }
1008}
1009