Skip to content

Commit 449a682

Browse files
authored
Merge branch 'dongri:main' into main
2 parents 568f41b + 8ca5630 commit 449a682

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openai-api-rs"
3-
version = "5.2.3"
3+
version = "5.2.4"
44
edition = "2021"
55
authors = ["Dongri Jin <dongrium@gmail.com>"]
66
license = "MIT"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Check out the [docs.rs](https://docs.rs/openai-api-rs/).
77
Cargo.toml
88
```toml
99
[dependencies]
10-
openai-api-rs = "5.2.3"
10+
openai-api-rs = "5.2.4"
1111
```
1212

1313
## Usage

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/realtime/server_event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct ConversationCreated {
3131
#[derive(Debug, Serialize, Deserialize, Clone)]
3232
pub struct InputAudioBufferCommited {
3333
pub event_id: String,
34-
pub previous_item_id: String,
34+
pub previous_item_id: Option<String>,
3535
pub item_id: String,
3636
}
3737

src/realtime/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ pub enum ResponseStatus {
218218
#[derive(Debug, Serialize, Deserialize, Clone)]
219219
#[serde(tag = "type")]
220220
pub enum ResponseStatusDetail {
221+
#[serde(rename = "cancelled")]
222+
Cancelled { reason: CancelledReason },
221223
#[serde(rename = "incomplete")]
222224
Incomplete { reason: IncompleteReason },
223225
#[serde(rename = "failed")]
@@ -230,6 +232,13 @@ pub struct FailedError {
230232
pub message: String,
231233
}
232234

235+
#[derive(Debug, Serialize, Deserialize, Clone)]
236+
#[serde(rename_all = "snake_case")]
237+
pub enum CancelledReason {
238+
TurnDetected,
239+
ClientCancelled,
240+
}
241+
233242
#[derive(Debug, Serialize, Deserialize, Clone)]
234243
#[serde(rename_all = "snake_case")]
235244
pub enum IncompleteReason {

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)