openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/Images/ImagesTests.cs

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