diff --git a/examples/audio_transcriptions.rs b/examples/audio_transcriptions.rs index b2c7ced..49f5b88 100644 --- a/examples/audio_transcriptions.rs +++ b/examples/audio_transcriptions.rs @@ -12,7 +12,14 @@ async fn main() -> Result<(), Box> { WHISPER_1.to_string(), ); - let result = client.audio_transcription(req).await?; + let req_json = req.clone().response_format("json".to_string()); + + let result = client.audio_transcription(req_json).await?; + println!("{:?}", result); + + let req_raw = req.clone().response_format("text".to_string()); + + let result = client.audio_transcription_raw(req_raw).await?; println!("{:?}", result); Ok(()) diff --git a/src/v1/api.rs b/src/v1/api.rs index c1b5592..f669cca 100644 --- a/src/v1/api.rs +++ b/src/v1/api.rs @@ -211,6 +211,13 @@ impl OpenAIClient { self.handle_response(response).await } + async fn post_form_raw(&self, path: &str, form: Form) -> Result { + let request = self.build_request(Method::POST, path).await; + let request = request.multipart(form); + let response = request.send().await?; + Ok(response.bytes().await?) + } + async fn handle_response( &self, response: Response, @@ -303,10 +310,34 @@ impl OpenAIClient { &self, req: AudioTranscriptionRequest, ) -> Result { + // https://platform.openai.com/docs/api-reference/audio/createTranslation#audio-createtranslation-response_format + if let Some(response_format) = &req.response_format { + if response_format != "json" && response_format != "verbose_json" { + return Err(APIError::CustomError { + message: "response_format must be either 'json' or 'verbose_json' please use audio_transcription_raw".to_string(), + }); + } + } let form = Self::create_form(&req, "file")?; self.post_form("audio/transcriptions", form).await } + pub async fn audio_transcription_raw( + &self, + req: AudioTranscriptionRequest, + ) -> Result { + // https://platform.openai.com/docs/api-reference/audio/createTranslation#audio-createtranslation-response_format + if let Some(response_format) = &req.response_format { + if response_format != "text" && response_format != "srt" && response_format != "vtt" { + return Err(APIError::CustomError { + message: "response_format must be either 'text', 'srt' or 'vtt', please use audio_transcription".to_string(), + }); + } + } + let form = Self::create_form(&req, "file")?; + self.post_form_raw("audio/transcriptions", form).await + } + pub async fn audio_translation( &self, req: AudioTranslationRequest,