Skip to content

Commit 5fc8f98

Browse files
authored
Merge pull request #134 from dongri/fix-transcription
Add post_form_raw
2 parents fe38e13 + 85d204f commit 5fc8f98

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

examples/audio_transcriptions.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1212
WHISPER_1.to_string(),
1313
);
1414

15-
let result = client.audio_transcription(req).await?;
15+
let req_json = req.clone().response_format("json".to_string());
16+
17+
let result = client.audio_transcription(req_json).await?;
18+
println!("{:?}", result);
19+
20+
let req_raw = req.clone().response_format("text".to_string());
21+
22+
let result = client.audio_transcription_raw(req_raw).await?;
1623
println!("{:?}", result);
1724

1825
Ok(())

src/v1/api.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ impl OpenAIClient {
211211
self.handle_response(response).await
212212
}
213213

214+
async fn post_form_raw(&self, path: &str, form: Form) -> Result<Bytes, APIError> {
215+
let request = self.build_request(Method::POST, path).await;
216+
let request = request.multipart(form);
217+
let response = request.send().await?;
218+
Ok(response.bytes().await?)
219+
}
220+
214221
async fn handle_response<T: serde::de::DeserializeOwned>(
215222
&self,
216223
response: Response,
@@ -303,10 +310,34 @@ impl OpenAIClient {
303310
&self,
304311
req: AudioTranscriptionRequest,
305312
) -> Result<AudioTranscriptionResponse, APIError> {
313+
// https://platform.openai.com/docs/api-reference/audio/createTranslation#audio-createtranslation-response_format
314+
if let Some(response_format) = &req.response_format {
315+
if response_format != "json" && response_format != "verbose_json" {
316+
return Err(APIError::CustomError {
317+
message: "response_format must be either 'json' or 'verbose_json' please use audio_transcription_raw".to_string(),
318+
});
319+
}
320+
}
306321
let form = Self::create_form(&req, "file")?;
307322
self.post_form("audio/transcriptions", form).await
308323
}
309324

325+
pub async fn audio_transcription_raw(
326+
&self,
327+
req: AudioTranscriptionRequest,
328+
) -> Result<Bytes, APIError> {
329+
// https://platform.openai.com/docs/api-reference/audio/createTranslation#audio-createtranslation-response_format
330+
if let Some(response_format) = &req.response_format {
331+
if response_format != "text" && response_format != "srt" && response_format != "vtt" {
332+
return Err(APIError::CustomError {
333+
message: "response_format must be either 'text', 'srt' or 'vtt', please use audio_transcription".to_string(),
334+
});
335+
}
336+
}
337+
let form = Self::create_form(&req, "file")?;
338+
self.post_form_raw("audio/transcriptions", form).await
339+
}
340+
310341
pub async fn audio_translation(
311342
&self,
312343
req: AudioTranslationRequest,

0 commit comments

Comments
 (0)