Merge pull request #134 from dongri/fix-transcription

Add post_form_raw
This commit is contained in:
Dongri Jin
2024-12-15 18:18:53 +09:00
committed by GitHub
2 changed files with 39 additions and 1 deletions

View File

@ -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(())

View File

@ -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,
@ -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,