-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathOpenAI.Images.pas
More file actions
1057 lines (932 loc) · 35.9 KB
/
Copy pathOpenAI.Images.pas
File metadata and controls
1057 lines (932 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit OpenAI.Images;
interface
uses
System.Classes, System.SysUtils, System.Net.Mime, OpenAI.API.Params,
OpenAI.API;
{$SCOPEDENUMS ON}
type
TImageResponseFormat = (Url, B64Json);
TImageResponseFormatHelper = record helper for TImageResponseFormat
function ToString: string;
end;
TImageOutputFormat = (PNG, JPEG, WEBP);
TImageOutputFormatHelper = record helper for TImageOutputFormat
function ToString: string;
end;
TImageSize = (Auto, s256x256, s512x512, s1024x1024, s1792x1024, s1024x1792, s1536x1024, s1024x1536);
TImageSizeHelper = record helper for TImageSize
function ToString: string;
end;
TImageBackground = (Transparent, Opaque, Auto);
TImageBackgroundHelper = record helper for TImageBackground
function ToString: string;
class function FromString(const Value: string): TImageBackground; static;
end;
TImageModeration = (Low, Auto);
TImageModerationHelper = record helper for TImageModeration
function ToString: string;
end;
TImageQuality = (Auto, High, Medium, Low, HD, Standard);
TImageQualityHelper = record helper for TImageQuality
function ToString: string;
end;
TImageInputFidelity = (High, Low);
TImageInputFidelityHelper = record helper for TImageInputFidelity
function ToString: string;
end;
TImageCreateParams = class(TJSONParam)
/// <summary>
/// A text description of the desired image(s). The maximum length is 32000 characters for the GPT image models,
/// 1000 characters for dall-e-2 and 4000 characters for dall-e-3.
/// </summary>
function Prompt(const Value: string): TImageCreateParams; overload;
/// <summary>
/// Allows to set transparency for the background of the generated image(s).
/// This parameter is only supported for the GPT image models. Must be one of transparent,
/// opaque or auto (default value). When auto is used, the model will automatically determine
/// the best background for the image. </br>
/// If transparent, the output format needs to support transparency,
/// so it should be set to either png (default value) or webp.
/// </summary>
function Background(const Value: TImageBackground): TImageCreateParams;
/// <summary>
/// Control the content-moderation level for images generated by the GPT image models.
/// Must be either low for less restrictive filtering or auto (default value).
/// </summary>
function Moderation(const Value: TImageModeration): TImageCreateParams;
/// <summary>
/// The model to use for image generation. One of dall-e-2, dall-e-3,
/// or a GPT image model (gpt-image-1, gpt-image-1-mini, gpt-image-1.5).
/// Defaults to dall-e-2 unless a parameter specific to the GPT image models is used.
/// </summary>
function Model(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 is supported.
/// </br> minimum - 1 maximum - 10
/// </summary>
/// <summary>
function N(const Value: Integer = 1): TImageCreateParams;
/// <summary>
/// The quality of the image that will be generated.
/// <para> - auto (default value) will automatically select the best quality for the given model. </para>
/// <para> - high, medium and low are supported for the GPT image models. </para>
/// <para> - hd and standard are supported for dall-e-3. </para>
/// <para> - standard is the only option for dall-e-2. </para>
/// </summary>
function Quality(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The quality of the image that will be generated.
/// <para> - auto (default value) will automatically select the best quality for the given model. </para>
/// <para> - high, medium and low are supported for the GPT image models. </para>
/// <para> - hd and standard are supported for dall-e-3. </para>
/// <para> - standard is the only option for dall-e-2. </para>
/// </summary>
function Quality(const Value: TImageQuality): TImageCreateParams; overload;
/// <summary>
/// The compression level (0-100%) for the generated images.
/// This parameter is only supported for the GPT image models with the webp or jpeg output formats, and defaults to 100.
/// </summary>
function OutputCompression(const Value: Integer): TImageCreateParams;
/// <summary>
/// The size of the generated images. Must be one of 1024x1024, 1536x1024 (landscape), 1024x1536 (portrait),
/// or auto (default value) for the GPT image models, one of 256x256, 512x512, or 1024x1024 for dall-e-2,
/// and one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3.
/// </summary>
function Size(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The size of the generated images. Must be one of 1024x1024, 1536x1024 (landscape), 1024x1536 (portrait),
/// or auto (default value) for the GPT image models, one of 256x256, 512x512, or 1024x1024 for dall-e-2,
/// and one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3.
/// </summary>
function Size(const Value: TImageSize = TImageSize.s1024x1024): TImageCreateParams; overload;
/// <summary>
/// The format in which the generated images are returned. Must be one of "url" or "b64_json"
/// </summary>
function ResponseFormat(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The format in which the generated images are returned. This parameter is only supported for the GPT image models.
/// Must be one of png, jpeg, or webp.
/// </summary>
function OutputFormat(const Value: TImageOutputFormat): TImageCreateParams; overload;
/// <summary>
/// The format in which the generated images are returned. Must be one of "url" or "b64_json"
/// </summary>
function ResponseFormat(const Value: TImageResponseFormat = TImageResponseFormat.Url): TImageCreateParams; overload;
/// <summary>
/// The number of partial images to generate. This parameter is used for streaming responses that return partial images.
/// Value must be between 0 and 3. When set to 0, the response will be a single image sent in one streaming event.
/// Note that the final image may be sent before the full number of partial images are generated if
/// the full image is generated more quickly.
/// </br> maximum - 3 minimum - 0
/// </summary>
function PartialImages(const Value: Extended): TImageCreateParams; overload;
/// <summary>
/// The style of the generated images. This parameter is only supported for dall-e-3.
/// Must be one of vivid or natural. Vivid causes the model to lean towards generating hyper-real and
/// dramatic images. Natural causes the model to produce more natural, less hyper-real looking images.
/// </summary>
function Style(const Value: string): TImageCreateParams; overload;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
/// <seealso>https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids</seealso>
function User(const Value: string): TImageCreateParams;
end;
TImageAzureCreateParams = class(TJSONParam)
/// <summary>
/// A text description of the desired image(s) for the azure-environment. The maximum length is 1000 characters.
/// </summary>
function Caption(const Value: string): TImageAzureCreateParams; overload;
/// <summary>
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
/// </summary>
function Resolution(const Value: string): TImageAzureCreateParams; overload;
/// <summary>
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
/// </summary>
function Resolution(const Value: TImageSize = TImageSize.s256x256): TImageAzureCreateParams; overload;
/// <summary>
/// The format in which the generated images are returned. Must be one of url or b64_json
/// </summary>
function ResponseFormat(const Value: string): TImageAzureCreateParams; overload;
/// <summary>
/// The format in which the generated images are returned. Must be one of url or b64_json
/// </summary>
function ResponseFormat(const Value: TImageResponseFormat = TImageResponseFormat.Url): TImageAzureCreateParams; overload;
/// <summary>
/// The number of images to generate. Must be between 1 and 10.
/// </summary>
function N(const Value: Integer = 1): TImageAzureCreateParams;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
/// <seealso>https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids</seealso>
function User(const Value: string): TImageAzureCreateParams;
end;
TImageEditParams = class(TMultipartFormData)
/// <summary>
/// Input image references to edit. For GPT image models, you can provide up to 16 images.
/// </summary>
function Image(const FileName: TFileName): TImageEditParams; overload;
/// <summary>
/// Input image references to edit. For GPT image models, you can provide up to 16 images.
/// </summary>
function Image(const Stream: TStream; const FileName: TFileName): TImageEditParams; overload;
/// <summary>
/// Background behavior for generated image output.
/// </summary>
function Background(const Value: TImageBackground): TImageEditParams;
/// <summary>
/// Controls fidelity to the original input image(s).
/// </summary>
function InputFidelity(const Value: TImageInputFidelity): TImageEditParams;
/// <summary>
/// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited.
/// Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
/// </summary>
function Mask(const FileName: TFileName): TImageEditParams; overload;
/// <summary>
/// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited.
/// Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
/// </summary>
function Mask(const Stream: TStream; const FileName: TFileName): TImageEditParams; overload;
/// <summary>
/// The model to use for image editing. ("gpt-image-1.5" or "gpt-image-1" or "gpt-image-1-mini" or "chatgpt-image-latest")
/// </summary>
function Model(const Value: string): TImageEditParams; overload;
/// <summary>
/// Moderation level for GPT image models.
/// </summary>
function Moderation(const Value: TImageModeration): TImageEditParams;
/// <summary>
/// A text description of the desired image edit. (len 1 - 32000)
/// </summary>
function Prompt(const Value: string): TImageEditParams; overload;
/// <summary>
/// The quality of the image that will be generated.
/// <para> - auto (default value) will automatically select the best quality for the given model. </para>
/// <para> - high, medium and low are supported for the GPT image models. </para>
/// <para> - hd and standard are supported for dall-e-3. </para>
/// <para> - standard is the only option for dall-e-2. </para>
/// </summary>
function Quality(const Value: TImageQuality): TImageEditParams; overload;
/// <summary>
/// The number of edited images to generate. Must be between 1 and 10.
/// </summary>
function N(const Value: Integer = 1): TImageEditParams;
/// <summary>
/// Compression level for jpeg or webp output. (0 - 100)
/// </summary>
function OutputCompression(const Value: Integer): TImageEditParams;
/// <summary>
/// Output image format. Supported for GPT image models.
/// </summary>
function OutputFormat(const Value: TImageOutputFormat): TImageEditParams; overload;
/// <summary>
/// The number of partial images to generate. This parameter is used for streaming responses that return partial images.
/// Value must be between 0 and 3. When set to 0, the response will be a single image sent in one streaming event.
/// Note that the final image may be sent before the full number of partial images are generated if
/// the full image is generated more quickly.
/// </br> maximum - 3 minimum - 0
/// </summary>
function PartialImages(const Value: Extended): TImageEditParams; overload;
/// <summary>
/// The size of the generated images. Must be one of "256x256", "512x512", or "1024x1024".
/// </summary>
function Size(const Value: string): TImageEditParams; overload;
/// <summary>
/// The size of the generated images. Must be one of "256x256", "512x512", or "1024x1024".
/// </summary>
function Size(const Value: TImageSize = TImageSize.s256x256): TImageEditParams; overload;
/// <summary>
/// The format in which the generated images are returned. Must be one of "url" or "b64_json".
/// </summary>
function ResponseFormat(const Value: string): TImageEditParams; overload;
/// <summary>
/// The format in which the generated images are returned. Must be one of "url" or "b64_json".
/// </summary>
function ResponseFormat(const Value: TImageResponseFormat = TImageResponseFormat.Url): TImageEditParams; overload;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
/// <seealso>https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids</seealso>
function User(const Value: string): TImageEditParams;
constructor Create; reintroduce;
end;
TImageVariationParams = class(TMultipartFormData)
/// <summary>
/// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
/// </summary>
function Image(const FileName: TFileName): TImageVariationParams; overload;
/// <summary>
/// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
/// </summary>
function Image(const Stream: TStream; const FileName: TFileName): TImageVariationParams; overload;
/// <summary>
/// The model to use for image generation. Only "dall-e-2" is supported at this time.
/// </summary>
function Model(const Value: string): TImageVariationParams; overload;
/// <summary>
/// The number of images to generate. Must be between 1 and 10. For "dall-e-3", only "n=1" is supported.
/// </summary>
function N(const Value: Integer = 1): TImageVariationParams;
/// <summary>
/// The size of the generated images. Must be one of "256x256", "512x512", or "1024x1024".
/// </summary>
function Size(const Value: string): TImageVariationParams; overload;
/// <summary>
/// The size of the generated images. Must be one of "256x256", "512x512", or "1024x1024".
/// </summary>
function Size(const Value: TImageSize = TImageSize.s256x256): TImageVariationParams; overload;
/// <summary>
/// The format in which the generated images are returned. Must be one of "url" or "b64_json".
/// </summary>
function ResponseFormat(const Value: string): TImageVariationParams; overload;
/// <summary>
/// The format in which the generated images are returned. Must be one of "url" or "b64_json".
/// </summary>
function ResponseFormat(const Value: TImageResponseFormat = TImageResponseFormat.Url): TImageVariationParams; overload;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
/// <seealso>https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids</seealso>
function User(const Value: string): TImageVariationParams;
constructor Create; reintroduce;
end;
TOutputTokensDetails = class
private
FText_tokens: Int64;
FImage_tokens: Int64;
public
/// <summary>
/// The number of image output tokens generated by the model.
/// </summary>
property ImageTokens: Int64 read FImage_tokens write FImage_tokens;
/// <summary>
/// The number of text output tokens generated by the model.
/// </summary>
property TextTokens: Int64 read FText_tokens write FText_tokens;
end;
TInputTokensDetails = class
private
FText_tokens: Int64;
FImage_tokens: Int64;
public
/// <summary>
/// The number of image tokens in the input prompt.
/// </summary>
property ImageTokens: Int64 read FImage_tokens write FImage_tokens;
/// <summary>
/// The number of text tokens in the input prompt.
/// </summary>
property TextTokens: Int64 read FText_tokens write FText_tokens;
end;
/// <summary>
/// For gpt-image-1 only, the token usage information for the image generation.
/// </summary>
TImageUsage = class
private
FOutput_tokens: Int64;
FInput_tokens: Int64;
FTotal_tokens: Int64;
FOutput_tokens_details: TOutputTokensDetails;
FInput_tokens_details: TInputTokensDetails;
public
/// <summary>
/// The number of tokens (images and text) in the input prompt.
/// </summary>
property InputTokens: Int64 read FInput_tokens write FInput_tokens;
/// <summary>
/// The input tokens detailed information for the image generation.
/// </summary>
property InputTokensDetails: TInputTokensDetails read FInput_tokens_details write FInput_tokens_details;
/// <summary>
/// The number of output tokens generated by the model.
/// </summary>
property OutputTokens: Int64 read FOutput_tokens write FOutput_tokens;
/// <summary>
/// The total number of tokens (images and text) used for the image generation.
/// </summary>
property TotalTokens: Int64 read FTotal_tokens write FTotal_tokens;
/// <summary>
/// The output token details for the image generation.
/// </summary>
property OutputTokensDetails: TOutputTokensDetails read FOutput_tokens_details write FOutput_tokens_details;
destructor Destroy; override;
end;
/// <summary>
/// Represents the url or the content of an image generated by the OpenAI API.
/// </summary>
TImageData = class
private
FUrl: string;
FB64_json: string;
FRevised_prompt: string;
public
/// <summary>
/// When using dall-e-2 or dall-e-3, the URL of the generated image if response_format is set to url (default value).
/// Unsupported for the GPT image models.
/// </summary>
property Url: string read FUrl write FUrl;
/// <summary>
/// The base64-encoded JSON of the generated image. Returned by default for the GPT image models,
/// and only present if response_format is set to b64_json for dall-e-2 and dall-e-3.
/// </summary>
property B64Json: string read FB64_json write FB64_json;
/// <summary>
/// For dall-e-3 only, the revised prompt that was used to generate the image.
/// </summary>
property RevisedPrompt: string read FRevised_prompt write FRevised_prompt;
end;
TImageGenerations = class
private
FData: TArray<TImageData>;
FCreated: Int64;
FBackground: string;
FOutput_format: string;
FQuality: string;
FSize: string;
FUsage: TImageUsage;
function GetBackground: TImageBackground;
public
/// <summary>
/// The list of generated images.
/// </summary>
property Data: TArray<TImageData> read FData write FData;
/// <summary>
/// The Unix timestamp (in seconds) of when the image was created.
/// </summary>
property Created: Int64 read FCreated write FCreated;
/// <summary>
/// The background parameter used for the image generation. Either transparent or opaque.
/// </summary>
property Background: TImageBackground read GetBackground;
/// <summary>
/// The output format of the image generation. Either png, webp, or jpeg.
/// </summary>
property OutputFormat: string read FOutput_format write FOutput_format;
/// <summary>
/// The quality of the image generated. Either low, medium, or high.
/// </summary>
property Quality: string read FQuality write FQuality;
/// <summary>
/// The size of the image generated. Either 1024x1024, 1024x1536, or 1536x1024.
/// </summary>
property Size: string read FSize write FSize;
/// <summary>
/// For gpt-image-1 only, the token usage information for the image generation.
/// </summary>
property Usage: TImageUsage read FUsage write FUsage;
destructor Destroy; override;
end;
TAzureError = class
private
FCode: string;
FMessage: string;
public
property Code: string read FCode write FCode;
property Message: string read FMessage write FMessage;
end;
TAzureImageData = class
private
FCaption: string;
FContentURL: string;
FContentURLExpiresAt: string;
FCreatedDateTime: string;
public
property Caption: string read FCaption write FCaption;
property ContentURL: string read FContentURL write FContentURL;
property ContentURLExpiresAt: string read FContentURLExpiresAt write FContentURLExpiresAt;
property CreatedDateTime: string read FCreatedDateTime write FCreatedDateTime;
end;
TAzureImageResponse = class
private
FID: string;
FStatus: string;
FResult: TAzureImageData;
FError: TAzureError;
public
destructor Destroy; override;
property Result: TAzureImageData read FResult write FResult;
property Error: TAzureError read FError write FError;
property ID: string read FID write FID;
property Status: string read FStatus write FStatus;
end;
TImagesRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Creates an image given a prompt.
/// </summary>
function Create(ParamProc: TProc<TImageCreateParams>): TImageGenerations;
/// <summary>
/// Creates an edited or extended image given an original image and a prompt.
/// </summary>
function Edit(ParamProc: TProc<TImageEditParams>): TImageGenerations;
/// <summary>
/// Creates a variation of a given image.
/// </summary>
function Variation(ParamProc: TProc<TImageVariationParams>): TImageGenerations;
end;
TCancelCallback = reference to procedure(var Cancel: Boolean);
TImagesAzureRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Creates an image given a prompt.
/// </summary>
function Create(ParamProc: TProc<TImageAzureCreateParams>; CancelCallback: TCancelCallback = nil): TAzureImageResponse;
end;
const
AzureFailed = 'Failed';
AzureSuccessed = 'Succeeded';
implementation
{ TImagesRoute }
function TImagesRoute.Create(ParamProc: TProc<TImageCreateParams>): TImageGenerations;
begin
Result := API.Post<TImageGenerations, TImageCreateParams>('images/generations', ParamProc);
end;
function TImagesRoute.Edit(ParamProc: TProc<TImageEditParams>): TImageGenerations;
begin
Result := API.PostForm<TImageGenerations, TImageEditParams>('images/edits', ParamProc);
end;
function TImagesRoute.Variation(ParamProc: TProc<TImageVariationParams>): TImageGenerations;
begin
Result := API.PostForm<TImageGenerations, TImageVariationParams>('images/variations', ParamProc);
end;
{ TImageGenerations }
destructor TImageGenerations.Destroy;
var
Item: TImageData;
begin
for Item in FData do
Item.Free;
FUsage.Free;
inherited;
end;
function TImageGenerations.GetBackground: TImageBackground;
begin
Result := TImageBackground.FromString(FBackground);
end;
{ TImageCreateParams }
function TImageCreateParams.Background(const Value: TImageBackground): TImageCreateParams;
begin
Result := TImageCreateParams(Add('background', Value.ToString));
end;
function TImageCreateParams.Model(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('model', Value));
end;
function TImageCreateParams.Moderation(const Value: TImageModeration): TImageCreateParams;
begin
Result := TImageCreateParams(Add('moderation', Value.ToString));
end;
function TImageCreateParams.N(const Value: Integer): TImageCreateParams;
begin
Result := TImageCreateParams(Add('n', Value));
end;
function TImageCreateParams.OutputCompression(const Value: Integer): TImageCreateParams;
begin
Result := TImageCreateParams(Add('output_compression', Value));
end;
function TImageCreateParams.OutputFormat(const Value: TImageOutputFormat): TImageCreateParams;
begin
Result := TImageCreateParams(Add('output_format', Value.ToString));
end;
function TImageCreateParams.PartialImages(const Value: Extended): TImageCreateParams;
begin
Result := TImageCreateParams(Add('partial_images', Value));
end;
function TImageCreateParams.Prompt(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('prompt', Value));
end;
function TImageCreateParams.Quality(const Value: TImageQuality): TImageCreateParams;
begin
Result := TImageCreateParams(Add('quality', Value.ToString));
end;
function TImageCreateParams.Quality(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('quality', Value));
end;
function TImageCreateParams.ResponseFormat(const Value: TImageResponseFormat): TImageCreateParams;
begin
Result := ResponseFormat(Value.ToString);
end;
function TImageCreateParams.Size(const Value: TImageSize): TImageCreateParams;
begin
Result := Size(Value.ToString);
end;
function TImageCreateParams.Style(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('style', Value));
end;
function TImageCreateParams.Size(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('size', Value));
end;
function TImageCreateParams.ResponseFormat(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('response_format', Value));
end;
function TImageCreateParams.User(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('user', Value));
end;
{ TImageEditParams }
function TImageEditParams.Image(const FileName: TFileName): TImageEditParams;
begin
AddFile('image', FileName);
Result := Self;
end;
function TImageEditParams.Background(const Value: TImageBackground): TImageEditParams;
begin
AddField('background', Value.ToString);
Result := Self;
end;
constructor TImageEditParams.Create;
begin
inherited Create(True);
end;
function TImageEditParams.Image(const Stream: TStream; const FileName: TFileName): TImageEditParams;
begin
{$IF CompilerVersion <= 35}
AddStream('image', Stream, FileName);
{$ELSE}
AddStream('image', Stream, False, FileName);
{$ENDIF}
Result := Self;
end;
function TImageEditParams.InputFidelity(const Value: TImageInputFidelity): TImageEditParams;
begin
AddField('input_fidelity', Value.ToString);
Result := Self;
end;
function TImageEditParams.Mask(const Stream: TStream; const FileName: TFileName): TImageEditParams;
begin
{$IF CompilerVersion <= 35}
AddStream('mask', Stream, FileName);
{$ELSE}
AddStream('mask', Stream, False, FileName);
{$ENDIF}
Result := Self;
end;
function TImageEditParams.Model(const Value: string): TImageEditParams;
begin
AddFile('model', Value);
Result := Self;
end;
function TImageEditParams.Moderation(const Value: TImageModeration): TImageEditParams;
begin
AddField('moderation', Value.ToString);
Result := Self;
end;
function TImageEditParams.Mask(const FileName: TFileName): TImageEditParams;
begin
AddFile('mask', FileName);
Result := Self;
end;
function TImageEditParams.N(const Value: Integer): TImageEditParams;
begin
AddField('n', Value.ToString);
Result := Self;
end;
function TImageEditParams.OutputCompression(const Value: Integer): TImageEditParams;
begin
AddField('output_compression', Value.ToString);
Result := Self;
end;
function TImageEditParams.OutputFormat(const Value: TImageOutputFormat): TImageEditParams;
begin
AddField('output_format', Value.ToString);
Result := Self;
end;
function TImageEditParams.PartialImages(const Value: Extended): TImageEditParams;
begin
AddField('partial_images', Value.ToString);
Result := Self;
end;
function TImageEditParams.Prompt(const Value: string): TImageEditParams;
begin
AddField('prompt', Value);
Result := Self;
end;
function TImageEditParams.Quality(const Value: TImageQuality): TImageEditParams;
begin
AddField('quality', Value.ToString);
Result := Self;
end;
function TImageEditParams.ResponseFormat(const Value: string): TImageEditParams;
begin
AddField('response_format', Value);
Result := Self;
end;
function TImageEditParams.ResponseFormat(const Value: TImageResponseFormat): TImageEditParams;
begin
AddField('response_format', Value.ToString);
Result := Self;
end;
function TImageEditParams.Size(const Value: string): TImageEditParams;
begin
AddField('size', Value);
Result := Self;
end;
function TImageEditParams.Size(const Value: TImageSize): TImageEditParams;
begin
AddField('size', Value.ToString);
Result := Self;
end;
function TImageEditParams.User(const Value: string): TImageEditParams;
begin
AddField('user', Value);
Result := Self;
end;
{ TImageVariationParams }
function TImageVariationParams.Image(const FileName: TFileName): TImageVariationParams;
begin
AddFile('image', FileName);
Result := Self;
end;
constructor TImageVariationParams.Create;
begin
inherited Create(true);
end;
function TImageVariationParams.Image(const Stream: TStream; const FileName: TFileName): TImageVariationParams;
begin
{$IF CompilerVersion <= 35}
AddStream('image', Stream, FileName);
{$ELSE}
AddStream('image', Stream, False, FileName);
{$ENDIF}
Result := Self;
end;
function TImageVariationParams.Model(const Value: string): TImageVariationParams;
begin
AddField('model', Value);
Result := Self;
end;
function TImageVariationParams.N(const Value: Integer): TImageVariationParams;
begin
AddField('n', Value.ToString);
Result := Self;
end;
function TImageVariationParams.ResponseFormat(const Value: string): TImageVariationParams;
begin
AddField('response_format', Value);
Result := Self;
end;
function TImageVariationParams.ResponseFormat(const Value: TImageResponseFormat): TImageVariationParams;
begin
AddField('response_format', Value.ToString);
Result := Self;
end;
function TImageVariationParams.Size(const Value: string): TImageVariationParams;
begin
AddField('size', Value);
Result := Self;
end;
function TImageVariationParams.Size(const Value: TImageSize = TImageSize.s256x256): TImageVariationParams;
begin
AddField('size', Value.ToString);
Result := Self;
end;
function TImageVariationParams.User(const Value: string): TImageVariationParams;
begin
AddField('user', Value);
Result := Self;
end;
{ TImageResponseFormatHelper }
function TImageResponseFormatHelper.ToString: string;
begin
case Self of
TImageResponseFormat.Url:
Result := 'url';
TImageResponseFormat.B64Json:
Result := 'b64_json';
end;
end;
{ TImageSizeHelper }
function TImageSizeHelper.ToString: string;
begin
case Self of
TImageSize.Auto:
Result := 'auto';
TImageSize.s1536x1024:
Result := '1536x1024';
TImageSize.s1024x1536:
Result := '1024x1536';
TImageSize.s256x256:
Result := '256x256';
TImageSize.s512x512:
Result := '512x512';
TImageSize.s1024x1024:
Result := '1024x1024';
TImageSize.s1792x1024:
Result := '1792x1024';
TImageSize.s1024x1792:
Result := '1024x1792';
end;
end;
{ TAzureImageResponse }
destructor TAzureImageResponse.Destroy;
begin
if Assigned(FResult) then
FResult.Free;
if Assigned(FError) then
FError.Free;
inherited;
end;
{ TImagesAzureRoute }
function TImagesAzureRoute.Create(ParamProc: TProc<TImageAzureCreateParams>; CancelCallback: TCancelCallback): TAzureImageResponse;
const
Timeout: Integer = 20000; // 20 second timeout
PollInterval: Integer = 1000; // poll for image once per second
var
StartTime: UInt64;
Cancel: Boolean;
OperationID: string;
begin
// First place the task with POST
Result := API.Post<TAzureImageResponse, TImageAzureCreateParams>('text-to-image', ParamProc);
// Check if we got a valid id - current azure documentation is not that precise here if we always get "NotStarted".
// Otherwise we get "failed" and in the "error"-property we can find "code" and "message" of the error
if (Result.ID = '') or (Result.Status = AzureFailed) then
Exit;
// Timeout timestamp
{$IF CompilerVersion > 34}
StartTime := TThread.GetTickCount64;
{$ELSE}
StartTime := TThread.GetTickCount;
{$ENDIF}
Cancel := False;
OperationID := Result.ID;
// Repeat GET requesting the operations-endpoint until we have a "succeeded" response
while not Cancel do
begin
Result.Free;
Result := API.Get<TAzureImageResponse>('text-to-image/operations/' + OperationID);
// The TAzureImageResponse holds an error object in this case that can be analyzed by the developer
if (Result.Status = AzureSuccessed) or (Result.Status = AzureFailed) then
Exit;
// Check timeout - current documentation is not precise what to expect when the state is "inProgress"
// but the result at this point should contain all relevant information
{$IF CompilerVersion > 34}
if TThread.GetTickCount64 - StartTime > Timeout then
Exit;
{$ELSE}
if TThread.GetTickCount - StartTime > Timeout then
Exit;
{$ENDIF}
Sleep(PollInterval);
if Assigned(CancelCallback) then
CancelCallback(Cancel);
end;
end;
{ TImageAzureCreateParams }
function TImageAzureCreateParams.Caption(const Value: string): TImageAzureCreateParams;
begin
Result := TImageAzureCreateParams(Add('caption', Value));
end;
function TImageAzureCreateParams.N(const Value: Integer): TImageAzureCreateParams;
begin
Result := TImageAzureCreateParams(Add('n', Value));
end;
function TImageAzureCreateParams.ResponseFormat(const Value: string): TImageAzureCreateParams;
begin
Result := TImageAzureCreateParams(Add('response_format', Value));
end;
function TImageAzureCreateParams.ResponseFormat(const Value: TImageResponseFormat): TImageAzureCreateParams;
begin
Result := ResponseFormat(Value.ToString);
end;
function TImageAzureCreateParams.Resolution(const Value: string): TImageAzureCreateParams;
begin
Result := TImageAzureCreateParams(Add('resolution', Value));
end;
function TImageAzureCreateParams.Resolution(const Value: TImageSize): TImageAzureCreateParams;
begin
Result := Resolution(Value.ToString);
end;
function TImageAzureCreateParams.User(const Value: string): TImageAzureCreateParams;
begin
Result := TImageAzureCreateParams(Add('user', Value));
end;
{ TImageBackgroundHelper }
class function TImageBackgroundHelper.FromString(const Value: string): TImageBackground;
begin
if Value = 'transparent' then
Exit(TImageBackground.Transparent)
else if Value = 'opaque' then
Exit(TImageBackground.Opaque)
else if Value = 'auto' then
Exit(TImageBackground.Auto)
else
Exit(TImageBackground.Auto)
end;
function TImageBackgroundHelper.ToString: string;
begin
case Self of
TImageBackground.Transparent:
Result := 'transparent';
TImageBackground.Opaque:
Result := 'opaque';
TImageBackground.Auto:
Result := 'auto';
end;
end;
{ TImageModerationHelper }
function TImageModerationHelper.ToString: string;
begin
case Self of
TImageModeration.Low:
Result := 'low';
TImageModeration.Auto:
Result := 'auto';
end;
end;