@@ -310,31 +310,49 @@ impl OpenAIClient {
310
310
& self ,
311
311
req : AudioTranscriptionRequest ,
312
312
) -> Result < AudioTranscriptionResponse , APIError > {
313
- // https://platform.openai.com/docs/api-reference/audio/createTranslation #audio-createtranslation -response_format
313
+ // https://platform.openai.com/docs/api-reference/audio/createTranscription #audio-createtranscription -response_format
314
314
if let Some ( response_format) = & req. response_format {
315
315
if response_format != "json" && response_format != "verbose_json" {
316
316
return Err ( APIError :: CustomError {
317
317
message : "response_format must be either 'json' or 'verbose_json' please use audio_transcription_raw" . to_string ( ) ,
318
318
} ) ;
319
319
}
320
320
}
321
- let form = Self :: create_form ( & req, "file" ) ?;
321
+ let form: Form ;
322
+ if req. clone ( ) . file . is_some ( ) {
323
+ form = Self :: create_form ( & req, "file" ) ?;
324
+ } else if let Some ( bytes) = req. clone ( ) . bytes {
325
+ form = Self :: create_form_from_bytes ( & req, bytes) ?;
326
+ } else {
327
+ return Err ( APIError :: CustomError {
328
+ message : "Either file or bytes must be provided" . to_string ( ) ,
329
+ } ) ;
330
+ }
322
331
self . post_form ( "audio/transcriptions" , form) . await
323
332
}
324
333
325
334
pub async fn audio_transcription_raw (
326
335
& self ,
327
336
req : AudioTranscriptionRequest ,
328
337
) -> Result < Bytes , APIError > {
329
- // https://platform.openai.com/docs/api-reference/audio/createTranslation #audio-createtranslation -response_format
338
+ // https://platform.openai.com/docs/api-reference/audio/createTranscription #audio-createtranscription -response_format
330
339
if let Some ( response_format) = & req. response_format {
331
340
if response_format != "text" && response_format != "srt" && response_format != "vtt" {
332
341
return Err ( APIError :: CustomError {
333
342
message : "response_format must be either 'text', 'srt' or 'vtt', please use audio_transcription" . to_string ( ) ,
334
343
} ) ;
335
344
}
336
345
}
337
- let form = Self :: create_form ( & req, "file" ) ?;
346
+ let form: Form ;
347
+ if req. clone ( ) . file . is_some ( ) {
348
+ form = Self :: create_form ( & req, "file" ) ?;
349
+ } else if let Some ( bytes) = req. clone ( ) . bytes {
350
+ form = Self :: create_form_from_bytes ( & req, bytes) ?;
351
+ } else {
352
+ return Err ( APIError :: CustomError {
353
+ message : "Either file or bytes must be provided" . to_string ( ) ,
354
+ } ) ;
355
+ }
338
356
self . post_form_raw ( "audio/transcriptions" , form) . await
339
357
}
340
358
@@ -823,4 +841,36 @@ impl OpenAIClient {
823
841
824
842
Ok ( form)
825
843
}
844
+
845
+ fn create_form_from_bytes < T > ( req : & T , bytes : Vec < u8 > ) -> Result < Form , APIError >
846
+ where
847
+ T : Serialize ,
848
+ {
849
+ let json = match serde_json:: to_value ( req) {
850
+ Ok ( json) => json,
851
+ Err ( e) => {
852
+ return Err ( APIError :: CustomError {
853
+ message : e. to_string ( ) ,
854
+ } )
855
+ }
856
+ } ;
857
+
858
+ let mut form = Form :: new ( ) . part ( "file" , Part :: bytes ( bytes. clone ( ) ) . file_name ( "file.mp3" ) ) ;
859
+
860
+ if let Value :: Object ( map) = json {
861
+ for ( key, value) in map. into_iter ( ) {
862
+ match value {
863
+ Value :: String ( s) => {
864
+ form = form. text ( key, s) ;
865
+ }
866
+ Value :: Number ( n) => {
867
+ form = form. text ( key, n. to_string ( ) ) ;
868
+ }
869
+ _ => { }
870
+ }
871
+ }
872
+ }
873
+
874
+ Ok ( form)
875
+ }
826
876
}
0 commit comments