31
31
% Copyright 2024 The MathWorks, Inc.
32
32
33
33
properties (SetAccess = private )
34
- % MODELNAME Model name.
34
+ % ModelName Model name.
35
35
ModelName
36
36
37
- % TIMEOUT Connection timeout in seconds (default 10 secs)
37
+ % TimeOut Connection timeout in seconds (default 10 secs)
38
38
TimeOut
39
39
end
40
40
77
77
% 1792x1024, or 1024x1792
78
78
%
79
79
% Quality - Quality of the images to generate.
80
- % "standard" (default) or 'hd' .
80
+ % "standard" (default) or "hd" .
81
81
% Only "dall-e-3" supports this parameter.
82
82
%
83
83
% Style - The style of the generated images.
89
89
prompt {mustBeTextScalar }
90
90
nvp.NumImages (1 ,1 ) {mustBePositive , mustBeInteger ,...
91
91
mustBeLessThanOrEqual(nvp .NumImages ,10)} = 1
92
- nvp.Size (1 ,1 ) {mustBeMember(nvp .Size , [" 256x256" , " 512x512" , ...
92
+ nvp.Size (1 ,1 ) string {mustBeMember(nvp .Size , [" 256x256" , " 512x512" , ...
93
93
" 1024x1024" , " 1792x1024" , ...
94
94
" 1024x1792" ])} = " 1024x1024"
95
- nvp.Quality (1 ,1 ) {mustBeMember(nvp .Quality ,[" standard" , " hd" ])}
96
- nvp.Style (1 ,1 ) {mustBeMember(nvp .Style ,[" vivid" , " natural" ])}
95
+ nvp.Quality (1 ,1 ) string {mustBeMember(nvp .Quality ,[" standard" , " hd" ])}
96
+ nvp.Style (1 ,1 ) string {mustBeMember(nvp .Style ,[" vivid" , " natural" ])}
97
97
end
98
98
99
99
endpoint = " https://api.openai.com/v1/images/generations" ;
106
106
" n" ,nvp .NumImages ,...
107
107
" size" ,nvp .Size );
108
108
109
- if this .ModelName ~= " dall-e-3 "
109
+ if this .ModelName == " dall-e-2 "
110
110
% dall-e-3 only params
111
111
if isfield(nvp , " Quality" )
112
112
error(" llms:invalidOptionForModel" , ...
180
180
nvp.MaskImagePath {mustBeValidFileType(nvp .MaskImagePath )}
181
181
nvp.NumImages (1 ,1 ) {mustBePositive , mustBeInteger ,...
182
182
mustBeLessThanOrEqual(nvp .NumImages ,10)} = 1
183
- nvp.Size (1 ,1 ) {mustBeMember(nvp .Size ,[" 256x256" , ...
183
+ nvp.Size (1 ,1 ) string {mustBeMember(nvp .Size ,[" 256x256" , ...
184
184
" 512x512" , ...
185
185
" 1024x1024" ])} = " 1024x1024"
186
186
end
197
197
endpoint = ' https://api.openai.com/v1/images/edits' ;
198
198
199
199
% Required params
200
+ numImages = num2str(nvp .NumImages );
200
201
body = matlab .net .http .io .MultipartFormProvider( ...
201
202
' image' ,matlab .net .http .io .FileProvider(imagePath ), ...
202
- ' prompt' ,matlab .net .http .io .FormProvider(prompt ));
203
-
204
- body.Names = [body .Names ," n" ];
205
- numImages = num2str(nvp .NumImages );
206
- body.Parts = [body .Parts ,{matlab .net .http .io .FormProvider(numImages )}];
207
- body.Names = [body .Names ," size" ];
208
- body.Parts = [body .Parts ,{matlab .net .http .io .FormProvider(nvp .Size )}];
203
+ ' prompt' ,matlab .net .http .io .FormProvider(prompt ), ...
204
+ ' n' ,matlab .net .http .io .FormProvider(numImages ),...
205
+ ' size' ,matlab .net .http .io .FormProvider(nvp .Size ));
209
206
210
207
% Optional param
211
208
if isfield(nvp ," MaskImagePath" )
230
227
% and square.
231
228
%
232
229
% [IMAGES, RESPONSE] = createVariation(__, Name=Value) specifies
233
- % additiona options.
230
+ % additional options.
234
231
%
235
232
% NumImages - Number of images to generate.
236
233
% Default value is 1. The max is 10.
237
234
%
238
235
% Size - Size of the generated images.
239
- % Must be one of 256x256, 512x512, or
240
- % 1024x1024 (default)
236
+ % Must be one of " 256x256", " 512x512" , or
237
+ % " 1024x1024" (default)
241
238
242
239
arguments
243
240
this (1 ,1 ) openAIImages
244
241
imagePath {mustBeValidFileType(imagePath )}
245
242
nvp.NumImages (1 ,1 ) {mustBePositive , mustBeInteger ,...
246
243
mustBeLessThanOrEqual(nvp .NumImages ,10)} = 1
247
- nvp.Size (1 ,1 ) {mustBeMember(nvp .Size ,[" 256x256" , ...
244
+ nvp.Size (1 ,1 ) string {mustBeMember(nvp .Size ,[" 256x256" , ...
248
245
" 512x512" ," 1024x1024" ])} = " 1024x1024"
249
246
end
250
247
257
254
258
255
endpoint = ' https://api.openai.com/v1/images/variations' ;
259
256
260
- body = matlab .net .http .io .MultipartFormProvider(' image' , ...
261
- matlab .net .http .io .FileProvider(imagePath ));
262
-
263
- body.Names = [body .Names ," n" ];
264
257
numImages = num2str(nvp .NumImages );
265
- body.Parts = [ body . Parts ,{ matlab .net .http .io .FormProvider( numImages )}];
266
-
267
- body.Names = [ body . Names , " size " ];
268
- body.Parts = [ body . Parts ,{ matlab .net .http .io .FormProvider(nvp .Size )}] ;
258
+ body = matlab .net .http .io .MultipartFormProvider(...
259
+ ' image ' , matlab . net . http . io .FileProvider( imagePath ),...
260
+ ' n ' , matlab . net . http . io .FormProvider( numImages ),...
261
+ ' size ' , matlab .net .http .io .FormProvider(nvp .Size )) ;
269
262
270
263
% Send the HTTP Request
271
264
response = sendRequest(this , endpoint , body );
274
267
end
275
268
276
269
function response = sendRequest(this , endpoint , body )
277
- % getResponse get the response from the given endpoint
270
+ % sendRequest send request to the given endpoint, return response
271
+ headers = matlab .net .http .HeaderField(' Authorization' , " Bearer " + this .ApiKey );
278
272
if isa(body ,' struct' )
279
- headers = matlab .net .http .HeaderField(' Content-Type' , ' application/json' );
280
- headers(2 ) = matlab .net .http .HeaderField(' Authorization' , " Bearer " + this .ApiKey );
281
- else
282
- headers = matlab .net .http .HeaderField(' Authorization' , " Bearer " + this .ApiKey );
273
+ headers(2 ) = matlab .net .http .HeaderField(' Content-Type' , ' application/json' );
283
274
end
284
275
request = matlab .net .http .RequestMessage(' post' , headers , body );
285
276
286
- % Create a HTTPOptions object;
287
277
httpOpts = matlab .net .http .HTTPOptions ;
288
278
httpOpts.ConnectTimeout = this .TimeOut ;
289
279
response = send(request , matlab .net .URI(endpoint ), httpOpts );
@@ -304,14 +294,11 @@ function mustBeValidSize(this, imagesize)
304
294
305
295
function images = extractImages(response )
306
296
307
- if response .StatusCode ==" OK"
297
+ if response .StatusCode ==" OK" && isfield( response . Body . Data . data , " url " )
308
298
% Output the images
309
299
if isfield(response .Body .Data .data ," url" )
310
300
urls = arrayfun(@(x ) string(x .url ), response .Body .Data .data );
311
- images = cell(1 ,numel(urls ));
312
- for ii = 1 : numel(urls )
313
- images{ii } = imread(urls(ii ));
314
- end
301
+ images = arrayfun(@imread ,urls ,UniformOutput= false );
315
302
else
316
303
images = [];
317
304
end
@@ -322,13 +309,13 @@ function mustBeValidSize(this, imagesize)
322
309
end
323
310
324
311
function validateSizeNVP(model , size )
325
- if ismember(size ,[" 1792x1024" , " 1024x1792" ]) && model ~= " dall-e-3 "
312
+ if ismember(size ,[" 1792x1024" , " 1024x1792" ]) && model == " dall-e-2 "
326
313
error(" llms:invalidOptionAndValueForModel" , ...
327
314
llms .utils .errorMessageCatalog .getMessage(" llms:invalidOptionAndValueForModel" , ...
328
315
" Size" , size , model ));
329
316
end
330
317
331
- if ismember(size ,[" 256x256" , " 512x512" ]) && model ~= " dall-e-2 "
318
+ if ismember(size ,[" 256x256" , " 512x512" ]) && model == " dall-e-3 "
332
319
error(" llms:invalidOptionAndValueForModel" , ...
333
320
llms .utils .errorMessageCatalog .getMessage(" llms:invalidOptionAndValueForModel" , ...
334
321
" Size" , size , model ));
0 commit comments