mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-23 15:48:07 +00:00
Merge pull request #134 from dongri/fix-transcription
Add post_form_raw
This commit is contained in:
@ -12,7 +12,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
WHISPER_1.to_string(),
|
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);
|
println!("{:?}", result);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -211,6 +211,13 @@ impl OpenAIClient {
|
|||||||
self.handle_response(response).await
|
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>(
|
async fn handle_response<T: serde::de::DeserializeOwned>(
|
||||||
&self,
|
&self,
|
||||||
response: Response,
|
response: Response,
|
||||||
@ -303,10 +310,34 @@ impl OpenAIClient {
|
|||||||
&self,
|
&self,
|
||||||
req: AudioTranscriptionRequest,
|
req: AudioTranscriptionRequest,
|
||||||
) -> Result<AudioTranscriptionResponse, APIError> {
|
) -> 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")?;
|
let form = Self::create_form(&req, "file")?;
|
||||||
self.post_form("audio/transcriptions", form).await
|
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(
|
pub async fn audio_translation(
|
||||||
&self,
|
&self,
|
||||||
req: AudioTranslationRequest,
|
req: AudioTranslationRequest,
|
||||||
|
Reference in New Issue
Block a user