forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAI.Images.pas
349 lines (298 loc) · 10.8 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
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;
TImageSize = (x256, x512, x1024);
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.
/// </summary>
function Prompt(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
/// </summary>
function Size(const Value: string): TImageCreateParams; overload;
/// <summary>
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
/// </summary>
function Size(const Value: TImageSize = TImageSize.x256): 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 number of images to generate. Must be between 1 and 10.
/// </summary>
function N(const Value: Integer = 1): TImageCreateParams;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
function User(const Value: string): TImageCreateParams;
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: string): 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: string): 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: string): 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: 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 = '1024x1024'): 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 = 'url'): TImageEditParams;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
function User(const Value: string): TImageEditParams;
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: string): 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: string): TImageVariationParams; overload;
/// <summary>
/// The number of images to generate. Must be between 1 and 10.
/// </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 = '1024x1024'): 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 = 'url'): TImageVariationParams;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
function User(const Value: string): TImageVariationParams;
end;
TImageData = class
private
FUrl: string;
FB64_json: string;
public
property Url: string read FUrl write FUrl;
property B64Json: string read FB64_json write FB64_json;
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;
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;
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
if Assigned(Item) then
Item.Free;
inherited;
end;
{ TImageCreateParams }
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.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.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: string): TImageEditParams;
begin
AddFile('image', FileName);
Result := Self;
end;
function TImageEditParams.Image(const Stream: TStream; const FileName: string): TImageEditParams;
begin
AddStream('image', Stream, FileName);
Result := Self;
end;
function TImageEditParams.Mask(const Stream: TStream; const FileName: string): TImageEditParams;
begin
AddStream('mask', Stream, FileName);
Result := Self;
end;
function TImageEditParams.Mask(const FileName: string): 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.Size(const Value: string): TImageEditParams;
begin
AddField('size', Value);
Result := Self;
end;
function TImageEditParams.User(const Value: string): TImageEditParams;
begin
AddField('user', Value);
Result := Self;
end;
{ TImageVariationParams }
function TImageVariationParams.Image(const FileName: string): TImageVariationParams;
begin
AddFile('image', FileName);
Result := Self;
end;
function TImageVariationParams.Image(const Stream: TStream; const FileName: string): TImageVariationParams;
begin
AddStream('image', Stream, FileName);
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.Size(const Value: string): TImageVariationParams;
begin
AddField('size', Value);
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.x256:
Result := '256x256';
TImageSize.x512:
Result := '512x512';
TImageSize.x1024:
Result := '1024x1024';
end;
end;
end.