3131% Copyright 2024 The MathWorks, Inc.
3232
3333 properties (SetAccess = private )
34- % MODELNAME Model name.
34+ % ModelName Model name.
3535 ModelName
3636
37- % TIMEOUT Connection timeout in seconds (default 10 secs)
37+ % TimeOut Connection timeout in seconds (default 10 secs)
3838 TimeOut
3939 end
4040
7777 % 1792x1024, or 1024x1792
7878 %
7979 % Quality - Quality of the images to generate.
80- % "standard" (default) or 'hd' .
80+ % "standard" (default) or "hd" .
8181 % Only "dall-e-3" supports this parameter.
8282 %
8383 % Style - The style of the generated images.
8989 prompt {mustBeTextScalar }
9090 nvp.NumImages (1 ,1 ) {mustBePositive , mustBeInteger ,...
9191 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" , ...
9393 " 1024x1024" , " 1792x1024" , ...
9494 " 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" ])}
9797 end
9898
9999 endpoint = " https://api.openai.com/v1/images/generations" ;
106106 " n" ,nvp .NumImages ,...
107107 " size" ,nvp .Size );
108108
109- if this .ModelName ~= " dall-e-3 "
109+ if this .ModelName == " dall-e-2 "
110110 % dall-e-3 only params
111111 if isfield(nvp , " Quality" )
112112 error(" llms:invalidOptionForModel" , ...
180180 nvp.MaskImagePath {mustBeValidFileType(nvp .MaskImagePath )}
181181 nvp.NumImages (1 ,1 ) {mustBePositive , mustBeInteger ,...
182182 mustBeLessThanOrEqual(nvp .NumImages ,10)} = 1
183- nvp.Size (1 ,1 ) {mustBeMember(nvp .Size ,[" 256x256" , ...
183+ nvp.Size (1 ,1 ) string {mustBeMember(nvp .Size ,[" 256x256" , ...
184184 " 512x512" , ...
185185 " 1024x1024" ])} = " 1024x1024"
186186 end
197197 endpoint = ' https://api.openai.com/v1/images/edits' ;
198198
199199 % Required params
200+ numImages = num2str(nvp .NumImages );
200201 body = matlab .net .http .io .MultipartFormProvider( ...
201202 ' 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 ));
209206
210207 % Optional param
211208 if isfield(nvp ," MaskImagePath" )
230227 % and square.
231228 %
232229 % [IMAGES, RESPONSE] = createVariation(__, Name=Value) specifies
233- % additiona options.
230+ % additional options.
234231 %
235232 % NumImages - Number of images to generate.
236233 % Default value is 1. The max is 10.
237234 %
238235 % 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)
241238
242239 arguments
243240 this (1 ,1 ) openAIImages
244241 imagePath {mustBeValidFileType(imagePath )}
245242 nvp.NumImages (1 ,1 ) {mustBePositive , mustBeInteger ,...
246243 mustBeLessThanOrEqual(nvp .NumImages ,10)} = 1
247- nvp.Size (1 ,1 ) {mustBeMember(nvp .Size ,[" 256x256" , ...
244+ nvp.Size (1 ,1 ) string {mustBeMember(nvp .Size ,[" 256x256" , ...
248245 " 512x512" ," 1024x1024" ])} = " 1024x1024"
249246 end
250247
257254
258255 endpoint = ' https://api.openai.com/v1/images/variations' ;
259256
260- body = matlab .net .http .io .MultipartFormProvider(' image' , ...
261- matlab .net .http .io .FileProvider(imagePath ));
262-
263- body.Names = [body .Names ," n" ];
264257 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 )) ;
269262
270263 % Send the HTTP Request
271264 response = sendRequest(this , endpoint , body );
274267 end
275268
276269 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 );
278272 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' );
283274 end
284275 request = matlab .net .http .RequestMessage(' post' , headers , body );
285276
286- % Create a HTTPOptions object;
287277 httpOpts = matlab .net .http .HTTPOptions ;
288278 httpOpts.ConnectTimeout = this .TimeOut ;
289279 response = send(request , matlab .net .URI(endpoint ), httpOpts );
@@ -304,14 +294,11 @@ function mustBeValidSize(this, imagesize)
304294
305295function images = extractImages(response )
306296
307- if response .StatusCode ==" OK"
297+ if response .StatusCode ==" OK" && isfield( response . Body . Data . data , " url " )
308298 % Output the images
309299 if isfield(response .Body .Data .data ," url" )
310300 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 );
315302 else
316303 images = [];
317304 end
@@ -322,13 +309,13 @@ function mustBeValidSize(this, imagesize)
322309end
323310
324311function validateSizeNVP(model , size )
325- if ismember(size ,[" 1792x1024" , " 1024x1792" ]) && model ~= " dall-e-3 "
312+ if ismember(size ,[" 1792x1024" , " 1024x1792" ]) && model == " dall-e-2 "
326313 error(" llms:invalidOptionAndValueForModel" , ...
327314 llms .utils .errorMessageCatalog .getMessage(" llms:invalidOptionAndValueForModel" , ...
328315 " Size" , size , model ));
329316end
330317
331- if ismember(size ,[" 256x256" , " 512x512" ]) && model ~= " dall-e-2 "
318+ if ismember(size ,[" 256x256" , " 512x512" ]) && model == " dall-e-3 "
332319 error(" llms:invalidOptionAndValueForModel" , ...
333320 llms .utils .errorMessageCatalog .getMessage(" llms:invalidOptionAndValueForModel" , ...
334321 " Size" , size , model ));
0 commit comments