Skip to content

Commit 1e8c2fa

Browse files
authored
spec updates; audio translate SRT support (64bit#231)
* cargo fmt * updates from spec * updated spec * translate_raw for SRT support * udpated translation example to include SRT
1 parent cca439c commit 1e8c2fa

File tree

10 files changed

+148
-60
lines changed

10 files changed

+148
-60
lines changed

async-openai/src/audio.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
Client,
1313
};
1414

15-
/// Turn audio into text
15+
/// Turn audio into text or text into audio.
1616
/// Related guide: [Speech to text](https://platform.openai.com/docs/guides/speech-to-text)
1717
pub struct Audio<'c, C: Config> {
1818
client: &'c Client<C>,
@@ -69,6 +69,16 @@ impl<'c, C: Config> Audio<'c, C> {
6969
self.client.post_form("/audio/translations", request).await
7070
}
7171

72+
/// Transcribes audio into the input language.
73+
pub async fn translate_raw(
74+
&self,
75+
request: CreateTranslationRequest,
76+
) -> Result<Bytes, OpenAIError> {
77+
self.client
78+
.post_form_raw("/audio/translations", request)
79+
.await
80+
}
81+
7282
/// Generates audio from the input text.
7383
pub async fn speech(
7484
&self,

async-openai/src/types/chat.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ pub struct CreateChatCompletionRequest {
483483
#[serde(skip_serializing_if = "Option::is_none")]
484484
pub tool_choice: Option<ChatCompletionToolChoiceOption>,
485485

486+
/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
487+
#[serde(skip_serializing_if = "Option::is_none")]
488+
pub parallel_tool_calls: Option<bool>,
489+
486490
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
487491
#[serde(skip_serializing_if = "Option::is_none")]
488492
pub user: Option<String>,

async-openai/src/types/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55

66
use crate::error::OpenAIError;
77

8-
use super::{AssistantToolsFileSearch, ImageDetail, ImageUrl};
8+
use super::{ImageDetail, ImageUrl};
99

1010
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
1111
#[serde(rename_all = "lowercase")]
@@ -94,7 +94,7 @@ pub struct MessageAttachment {
9494
#[serde(rename_all = "snake_case")]
9595
pub enum MessageAttachmentTool {
9696
CodeInterpreter,
97-
FileSearch(AssistantToolsFileSearch),
97+
FileSearch,
9898
}
9999

100100
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]

async-openai/src/types/run.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ pub struct RunObject {
7979

8080
pub tool_choice: Option<AssistantsApiToolChoiceOption>,
8181

82+
/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
83+
pub parallel_tool_calls: bool,
84+
8285
pub response_format: Option<AssistantsApiResponseFormatOption>,
8386
}
8487

@@ -224,6 +227,9 @@ pub struct CreateRunRequest {
224227

225228
pub tool_choice: Option<AssistantsApiToolChoiceOption>,
226229

230+
/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
231+
pub parallel_tool_calls: Option<bool>,
232+
227233
pub response_format: Option<AssistantsApiResponseFormatOption>,
228234
}
229235

async-openai/src/types/thread.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ pub struct CreateThreadAndRunRequest {
125125
#[serde(skip_serializing_if = "Option::is_none")]
126126
pub tool_choice: Option<AssistantsApiToolChoiceOption>,
127127

128+
/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
129+
#[serde(skip_serializing_if = "Option::is_none")]
130+
pub parallel_tool_calls: Option<bool>,
131+
128132
#[serde(skip_serializing_if = "Option::is_none")]
129133
pub response_format: Option<AssistantsApiResponseFormatOption>,
130134
}

async-openai/src/types/vector_store.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ pub enum VectorStoreFileErrorCode {
192192
pub enum VectorStoreFileObjectChunkingStrategy {
193193
/// This is returned when the chunking strategy is unknown. Typically, this is because the file was indexed before the `chunking_strategy` concept was introduced in the API.
194194
Other,
195-
Static{ r#static: StaticChunkingStrategy },
195+
Static {
196+
r#static: StaticChunkingStrategy,
197+
},
196198
}
197199

198200
#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq)]

async-openai/src/vector_store_files.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,52 +78,56 @@ impl<'c, C: Config> VectorStoreFiles<'c, C> {
7878

7979
#[cfg(test)]
8080
mod tests {
81+
use crate::types::{
82+
CreateFileRequest, CreateVectorStoreFileRequest, CreateVectorStoreRequest, FileInput,
83+
FilePurpose,
84+
};
8185
use crate::Client;
82-
use crate::types::{CreateFileRequest, CreateVectorStoreFileRequest, CreateVectorStoreRequest, FileInput, FilePurpose};
8386

8487
#[tokio::test]
85-
async fn vector_store_file_creation_and_deletion() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
88+
async fn vector_store_file_creation_and_deletion(
89+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
8690
let client = Client::new();
8791

8892
// Create a file
8993
let file_handle = client
9094
.files()
91-
.create( CreateFileRequest {
95+
.create(CreateFileRequest {
9296
file: FileInput::from_vec_u8(
9397
String::from("meow.txt"),
94-
String::from(":3").into_bytes()
98+
String::from(":3").into_bytes(),
9599
),
96-
purpose: FilePurpose::Assistants
97-
}).await?;
100+
purpose: FilePurpose::Assistants,
101+
})
102+
.await?;
98103

99104
// Create a vector store
100105
let vector_store_handle = client
101106
.vector_stores()
102-
.create( CreateVectorStoreRequest {
107+
.create(CreateVectorStoreRequest {
103108
file_ids: Some(vec![file_handle.id.clone()]),
104109
name: None,
105110
expires_after: None,
106111
chunking_strategy: None,
107-
metadata: None
112+
metadata: None,
108113
})
109114
.await?;
110-
let vector_store_file = client
111-
.vector_stores()
112-
.files(&vector_store_handle.id)
113-
.retrieve(&file_handle.id)
114-
.await?;
115+
let vector_store_file = client
116+
.vector_stores()
117+
.files(&vector_store_handle.id)
118+
.retrieve(&file_handle.id)
119+
.await?;
115120

116-
assert_eq!(vector_store_file.id, file_handle.id);
121+
assert_eq!(vector_store_file.id, file_handle.id);
117122
// Delete the vector store
118123
client
119124
.vector_stores()
120-
.delete(&vector_store_handle.id).await?;
125+
.delete(&vector_store_handle.id)
126+
.await?;
121127

122128
// Delete the file
123-
client
124-
.files()
125-
.delete(&file_handle.id).await?;
129+
client.files().delete(&file_handle.id).await?;
126130

127131
Ok(())
128132
}
129-
}
133+
}

examples/assistants-file-search/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
9494
.content("What was the total annual profit of Uber and Lyft?")
9595
.attachments(vec![MessageAttachment {
9696
file_id: message_file.id.clone(),
97-
tools: vec![MessageAttachmentTool::FileSearch(
98-
AssistantToolsFileSearch::default(),
99-
)],
97+
tools: vec![MessageAttachmentTool::FileSearch],
10098
}])
10199
.build()?;
102100

examples/audio-translate/src/main.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
use async_openai::{types::CreateTranslationRequestArgs, Client};
1+
use async_openai::{
2+
types::{AudioResponseFormat, CreateTranslationRequestArgs},
3+
Client,
4+
};
25
use std::error::Error;
36

4-
#[tokio::main]
5-
async fn main() -> Result<(), Box<dyn Error>> {
7+
async fn translate_srt() -> Result<(), Box<dyn Error>> {
8+
let client = Client::new();
9+
let request = CreateTranslationRequestArgs::default()
10+
.file("./audio/koshish karne walon ki haar nahi hoti by amitabh bachchan_320kbps.mp3")
11+
.model("whisper-1")
12+
.response_format(AudioResponseFormat::Srt)
13+
.build()?;
14+
15+
let response = client.audio().translate_raw(request).await?;
16+
17+
println!("translate_srt:");
18+
println!("{}", String::from_utf8_lossy(response.as_ref()));
19+
Ok(())
20+
}
21+
22+
async fn translate_verbose_json() -> Result<(), Box<dyn Error>> {
623
let client = Client::new();
724
// Credits and Source for audio: https://www.youtube.com/watch?v=bHWmzQ4HTS0
825
let request = CreateTranslationRequestArgs::default()
@@ -12,7 +29,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
1229

1330
let response = client.audio().translate(request).await?;
1431

32+
println!("translate_verbose_json:");
1533
println!("{}", response.text);
1634

1735
Ok(())
1836
}
37+
38+
#[tokio::main]
39+
async fn main() -> Result<(), Box<dyn Error>> {
40+
translate_verbose_json().await?;
41+
translate_srt().await?;
42+
Ok(())
43+
}

0 commit comments

Comments
 (0)