forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAI.Images.pas
659 lines (574 loc) · 22.3 KB
/
OpenAI.Images.pas
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
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;
{ | dall-e-2 | dall-e-3 | }
TImageSize = (s256x256, s512x512, s1024x1024, s1792x1024, s1024x1792);
TImageSizeHelper = record helper for TImageSize
function ToString: string;
end;
TImageCreateParams = class(TJSONParam)
/// <summary>
/// A text description of the desired image(s).
/// The maximum length is 1000 characters for "dall-e-2" and 4000 characters for "dall-e-3".
/// </summary>
function Prompt(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The model to use for image generation. Defaults to "dall-e-2".
/// </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.
/// </summary>
/// <summary>
function N(const Value: Integer = 1): TImageCreateParams;
/// The quality of the image that will be generated.
/// "hd" creates images with finer details and greater consistency across the image.
/// This param is only supported for "dall-e-3".
/// </summary>
function Quality(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The size of the generated images. Must be one of "256x256", "512x512", or "1024x1024" for "dall-e-2".
/// Must be one of "1024x1024", "1792x1024", or "1024x1792" for "dall-e-3" models.
/// </summary>
function Size(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The size of the generated images. Must be one of "256x256", "512x512", or "1024x1024" for "dall-e-2".
/// Must be one of "1024x1024", "1792x1024", or "1024x1792" for "dall-e-3" models.
/// </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. Must be one of "url" or "b64_json"
/// </summary>
function ResponseFormat(const Value: TImageResponseFormat = TImageResponseFormat.Url): TImageCreateParams; overload;
/// <summary>
/// The style of the generated images. 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.
/// This param is only supported for "dall-e-3". Defaults to "vivid".
/// </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>
/// The image to edit. Must be a valid PNG file, less than 4MB, and square.
/// If mask is not provided, image must have transparency, which will be used as the mask.
/// </summary>
function Image(const FileName: TFileName): TImageEditParams; overload;
/// <summary>
/// The image to edit. Must be a valid PNG file, less than 4MB, and square.
/// If mask is not provided, image must have transparency, which will be used as the mask.
/// </summary>
function Image(const Stream: TStream; 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 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 generation. Only "dall-e-2" is supported at this time.
/// </summary>
function Model(const Value: string): TImageEditParams; overload;
/// <summary>
/// A text description of the desired image(s). The maximum length is 1000 characters.
/// </summary>
function Prompt(const Value: string): TImageEditParams; overload;
/// <summary>
/// The number of images to generate. Must be between 1 and 10.
/// </summary>
function N(const Value: Integer = 1): TImageEditParams;
/// <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;
/// <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>
/// The URL of the generated image, if response_format is url (default).
/// </summary>
property Url: string read FUrl write FUrl;
/// <summary>
/// The base64-encoded JSON of the generated image, if response_format is b64_json.
/// </summary>
property B64Json: string read FB64_json write FB64_json;
/// <summary>
/// The prompt that was used to generate the image, if there was any revision to the prompt.
/// </summary>
property RevisedPrompt: string read FRevised_prompt write FRevised_prompt;
end;
TImageGenerations = class
private
FData: TArray<TImageData>;
FCreated: Int64;
public
property Data: TArray<TImageData> read FData write FData;
property Created: Int64 read FCreated write FCreated;
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;
inherited;
end;
{ TImageCreateParams }
function TImageCreateParams.Model(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('model', Value));
end;
function TImageCreateParams.N(const Value: Integer): TImageCreateParams;
begin
Result := TImageCreateParams(Add('n', Value));
end;
function TImageCreateParams.Prompt(const Value: string): TImageCreateParams;
begin
Result := TImageCreateParams(Add('prompt', Value));
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;
constructor TImageEditParams.Create;
begin
inherited Create(true);
end;
function TImageEditParams.Image(const Stream: TStream; const FileName: TFileName): TImageEditParams;
begin
AddStream('image', Stream, FileName);
Result := Self;
end;
function TImageEditParams.Mask(const Stream: TStream; const FileName: TFileName): TImageEditParams;
begin
AddStream('mask', Stream, FileName);
Result := Self;
end;
function TImageEditParams.Model(const Value: string): TImageEditParams;
begin
AddFile('model', Value);
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.Prompt(const Value: string): TImageEditParams;
begin
AddField('prompt', Value);
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
AddStream('image', Stream, FileName);
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.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;
end.