Merge branch 'dongri:main' into main

This commit is contained in:
Folyd
2024-12-16 11:50:28 +08:00
committed by GitHub
6 changed files with 51 additions and 4 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "openai-api-rs"
version = "5.2.3"
version = "5.2.4"
edition = "2021"
authors = ["Dongri Jin <dongrium@gmail.com>"]
license = "MIT"

View File

@ -7,7 +7,7 @@ Check out the [docs.rs](https://docs.rs/openai-api-rs/).
Cargo.toml
```toml
[dependencies]
openai-api-rs = "5.2.3"
openai-api-rs = "5.2.4"
```
## Usage

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

@ -31,7 +31,7 @@ pub struct ConversationCreated {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct InputAudioBufferCommited {
pub event_id: String,
pub previous_item_id: String,
pub previous_item_id: Option<String>,
pub item_id: String,
}

View File

@ -218,6 +218,8 @@ pub enum ResponseStatus {
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum ResponseStatusDetail {
#[serde(rename = "cancelled")]
Cancelled { reason: CancelledReason },
#[serde(rename = "incomplete")]
Incomplete { reason: IncompleteReason },
#[serde(rename = "failed")]
@ -230,6 +232,13 @@ pub struct FailedError {
pub message: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum CancelledReason {
TurnDetected,
ClientCancelled,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum IncompleteReason {

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,