@@ -267,14 +267,36 @@ export class OpenAI {
267267 * https://platform.openai.com/docs/api-reference/images/create-edit
268268 */
269269 async createImageEdit ( options : ImageEditOptions ) : Promise < Image > {
270- return await this . #request( `/images/edits` , {
271- image : options . image ,
272- mask : options . mask ,
273- prompt : options . prompt ,
274- n : options . n ,
275- size : options . size ,
276- response_format : options . responseFormat ,
277- user : options . user ,
270+ const formData = new FormData ( ) ;
271+
272+ // Model specified
273+ formData . append ( "image" , options . image ) ;
274+
275+ // File data
276+ if ( typeof options . image === "string" ) {
277+ const fileData = await Deno . readFile ( options . image ) ;
278+
279+ formData . append (
280+ "image" ,
281+ new File ( [ fileData ] , basename ( options . image ) ) ,
282+ ) ;
283+ } else {
284+ // Deno types are wrong
285+ formData . append ( "image" , options . image as unknown as Blob ) ;
286+ }
287+
288+ if ( options . n ) { formData . append ( "n" , options . n ) ; }
289+ if ( options . mask ) { formData . append ( "mask" , options . mask ) ; }
290+ if ( options . prompt ) { formData . append ( "prompt" , options . prompt ) ; }
291+ if ( options . size ) { formData . append ( "size" , options . size ) ; }
292+ if ( options . user ) { formData . append ( "user" , options . user ) ; }
293+ if ( options . responseFormat ) {
294+ formData . append ( "response_format" , options . responseFormat ) ;
295+ }
296+
297+ return await this . #request( `/images/edits` , formData , {
298+ noContentType : true ,
299+ method : "POST" ,
278300 } ) ;
279301 }
280302
@@ -284,12 +306,34 @@ export class OpenAI {
284306 * https://platform.openai.com/docs/api-reference/images/create-variation
285307 */
286308 async createImageVariation ( options : ImageVariationOptions ) : Promise < Image > {
287- return await this . #request( `/images/variations` , {
288- image : options . image ,
289- n : options . n ,
290- size : options . size ,
291- response_format : options . responseFormat ,
292- user : options . user ,
309+ const formData = new FormData ( ) ;
310+
311+ // Model specified
312+ formData . append ( "image" , options . image ) ;
313+
314+ // File data
315+ if ( typeof options . image === "string" ) {
316+ const fileData = await Deno . readFile ( options . image ) ;
317+
318+ formData . append (
319+ "image" ,
320+ new File ( [ fileData ] , basename ( options . image ) ) ,
321+ ) ;
322+ } else {
323+ // Deno types are wrong
324+ formData . append ( "image" , options . image as unknown as Blob ) ;
325+ }
326+
327+ if ( options . n ) { formData . append ( "n" , options . n ) ; }
328+ if ( options . size ) { formData . append ( "size" , options . size ) ; }
329+ if ( options . user ) { formData . append ( "user" , options . user ) ; }
330+ if ( options . responseFormat ) {
331+ formData . append ( "response_format" , options . responseFormat ) ;
332+ }
333+
334+ return await this . #request( `/images/variations` , formData , {
335+ noContentType : true ,
336+ method : "POST" ,
293337 } ) ;
294338 }
295339
0 commit comments