Skip to content

Commit

Permalink
Merge pull request #134 from dongri/fix-transcription
Browse files Browse the repository at this point in the history
Add post_form_raw
  • Loading branch information
dongri authored Dec 15, 2024
2 parents fe38e13 + 85d204f commit 5fc8f98
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion examples/audio_transcriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
Expand Down
31 changes: 31 additions & 0 deletions src/v1/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ impl OpenAIClient {
self.handle_response(response).await
}

async fn post_form_raw(&self, path: &str, form: Form) -> Result<Bytes, APIError> {
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<T: serde::de::DeserializeOwned>(
&self,
response: Response,
Expand Down Expand Up @@ -303,10 +310,34 @@ impl OpenAIClient {
&self,
req: AudioTranscriptionRequest,
) -> Result<AudioTranscriptionResponse, APIError> {
// 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<Bytes, APIError> {
// 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,
Expand Down

0 comments on commit 5fc8f98

Please sign in to comment.