Skip to content

Commit

Permalink
spec updates; audio translate SRT support (#231)
Browse files Browse the repository at this point in the history
* cargo fmt

* updates from spec

* updated spec

* translate_raw for SRT support

* udpated translation example to include SRT
  • Loading branch information
64bit committed Jun 7, 2024
1 parent cca439c commit 1e8c2fa
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 60 deletions.
12 changes: 11 additions & 1 deletion async-openai/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
Client,
};

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

/// Transcribes audio into the input language.
pub async fn translate_raw(
&self,
request: CreateTranslationRequest,
) -> Result<Bytes, OpenAIError> {
self.client
.post_form_raw("/audio/translations", request)
.await
}

/// Generates audio from the input text.
pub async fn speech(
&self,
Expand Down
4 changes: 4 additions & 0 deletions async-openai/src/types/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ pub struct CreateChatCompletionRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_choice: Option<ChatCompletionToolChoiceOption>,

/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
#[serde(skip_serializing_if = "Option::is_none")]
pub parallel_tool_calls: Option<bool>,

/// 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).
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
Expand Down
4 changes: 2 additions & 2 deletions async-openai/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};

use crate::error::OpenAIError;

use super::{AssistantToolsFileSearch, ImageDetail, ImageUrl};
use super::{ImageDetail, ImageUrl};

#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -94,7 +94,7 @@ pub struct MessageAttachment {
#[serde(rename_all = "snake_case")]
pub enum MessageAttachmentTool {
CodeInterpreter,
FileSearch(AssistantToolsFileSearch),
FileSearch,
}

#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
Expand Down
6 changes: 6 additions & 0 deletions async-openai/src/types/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub struct RunObject {

pub tool_choice: Option<AssistantsApiToolChoiceOption>,

/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
pub parallel_tool_calls: bool,

pub response_format: Option<AssistantsApiResponseFormatOption>,
}

Expand Down Expand Up @@ -224,6 +227,9 @@ pub struct CreateRunRequest {

pub tool_choice: Option<AssistantsApiToolChoiceOption>,

/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
pub parallel_tool_calls: Option<bool>,

pub response_format: Option<AssistantsApiResponseFormatOption>,
}

Expand Down
4 changes: 4 additions & 0 deletions async-openai/src/types/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ pub struct CreateThreadAndRunRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_choice: Option<AssistantsApiToolChoiceOption>,

/// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
#[serde(skip_serializing_if = "Option::is_none")]
pub parallel_tool_calls: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
pub response_format: Option<AssistantsApiResponseFormatOption>,
}
4 changes: 3 additions & 1 deletion async-openai/src/types/vector_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ pub enum VectorStoreFileErrorCode {
pub enum VectorStoreFileObjectChunkingStrategy {
/// 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.
Other,
Static{ r#static: StaticChunkingStrategy },
Static {
r#static: StaticChunkingStrategy,
},
}

#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq)]
Expand Down
42 changes: 23 additions & 19 deletions async-openai/src/vector_store_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,52 +78,56 @@ impl<'c, C: Config> VectorStoreFiles<'c, C> {

#[cfg(test)]
mod tests {
use crate::types::{
CreateFileRequest, CreateVectorStoreFileRequest, CreateVectorStoreRequest, FileInput,
FilePurpose,
};
use crate::Client;
use crate::types::{CreateFileRequest, CreateVectorStoreFileRequest, CreateVectorStoreRequest, FileInput, FilePurpose};

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

// Create a file
let file_handle = client
.files()
.create( CreateFileRequest {
.create(CreateFileRequest {
file: FileInput::from_vec_u8(
String::from("meow.txt"),
String::from(":3").into_bytes()
String::from(":3").into_bytes(),
),
purpose: FilePurpose::Assistants
}).await?;
purpose: FilePurpose::Assistants,
})
.await?;

// Create a vector store
let vector_store_handle = client
.vector_stores()
.create( CreateVectorStoreRequest {
.create(CreateVectorStoreRequest {
file_ids: Some(vec![file_handle.id.clone()]),
name: None,
expires_after: None,
chunking_strategy: None,
metadata: None
metadata: None,
})
.await?;
let vector_store_file = client
.vector_stores()
.files(&vector_store_handle.id)
.retrieve(&file_handle.id)
.await?;
let vector_store_file = client
.vector_stores()
.files(&vector_store_handle.id)
.retrieve(&file_handle.id)
.await?;

assert_eq!(vector_store_file.id, file_handle.id);
assert_eq!(vector_store_file.id, file_handle.id);
// Delete the vector store
client
.vector_stores()
.delete(&vector_store_handle.id).await?;
.delete(&vector_store_handle.id)
.await?;

// Delete the file
client
.files()
.delete(&file_handle.id).await?;
client.files().delete(&file_handle.id).await?;

Ok(())
}
}
}
4 changes: 1 addition & 3 deletions examples/assistants-file-search/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.content("What was the total annual profit of Uber and Lyft?")
.attachments(vec![MessageAttachment {
file_id: message_file.id.clone(),
tools: vec![MessageAttachmentTool::FileSearch(
AssistantToolsFileSearch::default(),
)],
tools: vec![MessageAttachmentTool::FileSearch],
}])
.build()?;

Expand Down
31 changes: 28 additions & 3 deletions examples/audio-translate/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
use async_openai::{types::CreateTranslationRequestArgs, Client};
use async_openai::{
types::{AudioResponseFormat, CreateTranslationRequestArgs},
Client,
};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
async fn translate_srt() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let request = CreateTranslationRequestArgs::default()
.file("./audio/koshish karne walon ki haar nahi hoti by amitabh bachchan_320kbps.mp3")
.model("whisper-1")
.response_format(AudioResponseFormat::Srt)
.build()?;

let response = client.audio().translate_raw(request).await?;

println!("translate_srt:");
println!("{}", String::from_utf8_lossy(response.as_ref()));
Ok(())
}

async fn translate_verbose_json() -> Result<(), Box<dyn Error>> {
let client = Client::new();
// Credits and Source for audio: https://www.youtube.com/watch?v=bHWmzQ4HTS0
let request = CreateTranslationRequestArgs::default()
Expand All @@ -12,7 +29,15 @@ async fn main() -> Result<(), Box<dyn Error>> {

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

println!("translate_verbose_json:");
println!("{}", response.text);

Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
translate_verbose_json().await?;
translate_srt().await?;
Ok(())
}
Loading

0 comments on commit 1e8c2fa

Please sign in to comment.