diff --git a/.gitignore b/.gitignore index dc105c0c..0d4aa54e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ Cargo.lock # directory used to store images data + +*.mp3 diff --git a/Cargo.toml b/Cargo.toml index 38eac97e..3cad962b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,8 @@ [workspace] -members = [ - "async-openai", -] +members = [ "async-openai", "async-openai-*", "examples/*" ] +# Only check / build main crates by default (check all with `--workspace`) +default-members = ["async-openai", "async-openai-*"] +resolver = "2" + +[workspace.package] +rust-version = "1.75" diff --git a/async-openai-macros/Cargo.toml b/async-openai-macros/Cargo.toml new file mode 100644 index 00000000..4b57cfa5 --- /dev/null +++ b/async-openai-macros/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "async-openai-macros" +version = "0.1.0" +authors = ["Himanshu Neema"] +keywords = ["openai", "macros", "ai"] +description = "Macros for async-openai" +edition = "2021" +license = "MIT" +homepage = "https://github.com/64bit/async-openai" +repository = "https://github.com/64bit/async-openai" +rust-version = { workspace = true } + +[lib] +proc-macro = true + +[dependencies] +syn = { version = "2.0", features = ["full"] } +quote = "1.0" +proc-macro2 = "1.0" diff --git a/async-openai-macros/src/lib.rs b/async-openai-macros/src/lib.rs new file mode 100644 index 00000000..6a421f2d --- /dev/null +++ b/async-openai-macros/src/lib.rs @@ -0,0 +1,141 @@ +use proc_macro::TokenStream; +use quote::{quote, ToTokens}; +use syn::{ + parse::{Parse, ParseStream}, + parse_macro_input, + punctuated::Punctuated, + token::Comma, + FnArg, GenericParam, Generics, ItemFn, Pat, PatType, TypeParam, WhereClause, +}; + +// Parse attribute arguments like #[byot(T0: Display + Debug, T1: Clone, R: Serialize)] +struct BoundArgs { + bounds: Vec<(String, syn::TypeParamBound)>, + where_clause: Option, + stream: bool, // Add stream flag +} + +impl Parse for BoundArgs { + fn parse(input: ParseStream) -> syn::Result { + let mut bounds = Vec::new(); + let mut where_clause = None; + let mut stream = false; // Default to false + let vars = Punctuated::::parse_terminated(input)?; + + for var in vars { + let name = var.path.get_ident().unwrap().to_string(); + match name.as_str() { + "where_clause" => { + where_clause = Some(var.value.into_token_stream().to_string()); + } + "stream" => { + stream = var.value.into_token_stream().to_string().contains("true"); + } + _ => { + let bound: syn::TypeParamBound = + syn::parse_str(&var.value.into_token_stream().to_string())?; + bounds.push((name, bound)); + } + } + } + Ok(BoundArgs { + bounds, + where_clause, + stream, + }) + } +} + +#[proc_macro_attribute] +pub fn byot_passthrough(_args: TokenStream, item: TokenStream) -> TokenStream { + item +} + +#[proc_macro_attribute] +pub fn byot(args: TokenStream, item: TokenStream) -> TokenStream { + let bounds_args = parse_macro_input!(args as BoundArgs); + let input = parse_macro_input!(item as ItemFn); + let mut new_generics = Generics::default(); + let mut param_count = 0; + + // Process function arguments + let mut new_params = Vec::new(); + let args = input + .sig + .inputs + .iter() + .map(|arg| { + match arg { + FnArg::Receiver(receiver) => receiver.to_token_stream(), + FnArg::Typed(PatType { pat, .. }) => { + if let Pat::Ident(pat_ident) = &**pat { + let generic_name = format!("T{}", param_count); + let generic_ident = + syn::Ident::new(&generic_name, proc_macro2::Span::call_site()); + + // Create type parameter with optional bounds + let mut type_param = TypeParam::from(generic_ident.clone()); + if let Some((_, bound)) = bounds_args + .bounds + .iter() + .find(|(name, _)| name == &generic_name) + { + type_param.bounds.extend(vec![bound.clone()]); + } + + new_params.push(GenericParam::Type(type_param)); + param_count += 1; + quote! { #pat_ident: #generic_ident } + } else { + arg.to_token_stream() + } + } + } + }) + .collect::>(); + + // Add R type parameter with optional bounds + let generic_r = syn::Ident::new("R", proc_macro2::Span::call_site()); + let mut return_type_param = TypeParam::from(generic_r.clone()); + if let Some((_, bound)) = bounds_args.bounds.iter().find(|(name, _)| name == "R") { + return_type_param.bounds.extend(vec![bound.clone()]); + } + new_params.push(GenericParam::Type(return_type_param)); + + // Add all generic parameters + new_generics.params.extend(new_params); + + let fn_name = &input.sig.ident; + let byot_fn_name = syn::Ident::new(&format!("{}_byot", fn_name), fn_name.span()); + let vis = &input.vis; + let block = &input.block; + let attrs = &input.attrs; + let asyncness = &input.sig.asyncness; + + // Parse where clause if provided + let where_clause = if let Some(where_str) = bounds_args.where_clause { + match syn::parse_str::(&format!("where {}", where_str.replace("\"", ""))) { + Ok(where_clause) => quote! { #where_clause }, + Err(e) => return TokenStream::from(e.to_compile_error()), + } + } else { + quote! {} + }; + + // Generate return type based on stream flag + let return_type = if bounds_args.stream { + quote! { Result<::std::pin::Pin> + Send>>, OpenAIError> } + } else { + quote! { Result } + }; + + let expanded = quote! { + #(#attrs)* + #input + + #(#attrs)* + #vis #asyncness fn #byot_fn_name #new_generics (#(#args),*) -> #return_type #where_clause #block + }; + + expanded.into() +} diff --git a/async-openai/Cargo.toml b/async-openai/Cargo.toml index 60818479..ae924aeb 100644 --- a/async-openai/Cargo.toml +++ b/async-openai/Cargo.toml @@ -1,33 +1,64 @@ [package] name = "async-openai" -version = "0.1.9" -authors = [ - "Himanshu Neema" -] +version = "0.29.3" +authors = ["Himanshu Neema"] categories = ["api-bindings", "web-programming", "asynchronous"] keywords = ["openai", "async", "openapi", "ai"] -description = "Async bindings for OpenAI REST API based on OpenAPI spec" +description = "Rust library for OpenAI" edition = "2021" -rust-version = "1.65" +rust-version = { workspace = true } license = "MIT" readme = "README.md" homepage = "https://github.com/64bit/async-openai" repository = "https://github.com/64bit/async-openai" +[features] +default = ["rustls"] +# Enable rustls for TLS support +rustls = ["reqwest/rustls-tls-native-roots"] +# Enable rustls and webpki-roots +rustls-webpki-roots = ["reqwest/rustls-tls-webpki-roots"] +# Enable native-tls for TLS support +native-tls = ["reqwest/native-tls"] +# Remove dependency on OpenSSL +native-tls-vendored = ["reqwest/native-tls-vendored"] +realtime = ["dep:tokio-tungstenite"] +# Bring your own types +byot = [] [dependencies] -backoff = {version = "0.4.0", features = ["tokio"] } -base64 = "0.13.1" -futures = "0.3.25" -rand = "0.8.5" -reqwest = { version = "0.11.13", features = ["json", "stream", "multipart"] } -reqwest-eventsource = "0.4.0" -serde = { version = "1.0.148", features = ["derive", "rc"] } -serde_json = "1.0.87" -thiserror = "1.0.37" -tokio = { version = "1.22.0", features = ["fs", "macros"] } -tokio-stream = "0.1.11" -tokio-util = { version = "0.7.4", features = ["codec", "io-util"] } +async-openai-macros = { path = "../async-openai-macros", version = "0.1.0" } +backoff = { version = "0.4.0", features = ["tokio"] } +base64 = "0.22.1" +futures = "0.3.31" +rand = "0.9.0" +reqwest = { version = "0.12.12", features = [ + "json", + "stream", + "multipart", +], default-features = false } +reqwest-eventsource = "0.6.0" +serde = { version = "1.0.217", features = ["derive", "rc"] } +serde_json = "1.0.135" +thiserror = "2.0.11" +tokio = { version = "1.43.0", features = ["fs", "macros"] } +tokio-stream = "0.1.17" +tokio-util = { version = "0.7.13", features = ["codec", "io-util"] } +tracing = "0.1.41" +derive_builder = "0.20.2" +secrecy = { version = "0.10.3", features = ["serde"] } +bytes = "1.9.0" +eventsource-stream = "0.2.3" +tokio-tungstenite = { version = "0.26.1", optional = true, default-features = false } [dev-dependencies] -tokio-test = "0.4.2" +tokio-test = "0.4.4" +serde_json = "1.0" + +[[test]] +name = "bring-your-own-type" +required-features = ["byot"] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] diff --git a/async-openai/README.md b/async-openai/README.md index 9547871a..f5bd50b4 100644 --- a/async-openai/README.md +++ b/async-openai/README.md @@ -13,29 +13,40 @@ +
+Logo created by this repo itself +
## Overview -`async-openai` is an unofficial Rust library for OpenAI REST API. +`async-openai` is an unofficial Rust library for OpenAI. - It's based on [OpenAI OpenAPI spec](https://github.com/openai/openai-openapi) -- Non-streaming requests are retried with exponential backoff when [rate limited](https://help.openai.com/en/articles/5955598-is-api-usage-subject-to-any-rate-limits) by the API server. - Current features: - - [x] Completions (including SSE streaming) - - [x] Edits - - [ ] Embeddings - - [x] Files (List, Upload, Delete, Retrieve, Retrieve Content) - - [ ] Fine-tunes - - [x] Images (Generation, Edit, Variation) - - [ ] Microsoft Azure Endpoints / AD Authentication + - [x] Assistants (v2) + - [x] Audio + - [x] Batch + - [x] Chat + - [x] Completions (Legacy) + - [x] Embeddings + - [x] Files + - [x] Fine-Tuning + - [x] Images - [x] Models - [x] Moderations - -*Being a young project there are rough edges* + - [x] Organizations | Administration (partially implemented) + - [x] Realtime (Beta) (partially implemented) + - [x] Responses (partially implemented) + - [x] Uploads +- Bring your own custom types for Request or Response objects. +- SSE streaming on available APIs +- Requests (except SSE streaming) including form submissions are retried with exponential backoff when [rate limited](https://platform.openai.com/docs/guides/rate-limits). +- Ergonomic builder pattern for all request objects. +- Microsoft Azure OpenAI Service (only for APIs matching OpenAI spec) ## Usage -The library reads [API key](https://beta.openai.com/account/api-keys) from the environment variable `OPENAI_API_KEY`. +The library reads [API key](https://platform.openai.com/account/api-keys) from the environment variable `OPENAI_API_KEY`. ```bash # On macOS/Linux @@ -50,36 +61,43 @@ $Env:OPENAI_API_KEY='sk-...' - Visit [examples](https://github.com/64bit/async-openai/tree/main/examples) directory on how to use `async-openai`. - Visit [docs.rs/async-openai](https://docs.rs/async-openai) for docs. +## Realtime API + +Only types for Realtime API are implemented, and can be enabled with feature flag `realtime`. +These types were written before OpenAI released official specs. + ## Image Generation Example ```rust -use std::error::Error; - -use async_openai as openai; -use openai::{ - types::{CreateImageRequest, ImageSize, ResponseFormat}, - Client, Image, +use async_openai::{ + types::{CreateImageRequestArgs, ImageSize, ImageResponseFormat}, + Client, }; +use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { // create client, reads OPENAI_API_KEY environment variable for API key. let client = Client::new(); - let request = CreateImageRequest { - prompt: "cats on sofa and carpet in living room".to_owned(), - n: Some(2), - response_format: Some(ResponseFormat::Url), - size: Some(ImageSize::S256x256), - user: Some("async-openai".to_owned()), - }; + let request = CreateImageRequestArgs::default() + .prompt("cats on sofa and carpet in living room") + .n(2) + .response_format(ImageResponseFormat::Url) + .size(ImageSize::S256x256) + .user("async-openai") + .build()?; - let response = Image::create(&client, request).await?; + let response = client.images().create(request).await?; - // Download and save images to ./data directory - // Each url download and save happens in dedicated Tokio task - // (creates directory when it doesn't exist) - response.save("./data").await?; + // Download and save images to ./data directory. + // Each url is downloaded and saved in dedicated Tokio task. + // Directory is created if it doesn't exist. + let paths = response.save("./data").await?; + + paths + .iter() + .for_each(|path| println!("Image file path: {}", path.display())); Ok(()) } @@ -92,12 +110,78 @@ async fn main() -> Result<(), Box> { Scaled up for README, actual size 256x256 +## Bring Your Own Types + +Enable methods whose input and outputs are generics with `byot` feature. It creates a new method with same name and `_byot` suffix. + +For example, to use `serde_json::Value` as request and response type: +```rust +let response: Value = client + .chat() + .create_byot(json!({ + "messages": [ + { + "role": "developer", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "What do you think about life?" + } + ], + "model": "gpt-4o", + "store": false + })) + .await?; +``` + +This can be useful in many scenarios: +- To use this library with other OpenAI compatible APIs whose types don't exactly match OpenAI. +- Extend existing types in this crate with new fields with `serde`. +- To avoid verbose types. +- To escape deserialization errors. + +Visit [examples/bring-your-own-type](https://github.com/64bit/async-openai/tree/main/examples/bring-your-own-type) +directory to learn more. + +## Dynamic Dispatch for Different Providers + +For any struct that implements `Config` trait, you can wrap it in a smart pointer and cast the pointer to `dyn Config` +trait object, then your client can accept any wrapped configuration type. + +For example, + +```rust +use async_openai::{Client, config::Config, config::OpenAIConfig}; + +let openai_config = OpenAIConfig::default(); +// You can use `std::sync::Arc` to wrap the config as well +let config = Box::new(openai_config) as Box; +let client: Client > = Client::with_config(config); +``` + ## Contributing -Thank you for your time to contribute and improve the project, I'd be happy to have you! +Thank you for taking the time to contribute and improve the project. I'd be happy to have you! + +All forms of contributions, such as new features requests, bug fixes, issues, documentation, testing, comments, [examples](https://github.com/64bit/async-openai/tree/main/examples) etc. are welcome. + +A good starting point would be to look at existing [open issues](https://github.com/64bit/async-openai/issues). + +To maintain quality of the project, a minimum of the following is a must for code contribution: + +- **Names & Documentation**: All struct names, field names and doc comments are from OpenAPI spec. Nested objects in spec without names leaves room for making appropriate name. +- **Tested**: For changes supporting test(s) and/or example is required. Existing examples, doc tests, unit tests, and integration tests should be made to work with the changes if applicable. +- **Scope**: Keep scope limited to APIs available in official documents such as [API Reference](https://platform.openai.com/docs/api-reference) or [OpenAPI spec](https://github.com/openai/openai-openapi/). Other LLMs or AI Providers offer OpenAI-compatible APIs, yet they may not always have full parity. In such cases, the OpenAI spec takes precedence. +- **Consistency**: Keep code style consistent across all the "APIs" that library exposes; it creates a great developer experience. + +This project adheres to [Rust Code of Conduct](https://www.rust-lang.org/policies/code-of-conduct) + +## Complimentary Crates + +- [openai-func-enums](https://github.com/frankfralick/openai-func-enums) provides procedural macros that make it easier to use this library with OpenAI API's tool calling feature. It also provides derive macros you can add to existing [clap](https://github.com/clap-rs/clap) application subcommands for natural language use of command line tools. It also supports openai's [parallel tool calls](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) and allows you to choose between running multiple tool calls concurrently or own their own OS threads. +- [async-openai-wasm](https://github.com/ifsheldon/async-openai-wasm) provides WASM support. ## License This project is licensed under [MIT license](https://github.com/64bit/async-openai/blob/main/LICENSE). - -Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in async-openai by you, shall be licensed as MIT, without any additional terms or conditions. diff --git a/async-openai/src/assistants.rs b/async-openai/src/assistants.rs new file mode 100644 index 00000000..494f2dec --- /dev/null +++ b/async-openai/src/assistants.rs @@ -0,0 +1,70 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + AssistantObject, CreateAssistantRequest, DeleteAssistantResponse, ListAssistantsResponse, + ModifyAssistantRequest, + }, + Client, +}; + +/// Build assistants that can call models and use tools to perform tasks. +/// +/// [Get started with the Assistants API](https://platform.openai.com/docs/assistants) +pub struct Assistants<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Assistants<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Create an assistant with a model and instructions. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: CreateAssistantRequest, + ) -> Result { + self.client.post("/assistants", request).await + } + + /// Retrieves an assistant. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, assistant_id: &str) -> Result { + self.client + .get(&format!("/assistants/{assistant_id}")) + .await + } + + /// Modifies an assistant. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn update( + &self, + assistant_id: &str, + request: ModifyAssistantRequest, + ) -> Result { + self.client + .post(&format!("/assistants/{assistant_id}"), request) + .await + } + + /// Delete an assistant. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, assistant_id: &str) -> Result { + self.client + .delete(&format!("/assistants/{assistant_id}")) + .await + } + + /// Returns a list of assistants. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client.get_with_query("/assistants", &query).await + } +} diff --git a/async-openai/src/audio.rs b/async-openai/src/audio.rs new file mode 100644 index 00000000..1ee4631d --- /dev/null +++ b/async-openai/src/audio.rs @@ -0,0 +1,111 @@ +use bytes::Bytes; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + CreateSpeechRequest, CreateSpeechResponse, CreateTranscriptionRequest, + CreateTranscriptionResponseJson, CreateTranscriptionResponseVerboseJson, + CreateTranslationRequest, CreateTranslationResponseJson, + CreateTranslationResponseVerboseJson, + }, + Client, +}; + +/// 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, +} + +impl<'c, C: Config> Audio<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Transcribes audio into the input language. + #[crate::byot( + T0 = Clone, + R = serde::de::DeserializeOwned, + where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom", + )] + pub async fn transcribe( + &self, + request: CreateTranscriptionRequest, + ) -> Result { + self.client + .post_form("/audio/transcriptions", request) + .await + } + + /// Transcribes audio into the input language. + #[crate::byot( + T0 = Clone, + R = serde::de::DeserializeOwned, + where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom", + )] + pub async fn transcribe_verbose_json( + &self, + request: CreateTranscriptionRequest, + ) -> Result { + self.client + .post_form("/audio/transcriptions", request) + .await + } + + /// Transcribes audio into the input language. + pub async fn transcribe_raw( + &self, + request: CreateTranscriptionRequest, + ) -> Result { + self.client + .post_form_raw("/audio/transcriptions", request) + .await + } + + /// Translates audio into English. + #[crate::byot( + T0 = Clone, + R = serde::de::DeserializeOwned, + where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom", + )] + pub async fn translate( + &self, + request: CreateTranslationRequest, + ) -> Result { + self.client.post_form("/audio/translations", request).await + } + + /// Translates audio into English. + #[crate::byot( + T0 = Clone, + R = serde::de::DeserializeOwned, + where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom", + )] + pub async fn translate_verbose_json( + &self, + request: CreateTranslationRequest, + ) -> Result { + self.client.post_form("/audio/translations", request).await + } + + /// Transcribes audio into the input language. + pub async fn translate_raw( + &self, + request: CreateTranslationRequest, + ) -> Result { + self.client + .post_form_raw("/audio/translations", request) + .await + } + + /// Generates audio from the input text. + pub async fn speech( + &self, + request: CreateSpeechRequest, + ) -> Result { + let bytes = self.client.post_raw("/audio/speech", request).await?; + + Ok(CreateSpeechResponse { bytes }) + } +} diff --git a/async-openai/src/audit_logs.rs b/async-openai/src/audit_logs.rs new file mode 100644 index 00000000..753c318b --- /dev/null +++ b/async-openai/src/audit_logs.rs @@ -0,0 +1,27 @@ +use serde::Serialize; + +use crate::{config::Config, error::OpenAIError, types::ListAuditLogsResponse, Client}; + +/// Logs of user actions and configuration changes within this organization. +/// To log events, you must activate logging in the [Organization Settings](https://platform.openai.com/settings/organization/general). +/// Once activated, for security reasons, logging cannot be deactivated. +pub struct AuditLogs<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> AuditLogs<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// List user actions and configuration changes within this organization. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn get(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query("/organization/audit_logs", &query) + .await + } +} diff --git a/async-openai/src/batches.rs b/async-openai/src/batches.rs new file mode 100644 index 00000000..57910490 --- /dev/null +++ b/async-openai/src/batches.rs @@ -0,0 +1,53 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{Batch, BatchRequest, ListBatchesResponse}, + Client, +}; + +/// Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount. +/// +/// Related guide: [Batch](https://platform.openai.com/docs/guides/batch) +pub struct Batches<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Batches<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Creates and executes a batch from an uploaded file of requests + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create(&self, request: BatchRequest) -> Result { + self.client.post("/batches", request).await + } + + /// List your organization's batches. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client.get_with_query("/batches", &query).await + } + + /// Retrieves a batch. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, batch_id: &str) -> Result { + self.client.get(&format!("/batches/{batch_id}")).await + } + + /// Cancels an in-progress batch. The batch will be in status `cancelling` for up to 10 minutes, before changing to `cancelled`, where it will have partial results (if any) available in the output file. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn cancel(&self, batch_id: &str) -> Result { + self.client + .post( + &format!("/batches/{batch_id}/cancel"), + serde_json::json!({}), + ) + .await + } +} diff --git a/async-openai/src/chat.rs b/async-openai/src/chat.rs new file mode 100644 index 00000000..28c89f9d --- /dev/null +++ b/async-openai/src/chat.rs @@ -0,0 +1,88 @@ +use crate::{ + config::Config, + error::OpenAIError, + types::{ + ChatCompletionResponseStream, CreateChatCompletionRequest, CreateChatCompletionResponse, + }, + Client, +}; + +/// Given a list of messages comprising a conversation, the model will return a response. +/// +/// Related guide: [Chat completions](https://platform.openai.com//docs/guides/text-generation) +pub struct Chat<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Chat<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Creates a model response for the given chat conversation. Learn more in + /// the + /// + /// [text generation](https://platform.openai.com/docs/guides/text-generation), + /// [vision](https://platform.openai.com/docs/guides/vision), + /// + /// and [audio](https://platform.openai.com/docs/guides/audio) guides. + /// + /// + /// Parameter support can differ depending on the model used to generate the + /// response, particularly for newer reasoning models. Parameters that are + /// only supported for reasoning models are noted below. For the current state + /// of unsupported parameters in reasoning models, + /// + /// [refer to the reasoning guide](https://platform.openai.com/docs/guides/reasoning). + /// + /// byot: You must ensure "stream: false" in serialized `request` + #[crate::byot( + T0 = serde::Serialize, + R = serde::de::DeserializeOwned + )] + pub async fn create( + &self, + request: CreateChatCompletionRequest, + ) -> Result { + #[cfg(not(feature = "byot"))] + { + if request.stream.is_some() && request.stream.unwrap() { + return Err(OpenAIError::InvalidArgument( + "When stream is true, use Chat::create_stream".into(), + )); + } + } + self.client.post("/chat/completions", request).await + } + + /// Creates a completion for the chat message + /// + /// partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message. + /// + /// [ChatCompletionResponseStream] is a parsed SSE stream until a \[DONE\] is received from server. + /// + /// byot: You must ensure "stream: true" in serialized `request` + #[crate::byot( + T0 = serde::Serialize, + R = serde::de::DeserializeOwned, + stream = "true", + where_clause = "R: std::marker::Send + 'static" + )] + #[allow(unused_mut)] + pub async fn create_stream( + &self, + mut request: CreateChatCompletionRequest, + ) -> Result { + #[cfg(not(feature = "byot"))] + { + if request.stream.is_some() && !request.stream.unwrap() { + return Err(OpenAIError::InvalidArgument( + "When stream is false, use Chat::create".into(), + )); + } + + request.stream = Some(true); + } + Ok(self.client.post_stream("/chat/completions", request).await) + } +} diff --git a/async-openai/src/client.rs b/async-openai/src/client.rs index aeefe994..fe2ed232 100644 --- a/async-openai/src/client.rs +++ b/async-openai/src/client.rs @@ -1,60 +1,174 @@ +use std::pin::Pin; + +use bytes::Bytes; +use futures::{stream::StreamExt, Stream}; +use reqwest::multipart::Form; +use reqwest_eventsource::{Event, EventSource, RequestBuilderExt}; use serde::{de::DeserializeOwned, Serialize}; -use crate::error::{OpenAIError, WrappedError}; +use crate::{ + config::{Config, OpenAIConfig}, + error::{map_deserialization_error, ApiError, OpenAIError, WrappedError}, + file::Files, + image::Images, + moderation::Moderations, + traits::AsyncTryFrom, + Assistants, Audio, AuditLogs, Batches, Chat, Completions, Embeddings, FineTuning, Invites, + Models, Projects, Responses, Threads, Uploads, Users, VectorStores, +}; -#[derive(Debug, Default)] -/// Client container for api key, base url and other metadata -/// required to make API calls. -pub struct Client { - api_key: String, - api_base: String, - org_id: String, +#[derive(Debug, Clone, Default)] +/// Client is a container for config, backoff and http_client +/// used to make API calls. +pub struct Client { + http_client: reqwest::Client, + config: C, backoff: backoff::ExponentialBackoff, - //headers: reqwest::header::HeaderMap, } -/// Default v1 API base url -pub const API_BASE: &str = "https://api.openai.com/v1"; - -impl Client { - /// Create client with default [API_BASE] url and default API key from OPENAI_API_KEY env var +impl Client { + /// Client with default [OpenAIConfig] pub fn new() -> Self { - Self { - api_base: API_BASE.to_string(), - api_key: std::env::var("OPENAI_API_KEY").unwrap_or_else(|_| "".to_string()), - ..Default::default() - } + Self::default() } +} - /// To use a different API key different from default OPENAI_API_KEY env var - pub fn with_api_key>(mut self, api_key: S) -> Self { - self.api_key = api_key.into(); - self +impl Client { + /// Create client with a custom HTTP client, OpenAI config, and backoff. + pub fn build( + http_client: reqwest::Client, + config: C, + backoff: backoff::ExponentialBackoff, + ) -> Self { + Self { + http_client, + config, + backoff, + } } - pub fn with_org_id>(mut self, org_id: S) -> Self { - self.org_id = org_id.into(); - self + /// Create client with [OpenAIConfig] or [crate::config::AzureConfig] + pub fn with_config(config: C) -> Self { + Self { + http_client: reqwest::Client::new(), + config, + backoff: Default::default(), + } } - /// To use a API base url different from default [API_BASE] - pub fn with_api_base>(mut self, api_base: S) -> Self { - self.api_base = api_base.into(); + /// Provide your own [client] to make HTTP requests with. + /// + /// [client]: reqwest::Client + pub fn with_http_client(mut self, http_client: reqwest::Client) -> Self { + self.http_client = http_client; self } - /// Exponential backoff for retrying [rate limited](https://help.openai.com/en/articles/5955598-is-api-usage-subject-to-any-rate-limits) requests. Form submissions are not retried. + /// Exponential backoff for retrying [rate limited](https://platform.openai.com/docs/guides/rate-limits) requests. pub fn with_backoff(mut self, backoff: backoff::ExponentialBackoff) -> Self { self.backoff = backoff; self } - pub fn api_base(&self) -> &str { - &self.api_base + // API groups + + /// To call [Models] group related APIs using this client. + pub fn models(&self) -> Models { + Models::new(self) + } + + /// To call [Completions] group related APIs using this client. + pub fn completions(&self) -> Completions { + Completions::new(self) + } + + /// To call [Chat] group related APIs using this client. + pub fn chat(&self) -> Chat { + Chat::new(self) + } + + /// To call [Images] group related APIs using this client. + pub fn images(&self) -> Images { + Images::new(self) } - pub fn api_key(&self) -> &str { - &self.api_key + /// To call [Moderations] group related APIs using this client. + pub fn moderations(&self) -> Moderations { + Moderations::new(self) + } + + /// To call [Files] group related APIs using this client. + pub fn files(&self) -> Files { + Files::new(self) + } + + /// To call [Uploads] group related APIs using this client. + pub fn uploads(&self) -> Uploads { + Uploads::new(self) + } + + /// To call [FineTuning] group related APIs using this client. + pub fn fine_tuning(&self) -> FineTuning { + FineTuning::new(self) + } + + /// To call [Embeddings] group related APIs using this client. + pub fn embeddings(&self) -> Embeddings { + Embeddings::new(self) + } + + /// To call [Audio] group related APIs using this client. + pub fn audio(&self) -> Audio { + Audio::new(self) + } + + /// To call [Assistants] group related APIs using this client. + pub fn assistants(&self) -> Assistants { + Assistants::new(self) + } + + /// To call [Threads] group related APIs using this client. + pub fn threads(&self) -> Threads { + Threads::new(self) + } + + /// To call [VectorStores] group related APIs using this client. + pub fn vector_stores(&self) -> VectorStores { + VectorStores::new(self) + } + + /// To call [Batches] group related APIs using this client. + pub fn batches(&self) -> Batches { + Batches::new(self) + } + + /// To call [AuditLogs] group related APIs using this client. + pub fn audit_logs(&self) -> AuditLogs { + AuditLogs::new(self) + } + + /// To call [Invites] group related APIs using this client. + pub fn invites(&self) -> Invites { + Invites::new(self) + } + + /// To call [Users] group related APIs using this client. + pub fn users(&self) -> Users { + Users::new(self) + } + + /// To call [Projects] group related APIs using this client. + pub fn projects(&self) -> Projects { + Projects::new(self) + } + + /// To call [Responses] group related APIs using this client. + pub fn responses(&self) -> Responses { + Responses::new(self) + } + + pub fn config(&self) -> &C { + &self.config } /// Make a GET request to {path} and deserialize the response body @@ -62,12 +176,35 @@ impl Client { where O: DeserializeOwned, { - let request = reqwest::Client::new() - .get(format!("{}{path}", self.api_base())) - .bearer_auth(self.api_key()) - .build()?; + let request_maker = || async { + Ok(self + .http_client + .get(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .build()?) + }; + + self.execute(request_maker).await + } + + /// Make a GET request to {path} with given Query and deserialize the response body + pub(crate) async fn get_with_query(&self, path: &str, query: &Q) -> Result + where + O: DeserializeOwned, + Q: Serialize + ?Sized, + { + let request_maker = || async { + Ok(self + .http_client + .get(self.config.url(path)) + .query(&self.config.query()) + .query(query) + .headers(self.config.headers()) + .build()?) + }; - self.execute(request).await + self.execute(request_maker).await } /// Make a DELETE request to {path} and deserialize the response body @@ -75,12 +212,48 @@ impl Client { where O: DeserializeOwned, { - let request = reqwest::Client::new() - .delete(format!("{}{path}", self.api_base())) - .bearer_auth(self.api_key()) - .build()?; + let request_maker = || async { + Ok(self + .http_client + .delete(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .build()?) + }; + + self.execute(request_maker).await + } + + /// Make a GET request to {path} and return the response body + pub(crate) async fn get_raw(&self, path: &str) -> Result { + let request_maker = || async { + Ok(self + .http_client + .get(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .build()?) + }; - self.execute(request).await + self.execute_raw(request_maker).await + } + + /// Make a POST request to {path} and return the response body + pub(crate) async fn post_raw(&self, path: &str, request: I) -> Result + where + I: Serialize, + { + let request_maker = || async { + Ok(self + .http_client + .post(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .json(&request) + .build()?) + }; + + self.execute_raw(request_maker).await } /// Make a POST request to {path} and deserialize the response body @@ -89,107 +262,305 @@ impl Client { I: Serialize, O: DeserializeOwned, { - let request = reqwest::Client::new() - .post(format!("{}{path}", self.api_base())) - .bearer_auth(self.api_key()) - .json(&request) - .build()?; + let request_maker = || async { + Ok(self + .http_client + .post(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .json(&request) + .build()?) + }; + + self.execute(request_maker).await + } + + /// POST a form at {path} and return the response body + pub(crate) async fn post_form_raw(&self, path: &str, form: F) -> Result + where + Form: AsyncTryFrom, + F: Clone, + { + let request_maker = || async { + Ok(self + .http_client + .post(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .multipart(
>::try_from(form.clone()).await?) + .build()?) + }; - self.execute(request).await + self.execute_raw(request_maker).await } /// POST a form at {path} and deserialize the response body - pub(crate) async fn post_form( - &self, - path: &str, - form: reqwest::multipart::Form, - ) -> Result + pub(crate) async fn post_form(&self, path: &str, form: F) -> Result where O: DeserializeOwned, + Form: AsyncTryFrom, + F: Clone, { - let request = reqwest::Client::new() - .post(format!("{}{path}", self.api_base())) - .bearer_auth(self.api_key()) - .multipart(form) - .build()?; + let request_maker = || async { + Ok(self + .http_client + .post(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .multipart(>::try_from(form.clone()).await?) + .build()?) + }; - self.execute(request).await + self.execute(request_maker).await } - /// Deserialize response body from either error object or actual response object - async fn process_response(&self, response: reqwest::Response) -> Result + /// Execute a HTTP request and retry on rate limit + /// + /// request_maker serves one purpose: to be able to create request again + /// to retry API call after getting rate limited. request_maker is async because + /// reqwest::multipart::Form is created by async calls to read files for uploads. + async fn execute_raw(&self, request_maker: M) -> Result where - O: DeserializeOwned, + M: Fn() -> Fut, + Fut: core::future::Future>, { - let status = response.status(); - let bytes = response.bytes().await?; + let client = self.http_client.clone(); - if !status.is_success() { - let wrapped_error: WrappedError = - serde_json::from_slice(bytes.as_ref()).map_err(OpenAIError::JSONDeserialize)?; + backoff::future::retry(self.backoff.clone(), || async { + let request = request_maker().await.map_err(backoff::Error::Permanent)?; + let response = client + .execute(request) + .await + .map_err(OpenAIError::Reqwest) + .map_err(backoff::Error::Permanent)?; - return Err(OpenAIError::ApiError(wrapped_error.error)); - } + let status = response.status(); + let bytes = response + .bytes() + .await + .map_err(OpenAIError::Reqwest) + .map_err(backoff::Error::Permanent)?; - let response: O = - serde_json::from_slice(bytes.as_ref()).map_err(OpenAIError::JSONDeserialize)?; - Ok(response) + if status.is_server_error() { + // OpenAI does not guarantee server errors are returned as JSON so we cannot deserialize them. + let message: String = String::from_utf8_lossy(&bytes).into_owned(); + tracing::warn!("Server error: {status} - {message}"); + return Err(backoff::Error::Transient { + err: OpenAIError::ApiError(ApiError { + message, + r#type: None, + param: None, + code: None, + }), + retry_after: None, + }); + } + + // Deserialize response body from either error object or actual response object + if !status.is_success() { + let wrapped_error: WrappedError = serde_json::from_slice(bytes.as_ref()) + .map_err(|e| map_deserialization_error(e, bytes.as_ref())) + .map_err(backoff::Error::Permanent)?; + + if status.as_u16() == 429 + // API returns 429 also when: + // "You exceeded your current quota, please check your plan and billing details." + && wrapped_error.error.r#type != Some("insufficient_quota".to_string()) + { + // Rate limited retry... + tracing::warn!("Rate limited: {}", wrapped_error.error.message); + return Err(backoff::Error::Transient { + err: OpenAIError::ApiError(wrapped_error.error), + retry_after: None, + }); + } else { + return Err(backoff::Error::Permanent(OpenAIError::ApiError( + wrapped_error.error, + ))); + } + } + + Ok(bytes) + }) + .await } - /// Execute any HTTP requests and retry on rate limit, except streaming ones as they cannot be cloned for retrying. - async fn execute(&self, request: reqwest::Request) -> Result + /// Execute a HTTP request and retry on rate limit + /// + /// request_maker serves one purpose: to be able to create request again + /// to retry API call after getting rate limited. request_maker is async because + /// reqwest::multipart::Form is created by async calls to read files for uploads. + async fn execute(&self, request_maker: M) -> Result where O: DeserializeOwned, + M: Fn() -> Fut, + Fut: core::future::Future>, { - let client = reqwest::Client::new(); - - match request.try_clone() { - // Only clone-able requests can be retried - Some(request) => { - backoff::future::retry(self.backoff.clone(), || async { - let response = client - .execute(request.try_clone().unwrap()) - .await - .map_err(OpenAIError::Reqwest) - .map_err(backoff::Error::Permanent)?; - - let status = response.status(); - let bytes = response - .bytes() - .await - .map_err(OpenAIError::Reqwest) - .map_err(backoff::Error::Permanent)?; - - // Deserialize response body from either error object or actual response object - if !status.is_success() { - let wrapped_error: WrappedError = serde_json::from_slice(bytes.as_ref()) - .map_err(OpenAIError::JSONDeserialize) - .map_err(backoff::Error::Permanent)?; - - if status.as_u16() == 429 { - // Rate limited retry... - return Err(backoff::Error::Transient { - err: OpenAIError::ApiError(wrapped_error.error), - retry_after: None, - }); - } else { - return Err(backoff::Error::Permanent(OpenAIError::ApiError( - wrapped_error.error, - ))); - } + let bytes = self.execute_raw(request_maker).await?; + + let response: O = serde_json::from_slice(bytes.as_ref()) + .map_err(|e| map_deserialization_error(e, bytes.as_ref()))?; + + Ok(response) + } + + /// Make HTTP POST request to receive SSE + pub(crate) async fn post_stream( + &self, + path: &str, + request: I, + ) -> Pin> + Send>> + where + I: Serialize, + O: DeserializeOwned + std::marker::Send + 'static, + { + let event_source = self + .http_client + .post(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .json(&request) + .eventsource() + .unwrap(); + + stream(event_source).await + } + + pub(crate) async fn post_stream_mapped_raw_events( + &self, + path: &str, + request: I, + event_mapper: impl Fn(eventsource_stream::Event) -> Result + Send + 'static, + ) -> Pin> + Send>> + where + I: Serialize, + O: DeserializeOwned + std::marker::Send + 'static, + { + let event_source = self + .http_client + .post(self.config.url(path)) + .query(&self.config.query()) + .headers(self.config.headers()) + .json(&request) + .eventsource() + .unwrap(); + + stream_mapped_raw_events(event_source, event_mapper).await + } + + /// Make HTTP GET request to receive SSE + pub(crate) async fn _get_stream( + &self, + path: &str, + query: &Q, + ) -> Pin> + Send>> + where + Q: Serialize + ?Sized, + O: DeserializeOwned + std::marker::Send + 'static, + { + let event_source = self + .http_client + .get(self.config.url(path)) + .query(query) + .query(&self.config.query()) + .headers(self.config.headers()) + .eventsource() + .unwrap(); + + stream(event_source).await + } +} + +/// Request which responds with SSE. +/// [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format) +pub(crate) async fn stream( + mut event_source: EventSource, +) -> Pin> + Send>> +where + O: DeserializeOwned + std::marker::Send + 'static, +{ + let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); + + tokio::spawn(async move { + while let Some(ev) = event_source.next().await { + match ev { + Err(e) => { + if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e.to_string()))) { + // rx dropped + break; } + } + Ok(event) => match event { + Event::Message(message) => { + if message.data == "[DONE]" { + break; + } - let response: O = serde_json::from_slice(bytes.as_ref()) - .map_err(OpenAIError::JSONDeserialize) - .map_err(backoff::Error::Permanent)?; - Ok(response) - }) - .await + let response = match serde_json::from_str::(&message.data) { + Err(e) => Err(map_deserialization_error(e, message.data.as_bytes())), + Ok(output) => Ok(output), + }; + + if let Err(_e) = tx.send(response) { + // rx dropped + break; + } + } + Event::Open => continue, + }, } - None => { - let response = client.execute(request).await?; - self.process_response(response).await + } + + event_source.close(); + }); + + Box::pin(tokio_stream::wrappers::UnboundedReceiverStream::new(rx)) +} + +pub(crate) async fn stream_mapped_raw_events( + mut event_source: EventSource, + event_mapper: impl Fn(eventsource_stream::Event) -> Result + Send + 'static, +) -> Pin> + Send>> +where + O: DeserializeOwned + std::marker::Send + 'static, +{ + let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); + + tokio::spawn(async move { + while let Some(ev) = event_source.next().await { + match ev { + Err(e) => { + if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e.to_string()))) { + // rx dropped + break; + } + } + Ok(event) => match event { + Event::Message(message) => { + let mut done = false; + + if message.data == "[DONE]" { + done = true; + } + + let response = event_mapper(message); + + if let Err(_e) = tx.send(response) { + // rx dropped + break; + } + + if done { + break; + } + } + Event::Open => continue, + }, } } - } + + event_source.close(); + }); + + Box::pin(tokio_stream::wrappers::UnboundedReceiverStream::new(rx)) } diff --git a/async-openai/src/completion.rs b/async-openai/src/completion.rs index 92b4b4df..432201c3 100644 --- a/async-openai/src/completion.rs +++ b/async-openai/src/completion.rs @@ -1,97 +1,77 @@ -use futures::stream::StreamExt; -use reqwest_eventsource::{Event, RequestBuilderExt}; - use crate::{ client::Client, + config::Config, error::OpenAIError, types::{CompletionResponseStream, CreateCompletionRequest, CreateCompletionResponse}, }; -/// Given a prompt, the model will return one or more predicted -/// completions, and can also return the probabilities of alternative -/// tokens at each position. -pub struct Completion; +/// Given a prompt, the model will return one or more predicted completions, +/// and can also return the probabilities of alternative tokens at each position. +/// We recommend most users use our Chat completions API. +/// [Learn more](https://platform.openai.com/docs/deprecations/2023-07-06-gpt-and-embeddings) +/// +/// Related guide: [Legacy Completions](https://platform.openai.com/docs/guides/gpt/completions-api) +pub struct Completions<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Completions<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } -impl Completion { /// Creates a completion for the provided prompt and parameters + /// + /// You must ensure that "stream: false" in serialized `request` + #[crate::byot( + T0 = serde::Serialize, + R = serde::de::DeserializeOwned + )] pub async fn create( - client: &Client, + &self, request: CreateCompletionRequest, ) -> Result { - if request.stream.is_some() && request.stream.unwrap() { - return Err(OpenAIError::InvalidArgument( - "When stream is true, use Completion::create_stream".into(), - )); + #[cfg(not(feature = "byot"))] + { + if request.stream.is_some() && request.stream.unwrap() { + return Err(OpenAIError::InvalidArgument( + "When stream is true, use Completion::create_stream".into(), + )); + } } - client.post("/completions", request).await + self.client.post("/completions", request).await } /// Creates a completion request for the provided prompt and parameters /// /// Stream back partial progress. Tokens will be sent as data-only /// [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format) - /// as they become available, with the stream terminated by a data: [DONE] message. + /// as they become available, with the stream terminated by a data: \[DONE\] message. /// - /// [CompletionResponseStream] is a parsed SSE stream until a [DONE] is received from server. + /// [CompletionResponseStream] is a parsed SSE stream until a \[DONE\] is received from server. + /// + /// You must ensure that "stream: true" in serialized `request` + #[crate::byot( + T0 = serde::Serialize, + R = serde::de::DeserializeOwned, + stream = "true", + where_clause = "R: std::marker::Send + 'static" + )] + #[allow(unused_mut)] pub async fn create_stream( - client: &Client, + &self, mut request: CreateCompletionRequest, ) -> Result { - if request.stream.is_some() && !request.stream.unwrap() { - return Err(OpenAIError::InvalidArgument( - "When stream is false, use Completion::create".into(), - )); - } - - request.stream = Some(true); - - let mut event_source = reqwest::Client::new() - .post(format!("{}/completions", client.api_base())) - .bearer_auth(client.api_key()) - .json(&request) - .eventsource() - .unwrap(); - - let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); - - tokio::spawn(async move { - while let Some(ev) = event_source.next().await { - match ev { - Err(e) => { - if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e.to_string()))) { - // rx dropped - break; - } - } - Ok(event) => match event { - Event::Message(message) => { - if message.data == "[DONE]" { - break; - } - - let response = match serde_json::from_str::( - &message.data, - ) { - Err(e) => Err(OpenAIError::JSONDeserialize(e)), - Ok(ccr) => Ok(ccr), - }; - - if let Err(_e) = tx.send(response) { - // rx dropped - break; - } - } - Event::Open => continue, - }, - } + #[cfg(not(feature = "byot"))] + { + if request.stream.is_some() && !request.stream.unwrap() { + return Err(OpenAIError::InvalidArgument( + "When stream is false, use Completion::create".into(), + )); } - event_source.close(); - }); - - Ok( - Box::pin(tokio_stream::wrappers::UnboundedReceiverStream::new(rx)) - as CompletionResponseStream, - ) + request.stream = Some(true); + } + Ok(self.client.post_stream("/completions", request).await) } } diff --git a/async-openai/src/config.rs b/async-openai/src/config.rs new file mode 100644 index 00000000..82ab043c --- /dev/null +++ b/async-openai/src/config.rs @@ -0,0 +1,291 @@ +//! Client configurations: [OpenAIConfig] for OpenAI, [AzureConfig] for Azure OpenAI Service. +use reqwest::header::{HeaderMap, AUTHORIZATION}; +use secrecy::{ExposeSecret, SecretString}; +use serde::Deserialize; + +/// Default v1 API base url +pub const OPENAI_API_BASE: &str = "https://api.openai.com/v1"; +/// Organization header +pub const OPENAI_ORGANIZATION_HEADER: &str = "OpenAI-Organization"; +/// Project header +pub const OPENAI_PROJECT_HEADER: &str = "OpenAI-Project"; + +/// Calls to the Assistants API require that you pass a Beta header +pub const OPENAI_BETA_HEADER: &str = "OpenAI-Beta"; + +/// [crate::Client] relies on this for every API call on OpenAI +/// or Azure OpenAI service +pub trait Config: Send + Sync { + fn headers(&self) -> HeaderMap; + fn url(&self, path: &str) -> String; + fn query(&self) -> Vec<(&str, &str)>; + + fn api_base(&self) -> &str; + + fn api_key(&self) -> &SecretString; +} + +/// Macro to implement Config trait for pointer types with dyn objects +macro_rules! impl_config_for_ptr { + ($t:ty) => { + impl Config for $t { + fn headers(&self) -> HeaderMap { + self.as_ref().headers() + } + fn url(&self, path: &str) -> String { + self.as_ref().url(path) + } + fn query(&self) -> Vec<(&str, &str)> { + self.as_ref().query() + } + fn api_base(&self) -> &str { + self.as_ref().api_base() + } + fn api_key(&self) -> &SecretString { + self.as_ref().api_key() + } + } + }; +} + +impl_config_for_ptr!(Box); +impl_config_for_ptr!(std::sync::Arc); + +/// Configuration for OpenAI API +#[derive(Clone, Debug, Deserialize)] +#[serde(default)] +pub struct OpenAIConfig { + api_base: String, + api_key: SecretString, + org_id: String, + project_id: String, +} + +impl Default for OpenAIConfig { + fn default() -> Self { + Self { + api_base: OPENAI_API_BASE.to_string(), + api_key: std::env::var("OPENAI_API_KEY") + .unwrap_or_else(|_| "".to_string()) + .into(), + org_id: Default::default(), + project_id: Default::default(), + } + } +} + +impl OpenAIConfig { + /// Create client with default [OPENAI_API_BASE] url and default API key from OPENAI_API_KEY env var + pub fn new() -> Self { + Default::default() + } + + /// To use a different organization id other than default + pub fn with_org_id>(mut self, org_id: S) -> Self { + self.org_id = org_id.into(); + self + } + + /// Non default project id + pub fn with_project_id>(mut self, project_id: S) -> Self { + self.project_id = project_id.into(); + self + } + + /// To use a different API key different from default OPENAI_API_KEY env var + pub fn with_api_key>(mut self, api_key: S) -> Self { + self.api_key = SecretString::from(api_key.into()); + self + } + + /// To use a API base url different from default [OPENAI_API_BASE] + pub fn with_api_base>(mut self, api_base: S) -> Self { + self.api_base = api_base.into(); + self + } + + pub fn org_id(&self) -> &str { + &self.org_id + } +} + +impl Config for OpenAIConfig { + fn headers(&self) -> HeaderMap { + let mut headers = HeaderMap::new(); + if !self.org_id.is_empty() { + headers.insert( + OPENAI_ORGANIZATION_HEADER, + self.org_id.as_str().parse().unwrap(), + ); + } + + if !self.project_id.is_empty() { + headers.insert( + OPENAI_PROJECT_HEADER, + self.project_id.as_str().parse().unwrap(), + ); + } + + headers.insert( + AUTHORIZATION, + format!("Bearer {}", self.api_key.expose_secret()) + .as_str() + .parse() + .unwrap(), + ); + + // hack for Assistants APIs + // Calls to the Assistants API require that you pass a Beta header + headers.insert(OPENAI_BETA_HEADER, "assistants=v2".parse().unwrap()); + + headers + } + + fn url(&self, path: &str) -> String { + format!("{}{}", self.api_base, path) + } + + fn api_base(&self) -> &str { + &self.api_base + } + + fn api_key(&self) -> &SecretString { + &self.api_key + } + + fn query(&self) -> Vec<(&str, &str)> { + vec![] + } +} + +/// Configuration for Azure OpenAI Service +#[derive(Clone, Debug, Deserialize)] +#[serde(default)] +pub struct AzureConfig { + api_version: String, + deployment_id: String, + api_base: String, + api_key: SecretString, +} + +impl Default for AzureConfig { + fn default() -> Self { + Self { + api_base: Default::default(), + api_key: std::env::var("OPENAI_API_KEY") + .unwrap_or_else(|_| "".to_string()) + .into(), + deployment_id: Default::default(), + api_version: Default::default(), + } + } +} + +impl AzureConfig { + pub fn new() -> Self { + Default::default() + } + + pub fn with_api_version>(mut self, api_version: S) -> Self { + self.api_version = api_version.into(); + self + } + + pub fn with_deployment_id>(mut self, deployment_id: S) -> Self { + self.deployment_id = deployment_id.into(); + self + } + + /// To use a different API key different from default OPENAI_API_KEY env var + pub fn with_api_key>(mut self, api_key: S) -> Self { + self.api_key = SecretString::from(api_key.into()); + self + } + + /// API base url in form of + pub fn with_api_base>(mut self, api_base: S) -> Self { + self.api_base = api_base.into(); + self + } +} + +impl Config for AzureConfig { + fn headers(&self) -> HeaderMap { + let mut headers = HeaderMap::new(); + + headers.insert("api-key", self.api_key.expose_secret().parse().unwrap()); + + headers + } + + fn url(&self, path: &str) -> String { + format!( + "{}/openai/deployments/{}{}", + self.api_base, self.deployment_id, path + ) + } + + fn api_base(&self) -> &str { + &self.api_base + } + + fn api_key(&self) -> &SecretString { + &self.api_key + } + + fn query(&self) -> Vec<(&str, &str)> { + vec![("api-version", &self.api_version)] + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::types::{ + ChatCompletionRequestMessage, ChatCompletionRequestUserMessage, CreateChatCompletionRequest, + }; + use crate::Client; + use std::sync::Arc; + #[test] + fn test_client_creation() { + unsafe { std::env::set_var("OPENAI_API_KEY", "test") } + let openai_config = OpenAIConfig::default(); + let config = Box::new(openai_config.clone()) as Box; + let client = Client::with_config(config); + assert!(client.config().url("").ends_with("/v1")); + + let config = Arc::new(openai_config) as Arc; + let client = Client::with_config(config); + assert!(client.config().url("").ends_with("/v1")); + let cloned_client = client.clone(); + assert!(cloned_client.config().url("").ends_with("/v1")); + } + + async fn dynamic_dispatch_compiles(client: &Client>) { + let _ = client.chat().create(CreateChatCompletionRequest { + model: "gpt-4o".to_string(), + messages: vec![ChatCompletionRequestMessage::User( + ChatCompletionRequestUserMessage { + content: "Hello, world!".into(), + ..Default::default() + }, + )], + ..Default::default() + }); + } + + #[tokio::test] + async fn test_dynamic_dispatch() { + let openai_config = OpenAIConfig::default(); + let azure_config = AzureConfig::default(); + + let azure_client = Client::with_config(Box::new(azure_config.clone()) as Box); + let oai_client = Client::with_config(Box::new(openai_config.clone()) as Box); + + let _ = dynamic_dispatch_compiles(&azure_client).await; + let _ = dynamic_dispatch_compiles(&oai_client).await; + + let _ = tokio::spawn(async move { dynamic_dispatch_compiles(&azure_client).await }); + let _ = tokio::spawn(async move { dynamic_dispatch_compiles(&oai_client).await }); + } +} diff --git a/async-openai/src/download.rs b/async-openai/src/download.rs index a2ec8dcb..e3d9a1e4 100644 --- a/async-openai/src/download.rs +++ b/async-openai/src/download.rs @@ -1,6 +1,7 @@ use std::path::{Path, PathBuf}; -use rand::{distributions::Alphanumeric, Rng}; +use base64::{engine::general_purpose, Engine as _}; +use rand::{distr::Alphanumeric, Rng}; use reqwest::Url; use crate::error::OpenAIError; @@ -21,7 +22,10 @@ fn create_paths>(url: &Url, base_dir: P) -> (PathBuf, PathBuf) { (dir, path) } -pub(crate) async fn download_url>(url: &str, dir: P) -> Result<(), OpenAIError> { +pub(crate) async fn download_url>( + url: &str, + dir: P, +) -> Result { let parsed_url = Url::parse(url).map_err(|e| OpenAIError::FileSaveError(e.to_string()))?; let response = reqwest::get(url) .await @@ -29,32 +33,31 @@ pub(crate) async fn download_url>(url: &str, dir: P) -> Result<() if !response.status().is_success() { return Err(OpenAIError::FileSaveError(format!( - "couldn't download file (status: {})", + "couldn't download file, status: {}, url: {url}", response.status() ))); } let (dir, file_path) = create_paths(&parsed_url, dir); - tokio::fs::create_dir_all(dir) + tokio::fs::create_dir_all(dir.as_path()) .await - .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?; + .map_err(|e| OpenAIError::FileSaveError(format!("{}, dir: {}", e, dir.display())))?; tokio::fs::write( - file_path, - response - .bytes() - .await - .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?, + file_path.as_path(), + response.bytes().await.map_err(|e| { + OpenAIError::FileSaveError(format!("{}, file path: {}", e, file_path.display())) + })?, ) .await .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?; - Ok(()) + Ok(file_path) } -pub(crate) async fn save_b64>(b64: &str, dir: P) -> Result<(), OpenAIError> { - let filename: String = rand::thread_rng() +pub(crate) async fn save_b64>(b64: &str, dir: P) -> Result { + let filename: String = rand::rng() .sample_iter(&Alphanumeric) .take(10) .map(char::from) @@ -65,11 +68,13 @@ pub(crate) async fn save_b64>(b64: &str, dir: P) -> Result<(), Op let path = PathBuf::from(dir.as_ref()).join(filename); tokio::fs::write( - path, - base64::decode(b64).map_err(|e| OpenAIError::FileSaveError(e.to_string()))?, + path.as_path(), + general_purpose::STANDARD + .decode(b64) + .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?, ) .await - .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?; + .map_err(|e| OpenAIError::FileSaveError(format!("{}, path: {}", e, path.display())))?; - Ok(()) + Ok(path) } diff --git a/async-openai/src/edit.rs b/async-openai/src/edit.rs deleted file mode 100644 index e713b98d..00000000 --- a/async-openai/src/edit.rs +++ /dev/null @@ -1,19 +0,0 @@ -use crate::{ - error::OpenAIError, - types::{CreateEditRequest, CreateEditResponse}, - Client, -}; - -/// Given a prompt and an instruction, the model will return -/// an edited version of the prompt. -pub struct Edit; - -impl Edit { - /// Creates a new edit for the provided input, instruction, and parameters - pub async fn create( - client: &Client, - request: CreateEditRequest, - ) -> Result { - client.post("/edits", request).await - } -} diff --git a/async-openai/src/embedding.rs b/async-openai/src/embedding.rs new file mode 100644 index 00000000..f5759296 --- /dev/null +++ b/async-openai/src/embedding.rs @@ -0,0 +1,219 @@ +use crate::{ + config::Config, + error::OpenAIError, + types::{CreateBase64EmbeddingResponse, CreateEmbeddingRequest, CreateEmbeddingResponse}, + Client, +}; + +#[cfg(not(feature = "byot"))] +use crate::types::EncodingFormat; + +/// Get a vector representation of a given input that can be easily +/// consumed by machine learning models and algorithms. +/// +/// Related guide: [Embeddings](https://platform.openai.com/docs/guides/embeddings/what-are-embeddings) +pub struct Embeddings<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Embeddings<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Creates an embedding vector representing the input text. + /// + /// byot: In serialized `request` you must ensure "encoding_format" is not "base64" + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: CreateEmbeddingRequest, + ) -> Result { + #[cfg(not(feature = "byot"))] + { + if matches!(request.encoding_format, Some(EncodingFormat::Base64)) { + return Err(OpenAIError::InvalidArgument( + "When encoding_format is base64, use Embeddings::create_base64".into(), + )); + } + } + self.client.post("/embeddings", request).await + } + + /// Creates an embedding vector representing the input text. + /// + /// The response will contain the embedding in base64 format. + /// + /// byot: In serialized `request` you must ensure "encoding_format" is "base64" + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create_base64( + &self, + request: CreateEmbeddingRequest, + ) -> Result { + #[cfg(not(feature = "byot"))] + { + if !matches!(request.encoding_format, Some(EncodingFormat::Base64)) { + return Err(OpenAIError::InvalidArgument( + "When encoding_format is not base64, use Embeddings::create".into(), + )); + } + } + self.client.post("/embeddings", request).await + } +} + +#[cfg(test)] +mod tests { + use crate::error::OpenAIError; + use crate::types::{CreateEmbeddingResponse, Embedding, EncodingFormat}; + use crate::{types::CreateEmbeddingRequestArgs, Client}; + + #[tokio::test] + async fn test_embedding_string() { + let client = Client::new(); + + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-ada-002") + .input("The food was delicious and the waiter...") + .build() + .unwrap(); + + let response = client.embeddings().create(request).await; + + assert!(response.is_ok()); + } + + #[tokio::test] + async fn test_embedding_string_array() { + let client = Client::new(); + + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-ada-002") + .input(["The food was delicious", "The waiter was good"]) + .build() + .unwrap(); + + let response = client.embeddings().create(request).await; + + assert!(response.is_ok()); + } + + #[tokio::test] + async fn test_embedding_integer_array() { + let client = Client::new(); + + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-ada-002") + .input([1, 2, 3]) + .build() + .unwrap(); + + let response = client.embeddings().create(request).await; + + assert!(response.is_ok()); + } + + #[tokio::test] + async fn test_embedding_array_of_integer_array_matrix() { + let client = Client::new(); + + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-ada-002") + .input([[1, 2, 3], [4, 5, 6], [7, 8, 10]]) + .build() + .unwrap(); + + let response = client.embeddings().create(request).await; + + assert!(response.is_ok()); + } + + #[tokio::test] + async fn test_embedding_array_of_integer_array() { + let client = Client::new(); + + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-ada-002") + .input([vec![1, 2, 3], vec![4, 5, 6, 7], vec![7, 8, 10, 11, 100257]]) + .build() + .unwrap(); + + let response = client.embeddings().create(request).await; + + assert!(response.is_ok()); + } + + #[tokio::test] + async fn test_embedding_with_reduced_dimensions() { + let client = Client::new(); + let dimensions = 256u32; + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-3-small") + .input("The food was delicious and the waiter...") + .dimensions(dimensions) + .build() + .unwrap(); + + let response = client.embeddings().create(request).await; + + assert!(response.is_ok()); + + let CreateEmbeddingResponse { mut data, .. } = response.unwrap(); + assert_eq!(data.len(), 1); + let Embedding { embedding, .. } = data.pop().unwrap(); + assert_eq!(embedding.len(), dimensions as usize); + } + + #[tokio::test] + #[cfg(not(feature = "byot"))] + async fn test_cannot_use_base64_encoding_with_normal_create_request() { + let client = Client::new(); + + const MODEL: &str = "text-embedding-ada-002"; + const INPUT: &str = "You shall not pass."; + + let b64_request = CreateEmbeddingRequestArgs::default() + .model(MODEL) + .input(INPUT) + .encoding_format(EncodingFormat::Base64) + .build() + .unwrap(); + let b64_response = client.embeddings().create(b64_request).await; + assert!(matches!(b64_response, Err(OpenAIError::InvalidArgument(_)))); + } + + #[tokio::test] + async fn test_embedding_create_base64() { + let client = Client::new(); + + const MODEL: &str = "text-embedding-ada-002"; + const INPUT: &str = "CoLoop will eat the other qual research tools..."; + + let b64_request = CreateEmbeddingRequestArgs::default() + .model(MODEL) + .input(INPUT) + .encoding_format(EncodingFormat::Base64) + .build() + .unwrap(); + let b64_response = client + .embeddings() + .create_base64(b64_request) + .await + .unwrap(); + let b64_embedding = b64_response.data.into_iter().next().unwrap().embedding; + let b64_embedding: Vec = b64_embedding.into(); + + let request = CreateEmbeddingRequestArgs::default() + .model(MODEL) + .input(INPUT) + .build() + .unwrap(); + let response = client.embeddings().create(request).await.unwrap(); + let embedding = response.data.into_iter().next().unwrap().embedding; + + assert_eq!(b64_embedding.len(), embedding.len()); + for (b64, normal) in b64_embedding.iter().zip(embedding.iter()) { + assert!((b64 - normal).abs() < 1e-6); + } + } +} diff --git a/async-openai/src/error.rs b/async-openai/src/error.rs index dbc6d929..a1139c9f 100644 --- a/async-openai/src/error.rs +++ b/async-openai/src/error.rs @@ -1,5 +1,5 @@ //! Errors originating from API calls, parsing responses, and reading-or-writing to the file system. -use serde::Deserialize; +use serde::{Deserialize, Serialize}; #[derive(Debug, thiserror::Error)] pub enum OpenAIError { @@ -7,7 +7,7 @@ pub enum OpenAIError { #[error("http error: {0}")] Reqwest(#[from] reqwest::Error), /// OpenAI returns error object with details of API call failure - #[error("{}: {}", .0.r#type, .0.message)] + #[error("{0}")] ApiError(ApiError), /// Error when a response cannot be deserialized into a Rust type #[error("failed to deserialize api response: {0}")] @@ -18,25 +18,59 @@ pub enum OpenAIError { /// Error on the client side when reading file from file system #[error("failed to read file: {0}")] FileReadError(String), - /// Error when trying to stream completions SSE + /// Error on SSE streaming #[error("stream failed: {0}")] StreamError(String), - /// Error from client side validation before making API call + /// Error from client side validation + /// or when builder fails to build request before making API call #[error("invalid args: {0}")] InvalidArgument(String), } /// OpenAI API returns error object on failure -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct ApiError { pub message: String, - pub r#type: String, - pub param: Option, - pub code: Option, + pub r#type: Option, + pub param: Option, + pub code: Option, +} + +impl std::fmt::Display for ApiError { + /// If all fields are available, `ApiError` is formatted as: + /// `{type}: {message} (param: {param}) (code: {code})` + /// Otherwise, missing fields will be ignored. + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut parts = Vec::new(); + + if let Some(r#type) = &self.r#type { + parts.push(format!("{}:", r#type)); + } + + parts.push(self.message.clone()); + + if let Some(param) = &self.param { + parts.push(format!("(param: {param})")); + } + + if let Some(code) = &self.code { + parts.push(format!("(code: {code})")); + } + + write!(f, "{}", parts.join(" ")) + } } /// Wrapper to deserialize the error object nested in "error" JSON key -#[derive(Deserialize)] -pub(crate) struct WrappedError { - pub(crate) error: ApiError, +#[derive(Debug, Deserialize, Serialize)] +pub struct WrappedError { + pub error: ApiError, +} + +pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError { + tracing::error!( + "failed deserialization of: {}", + String::from_utf8_lossy(bytes) + ); + OpenAIError::JSONDeserialize(e) } diff --git a/async-openai/src/file.rs b/async-openai/src/file.rs index 5e390207..cfca19c7 100644 --- a/async-openai/src/file.rs +++ b/async-openai/src/file.rs @@ -1,45 +1,68 @@ +use bytes::Bytes; +use serde::Serialize; + use crate::{ + config::Config, error::OpenAIError, types::{CreateFileRequest, DeleteFileResponse, ListFilesResponse, OpenAIFile}, - util::create_file_part, Client, }; -/// Files are used to upload documents that can be used with features like [Fine-tuning](https://beta.openai.com/docs/api-reference/fine-tunes). -pub struct File; - -impl File { - /// Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit. - pub async fn create( - client: &Client, - request: CreateFileRequest, - ) -> Result { - let file_part = create_file_part(&request.file.path).await?; - let form = reqwest::multipart::Form::new() - .part("file", file_part) - .text("purpose", request.purpose); - client.post_form("/files", form).await +/// Files are used to upload documents that can be used with features like Assistants and Fine-tuning. +pub struct Files<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Files<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Upload a file that can be used across various endpoints. Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB. + /// + /// The Assistants API supports files up to 2 million tokens and of specific file types. See the [Assistants Tools guide](https://platform.openai.com/docs/assistants/tools) for details. + /// + /// The Fine-tuning API only supports `.jsonl` files. The input also has certain required formats for fine-tuning [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input) or [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input) models. + /// + ///The Batch API only supports `.jsonl` files up to 100 MB in size. The input also has a specific required [format](https://platform.openai.com/docs/api-reference/batch/request-input). + /// + /// Please [contact us](https://help.openai.com/) if you need to increase these storage limits. + #[crate::byot( + T0 = Clone, + R = serde::de::DeserializeOwned, + where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom", + )] + pub async fn create(&self, request: CreateFileRequest) -> Result { + self.client.post_form("/files", request).await } /// Returns a list of files that belong to the user's organization. - pub async fn list(client: &Client) -> Result { - client.get("/files").await + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client.get_with_query("/files", &query).await } /// Returns information about a specific file. - pub async fn retrieve(client: &Client, file_id: &str) -> Result { - client.get(format!("/files/{file_id}").as_str()).await + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, file_id: &str) -> Result { + self.client.get(format!("/files/{file_id}").as_str()).await } /// Delete a file. - pub async fn delete(client: &Client, file_id: &str) -> Result { - client.delete(format!("/files/{file_id}").as_str()).await + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, file_id: &str) -> Result { + self.client + .delete(format!("/files/{file_id}").as_str()) + .await } /// Returns the contents of the specified file - pub async fn retrieve_content(client: &Client, file_id: &str) -> Result { - client - .get(format!("/files/{file_id}/content").as_str()) + pub async fn content(&self, file_id: &str) -> Result { + self.client + .get_raw(format!("/files/{file_id}/content").as_str()) .await } } @@ -47,63 +70,62 @@ impl File { #[cfg(test)] mod tests { use crate::{ - types::{CreateFileRequest, FileInput}, + types::{CreateFileRequestArgs, FilePurpose}, Client, }; - use super::File; #[tokio::test] async fn test_file_mod() { let test_file_path = "/tmp/test.jsonl"; - let contents = "{\"prompt\": \"\", \"completion\": \"\"} -{\"prompt\": \"\", \"completion\": \"\"}"; + let contents = concat!( + "{\"prompt\": \"\", \"completion\": \"\"}\n", // \n is to make it valid jsonl + "{\"prompt\": \"\", \"completion\": \"\"}" + ); + tokio::fs::write(test_file_path, contents).await.unwrap(); let client = Client::new(); - let request = CreateFileRequest { - file: FileInput::new(test_file_path), - purpose: "fine-tune".to_owned(), - }; - let openai_file = File::create(&client, request).await.unwrap(); + let request = CreateFileRequestArgs::default() + .file(test_file_path) + .purpose(FilePurpose::FineTune) + .build() + .unwrap(); + + let openai_file = client.files().create(request).await.unwrap(); assert_eq!(openai_file.bytes, 135); assert_eq!(openai_file.filename, "test.jsonl"); - assert_eq!(openai_file.purpose, "fine-tune"); - //assert_eq!(openai_file.status, Some("processed".to_owned())); // uploaded or processed + //assert_eq!(openai_file.purpose, "fine-tune"); - //println!("CREATE: \n{:#?}", openai_file); + //assert_eq!(openai_file.status, Some("processed".to_owned())); // uploaded or processed + let query = [("purpose", "fine-tune")]; - let list_files = File::list(&client).await.unwrap(); + let list_files = client.files().list(&query).await.unwrap(); assert_eq!(list_files.data.into_iter().last().unwrap(), openai_file); - //println!("LIST: \n{:#?}", list_files); - - let retrieve_file = File::retrieve(&client, &openai_file.id).await.unwrap(); + let retrieved_file = client.files().retrieve(&openai_file.id).await.unwrap(); - // println!("RETRIEVE: \n{:#?}", retrieve_file); - - assert_eq!(retrieve_file, openai_file); + assert_eq!(openai_file.created_at, retrieved_file.created_at); + assert_eq!(openai_file.bytes, retrieved_file.bytes); + assert_eq!(openai_file.filename, retrieved_file.filename); + assert_eq!(openai_file.purpose, retrieved_file.purpose); /* // "To help mitigate abuse, downloading of fine-tune training files is disabled for free accounts." - let retrieved_contents = File::retrieve_content(&client, &openai_file.id) + let retrieved_contents = client.files().retrieve_content(&openai_file.id) .await .unwrap(); - //println!("CONTENTS:\n{}", retrieve_contents); - assert_eq!(contents, retrieved_contents); */ // Sleep to prevent "File is still processing. Check back later." tokio::time::sleep(std::time::Duration::from_secs(15)).await; - let delete_response = File::delete(&client, &openai_file.id).await.unwrap(); - - // println!("DELETE: \n{:#?}", delete_response); + let delete_response = client.files().delete(&openai_file.id).await.unwrap(); assert_eq!(openai_file.id, delete_response.id); - assert_eq!(delete_response.deleted, true); + assert!(delete_response.deleted); } } diff --git a/async-openai/src/fine_tuning.rs b/async-openai/src/fine_tuning.rs new file mode 100644 index 00000000..c599ae63 --- /dev/null +++ b/async-openai/src/fine_tuning.rs @@ -0,0 +1,107 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + CreateFineTuningJobRequest, FineTuningJob, ListFineTuningJobCheckpointsResponse, + ListFineTuningJobEventsResponse, ListPaginatedFineTuningJobsResponse, + }, + Client, +}; + +/// Manage fine-tuning jobs to tailor a model to your specific training data. +/// +/// Related guide: [Fine-tune models](https://platform.openai.com/docs/guides/fine-tuning) +pub struct FineTuning<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> FineTuning<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Creates a job that fine-tunes a specified model from a given dataset. + /// + /// Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete. + /// + /// [Learn more about Fine-tuning](https://platform.openai.com/docs/guides/fine-tuning) + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: CreateFineTuningJobRequest, + ) -> Result { + self.client.post("/fine_tuning/jobs", request).await + } + + /// List your organization's fine-tuning jobs + #[crate::byot(T0 = serde::Serialize, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list_paginated( + &self, + query: &Q, + ) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query("/fine_tuning/jobs", &query) + .await + } + + /// Gets info about the fine-tune job. + /// + /// [Learn more about Fine-tuning](https://platform.openai.com/docs/guides/fine-tuning) + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, fine_tuning_job_id: &str) -> Result { + self.client + .get(format!("/fine_tuning/jobs/{fine_tuning_job_id}").as_str()) + .await + } + + /// Immediately cancel a fine-tune job. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn cancel(&self, fine_tuning_job_id: &str) -> Result { + self.client + .post( + format!("/fine_tuning/jobs/{fine_tuning_job_id}/cancel").as_str(), + (), + ) + .await + } + + /// Get fine-grained status updates for a fine-tune job. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list_events( + &self, + fine_tuning_job_id: &str, + query: &Q, + ) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query( + format!("/fine_tuning/jobs/{fine_tuning_job_id}/events").as_str(), + &query, + ) + .await + } + + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list_checkpoints( + &self, + fine_tuning_job_id: &str, + query: &Q, + ) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query( + format!("/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints").as_str(), + &query, + ) + .await + } +} diff --git a/async-openai/src/image.rs b/async-openai/src/image.rs index 549b790d..fd7394a8 100644 --- a/async-openai/src/image.rs +++ b/async-openai/src/image.rs @@ -1,89 +1,53 @@ use crate::{ + config::Config, error::OpenAIError, types::{ - CreateImageEditRequest, CreateImageRequest, CreateImageVariationRequest, ImageResponse, + CreateImageEditRequest, CreateImageRequest, CreateImageVariationRequest, ImagesResponse, }, - util::create_file_part, Client, }; /// Given a prompt and/or an input image, the model will generate a new image. /// -/// Related guide: [Image generation](https://beta.openai.com/docs/guides/images/introduction) -pub struct Image; +/// Related guide: [Image generation](https://platform.openai.com/docs/guides/images) +pub struct Images<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Images<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } -impl Image { /// Creates an image given a prompt. - pub async fn create( - client: &Client, - request: CreateImageRequest, - ) -> Result { - client.post("/images/generations", request).await + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create(&self, request: CreateImageRequest) -> Result { + self.client.post("/images/generations", request).await } /// Creates an edited or extended image given an original image and a prompt. + #[crate::byot( + T0 = Clone, + R = serde::de::DeserializeOwned, + where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom", + )] pub async fn create_edit( - client: &Client, + &self, request: CreateImageEditRequest, - ) -> Result { - let image_part = create_file_part(&request.image.path).await?; - let mask_part = create_file_part(&request.mask.path).await?; - - let mut form = reqwest::multipart::Form::new() - .part("image", image_part) - .part("mask", mask_part) - .text("prompt", request.prompt); - - if request.n.is_some() { - form = form.text("n", request.n.unwrap().to_string()) - } - - if request.size.is_some() { - form = form.text("size", request.size.unwrap().to_string()) - } - - if request.response_format.is_some() { - form = form.text( - "response_format", - request.response_format.unwrap().to_string(), - ) - } - - if request.user.is_some() { - form = form.text("user", request.user.unwrap()) - } - - client.post_form("/images/edits", form).await + ) -> Result { + self.client.post_form("/images/edits", request).await } /// Creates a variation of a given image. + #[crate::byot( + T0 = Clone, + R = serde::de::DeserializeOwned, + where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom", + )] pub async fn create_variation( - client: &Client, + &self, request: CreateImageVariationRequest, - ) -> Result { - let image_part = create_file_part(&request.image.path).await?; - - let mut form = reqwest::multipart::Form::new().part("image", image_part); - - if request.n.is_some() { - form = form.text("n", request.n.unwrap().to_string()) - } - - if request.size.is_some() { - form = form.text("size", request.size.unwrap().to_string()) - } - - if request.response_format.is_some() { - form = form.text( - "response_format", - request.response_format.unwrap().to_string(), - ) - } - - if request.user.is_some() { - form = form.text("user", request.user.unwrap()) - } - - client.post_form("/images/variations", form).await + ) -> Result { + self.client.post_form("/images/variations", request).await } } diff --git a/async-openai/src/invites.rs b/async-openai/src/invites.rs new file mode 100644 index 00000000..83600176 --- /dev/null +++ b/async-openai/src/invites.rs @@ -0,0 +1,52 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{Invite, InviteDeleteResponse, InviteListResponse, InviteRequest}, + Client, +}; + +/// Invite and manage invitations for an organization. Invited users are automatically added to the Default project. +pub struct Invites<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Invites<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Returns a list of invites in the organization. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query("/organization/invites", &query) + .await + } + + /// Retrieves an invite. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, invite_id: &str) -> Result { + self.client + .get(format!("/organization/invites/{invite_id}").as_str()) + .await + } + + /// Create an invite for a user to the organization. The invite must be accepted by the user before they have access to the organization. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create(&self, request: InviteRequest) -> Result { + self.client.post("/organization/invites", request).await + } + + /// Delete an invite. If the invite has already been accepted, it cannot be deleted. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, invite_id: &str) -> Result { + self.client + .delete(format!("/organization/invites/{invite_id}").as_str()) + .await + } +} diff --git a/async-openai/src/lib.rs b/async-openai/src/lib.rs index 879ec6c6..c94bc495 100644 --- a/async-openai/src/lib.rs +++ b/async-openai/src/lib.rs @@ -1,60 +1,205 @@ -//! Async Rust library for OpenAI REST API based on OpenAPI spec. +//! Rust library for OpenAI //! //! ## Creating client //! //! ``` -//! use async_openai as openai; +//! use async_openai::{Client, config::OpenAIConfig}; //! -//! // Create a client with api key from env var OPENAI_API_KEY and default base url. -//! let client = openai::Client::new(); +//! // Create a OpenAI client with api key from env var OPENAI_API_KEY and default base url. +//! let client = Client::new(); +//! +//! // Above is shortcut for +//! let config = OpenAIConfig::default(); +//! let client = Client::with_config(config); +//! +//! // OR use API key from different source and a non default organization +//! let api_key = "sk-..."; // This secret could be from a file, or environment variable. +//! let config = OpenAIConfig::new() +//! .with_api_key(api_key) +//! .with_org_id("the-continental"); //! -//! // OR use API key from different source -//! let api_key = "sk-..."; // This could be from a file, hard coding secret is not a best practice. -//! let client = openai::Client::new().with_api_key(api_key); +//! let client = Client::with_config(config); +//! +//! // Use custom reqwest client +//! let http_client = reqwest::ClientBuilder::new().user_agent("async-openai").build().unwrap(); +//! let client = Client::new().with_http_client(http_client); //! ``` //! +//! //! ## Making requests //! //!``` //!# tokio_test::block_on(async { -//! use async_openai as openai; -//! use openai::{Client, Completion, types::{CreateCompletionRequest}}; +//! +//! use async_openai::{Client, types::{CreateCompletionRequestArgs}}; //! //! // Create client //! let client = Client::new(); -//! // Create request -//! let request = CreateCompletionRequest { -//! model: "text-davinci-003".to_owned(), -//! prompt: Some("Tell me a joke about the universe".to_owned()), -//! ..Default::default() -//! }; +//! +//! // Create request using builder pattern +//! // Every request struct has companion builder struct with same name + Args suffix +//! let request = CreateCompletionRequestArgs::default() +//! .model("gpt-3.5-turbo-instruct") +//! .prompt("Tell me the recipe of alfredo pasta") +//! .max_tokens(40_u32) +//! .build() +//! .unwrap(); +//! //! // Call API -//! let response = Completion::create(&client, request).await.unwrap(); +//! let response = client +//! .completions() // Get the API "group" (completions, images, etc.) from the client +//! .create(request) // Make the API call in that "group" +//! .await +//! .unwrap(); //! //! println!("{}", response.choices.first().unwrap().text); //! # }); //!``` //! +//! ## Bring Your Own Types +//! +//! To use custom types for inputs and outputs, enable `byot` feature which provides additional generic methods with same name and `_byot` suffix. +//! This feature is available on methods whose return type is not `Bytes` +//! +//!``` +//!# #[cfg(feature = "byot")] +//!# tokio_test::block_on(async { +//! use async_openai::Client; +//! use serde_json::{Value, json}; +//! +//! let client = Client::new(); +//! +//! let response: Value = client +//! .chat() +//! .create_byot(json!({ +//! "messages": [ +//! { +//! "role": "developer", +//! "content": "You are a helpful assistant" +//! }, +//! { +//! "role": "user", +//! "content": "What do you think about life?" +//! } +//! ], +//! "model": "gpt-4o", +//! "store": false +//! })) +//! .await +//! .unwrap(); +//! +//! if let Some(content) = response["choices"][0]["message"]["content"].as_str() { +//! println!("{}", content); +//! } +//! # }); +//!``` +//! +//! ## Dynamic Dispatch for Different Providers +//! +//! For any struct that implements `Config` trait, you can wrap it in a smart pointer and cast the pointer to `dyn Config` +//! trait object, then your client can accept any wrapped configuration type. +//! +//! For example, +//! ``` +//! use async_openai::{Client, config::Config, config::OpenAIConfig}; +//! unsafe { std::env::set_var("OPENAI_API_KEY", "only for doc test") } +//! +//! let openai_config = OpenAIConfig::default(); +//! // You can use `std::sync::Arc` to wrap the config as well +//! let config = Box::new(openai_config) as Box; +//! let client: Client > = Client::with_config(config); +//! ``` +//! +//! ## Microsoft Azure +//! +//! ``` +//! use async_openai::{Client, config::AzureConfig}; +//! +//! let config = AzureConfig::new() +//! .with_api_base("https://my-resource-name.openai.azure.com") +//! .with_api_version("2023-03-15-preview") +//! .with_deployment_id("deployment-id") +//! .with_api_key("..."); +//! +//! let client = Client::with_config(config); +//! +//! // Note that `async-openai` only implements OpenAI spec +//! // and doesn't maintain parity with the spec of Azure OpenAI service. +//! +//! ``` +//! +//! //! ## Examples //! For full working examples for all supported features see [examples](https://github.com/64bit/async-openai/tree/main/examples) directory in the repository. //! +#![cfg_attr(docsrs, feature(doc_cfg))] + +#[cfg(feature = "byot")] +pub(crate) use async_openai_macros::byot; + +#[cfg(not(feature = "byot"))] +pub(crate) use async_openai_macros::byot_passthrough as byot; + +mod assistants; +mod audio; +mod audit_logs; +mod batches; +mod chat; mod client; mod completion; +pub mod config; mod download; -mod edit; +mod embedding; pub mod error; mod file; +mod fine_tuning; mod image; +mod invites; +mod messages; mod model; mod moderation; +mod project_api_keys; +mod project_service_accounts; +mod project_users; +mod projects; +mod responses; +mod runs; +mod steps; +mod threads; +pub mod traits; pub mod types; +mod uploads; +mod users; mod util; +mod vector_store_file_batches; +mod vector_store_files; +mod vector_stores; +pub use assistants::Assistants; +pub use audio::Audio; +pub use audit_logs::AuditLogs; +pub use batches::Batches; +pub use chat::Chat; pub use client::Client; -pub use client::API_BASE; -pub use completion::Completion; -pub use edit::Edit; -pub use file::File; -pub use image::Image; +pub use completion::Completions; +pub use embedding::Embeddings; +pub use file::Files; +pub use fine_tuning::FineTuning; +pub use image::Images; +pub use invites::Invites; +pub use messages::Messages; pub use model::Models; -pub use moderation::Moderation; +pub use moderation::Moderations; +pub use project_api_keys::ProjectAPIKeys; +pub use project_service_accounts::ProjectServiceAccounts; +pub use project_users::ProjectUsers; +pub use projects::Projects; +pub use responses::Responses; +pub use runs::Runs; +pub use steps::Steps; +pub use threads::Threads; +pub use uploads::Uploads; +pub use users::Users; +pub use vector_store_file_batches::VectorStoreFileBatches; +pub use vector_store_files::VectorStoreFiles; +pub use vector_stores::VectorStores; diff --git a/async-openai/src/messages.rs b/async-openai/src/messages.rs new file mode 100644 index 00000000..9368e114 --- /dev/null +++ b/async-openai/src/messages.rs @@ -0,0 +1,85 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + CreateMessageRequest, DeleteMessageResponse, ListMessagesResponse, MessageObject, + ModifyMessageRequest, + }, + Client, +}; + +/// Represents a message within a [thread](https://platform.openai.com/docs/api-reference/threads). +pub struct Messages<'c, C: Config> { + /// The ID of the [thread](https://platform.openai.com/docs/api-reference/threads) to create a message for. + pub thread_id: String, + client: &'c Client, +} + +impl<'c, C: Config> Messages<'c, C> { + pub fn new(client: &'c Client, thread_id: &str) -> Self { + Self { + client, + thread_id: thread_id.into(), + } + } + + /// Create a message. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: CreateMessageRequest, + ) -> Result { + self.client + .post(&format!("/threads/{}/messages", self.thread_id), request) + .await + } + + /// Retrieve a message. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, message_id: &str) -> Result { + self.client + .get(&format!( + "/threads/{}/messages/{message_id}", + self.thread_id + )) + .await + } + + /// Modifies a message. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn update( + &self, + message_id: &str, + request: ModifyMessageRequest, + ) -> Result { + self.client + .post( + &format!("/threads/{}/messages/{message_id}", self.thread_id), + request, + ) + .await + } + + /// Returns a list of messages for a given thread. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query(&format!("/threads/{}/messages", self.thread_id), &query) + .await + } + + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, message_id: &str) -> Result { + self.client + .delete(&format!( + "/threads/{}/messages/{message_id}", + self.thread_id + )) + .await + } +} diff --git a/async-openai/src/model.rs b/async-openai/src/model.rs index c796b72c..47cc8781 100644 --- a/async-openai/src/model.rs +++ b/async-openai/src/model.rs @@ -1,24 +1,41 @@ use crate::{ + config::Config, error::OpenAIError, - types::{ListModelResponse, Model}, + types::{DeleteModelResponse, ListModelResponse, Model}, Client, }; /// List and describe the various models available in the API. -/// You can refer to the [Models](https://beta.openai.com/docs/models) documentation to understand what +/// You can refer to the [Models](https://platform.openai.com/docs/models) documentation to understand what /// models are available and the differences between them. -pub struct Models; +pub struct Models<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Models<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } -impl Models { /// Lists the currently available models, and provides basic information /// about each one such as the owner and availability. - pub async fn list(client: &Client) -> Result { - client.get("/models").await + #[crate::byot(R = serde::de::DeserializeOwned)] + pub async fn list(&self) -> Result { + self.client.get("/models").await } /// Retrieves a model instance, providing basic information about the model /// such as the owner and permissioning. - pub async fn retrieve(client: &Client, id: &str) -> Result { - client.get(format!("/models/{id}").as_str()).await + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, id: &str) -> Result { + self.client.get(format!("/models/{id}").as_str()).await + } + + /// Delete a fine-tuned model. You must have the Owner role in your organization. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, model: &str) -> Result { + self.client + .delete(format!("/models/{model}").as_str()) + .await } } diff --git a/async-openai/src/moderation.rs b/async-openai/src/moderation.rs index e403dff5..6f831374 100644 --- a/async-openai/src/moderation.rs +++ b/async-openai/src/moderation.rs @@ -1,20 +1,29 @@ use crate::{ + config::Config, error::OpenAIError, types::{CreateModerationRequest, CreateModerationResponse}, Client, }; -/// Given a input text, outputs if the model classifies it as violating OpenAI's content policy. +/// Given text and/or image inputs, classifies if those inputs are potentially harmful across several categories. /// -/// Related guide: [Moderations](https://beta.openai.com/docs/guides/moderation/overview) -pub struct Moderation; +/// Related guide: [Moderations](https://platform.openai.com/docs/guides/moderation) +pub struct Moderations<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Moderations<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } -impl Moderation { - /// Classifies if text violates OpenAI's Content Policy + /// Classifies if text and/or image inputs are potentially harmful. Learn + /// more in the [moderation guide](https://platform.openai.com/docs/guides/moderation). + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] pub async fn create( - client: &Client, + &self, request: CreateModerationRequest, ) -> Result { - client.post("/moderations", request).await + self.client.post("/moderations", request).await } } diff --git a/async-openai/src/project_api_keys.rs b/async-openai/src/project_api_keys.rs new file mode 100644 index 00000000..6f3778d0 --- /dev/null +++ b/async-openai/src/project_api_keys.rs @@ -0,0 +1,66 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ProjectApiKey, ProjectApiKeyDeleteResponse, ProjectApiKeyListResponse}, + Client, +}; + +/// Manage API keys for a given project. Supports listing and deleting keys for users. +/// This API does not allow issuing keys for users, as users need to authorize themselves to generate keys. +pub struct ProjectAPIKeys<'c, C: Config> { + client: &'c Client, + pub project_id: String, +} + +impl<'c, C: Config> ProjectAPIKeys<'c, C> { + pub fn new(client: &'c Client, project_id: &str) -> Self { + Self { + client, + project_id: project_id.into(), + } + } + + /// Returns a list of API keys in the project. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query( + format!("/organization/projects/{}/api_keys", self.project_id).as_str(), + &query, + ) + .await + } + + /// Retrieves an API key in the project. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, api_key: &str) -> Result { + self.client + .get( + format!( + "/organization/projects/{}/api_keys/{api_key}", + self.project_id + ) + .as_str(), + ) + .await + } + + /// Deletes an API key from the project. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, api_key: &str) -> Result { + self.client + .delete( + format!( + "/organization/projects/{}/api_keys/{api_key}", + self.project_id + ) + .as_str(), + ) + .await + } +} diff --git a/async-openai/src/project_service_accounts.rs b/async-openai/src/project_service_accounts.rs new file mode 100644 index 00000000..04b02aaf --- /dev/null +++ b/async-openai/src/project_service_accounts.rs @@ -0,0 +1,100 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + ProjectServiceAccount, ProjectServiceAccountCreateRequest, + ProjectServiceAccountCreateResponse, ProjectServiceAccountDeleteResponse, + ProjectServiceAccountListResponse, + }, + Client, +}; + +/// Manage service accounts within a project. A service account is a bot user that is not +/// associated with a user. If a user leaves an organization, their keys and membership in projects +/// will no longer work. Service accounts do not have this limitation. +/// However, service accounts can also be deleted from a project. +pub struct ProjectServiceAccounts<'c, C: Config> { + client: &'c Client, + pub project_id: String, +} + +impl<'c, C: Config> ProjectServiceAccounts<'c, C> { + pub fn new(client: &'c Client, project_id: &str) -> Self { + Self { + client, + project_id: project_id.into(), + } + } + + /// Returns a list of service accounts in the project. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query( + format!( + "/organization/projects/{}/service_accounts", + self.project_id + ) + .as_str(), + &query, + ) + .await + } + + /// Creates a new service account in the project. This also returns an unredacted API key for the service account. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: ProjectServiceAccountCreateRequest, + ) -> Result { + self.client + .post( + format!( + "/organization/projects/{}/service_accounts", + self.project_id + ) + .as_str(), + request, + ) + .await + } + + /// Retrieves a service account in the project. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve( + &self, + service_account_id: &str, + ) -> Result { + self.client + .get( + format!( + "/organization/projects/{}/service_accounts/{service_account_id}", + self.project_id + ) + .as_str(), + ) + .await + } + + /// Deletes a service account from the project. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete( + &self, + service_account_id: &str, + ) -> Result { + self.client + .delete( + format!( + "/organization/projects/{}/service_accounts/{service_account_id}", + self.project_id + ) + .as_str(), + ) + .await + } +} diff --git a/async-openai/src/project_users.rs b/async-openai/src/project_users.rs new file mode 100644 index 00000000..bd790d5a --- /dev/null +++ b/async-openai/src/project_users.rs @@ -0,0 +1,86 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + ProjectUser, ProjectUserCreateRequest, ProjectUserDeleteResponse, ProjectUserListResponse, + ProjectUserUpdateRequest, + }, + Client, +}; + +/// Manage users within a project, including adding, updating roles, and removing users. +/// Users cannot be removed from the Default project, unless they are being removed from the organization. +pub struct ProjectUsers<'c, C: Config> { + client: &'c Client, + pub project_id: String, +} + +impl<'c, C: Config> ProjectUsers<'c, C> { + pub fn new(client: &'c Client, project_id: &str) -> Self { + Self { + client, + project_id: project_id.into(), + } + } + + /// Returns a list of users in the project. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query( + format!("/organization/projects/{}/users", self.project_id).as_str(), + &query, + ) + .await + } + + /// Adds a user to the project. Users must already be members of the organization to be added to a project. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: ProjectUserCreateRequest, + ) -> Result { + self.client + .post( + format!("/organization/projects/{}/users", self.project_id).as_str(), + request, + ) + .await + } + + /// Retrieves a user in the project. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, user_id: &str) -> Result { + self.client + .get(format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str()) + .await + } + + /// Modifies a user's role in the project. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn modify( + &self, + user_id: &str, + request: ProjectUserUpdateRequest, + ) -> Result { + self.client + .post( + format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str(), + request, + ) + .await + } + + /// Deletes a user from the project. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, user_id: &str) -> Result { + self.client + .delete(format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str()) + .await + } +} diff --git a/async-openai/src/projects.rs b/async-openai/src/projects.rs new file mode 100644 index 00000000..5b058636 --- /dev/null +++ b/async-openai/src/projects.rs @@ -0,0 +1,87 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + project_api_keys::ProjectAPIKeys, + types::{Project, ProjectCreateRequest, ProjectListResponse, ProjectUpdateRequest}, + Client, ProjectServiceAccounts, ProjectUsers, +}; + +/// Manage the projects within an organization includes creation, updating, and archiving or projects. +/// The Default project cannot be modified or archived. +pub struct Projects<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Projects<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + // call [ProjectUsers] group APIs + pub fn users(&self, project_id: &str) -> ProjectUsers { + ProjectUsers::new(self.client, project_id) + } + + // call [ProjectServiceAccounts] group APIs + pub fn service_accounts(&self, project_id: &str) -> ProjectServiceAccounts { + ProjectServiceAccounts::new(self.client, project_id) + } + + // call [ProjectAPIKeys] group APIs + pub fn api_keys(&self, project_id: &str) -> ProjectAPIKeys { + ProjectAPIKeys::new(self.client, project_id) + } + + /// Returns a list of projects. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query("/organization/projects", &query) + .await + } + + /// Create a new project in the organization. Projects can be created and archived, but cannot be deleted. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create(&self, request: ProjectCreateRequest) -> Result { + self.client.post("/organization/projects", request).await + } + + /// Retrieves a project. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, project_id: String) -> Result { + self.client + .get(format!("/organization/projects/{project_id}").as_str()) + .await + } + + /// Modifies a project in the organization. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn modify( + &self, + project_id: String, + request: ProjectUpdateRequest, + ) -> Result { + self.client + .post( + format!("/organization/projects/{project_id}").as_str(), + request, + ) + .await + } + + /// Archives a project in the organization. Archived projects cannot be used or updated. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn archive(&self, project_id: String) -> Result { + self.client + .post( + format!("/organization/projects/{project_id}/archive").as_str(), + (), + ) + .await + } +} diff --git a/async-openai/src/responses.rs b/async-openai/src/responses.rs new file mode 100644 index 00000000..9160b7be --- /dev/null +++ b/async-openai/src/responses.rs @@ -0,0 +1,55 @@ +use crate::{ + config::Config, + error::OpenAIError, + types::responses::{CreateResponse, Response, ResponseStream}, + Client, +}; + +/// Given text input or a list of context items, the model will generate a response. +/// +/// Related guide: [Responses](https://platform.openai.com/docs/api-reference/responses) +pub struct Responses<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Responses<'c, C> { + /// Constructs a new Responses client. + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Creates a model response for the given input. + #[crate::byot( + T0 = serde::Serialize, + R = serde::de::DeserializeOwned + )] + pub async fn create(&self, request: CreateResponse) -> Result { + self.client.post("/responses", request).await + } + + /// Creates a model response for the given input with streaming. + /// + /// Response events will be sent as server-sent events as they become available, + #[crate::byot( + T0 = serde::Serialize, + R = serde::de::DeserializeOwned, + stream = "true", + where_clause = "R: std::marker::Send + 'static" + )] + #[allow(unused_mut)] + pub async fn create_stream( + &self, + mut request: CreateResponse, + ) -> Result { + #[cfg(not(feature = "byot"))] + { + if matches!(request.stream, Some(false)) { + return Err(OpenAIError::InvalidArgument( + "When stream is false, use Responses::create".into(), + )); + } + request.stream = Some(true); + } + Ok(self.client.post_stream("/responses", request).await) + } +} diff --git a/async-openai/src/runs.rs b/async-openai/src/runs.rs new file mode 100644 index 00000000..4d022ec6 --- /dev/null +++ b/async-openai/src/runs.rs @@ -0,0 +1,178 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + steps::Steps, + types::{ + AssistantEventStream, CreateRunRequest, ListRunsResponse, ModifyRunRequest, RunObject, + SubmitToolOutputsRunRequest, + }, + Client, +}; + +/// Represents an execution run on a thread. +/// +/// Related guide: [Assistants](https://platform.openai.com/docs/assistants/overview) +pub struct Runs<'c, C: Config> { + pub thread_id: String, + client: &'c Client, +} + +impl<'c, C: Config> Runs<'c, C> { + pub fn new(client: &'c Client, thread_id: &str) -> Self { + Self { + client, + thread_id: thread_id.into(), + } + } + + /// [Steps] API group + pub fn steps(&self, run_id: &str) -> Steps { + Steps::new(self.client, &self.thread_id, run_id) + } + + /// Create a run. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create(&self, request: CreateRunRequest) -> Result { + self.client + .post(&format!("/threads/{}/runs", self.thread_id), request) + .await + } + + /// Create a run. + /// + /// byot: You must ensure "stream: true" in serialized `request` + #[crate::byot( + T0 = serde::Serialize, + R = serde::de::DeserializeOwned, + stream = "true", + where_clause = "R: std::marker::Send + 'static + TryFrom" + )] + #[allow(unused_mut)] + pub async fn create_stream( + &self, + mut request: CreateRunRequest, + ) -> Result { + #[cfg(not(feature = "byot"))] + { + if request.stream.is_some() && !request.stream.unwrap() { + return Err(OpenAIError::InvalidArgument( + "When stream is false, use Runs::create".into(), + )); + } + + request.stream = Some(true); + } + + Ok(self + .client + .post_stream_mapped_raw_events( + &format!("/threads/{}/runs", self.thread_id), + request, + TryFrom::try_from, + ) + .await) + } + + /// Retrieves a run. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, run_id: &str) -> Result { + self.client + .get(&format!("/threads/{}/runs/{run_id}", self.thread_id)) + .await + } + + /// Modifies a run. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn update( + &self, + run_id: &str, + request: ModifyRunRequest, + ) -> Result { + self.client + .post( + &format!("/threads/{}/runs/{run_id}", self.thread_id), + request, + ) + .await + } + + /// Returns a list of runs belonging to a thread. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query(&format!("/threads/{}/runs", self.thread_id), &query) + .await + } + + /// When a run has the status: "requires_action" and required_action.type is submit_tool_outputs, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn submit_tool_outputs( + &self, + run_id: &str, + request: SubmitToolOutputsRunRequest, + ) -> Result { + self.client + .post( + &format!( + "/threads/{}/runs/{run_id}/submit_tool_outputs", + self.thread_id + ), + request, + ) + .await + } + + /// byot: You must ensure "stream: true" in serialized `request` + #[crate::byot( + T0 = std::fmt::Display, + T1 = serde::Serialize, + R = serde::de::DeserializeOwned, + stream = "true", + where_clause = "R: std::marker::Send + 'static + TryFrom" + )] + #[allow(unused_mut)] + pub async fn submit_tool_outputs_stream( + &self, + run_id: &str, + mut request: SubmitToolOutputsRunRequest, + ) -> Result { + #[cfg(not(feature = "byot"))] + { + if request.stream.is_some() && !request.stream.unwrap() { + return Err(OpenAIError::InvalidArgument( + "When stream is false, use Runs::submit_tool_outputs".into(), + )); + } + + request.stream = Some(true); + } + + Ok(self + .client + .post_stream_mapped_raw_events( + &format!( + "/threads/{}/runs/{run_id}/submit_tool_outputs", + self.thread_id + ), + request, + TryFrom::try_from, + ) + .await) + } + + /// Cancels a run that is `in_progress` + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn cancel(&self, run_id: &str) -> Result { + self.client + .post( + &format!("/threads/{}/runs/{run_id}/cancel", self.thread_id), + (), + ) + .await + } +} diff --git a/async-openai/src/steps.rs b/async-openai/src/steps.rs new file mode 100644 index 00000000..924cda82 --- /dev/null +++ b/async-openai/src/steps.rs @@ -0,0 +1,50 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ListRunStepsResponse, RunStepObject}, + Client, +}; + +/// Represents a step in execution of a run. +pub struct Steps<'c, C: Config> { + pub thread_id: String, + pub run_id: String, + client: &'c Client, +} + +impl<'c, C: Config> Steps<'c, C> { + pub fn new(client: &'c Client, thread_id: &str, run_id: &str) -> Self { + Self { + client, + thread_id: thread_id.into(), + run_id: run_id.into(), + } + } + + /// Retrieves a run step. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, step_id: &str) -> Result { + self.client + .get(&format!( + "/threads/{}/runs/{}/steps/{step_id}", + self.thread_id, self.run_id + )) + .await + } + + /// Returns a list of run steps belonging to a run. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query( + &format!("/threads/{}/runs/{}/steps", self.thread_id, self.run_id), + &query, + ) + .await + } +} diff --git a/async-openai/src/threads.rs b/async-openai/src/threads.rs new file mode 100644 index 00000000..8e738a67 --- /dev/null +++ b/async-openai/src/threads.rs @@ -0,0 +1,101 @@ +use crate::{ + config::Config, + error::OpenAIError, + types::{ + AssistantEventStream, CreateThreadAndRunRequest, CreateThreadRequest, DeleteThreadResponse, + ModifyThreadRequest, RunObject, ThreadObject, + }, + Client, Messages, Runs, +}; + +/// Create threads that assistants can interact with. +/// +/// Related guide: [Assistants](https://platform.openai.com/docs/assistants/overview) +pub struct Threads<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Threads<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Call [Messages] group API to manage message in [thread_id] thread. + pub fn messages(&self, thread_id: &str) -> Messages { + Messages::new(self.client, thread_id) + } + + /// Call [Runs] group API to manage runs in [thread_id] thread. + pub fn runs(&self, thread_id: &str) -> Runs { + Runs::new(self.client, thread_id) + } + + /// Create a thread and run it in one request. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create_and_run( + &self, + request: CreateThreadAndRunRequest, + ) -> Result { + self.client.post("/threads/runs", request).await + } + + /// Create a thread and run it in one request (streaming). + /// + /// byot: You must ensure "stream: true" in serialized `request` + #[crate::byot( + T0 = serde::Serialize, + R = serde::de::DeserializeOwned, + stream = "true", + where_clause = "R: std::marker::Send + 'static + TryFrom" + )] + #[allow(unused_mut)] + pub async fn create_and_run_stream( + &self, + mut request: CreateThreadAndRunRequest, + ) -> Result { + #[cfg(not(feature = "byot"))] + { + if request.stream.is_some() && !request.stream.unwrap() { + return Err(OpenAIError::InvalidArgument( + "When stream is false, use Threads::create_and_run".into(), + )); + } + + request.stream = Some(true); + } + Ok(self + .client + .post_stream_mapped_raw_events("/threads/runs", request, TryFrom::try_from) + .await) + } + + /// Create a thread. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create(&self, request: CreateThreadRequest) -> Result { + self.client.post("/threads", request).await + } + + /// Retrieves a thread. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, thread_id: &str) -> Result { + self.client.get(&format!("/threads/{thread_id}")).await + } + + /// Modifies a thread. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn update( + &self, + thread_id: &str, + request: ModifyThreadRequest, + ) -> Result { + self.client + .post(&format!("/threads/{thread_id}"), request) + .await + } + + /// Delete a thread. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, thread_id: &str) -> Result { + self.client.delete(&format!("/threads/{thread_id}")).await + } +} diff --git a/async-openai/src/traits.rs b/async-openai/src/traits.rs new file mode 100644 index 00000000..62e8ae3c --- /dev/null +++ b/async-openai/src/traits.rs @@ -0,0 +1,7 @@ +pub trait AsyncTryFrom: Sized { + /// The type returned in the event of a conversion error. + type Error; + + /// Performs the conversion. + fn try_from(value: T) -> impl std::future::Future> + Send; +} diff --git a/async-openai/src/types.rs b/async-openai/src/types.rs deleted file mode 100644 index 67893506..00000000 --- a/async-openai/src/types.rs +++ /dev/null @@ -1,547 +0,0 @@ -//! Types used in OpenAI API requests and responses. -//! These types are created from component schemas in the [OpenAPI spec](https://github.com/openai/openai-openapi) -use std::{ - collections::HashMap, - fmt::Display, - path::{Path, PathBuf}, - pin::Pin, -}; - -use futures::Stream; -use serde::{Deserialize, Serialize}; - -use crate::{ - download::{download_url, save_b64}, - error::OpenAIError, -}; - -#[derive(Debug, Deserialize)] -pub struct Model { - pub id: String, - pub object: String, - pub created: u32, - pub owned_by: String, -} - -#[derive(Debug, Deserialize)] -pub struct ListModelResponse { - pub object: String, - pub data: Vec, -} - -#[derive(Serialize, Default, Debug)] -pub struct CreateCompletionRequest { - /// ID of the model to use. You can use the [List models](/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](/docs/models/overview) for descriptions of them. - pub model: String, - - /// The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays. - /// - /// Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document. - #[serde(skip_serializing_if = "Option::is_none")] - pub prompt: Option, // todo check type - - /// The suffix that comes after a completion of inserted text. - #[serde(skip_serializing_if = "Option::is_none")] - pub suffix: Option, // todo: default null - - /// The maximum number of [tokens](/tokenizer) to generate in the completion. - /// - /// The token count of your prompt plus `max_tokens` cannot exceed the model's context length. Most models have a context length of 2048 tokens (except for the newest models, which support 4096). - #[serde(skip_serializing_if = "Option::is_none")] - pub max_tokens: Option, - - /// What [sampling temperature](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277) to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. - /// - /// We generally recommend altering this or `top_p` but not both. - #[serde(skip_serializing_if = "Option::is_none")] - pub temperature: Option, // todo: min:0 ,max: 2, default: 1, - - /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. - /// - /// We generally recommend altering this or `temperature` but not both. - #[serde(skip_serializing_if = "Option::is_none")] - pub top_p: Option, // todo: min: 0, max: 1, default: 1 - - /// How many completions to generate for each prompt. - - /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. - /// - #[serde(skip_serializing_if = "Option::is_none")] - pub n: Option, // min:1 max: 128, default: 1 - - /// Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) - /// as they become available, with the stream terminated by a `data: [DONE]` message. - #[serde(skip_serializing_if = "Option::is_none")] - pub stream: Option, - - /// Include the log probabilities on the `logprobs` most likely tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response. - - /// The maximum value for `logprobs` is 5. If you need more than this, please contact us through our [Help center](https://help.openai.com) and describe your use case. - #[serde(skip_serializing_if = "Option::is_none")] - pub logprobs: Option, // min:0 , max: 5, default: null - - /// Echo back the prompt in addition to the completion - #[serde(skip_serializing_if = "Option::is_none")] - pub echo: Option, - - /// Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. - #[serde(skip_serializing_if = "Option::is_none")] - pub stop: Option, //todo: type? - - /// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. - /// - /// [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details) - #[serde(skip_serializing_if = "Option::is_none")] - pub presence_penalty: Option, // min: -2.0, max: 2.0, default 0 - - /// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. - /// - /// [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details) - #[serde(skip_serializing_if = "Option::is_none")] - pub frequency_penalty: Option, // min: -2.0, max: 2.0, default: 0 - - /// Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed. - /// - /// When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`. - /// - /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. - #[serde(skip_serializing_if = "Option::is_none")] - pub best_of: Option, //min: 0, max: 20, default: 1 - - /// Modify the likelihood of specified tokens appearing in the completion. - /// - /// Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](/tokenizer?view=bpe) (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. - /// - /// As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated. - #[serde(skip_serializing_if = "Option::is_none")] - pub logit_bias: Option>, // default: null - - /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](/docs/usage-policies/end-user-ids). - #[serde(skip_serializing_if = "Option::is_none")] - pub user: Option, -} - -#[derive(Debug, Deserialize)] -pub struct Logprobs { - pub tokens: Vec, - pub token_logprobs: Vec>, // Option is to account for null value in the list - pub top_logprobs: Vec, - pub text_offset: Vec, -} - -#[derive(Debug, Deserialize)] -pub struct Choice { - pub text: String, - pub index: u32, - pub logprobs: Option, - pub finish_reason: Option, -} - -#[derive(Debug, Deserialize)] -pub struct Usage { - pub prompt_tokens: u32, - pub completion_tokens: u32, - pub total_tokens: u32, -} - -#[derive(Debug, Deserialize)] -pub struct CreateCompletionResponse { - pub id: String, - pub object: String, - pub created: u32, - pub model: String, - pub choices: Vec, - pub usage: Option, -} - -/// Parsed server side events stream until an [DONE] is received from server. -pub type CompletionResponseStream = - Pin>>>; - -#[derive(Debug, Serialize, Default)] -pub struct CreateEditRequest { - /// ID of the model to use. You can use the [List models](/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](/docs/models/overview) for descriptions of them. - pub model: String, - - /// The input text to use as a starting point for the edit. - #[serde(skip_serializing_if = "Option::is_none")] - pub input: Option, // default '' - - /// The instruction that tells the model how to edit the prompt. - pub instruction: String, - - /// How many edits to generate for the input and instruction. - #[serde(skip_serializing_if = "Option::is_none")] - pub n: Option, // min:1 max: 20 default:1 - - /// What [sampling temperature](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277) to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. - /// - /// We generally recommend altering this or `top_p` but not both. - #[serde(skip_serializing_if = "Option::is_none")] - pub temperature: Option, // todo: min:0 ,max: 2, default: 1, - - /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. - /// - /// We generally recommend altering this or `temperature` but not both. - #[serde(skip_serializing_if = "Option::is_none")] - pub top_p: Option, // todo: min: 0, max: 1, default: 1 -} - -#[derive(Debug, Deserialize)] -pub struct CreateEditResponse { - pub id: Option, - pub object: String, - pub created: u32, - pub model: Option, - pub choices: Vec, - pub usage: Usage, -} - -#[derive(Default, Debug, Serialize)] -pub enum ImageSize { - #[serde(rename = "256x256")] - S256x256, - #[serde(rename = "512x512")] - S512x512, - #[default] - #[serde(rename = "1024x1024")] - S1024x1024, -} - -impl Display for ImageSize { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - ImageSize::S256x256 => "256x256", - ImageSize::S512x512 => "512x512", - ImageSize::S1024x1024 => "1024x1024", - } - ) - } -} - -#[derive(Debug, Serialize, Default)] -#[serde(rename_all = "lowercase")] -pub enum ResponseFormat { - #[default] - Url, - #[serde(rename = "b64_json")] - B64Json, -} - -impl Display for ResponseFormat { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - ResponseFormat::Url => "url", - ResponseFormat::B64Json => "b64_json", - } - ) - } -} - -#[derive(Debug, Serialize, Default)] -pub struct CreateImageRequest { - /// A text description of the desired image(s). The maximum length is 1000 characters. - pub prompt: String, - - /// The number of images to generate. Must be between 1 and 10. - #[serde(skip_serializing_if = "Option::is_none")] - pub n: Option, // min:1 max:10 default:1 - - /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`. - #[serde(skip_serializing_if = "Option::is_none")] - pub size: Option, - - /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. - #[serde(skip_serializing_if = "Option::is_none")] - pub response_format: Option, - - /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](/docs/usage-policies/end-user-ids). - #[serde(skip_serializing_if = "Option::is_none")] - pub user: Option, -} - -#[derive(Debug, Deserialize)] -#[serde(rename_all = "lowercase")] -pub enum ImageData { - Url(std::sync::Arc), - #[serde(rename = "b64_json")] - B64Json(std::sync::Arc), -} - -#[derive(Debug, Deserialize)] -pub struct ImageResponse { - pub created: u32, - pub data: Vec>, -} - -#[derive(Debug, Default)] -pub struct ImageInput { - pub path: PathBuf, -} - -impl ImageInput { - pub fn new>(path: P) -> Self { - ImageInput { - path: PathBuf::from(path.as_ref()), - } - } -} - -impl ImageResponse { - pub async fn save>(&self, dir: P) -> Result<(), OpenAIError> { - let exists = match Path::try_exists(dir.as_ref()) { - Ok(exists) => exists, - Err(e) => return Err(OpenAIError::FileSaveError(e.to_string())), - }; - - if !exists { - std::fs::create_dir_all(dir.as_ref()) - .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?; - } - - let mut handles = vec![]; - for id in self.data.clone() { - let dir_buf = PathBuf::from(dir.as_ref()); - handles.push(tokio::spawn(async move { id.save(dir_buf).await })); - } - - let result = futures::future::join_all(handles).await; - - let errors: Vec = result - .into_iter() - .filter(|r| r.is_err() || r.as_ref().ok().unwrap().is_err()) - .map(|r| match r { - Err(e) => OpenAIError::FileSaveError(e.to_string()), - Ok(inner) => inner.err().unwrap(), - }) - .collect(); - - if errors.len() > 0 { - Err(OpenAIError::FileSaveError( - errors - .into_iter() - .map(|e| e.to_string()) - .collect::>() - .join("; "), - )) - } else { - Ok(()) - } - } -} - -impl ImageData { - async fn save>(&self, dir: P) -> Result<(), OpenAIError> { - match self { - ImageData::Url(url) => download_url(url, dir).await?, - ImageData::B64Json(b64_json) => save_b64(b64_json, dir).await?, - } - Ok(()) - } -} - -#[derive(Debug, Default)] -pub struct CreateImageEditRequest { - /// The image to edit. Must be a valid PNG file, less than 4MB, and square. - pub image: ImageInput, - - /// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`. - pub mask: ImageInput, - - /// A text description of the desired image(s). The maximum length is 1000 characters. - pub prompt: String, - - /// The number of images to generate. Must be between 1 and 10. - pub n: Option, // min:1 max:10 default:1 - - /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`. - pub size: Option, - - /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. - pub response_format: Option, - - /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](/docs/usage-policies/end-user-ids). - pub user: Option, -} - -#[derive(Debug, Default)] -pub struct CreateImageVariationRequest { - /// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square. - pub image: ImageInput, - - /// The number of images to generate. Must be between 1 and 10. - pub n: Option, // min:1 max:10 default:1 - - /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`. - pub size: Option, - - /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. - pub response_format: Option, - - /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](/docs/usage-policies/end-user-ids). - pub user: Option, -} - -#[derive(Debug, Serialize)] -#[serde(untagged)] -pub enum Input { - Single(String), - Array(Vec), -} - -impl Default for Input { - fn default() -> Self { - Input::Single("".to_owned()) - } -} - -#[derive(Debug, Serialize, Default)] -pub enum TextModerationModel { - #[default] - #[serde(rename = "text-moderation-latest")] - Latest, - #[serde(rename = "text-moderation-stable")] - Stable, -} - -#[derive(Debug, Serialize, Default)] -pub struct CreateModerationRequest { - /// The input text to classify - pub input: Input, - - /// Two content moderations models are available: `text-moderation-stable` and `text-moderation-latest`. - /// - /// The default is `text-moderation-latest` which will be automatically upgraded over time. This ensures you are always using our most accurate model. If you use `text-moderation-stable`, we will provide advanced notice before updating the model. Accuracy of `text-moderation-stable` may be slightly lower than for `text-moderation-latest`. - #[serde(skip_serializing_if = "Option::is_none")] - pub model: Option, -} - -#[derive(Debug, Deserialize)] -pub struct Category { - pub hate: bool, - #[serde(rename = "hate/threatening")] - pub hate_threatening: bool, - #[serde(rename = "self-harm")] - pub self_harm: bool, - pub sexual: bool, - #[serde(rename = "sexual/minors")] - pub sexual_minors: bool, - pub violence: bool, - #[serde(rename = "violence/graphic")] - pub violence_graphic: bool, -} - -#[derive(Debug, Deserialize)] -pub struct CategoryScore { - pub hate: f32, - #[serde(rename = "hate/threatening")] - pub hate_threatening: f32, - #[serde(rename = "self-harm")] - pub self_harm: f32, - pub sexual: f32, - #[serde(rename = "sexual/minors")] - pub sexual_minors: f32, - pub violence: f32, - #[serde(rename = "violence/graphic")] - pub violence_graphic: f32, -} - -#[derive(Debug, Deserialize)] -pub struct ContentModerationResult { - pub flagged: bool, - pub categories: Category, - pub category_scores: CategoryScore, -} - -#[derive(Debug, Deserialize)] -pub struct CreateModerationResponse { - pub id: String, - pub model: String, - pub results: Vec, -} - -pub struct FileInput { - pub path: PathBuf, -} - -impl FileInput { - pub fn new>(path: P) -> Self { - Self { - path: PathBuf::from(path.as_ref()), - } - } -} - -pub struct CreateFileRequest { - /// Name of the [JSON Lines](https://jsonlines.readthedocs.io/en/latest/) file to be uploaded. - /// - /// If the `purpose` is set to "fine-tune", each line is a JSON record with "prompt" and "completion" fields representing your [training examples](/docs/guides/fine-tuning/prepare-training-data). - pub file: FileInput, - - /// The intended purpose of the uploaded documents. - /// - /// Use "fine-tune" for [Fine-tuning](/docs/api-reference/fine-tunes). This allows us to validate the format of the uploaded file. - pub purpose: String, -} - -#[derive(Debug, Deserialize)] -pub struct ListFilesResponse { - pub object: String, - pub data: Vec, -} - -#[derive(Debug, Deserialize)] -pub struct DeleteFileResponse { - pub id: String, - pub object: String, - pub deleted: bool, -} - -#[derive(Debug, Deserialize, PartialEq)] -pub struct OpenAIFile { - pub id: String, - pub object: String, - pub bytes: u32, - pub created_at: u32, - pub filename: String, - pub purpose: String, - pub status: Option, - pub status_details: Option, // nullable: true -} - -/* Not used yet -pub struct FineTune { - pub id: String, - pub object: String, - pub created_at: u32, - pub updated_at: u32, - pub model: String, - pub fine_tuned_model: String, // nullable: true - pub organization_id: String, - pub status: String, - pub hyperparams: serde_json::Value, - pub training_files: Vec, - pub validation_files: Vec, - pub result_files: Vec, - pub events: Option, -} - -pub struct FineTuneEvent { - pub object: String, - pub created_at: u32, - pub level: String, - pub message: String, -} - -pub struct DeleteModelResponse { - pub id: String, - pub object: String, - pub deleted: bool, -} -*/ diff --git a/async-openai/src/types/assistant.rs b/async-openai/src/types/assistant.rs new file mode 100644 index 00000000..cd0aba47 --- /dev/null +++ b/async-openai/src/types/assistant.rs @@ -0,0 +1,326 @@ +use std::collections::HashMap; + +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +use super::{FunctionName, FunctionObject, ResponseFormat}; + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +pub struct AssistantToolCodeInterpreterResources { + ///A list of [file](https://platform.openai.com/docs/api-reference/files) IDs made available to the `code_interpreter`` tool. There can be a maximum of 20 files associated with the tool. + pub file_ids: Vec, // maxItems: 20 +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +pub struct AssistantToolFileSearchResources { + /// The ID of the [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant. + pub vector_store_ids: Vec, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct AssistantToolResources { + #[serde(skip_serializing_if = "Option::is_none")] + pub code_interpreter: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub file_search: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct CreateAssistantToolResources { + #[serde(skip_serializing_if = "Option::is_none")] + pub code_interpreter: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub file_search: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +pub struct CreateAssistantToolFileSearchResources { + /// The [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant. + pub vector_store_ids: Option>, + /// A helper to create a [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) with file_ids and attach it to this assistant. There can be a maximum of 1 vector store attached to the assistant. + pub vector_stores: Option>, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +pub struct AssistantVectorStore { + /// A list of [file](https://platform.openai.com/docs/api-reference/files) IDs to add to the vector store. There can be a maximum of 10000 files in a vector store. + pub file_ids: Vec, + + /// The chunking strategy used to chunk the file(s). If not set, will use the `auto` strategy. + pub chunking_strategy: Option, + + /// Set of 16 key-value pairs that can be attached to a vector store. This can be useful for storing additional information about the vector store in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long. + pub metadata: Option>, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +#[serde(tag = "type")] +pub enum AssistantVectorStoreChunkingStrategy { + /// The default strategy. This strategy currently uses a `max_chunk_size_tokens` of `800` and `chunk_overlap_tokens` of `400`. + #[default] + #[serde(rename = "auto")] + Auto, + #[serde(rename = "static")] + Static { r#static: StaticChunkingStrategy }, +} + +/// Static Chunking Strategy +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +pub struct StaticChunkingStrategy { + /// The maximum number of tokens in each chunk. The default value is `800`. The minimum value is `100` and the maximum value is `4096`. + pub max_chunk_size_tokens: u16, + /// The number of tokens that overlap between chunks. The default value is `400`. + /// + /// Note that the overlap must not exceed half of `max_chunk_size_tokens`. + pub chunk_overlap_tokens: u16, +} + +/// Represents an `assistant` that can call the model and use tools. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct AssistantObject { + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `assistant`. + pub object: String, + /// The Unix timestamp (in seconds) for when the assistant was created. + pub created_at: i32, + /// The name of the assistant. The maximum length is 256 characters. + pub name: Option, + /// The description of the assistant. The maximum length is 512 characters. + pub description: Option, + /// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models) for descriptions of them. + pub model: String, + /// The system instructions that the assistant uses. The maximum length is 256,000 characters. + pub instructions: Option, + /// A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types `code_interpreter`, `file_search`, or `function`. + #[serde(default)] + pub tools: Vec, + /// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + pub tool_resources: Option, + /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long. + pub metadata: Option>, + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + pub temperature: Option, + /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + /// We generally recommend altering this or temperature but not both. + pub top_p: Option, + + pub response_format: Option, +} + +/// Specifies the format that the model must output. Compatible with [GPT-4o](https://platform.openai.com/docs/models/gpt-4o), [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`. +/// +/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which guarantees the model will match your supplied JSON schema. Learn more in the [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs). +/// +/// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. +/// +/// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +pub enum AssistantsApiResponseFormatOption { + #[default] + #[serde(rename = "auto")] + Auto, + #[serde(untagged)] + Format(ResponseFormat), +} + +/// Retrieval tool +#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)] +pub struct AssistantToolsFileSearch { + /// Overrides for the file search tool. + #[serde(skip_serializing_if = "Option::is_none")] + pub file_search: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct AssistantToolsFileSearchOverrides { + /// The maximum number of results the file search tool should output. The default is 20 for gpt-4* models and 5 for gpt-3.5-turbo. This number should be between 1 and 50 inclusive. + /// + //// Note that the file search tool may output fewer than `max_num_results` results. See the [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search/customizing-file-search-settings) for more information. + pub max_num_results: Option, + pub ranking_options: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub enum FileSearchRanker { + #[serde(rename = "auto")] + Auto, + #[serde(rename = "default_2024_08_21")] + Default2024_08_21, +} + +/// The ranking options for the file search. If not specified, the file search tool will use the `auto` ranker and a score_threshold of 0. +/// +/// See the [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings) for more information. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct FileSearchRankingOptions { + /// The ranker to use for the file search. If not specified will use the `auto` ranker. + #[serde(skip_serializing_if = "Option::is_none")] + pub ranker: Option, + + /// The score threshold for the file search. All values must be a floating point number between 0 and 1. + pub score_threshold: f32, +} + +/// Function tool +#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)] +pub struct AssistantToolsFunction { + pub function: FunctionObject, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum AssistantTools { + CodeInterpreter, + FileSearch(AssistantToolsFileSearch), + Function(AssistantToolsFunction), +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "CreateAssistantRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateAssistantRequest { + /// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them. + pub model: String, + + /// The name of the assistant. The maximum length is 256 characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + + /// The description of the assistant. The maximum length is 512 characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, + + /// The system instructions that the assistant uses. The maximum length is 256,000 characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + + /// A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types `code_interpreter`, `file_search`, or `function`. + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + + /// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_resources: Option, + + /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long. + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, + + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, + + /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + /// + /// We generally recommend altering this or temperature but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub response_format: Option, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "ModifyAssistantRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ModifyAssistantRequest { + /// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them. + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, + + /// The name of the assistant. The maximum length is 256 characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + + /// The description of the assistant. The maximum length is 512 characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, + + /// The system instructions that the assistant uses. The maximum length is 256,000 characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + + /// A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types `code_interpreter`, `file_search`, or `function`. + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + + /// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_resources: Option, + /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long. + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, + + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, + + /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + /// + /// We generally recommend altering this or temperature but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub response_format: Option, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct DeleteAssistantResponse { + pub id: String, + pub deleted: bool, + pub object: String, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct ListAssistantsResponse { + pub object: String, + pub data: Vec, + pub first_id: Option, + pub last_id: Option, + pub has_more: bool, +} + +/// Controls which (if any) tool is called by the model. +/// `none` means the model will not call any tools and instead generates a message. +/// `auto` is the default value and means the model can pick between generating a message or calling one or more tools. +/// `required` means the model must call one or more tools before responding to the user. +/// Specifying a particular tool like `{"type": "file_search"}` or `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool. +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum AssistantsApiToolChoiceOption { + #[default] + None, + Auto, + Required, + #[serde(untagged)] + Named(AssistantsNamedToolChoice), +} + +/// Specifies a tool the model should use. Use to force the model to call a specific tool. +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct AssistantsNamedToolChoice { + /// The type of the tool. If type is `function`, the function name must be set + pub r#type: AssistantToolType, + + pub function: Option, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum AssistantToolType { + #[default] + Function, + CodeInterpreter, + FileSearch, +} diff --git a/async-openai/src/types/assistant_impls.rs b/async-openai/src/types/assistant_impls.rs new file mode 100644 index 00000000..bd8d4bf7 --- /dev/null +++ b/async-openai/src/types/assistant_impls.rs @@ -0,0 +1,65 @@ +use super::{ + AssistantToolCodeInterpreterResources, AssistantToolFileSearchResources, + AssistantToolResources, AssistantTools, AssistantToolsFileSearch, AssistantToolsFunction, + CreateAssistantToolFileSearchResources, CreateAssistantToolResources, FunctionObject, +}; + +impl From for AssistantTools { + fn from(value: AssistantToolsFileSearch) -> Self { + Self::FileSearch(value) + } +} + +impl From for AssistantTools { + fn from(value: AssistantToolsFunction) -> Self { + Self::Function(value) + } +} + +impl From for AssistantToolsFunction { + fn from(value: FunctionObject) -> Self { + Self { function: value } + } +} + +impl From for AssistantTools { + fn from(value: FunctionObject) -> Self { + Self::Function(value.into()) + } +} + +impl From for CreateAssistantToolResources { + fn from(value: CreateAssistantToolFileSearchResources) -> Self { + Self { + code_interpreter: None, + file_search: Some(value), + } + } +} + +impl From for CreateAssistantToolResources { + fn from(value: AssistantToolCodeInterpreterResources) -> Self { + Self { + code_interpreter: Some(value), + file_search: None, + } + } +} + +impl From for AssistantToolResources { + fn from(value: AssistantToolCodeInterpreterResources) -> Self { + Self { + code_interpreter: Some(value), + file_search: None, + } + } +} + +impl From for AssistantToolResources { + fn from(value: AssistantToolFileSearchResources) -> Self { + Self { + code_interpreter: None, + file_search: Some(value), + } + } +} diff --git a/async-openai/src/types/assistant_stream.rs b/async-openai/src/types/assistant_stream.rs new file mode 100644 index 00000000..fca835cf --- /dev/null +++ b/async-openai/src/types/assistant_stream.rs @@ -0,0 +1,215 @@ +use std::pin::Pin; + +use futures::Stream; +use serde::Deserialize; + +use crate::error::{map_deserialization_error, ApiError, OpenAIError}; + +use super::{ + MessageDeltaObject, MessageObject, RunObject, RunStepDeltaObject, RunStepObject, ThreadObject, +}; + +/// Represents an event emitted when streaming a Run. +/// +/// Each event in a server-sent events stream has an `event` and `data` property: +/// +/// ```text +/// event: thread.created +/// data: {"id": "thread_123", "object": "thread", ...} +/// ``` +/// +/// We emit events whenever a new object is created, transitions to a new state, or is being +/// streamed in parts (deltas). For example, we emit `thread.run.created` when a new run +/// is created, `thread.run.completed` when a run completes, and so on. When an Assistant chooses +/// to create a message during a run, we emit a `thread.message.created event`, a +/// `thread.message.in_progress` event, many `thread.message.delta` events, and finally a +/// `thread.message.completed` event. +/// +/// We may add additional events over time, so we recommend handling unknown events gracefully +/// in your code. See the [Assistants API quickstart](https://platform.openai.com/docs/assistants/overview) to learn how to +/// integrate the Assistants API with streaming. + +#[derive(Debug, Deserialize, Clone)] +#[serde(tag = "event", content = "data")] +#[non_exhaustive] +pub enum AssistantStreamEvent { + /// Occurs when a new [thread](https://platform.openai.com/docs/api-reference/threads/object) is created. + #[serde(rename = "thread.created")] + ThreadCreated(ThreadObject), + /// Occurs when a new [run](https://platform.openai.com/docs/api-reference/runs/object) is created. + #[serde(rename = "thread.run.created")] + ThreadRunCreated(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) moves to a `queued` status. + #[serde(rename = "thread.run.queued")] + ThreadRunQueued(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) moves to an `in_progress` status. + #[serde(rename = "thread.run.in_progress")] + ThreadRunInProgress(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) moves to a `requires_action` status. + #[serde(rename = "thread.run.requires_action")] + ThreadRunRequiresAction(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) is completed. + #[serde(rename = "thread.run.completed")] + ThreadRunCompleted(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) ends with status `incomplete`. + #[serde(rename = "thread.run.incomplete")] + ThreadRunIncomplete(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) fails. + #[serde(rename = "thread.run.failed")] + ThreadRunFailed(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) moves to a `cancelling` status. + #[serde(rename = "thread.run.cancelling")] + ThreadRunCancelling(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) is cancelled. + #[serde(rename = "thread.run.cancelled")] + ThreadRunCancelled(RunObject), + /// Occurs when a [run](https://platform.openai.com/docs/api-reference/runs/object) expires. + #[serde(rename = "thread.run.expired")] + ThreadRunExpired(RunObject), + /// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) is created. + #[serde(rename = "thread.run.step.created")] + ThreadRunStepCreated(RunStepObject), + /// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) moves to an `in_progress` state. + #[serde(rename = "thread.run.step.in_progress")] + ThreadRunStepInProgress(RunStepObject), + /// Occurs when parts of a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) are being streamed. + #[serde(rename = "thread.run.step.delta")] + ThreadRunStepDelta(RunStepDeltaObject), + /// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) is completed. + #[serde(rename = "thread.run.step.completed")] + ThreadRunStepCompleted(RunStepObject), + /// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) fails. + #[serde(rename = "thread.run.step.failed")] + ThreadRunStepFailed(RunStepObject), + /// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) is cancelled. + #[serde(rename = "thread.run.step.cancelled")] + ThreadRunStepCancelled(RunStepObject), + /// Occurs when a [run step](https://platform.openai.com/docs/api-reference/run-steps/step-object) expires. + #[serde(rename = "thread.run.step.expired")] + ThreadRunStepExpired(RunStepObject), + /// Occurs when a [message](https://platform.openai.com/docs/api-reference/messages/object) is created. + #[serde(rename = "thread.message.created")] + ThreadMessageCreated(MessageObject), + /// Occurs when a [message](https://platform.openai.com/docs/api-reference/messages/object) moves to an `in_progress` state. + #[serde(rename = "thread.message.in_progress")] + ThreadMessageInProgress(MessageObject), + /// Occurs when parts of a [Message](https://platform.openai.com/docs/api-reference/messages/object) are being streamed. + #[serde(rename = "thread.message.delta")] + ThreadMessageDelta(MessageDeltaObject), + /// Occurs when a [message](https://platform.openai.com/docs/api-reference/messages/object) is completed. + #[serde(rename = "thread.message.completed")] + ThreadMessageCompleted(MessageObject), + /// Occurs when a [message](https://platform.openai.com/docs/api-reference/messages/object) ends before it is completed. + #[serde(rename = "thread.message.incomplete")] + ThreadMessageIncomplete(MessageObject), + /// Occurs when an [error](https://platform.openai.com/docs/guides/error-codes/api-errors) occurs. This can happen due to an internal server error or a timeout. + #[serde(rename = "error")] + ErrorEvent(ApiError), + /// Occurs when a stream ends. + #[serde(rename = "done")] + Done(String), +} + +pub type AssistantEventStream = + Pin> + Send>>; + +impl TryFrom for AssistantStreamEvent { + type Error = OpenAIError; + fn try_from(value: eventsource_stream::Event) -> Result { + match value.event.as_str() { + "thread.created" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadCreated), + "thread.run.created" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunCreated), + "thread.run.queued" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunQueued), + "thread.run.in_progress" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunInProgress), + "thread.run.requires_action" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunRequiresAction), + "thread.run.completed" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunCompleted), + "thread.run.incomplete" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunIncomplete), + "thread.run.failed" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunFailed), + "thread.run.cancelling" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunCancelling), + "thread.run.cancelled" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunCancelled), + "thread.run.expired" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunExpired), + "thread.run.step.created" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunStepCreated), + "thread.run.step.in_progress" => { + serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunStepInProgress) + } + "thread.run.step.delta" => { + serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunStepDelta) + } + "thread.run.step.completed" => { + serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunStepCompleted) + } + "thread.run.step.failed" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunStepFailed), + "thread.run.step.cancelled" => { + serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunStepCancelled) + } + "thread.run.step.expired" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadRunStepExpired), + "thread.message.created" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadMessageCreated), + "thread.message.in_progress" => { + serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadMessageInProgress) + } + "thread.message.delta" => { + serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadMessageDelta) + } + "thread.message.completed" => { + serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadMessageCompleted) + } + "thread.message.incomplete" => { + serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ThreadMessageIncomplete) + } + "error" => serde_json::from_str::(value.data.as_str()) + .map_err(|e| map_deserialization_error(e, value.data.as_bytes())) + .map(AssistantStreamEvent::ErrorEvent), + "done" => Ok(AssistantStreamEvent::Done(value.data)), + + _ => Err(OpenAIError::StreamError( + "Unrecognized event: {value:?#}".into(), + )), + } + } +} diff --git a/async-openai/src/types/audio.rs b/async-openai/src/types/audio.rs new file mode 100644 index 00000000..aec11f1b --- /dev/null +++ b/async-openai/src/types/audio.rs @@ -0,0 +1,256 @@ +use bytes::Bytes; +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use super::InputSource; +use crate::error::OpenAIError; + +#[derive(Debug, Default, Clone, PartialEq)] +pub struct AudioInput { + pub source: InputSource, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum AudioResponseFormat { + #[default] + Json, + Text, + Srt, + VerboseJson, + Vtt, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum SpeechResponseFormat { + #[default] + Mp3, + Opus, + Aac, + Flac, + Pcm, + Wav, +} + +#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +#[non_exhaustive] +pub enum Voice { + #[default] + Alloy, + Ash, + Ballad, + Coral, + Echo, + Fable, + Onyx, + Nova, + Sage, + Shimmer, +} + +#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)] +pub enum SpeechModel { + #[default] + #[serde(rename = "tts-1")] + Tts1, + #[serde(rename = "tts-1-hd")] + Tts1Hd, + #[serde(untagged)] + Other(String), +} + +#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum TimestampGranularity { + Word, + #[default] + Segment, +} + +#[derive(Clone, Default, Debug, Builder, PartialEq)] +#[builder(name = "CreateTranscriptionRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateTranscriptionRequest { + /// The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm. + pub file: AudioInput, + + /// ID of the model to use. Only `whisper-1` (which is powered by our open source Whisper V2 model) is currently available. + pub model: String, + + /// An optional text to guide the model's style or continue a previous audio segment. The [prompt](https://platform.openai.com/docs/guides/speech-to-text#prompting) should match the audio language. + pub prompt: Option, + + /// The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt. + pub response_format: Option, + + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use [log probability](https://en.wikipedia.org/wiki/Log_probability) to automatically increase the temperature until certain thresholds are hit. + pub temperature: Option, // default: 0 + + /// The language of the input audio. Supplying the input language in [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) format will improve accuracy and latency. + pub language: Option, + + /// The timestamp granularities to populate for this transcription. `response_format` must be set `verbose_json` to use timestamp granularities. Either or both of these options are supported: `word`, or `segment`. Note: There is no additional latency for segment timestamps, but generating word timestamps incurs additional latency. + pub timestamp_granularities: Option>, +} + +/// Represents a transcription response returned by model, based on the provided +/// input. +#[derive(Debug, Deserialize, Clone, Serialize)] +pub struct CreateTranscriptionResponseJson { + /// The transcribed text. + pub text: String, +} + +/// Represents a verbose json transcription response returned by model, based on +/// the provided input. +#[derive(Debug, Deserialize, Clone, Serialize)] +pub struct CreateTranscriptionResponseVerboseJson { + /// The language of the input audio. + pub language: String, + + /// The duration of the input audio. + pub duration: f32, + + /// The transcribed text. + pub text: String, + + /// Extracted words and their corresponding timestamps. + #[serde(skip_serializing_if = "Option::is_none")] + pub words: Option>, + + /// Segments of the transcribed text and their corresponding details. + #[serde(skip_serializing_if = "Option::is_none")] + pub segments: Option>, +} + +#[derive(Debug, Deserialize, Clone, Serialize)] +pub struct TranscriptionWord { + /// The text content of the word. + pub word: String, + + /// Start time of the word in seconds. + pub start: f32, + + /// End time of the word in seconds. + pub end: f32, +} + +#[derive(Debug, Deserialize, Clone, Serialize)] +pub struct TranscriptionSegment { + /// Unique identifier of the segment. + pub id: i32, + + // Seek offset of the segment. + pub seek: i32, + + /// Start time of the segment in seconds. + pub start: f32, + + /// End time of the segment in seconds. + pub end: f32, + + /// Text content of the segment. + pub text: String, + + /// Array of token IDs for the text content. + pub tokens: Vec, + + /// Temperature parameter used for generating the segment. + pub temperature: f32, + + /// Average logprob of the segment. If the value is lower than -1, consider + /// the logprobs failed. + pub avg_logprob: f32, + + /// Compression ratio of the segment. If the value is greater than 2.4, + /// consider the compression failed. + pub compression_ratio: f32, + + /// Probability of no speech in the segment. If the value is higher than 1.0 + /// and the `avg_logprob` is below -1, consider this segment silent. + pub no_speech_prob: f32, +} + +#[derive(Clone, Default, Debug, Builder, PartialEq, Serialize, Deserialize)] +#[builder(name = "CreateSpeechRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateSpeechRequest { + /// The text to generate audio for. The maximum length is 4096 characters. + pub input: String, + + /// One of the available [TTS models](https://platform.openai.com/docs/models/tts): `tts-1` or `tts-1-hd` + pub model: SpeechModel, + + /// The voice to use when generating the audio. Supported voices are `alloy`, `ash`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, `shimmer` and `verse`. + + /// Previews of the voices are available in the [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options). + pub voice: Voice, + + /// Control the voice of your generated audio with additional instructions. + /// Does not work with `tts-1` or `tts-1-hd`. + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + + /// The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`, `wav`, and `pcm`. + #[serde(skip_serializing_if = "Option::is_none")] + pub response_format: Option, + + /// The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default. + #[serde(skip_serializing_if = "Option::is_none")] + pub speed: Option, // default: 1.0 +} + +#[derive(Clone, Default, Debug, Builder, PartialEq)] +#[builder(name = "CreateTranslationRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateTranslationRequest { + /// The audio file object (not file name) translate, in one of these + ///formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + pub file: AudioInput, + + /// ID of the model to use. Only `whisper-1` (which is powered by our open source Whisper V2 model) is currently available. + pub model: String, + + /// An optional text to guide the model's style or continue a previous audio segment. The [prompt](https://platform.openai.com/docs/guides/speech-to-text#prompting) should be in English. + pub prompt: Option, + + /// The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt. + pub response_format: Option, + + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use [log probability](https://en.wikipedia.org/wiki/Log_probability) to automatically increase the temperature until certain thresholds are hit. + pub temperature: Option, // default: 0 +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct CreateTranslationResponseJson { + pub text: String, +} + +#[derive(Debug, Deserialize, Clone, Serialize)] +pub struct CreateTranslationResponseVerboseJson { + /// The language of the output translation (always `english`). + pub language: String, + /// The duration of the input audio. + pub duration: String, + /// The translated text. + pub text: String, + /// Segments of the translated text and their corresponding details. + #[serde(skip_serializing_if = "Option::is_none")] + pub segments: Option>, +} + +#[derive(Debug, Clone)] +pub struct CreateSpeechResponse { + pub bytes: Bytes, +} diff --git a/async-openai/src/types/audit_log.rs b/async-openai/src/types/audit_log.rs new file mode 100644 index 00000000..d652cf40 --- /dev/null +++ b/async-openai/src/types/audit_log.rs @@ -0,0 +1,434 @@ +use serde::{Deserialize, Serialize}; + +/// The event type. +#[derive(Debug, Serialize, Deserialize)] +pub enum AuditLogEventType { + #[serde(rename = "api_key.created")] + ApiKeyCreated, + #[serde(rename = "api_key.updated")] + ApiKeyUpdated, + #[serde(rename = "api_key.deleted")] + ApiKeyDeleted, + #[serde(rename = "invite.sent")] + InviteSent, + #[serde(rename = "invite.accepted")] + InviteAccepted, + #[serde(rename = "invite.deleted")] + InviteDeleted, + #[serde(rename = "login.succeeded")] + LoginSucceeded, + #[serde(rename = "login.failed")] + LoginFailed, + #[serde(rename = "logout.succeeded")] + LogoutSucceeded, + #[serde(rename = "logout.failed")] + LogoutFailed, + #[serde(rename = "organization.updated")] + OrganizationUpdated, + #[serde(rename = "project.created")] + ProjectCreated, + #[serde(rename = "project.updated")] + ProjectUpdated, + #[serde(rename = "project.archived")] + ProjectArchived, + #[serde(rename = "service_account.created")] + ServiceAccountCreated, + #[serde(rename = "service_account.updated")] + ServiceAccountUpdated, + #[serde(rename = "service_account.deleted")] + ServiceAccountDeleted, + #[serde(rename = "user.added")] + UserAdded, + #[serde(rename = "user.updated")] + UserUpdated, + #[serde(rename = "user.deleted")] + UserDeleted, +} + +/// Represents a list of audit logs. +#[derive(Debug, Serialize, Deserialize)] +pub struct ListAuditLogsResponse { + /// The object type, which is always `list`. + pub object: String, + /// A list of `AuditLog` objects. + pub data: Vec, + /// The first `audit_log_id` in the retrieved `list`. + pub first_id: String, + /// The last `audit_log_id` in the retrieved `list`. + pub last_id: String, + /// The `has_more` property is used for pagination to indicate there are additional results. + pub has_more: bool, +} + +/// The project that the action was scoped to. Absent for actions not scoped to projects. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogProject { + /// The project ID. + pub id: String, + /// The project title. + pub name: String, +} + +/// The actor who performed the audit logged action. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogActor { + /// The type of actor. Is either `session` or `api_key`. + pub r#type: String, + /// The session in which the audit logged action was performed. + pub session: Option, + /// The API Key used to perform the audit logged action. + pub api_key: Option, +} + +/// The session in which the audit logged action was performed. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogActorSession { + /// The user who performed the audit logged action. + pub user: AuditLogActorUser, + /// The IP address from which the action was performed. + pub ip_address: String, +} + +/// The API Key used to perform the audit logged action. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogActorApiKey { + /// The tracking id of the API key. + pub id: String, + /// The type of API key. Can be either `user` or `service_account`. + pub r#type: AuditLogActorApiKeyType, + /// The user who performed the audit logged action, if applicable. + pub user: Option, + /// The service account that performed the audit logged action, if applicable. + pub service_account: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum AuditLogActorApiKeyType { + User, + ServiceAccount, +} + +/// The user who performed the audit logged action. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogActorUser { + /// The user id. + pub id: String, + /// The user email. + pub email: String, +} + +/// The service account that performed the audit logged action. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogActorServiceAccount { + /// The service account id. + pub id: String, +} + +/// A log of a user action or configuration change within this organization. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLog { + /// The ID of this log. + pub id: String, + /// The event type. + pub r#type: AuditLogEventType, + /// The Unix timestamp (in seconds) of the event. + pub effective_at: u32, + /// The project that the action was scoped to. Absent for actions not scoped to projects. + pub project: Option, + /// The actor who performed the audit logged action. + pub actor: AuditLogActor, + /// The details for events with the type `api_key.created`. + #[serde(rename = "api_key.created")] + pub api_key_created: Option, + /// The details for events with the type `api_key.updated`. + #[serde(rename = "api_key.updated")] + pub api_key_updated: Option, + /// The details for events with the type `api_key.deleted`. + #[serde(rename = "api_key.deleted")] + pub api_key_deleted: Option, + /// The details for events with the type `invite.sent`. + #[serde(rename = "invite.sent")] + pub invite_sent: Option, + /// The details for events with the type `invite.accepted`. + #[serde(rename = "invite.accepted")] + pub invite_accepted: Option, + /// The details for events with the type `invite.deleted`. + #[serde(rename = "invite.deleted")] + pub invite_deleted: Option, + /// The details for events with the type `login.failed`. + #[serde(rename = "login.failed")] + pub login_failed: Option, + /// The details for events with the type `logout.failed`. + #[serde(rename = "logout.failed")] + pub logout_failed: Option, + /// The details for events with the type `organization.updated`. + #[serde(rename = "organization.updated")] + pub organization_updated: Option, + /// The details for events with the type `project.created`. + #[serde(rename = "project.created")] + pub project_created: Option, + /// The details for events with the type `project.updated`. + #[serde(rename = "project.updated")] + pub project_updated: Option, + /// The details for events with the type `project.archived`. + #[serde(rename = "project.archived")] + pub project_archived: Option, + /// The details for events with the type `service_account.created`. + #[serde(rename = "service_account.created")] + pub service_account_created: Option, + /// The details for events with the type `service_account.updated`. + #[serde(rename = "service_account.updated")] + pub service_account_updated: Option, + /// The details for events with the type `service_account.deleted`. + #[serde(rename = "service_account.deleted")] + pub service_account_deleted: Option, + /// The details for events with the type `user.added`. + #[serde(rename = "user.added")] + pub user_added: Option, + /// The details for events with the type `user.updated`. + #[serde(rename = "user.updated")] + pub user_updated: Option, + /// The details for events with the type `user.deleted`. + #[serde(rename = "user.deleted")] + pub user_deleted: Option, +} + +/// The details for events with the type `api_key.created`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogApiKeyCreated { + /// The tracking ID of the API key. + pub id: String, + /// The payload used to create the API key. + pub data: Option, +} + +/// The payload used to create the API key. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogApiKeyCreatedData { + /// A list of scopes allowed for the API key, e.g. `["api.model.request"]`. + pub scopes: Option>, +} + +/// The details for events with the type `api_key.updated`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogApiKeyUpdated { + /// The tracking ID of the API key. + pub id: String, + /// The payload used to update the API key. + pub changes_requested: Option, +} + +/// The payload used to update the API key. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogApiKeyUpdatedChangesRequested { + /// A list of scopes allowed for the API key, e.g. `["api.model.request"]`. + pub scopes: Option>, +} + +/// The details for events with the type `api_key.deleted`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogApiKeyDeleted { + /// The tracking ID of the API key. + pub id: String, +} + +/// The details for events with the type `invite.sent`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogInviteSent { + /// The ID of the invite. + pub id: String, + /// The payload used to create the invite. + pub data: Option, +} + +/// The payload used to create the invite. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogInviteSentData { + /// The email invited to the organization. + pub email: String, + /// The role the email was invited to be. Is either `owner` or `member`. + pub role: String, +} + +/// The details for events with the type `invite.accepted`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogInviteAccepted { + /// The ID of the invite. + pub id: String, +} + +/// The details for events with the type `invite.deleted`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogInviteDeleted { + /// The ID of the invite. + pub id: String, +} + +/// The details for events with the type `login.failed`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogLoginFailed { + /// The error code of the failure. + pub error_code: String, + /// The error message of the failure. + pub error_message: String, +} + +/// The details for events with the type `logout.failed`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogLogoutFailed { + /// The error code of the failure. + pub error_code: String, + /// The error message of the failure. + pub error_message: String, +} + +/// The details for events with the type `organization.updated`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogOrganizationUpdated { + /// The organization ID. + pub id: String, + /// The payload used to update the organization settings. + pub changes_requested: Option, +} + +/// The payload used to update the organization settings. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogOrganizationUpdatedChangesRequested { + /// The organization title. + pub title: Option, + /// The organization description. + pub description: Option, + /// The organization name. + pub name: Option, + /// The organization settings. + pub settings: Option, +} + +/// The organization settings. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogOrganizationUpdatedChangesRequestedSettings { + /// Visibility of the threads page which shows messages created with the Assistants API and Playground. One of `ANY_ROLE`, `OWNERS`, or `NONE`. + pub threads_ui_visibility: Option, + /// Visibility of the usage dashboard which shows activity and costs for your organization. One of `ANY_ROLE` or `OWNERS`. + pub usage_dashboard_visibility: Option, +} + +/// The details for events with the type `project.created`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogProjectCreated { + /// The project ID. + pub id: String, + /// The payload used to create the project. + pub data: Option, +} + +/// The payload used to create the project. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogProjectCreatedData { + /// The project name. + pub name: String, + /// The title of the project as seen on the dashboard. + pub title: Option, +} + +/// The details for events with the type `project.updated`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogProjectUpdated { + /// The project ID. + pub id: String, + /// The payload used to update the project. + pub changes_requested: Option, +} + +/// The payload used to update the project. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogProjectUpdatedChangesRequested { + /// The title of the project as seen on the dashboard. + pub title: Option, +} + +/// The details for events with the type `project.archived`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogProjectArchived { + /// The project ID. + pub id: String, +} + +/// The details for events with the type `service_account.created`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogServiceAccountCreated { + /// The service account ID. + pub id: String, + /// The payload used to create the service account. + pub data: Option, +} + +/// The payload used to create the service account. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogServiceAccountCreatedData { + /// The role of the service account. Is either `owner` or `member`. + pub role: String, +} + +/// The details for events with the type `service_account.updated`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogServiceAccountUpdated { + /// The service account ID. + pub id: String, + /// The payload used to updated the service account. + pub changes_requested: Option, +} + +/// The payload used to updated the service account. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogServiceAccountUpdatedChangesRequested { + /// The role of the service account. Is either `owner` or `member`. + pub role: String, +} + +/// The details for events with the type `service_account.deleted`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogServiceAccountDeleted { + /// The service account ID. + pub id: String, +} + +/// The details for events with the type `user.added`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogUserAdded { + /// The user ID. + pub id: String, + /// The payload used to add the user to the project. + pub data: Option, +} + +/// The payload used to add the user to the project. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogUserAddedData { + /// The role of the user. Is either `owner` or `member`. + pub role: String, +} + +/// The details for events with the type `user.updated`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogUserUpdated { + /// The project ID. + pub id: String, + /// The payload used to update the user. + pub changes_requested: Option, +} + +/// The payload used to update the user. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogUserUpdatedChangesRequested { + /// The role of the user. Is either `owner` or `member`. + pub role: String, +} + +/// The details for events with the type `user.deleted`. +#[derive(Debug, Serialize, Deserialize)] +pub struct AuditLogUserDeleted { + /// The user ID. + pub id: String, +} diff --git a/async-openai/src/types/batch.rs b/async-openai/src/types/batch.rs new file mode 100644 index 00000000..5546285e --- /dev/null +++ b/async-openai/src/types/batch.rs @@ -0,0 +1,188 @@ +use std::collections::HashMap; + +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq, Deserialize)] +#[builder(name = "BatchRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct BatchRequest { + /// The ID of an uploaded file that contains requests for the new batch. + /// + /// See [upload file](https://platform.openai.com/docs/api-reference/files/create) for how to upload a file. + /// + /// Your input file must be formatted as a [JSONL file](https://platform.openai.com/docs/api-reference/batch/request-input), and must be uploaded with the purpose `batch`. The file can contain up to 50,000 requests, and can be up to 100 MB in size. + pub input_file_id: String, + + /// The endpoint to be used for all requests in the batch. Currently `/v1/chat/completions`, `/v1/embeddings`, and `/v1/completions` are supported. Note that `/v1/embeddings` batches are also restricted to a maximum of 50,000 embedding inputs across all requests in the batch. + pub endpoint: BatchEndpoint, + + /// The time frame within which the batch should be processed. Currently only `24h` is supported. + pub completion_window: BatchCompletionWindow, + + /// Optional custom metadata for the batch. + pub metadata: Option>, +} + +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)] +pub enum BatchEndpoint { + #[default] + #[serde(rename = "/v1/chat/completions")] + V1ChatCompletions, + #[serde(rename = "/v1/embeddings")] + V1Embeddings, + #[serde(rename = "/v1/completions")] + V1Completions, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Default, Deserialize)] +pub enum BatchCompletionWindow { + #[default] + #[serde(rename = "24h")] + W24H, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct Batch { + pub id: String, + /// The object type, which is always `batch`. + pub object: String, + /// The OpenAI API endpoint used by the batch. + pub endpoint: String, + pub errors: Option, + /// The ID of the input file for the batch. + pub input_file_id: String, + /// The time frame within which the batch should be processed. + pub completion_window: String, + /// The current status of the batch. + pub status: BatchStatus, + /// The ID of the file containing the outputs of successfully executed requests. + pub output_file_id: Option, + /// The ID of the file containing the outputs of requests with errors. + pub error_file_id: Option, + /// The Unix timestamp (in seconds) for when the batch was created. + pub created_at: u32, + /// The Unix timestamp (in seconds) for when the batch started processing. + pub in_progress_at: Option, + /// The Unix timestamp (in seconds) for when the batch will expire. + pub expires_at: Option, + /// The Unix timestamp (in seconds) for when the batch started finalizing. + pub finalizing_at: Option, + /// The Unix timestamp (in seconds) for when the batch was completed. + pub completed_at: Option, + /// The Unix timestamp (in seconds) for when the batch failed. + pub failed_at: Option, + /// he Unix timestamp (in seconds) for when the batch expired. + pub expired_at: Option, + /// The Unix timestamp (in seconds) for when the batch started cancelling. + pub cancelling_at: Option, + /// The Unix timestamp (in seconds) for when the batch was cancelled. + pub cancelled_at: Option, + /// The request counts for different statuses within the batch. + pub request_counts: Option, + /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long. + pub metadata: Option>, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct BatchErrors { + /// The object type, which is always `list`. + pub object: String, + pub data: Vec, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct BatchError { + /// An error code identifying the error type. + pub code: String, + /// A human-readable message providing more details about the error. + pub message: String, + /// The name of the parameter that caused the error, if applicable. + pub param: Option, + /// The line number of the input file where the error occurred, if applicable. + pub line: Option, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum BatchStatus { + Validating, + Failed, + InProgress, + Finalizing, + Completed, + Expired, + Cancelling, + Cancelled, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct BatchRequestCounts { + /// Total number of requests in the batch. + pub total: u32, + /// Number of requests that have been completed successfully. + pub completed: u32, + /// Number of requests that have failed. + pub failed: u32, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct ListBatchesResponse { + pub data: Vec, + pub first_id: Option, + pub last_id: Option, + pub has_more: bool, + pub object: String, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +#[serde(rename_all = "UPPERCASE")] +pub enum BatchRequestInputMethod { + POST, +} + +/// The per-line object of the batch input file +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct BatchRequestInput { + /// A developer-provided per-request id that will be used to match outputs to inputs. Must be unique for each request in a batch. + pub custom_id: String, + /// The HTTP method to be used for the request. Currently only `POST` is supported. + pub method: BatchRequestInputMethod, + /// The OpenAI API relative URL to be used for the request. Currently `/v1/chat/completions`, `/v1/embeddings`, and `/v1/completions` are supported. + pub url: BatchEndpoint, + pub body: Option, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct BatchRequestOutputResponse { + /// The HTTP status code of the response + pub status_code: u16, + /// An unique identifier for the OpenAI API request. Please include this request ID when contacting support. + pub request_id: String, + /// The JSON body of the response + pub body: serde_json::Value, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct BatchRequestOutputError { + /// A machine-readable error code. + pub code: String, + /// A human-readable error message. + pub message: String, +} + +/// The per-line object of the batch output and error files +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct BatchRequestOutput { + pub id: String, + /// A developer-provided per-request id that will be used to match outputs to inputs. + pub custom_id: String, + pub response: Option, + /// For requests that failed with a non-HTTP error, this will contain more information on the cause of the failure. + pub error: Option, +} diff --git a/async-openai/src/types/chat.rs b/async-openai/src/types/chat.rs new file mode 100644 index 00000000..d9373db6 --- /dev/null +++ b/async-openai/src/types/chat.rs @@ -0,0 +1,1061 @@ +use std::{collections::HashMap, pin::Pin}; + +use derive_builder::Builder; +use futures::Stream; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum Prompt { + String(String), + StringArray(Vec), + // Minimum value is 0, maximum value is 4_294_967_295 (inclusive). + IntegerArray(Vec), + ArrayOfIntegerArray(Vec>), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum Stop { + String(String), // nullable: true + StringArray(Vec), // minItems: 1; maxItems: 4 +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct Logprobs { + pub tokens: Vec, + pub token_logprobs: Vec>, // Option is to account for null value in the list + pub top_logprobs: Vec, + pub text_offset: Vec, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum CompletionFinishReason { + Stop, + Length, + ContentFilter, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct Choice { + pub text: String, + pub index: u32, + #[serde(skip_serializing_if = "Option::is_none")] + pub logprobs: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub finish_reason: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub enum ChatCompletionFunctionCall { + /// The model does not call a function, and responds to the end-user. + #[serde(rename = "none")] + None, + /// The model can pick between an end-user or calling a function. + #[serde(rename = "auto")] + Auto, + + // In spec this is ChatCompletionFunctionCallOption + // based on feedback from @m1guelpf in https://github.com/64bit/async-openai/pull/118 + // it is diverged from the spec + /// Forces the model to call the specified function. + #[serde(untagged)] + Function { name: String }, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum Role { + System, + #[default] + User, + Assistant, + Tool, + Function, +} + +/// The name and arguments of a function that should be called, as generated by the model. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct FunctionCall { + /// The name of the function to call. + pub name: String, + /// The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function. + pub arguments: String, +} + +/// Usage statistics for the completion request. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default)] +pub struct CompletionUsage { + /// Number of tokens in the prompt. + pub prompt_tokens: u32, + /// Number of tokens in the generated completion. + pub completion_tokens: u32, + /// Total number of tokens used in the request (prompt + completion). + pub total_tokens: u32, + /// Breakdown of tokens used in the prompt. + #[serde(skip_serializing_if = "Option::is_none")] + pub prompt_tokens_details: Option, + /// Breakdown of tokens used in a completion. + #[serde(skip_serializing_if = "Option::is_none")] + pub completion_tokens_details: Option, +} + +/// Breakdown of tokens used in a completion. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default)] +pub struct PromptTokensDetails { + /// Audio input tokens present in the prompt. + pub audio_tokens: Option, + /// Cached tokens present in the prompt. + pub cached_tokens: Option, +} + +/// Breakdown of tokens used in a completion. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default)] +pub struct CompletionTokensDetails { + pub accepted_prediction_tokens: Option, + /// Audio input tokens generated by the model. + pub audio_tokens: Option, + /// Tokens generated by the model for reasoning. + pub reasoning_tokens: Option, + /// When using Predicted Outputs, the number of tokens in the + /// prediction that did not appear in the completion. However, like + /// reasoning tokens, these tokens are still counted in the total + /// completion tokens for purposes of billing, output, and context + /// window limits. + pub rejected_prediction_tokens: Option, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestDeveloperMessageArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestDeveloperMessage { + /// The contents of the developer message. + pub content: ChatCompletionRequestDeveloperMessageContent, + + /// An optional name for the participant. Provides the model information to differentiate between participants of the same role. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum ChatCompletionRequestDeveloperMessageContent { + Text(String), + Array(Vec), +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestSystemMessageArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestSystemMessage { + /// The contents of the system message. + pub content: ChatCompletionRequestSystemMessageContent, + /// An optional name for the participant. Provides the model information to differentiate between participants of the same role. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestMessageContentPartTextArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestMessageContentPartText { + pub text: String, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +pub struct ChatCompletionRequestMessageContentPartRefusal { + /// The refusal message generated by the model. + pub refusal: String, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageDetail { + #[default] + Auto, + Low, + High, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ImageUrlArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ImageUrl { + /// Either a URL of the image or the base64 encoded image data. + pub url: String, + /// Specifies the detail level of the image. Learn more in the [Vision guide](https://platform.openai.com/docs/guides/vision/low-or-high-fidelity-image-understanding). + pub detail: Option, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestMessageContentPartImageArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestMessageContentPartImage { + pub image_url: ImageUrl, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum InputAudioFormat { + Wav, + #[default] + Mp3, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +pub struct InputAudio { + /// Base64 encoded audio data. + pub data: String, + /// The format of the encoded audio data. Currently supports "wav" and "mp3". + pub format: InputAudioFormat, +} + +/// Learn about [audio inputs](https://platform.openai.com/docs/guides/audio). +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestMessageContentPartAudioArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestMessageContentPartAudio { + pub input_audio: InputAudio, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum ChatCompletionRequestUserMessageContentPart { + Text(ChatCompletionRequestMessageContentPartText), + ImageUrl(ChatCompletionRequestMessageContentPartImage), + InputAudio(ChatCompletionRequestMessageContentPartAudio), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum ChatCompletionRequestSystemMessageContentPart { + Text(ChatCompletionRequestMessageContentPartText), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum ChatCompletionRequestAssistantMessageContentPart { + Text(ChatCompletionRequestMessageContentPartText), + Refusal(ChatCompletionRequestMessageContentPartRefusal), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum ChatCompletionRequestToolMessageContentPart { + Text(ChatCompletionRequestMessageContentPartText), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum ChatCompletionRequestSystemMessageContent { + /// The text contents of the system message. + Text(String), + /// An array of content parts with a defined type. For system messages, only type `text` is supported. + Array(Vec), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum ChatCompletionRequestUserMessageContent { + /// The text contents of the message. + Text(String), + /// An array of content parts with a defined type. Supported options differ based on the [model](https://platform.openai.com/docs/models) being used to generate the response. Can contain text, image, or audio inputs. + Array(Vec), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum ChatCompletionRequestAssistantMessageContent { + /// The text contents of the message. + Text(String), + /// An array of content parts with a defined type. Can be one or more of type `text`, or exactly one of type `refusal`. + Array(Vec), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum ChatCompletionRequestToolMessageContent { + /// The text contents of the tool message. + Text(String), + /// An array of content parts with a defined type. For tool messages, only type `text` is supported. + Array(Vec), +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestUserMessageArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestUserMessage { + /// The contents of the user message. + pub content: ChatCompletionRequestUserMessageContent, + /// An optional name for the participant. Provides the model information to differentiate between participants of the same role. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +pub struct ChatCompletionRequestAssistantMessageAudio { + /// Unique identifier for a previous audio response from the model. + pub id: String, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestAssistantMessageArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestAssistantMessage { + /// The contents of the assistant message. Required unless `tool_calls` or `function_call` is specified. + #[serde(skip_serializing_if = "Option::is_none")] + pub content: Option, + /// The refusal message by the assistant. + #[serde(skip_serializing_if = "Option::is_none")] + pub refusal: Option, + /// An optional name for the participant. Provides the model information to differentiate between participants of the same role. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + /// Data about a previous audio response from the model. + /// [Learn more](https://platform.openai.com/docs/guides/audio). + #[serde(skip_serializing_if = "Option::is_none")] + pub audio: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_calls: Option>, + /// Deprecated and replaced by `tool_calls`. The name and arguments of a function that should be called, as generated by the model. + #[deprecated] + #[serde(skip_serializing_if = "Option::is_none")] + pub function_call: Option, +} + +/// Tool message +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestToolMessageArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestToolMessage { + /// The contents of the tool message. + pub content: ChatCompletionRequestToolMessageContent, + pub tool_call_id: String, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "ChatCompletionRequestFunctionMessageArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionRequestFunctionMessage { + /// The return value from the function call, to return to the model. + pub content: Option, + /// The name of the function to call. + pub name: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "role")] +#[serde(rename_all = "lowercase")] +pub enum ChatCompletionRequestMessage { + Developer(ChatCompletionRequestDeveloperMessage), + System(ChatCompletionRequestSystemMessage), + User(ChatCompletionRequestUserMessage), + Assistant(ChatCompletionRequestAssistantMessage), + Tool(ChatCompletionRequestToolMessage), + Function(ChatCompletionRequestFunctionMessage), +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ChatCompletionMessageToolCall { + /// The ID of the tool call. + pub id: String, + /// The type of the tool. Currently, only `function` is supported. + pub r#type: ChatCompletionToolType, + /// The function that the model called. + pub function: FunctionCall, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +pub struct ChatCompletionResponseMessageAudio { + /// Unique identifier for this audio response. + pub id: String, + /// The Unix timestamp (in seconds) for when this audio response will no longer be accessible on the server for use in multi-turn conversations. + pub expires_at: u32, + /// Base64 encoded audio bytes generated by the model, in the format specified in the request. + pub data: String, + /// Transcript of the audio generated by the model. + pub transcript: String, +} + +/// A chat completion message generated by the model. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ChatCompletionResponseMessage { + /// The contents of the message. + #[serde(skip_serializing_if = "Option::is_none")] + pub content: Option, + /// The refusal message generated by the model. + #[serde(skip_serializing_if = "Option::is_none")] + pub refusal: Option, + /// The tool calls generated by the model, such as function calls. + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_calls: Option>, + + /// The role of the author of this message. + pub role: Role, + + /// Deprecated and replaced by `tool_calls`. + /// The name and arguments of a function that should be called, as generated by the model. + #[serde(skip_serializing_if = "Option::is_none")] + #[deprecated] + pub function_call: Option, + + /// If the audio output modality is requested, this object contains data about the audio response from the model. [Learn more](https://platform.openai.com/docs/guides/audio). + #[serde(skip_serializing_if = "Option::is_none")] + pub audio: Option, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "ChatCompletionFunctionsArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +#[deprecated] +pub struct ChatCompletionFunctions { + /// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + pub name: String, + /// A description of what the function does, used by the model to choose when and how to call the function. + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, + /// The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/text-generation/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format. + /// + /// Omitting `parameters` defines a function with an empty parameter list. + pub parameters: serde_json::Value, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "FunctionObjectArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct FunctionObject { + /// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + pub name: String, + /// A description of what the function does, used by the model to choose when and how to call the function. + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, + /// The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/text-generation/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format. + /// + /// Omitting `parameters` defines a function with an empty parameter list. + #[serde(skip_serializing_if = "Option::is_none")] + pub parameters: Option, + + /// Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the `parameters` field. Only a subset of JSON Schema is supported when `strict` is `true`. Learn more about Structured Outputs in the [function calling guide](https://platform.openai.com/docs/guides/function-calling). + #[serde(skip_serializing_if = "Option::is_none")] + pub strict: Option, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum ResponseFormat { + /// The type of response format being defined: `text` + Text, + /// The type of response format being defined: `json_object` + JsonObject, + /// The type of response format being defined: `json_schema` + JsonSchema { + json_schema: ResponseFormatJsonSchema, + }, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ResponseFormatJsonSchema { + /// A description of what the response format is for, used by the model to determine how to respond in the format. + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, + /// The name of the response format. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + pub name: String, + /// The schema for the response format, described as a JSON Schema object. + #[serde(skip_serializing_if = "Option::is_none")] + pub schema: Option, + /// Whether to enable strict schema adherence when generating the output. If set to true, the model will always follow the exact schema defined in the `schema` field. Only a subset of JSON Schema is supported when `strict` is `true`. To learn more, read the [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs). + #[serde(skip_serializing_if = "Option::is_none")] + pub strict: Option, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ChatCompletionToolType { + #[default] + Function, +} + +#[derive(Clone, Serialize, Default, Debug, Builder, Deserialize, PartialEq)] +#[builder(name = "ChatCompletionToolArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ChatCompletionTool { + #[builder(default = "ChatCompletionToolType::Function")] + pub r#type: ChatCompletionToolType, + pub function: FunctionObject, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct FunctionName { + /// The name of the function to call. + pub name: String, +} + +/// Specifies a tool the model should use. Use to force the model to call a specific function. +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct ChatCompletionNamedToolChoice { + /// The type of the tool. Currently, only `function` is supported. + pub r#type: ChatCompletionToolType, + + pub function: FunctionName, +} + +/// Controls which (if any) tool is called by the model. +/// `none` means the model will not call any tool and instead generates a message. +/// `auto` means the model can pick between generating a message or calling one or more tools. +/// `required` means the model must call one or more tools. +/// Specifying a particular tool via `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool. +/// +/// `none` is the default when no tools are present. `auto` is the default if tools are present. +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ChatCompletionToolChoiceOption { + #[default] + None, + Auto, + Required, + #[serde(untagged)] + Named(ChatCompletionNamedToolChoice), +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +/// The amount of context window space to use for the search. +pub enum WebSearchContextSize { + Low, + #[default] + Medium, + High, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum WebSearchUserLocationType { + Approximate, +} + +/// Approximate location parameters for the search. +#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)] +pub struct WebSearchLocation { + /// The two-letter [ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1) of the user, e.g. `US`. + pub country: Option, + /// Free text input for the region of the user, e.g. `California`. + pub region: Option, + /// Free text input for the city of the user, e.g. `San Francisco`. + pub city: Option, + /// The [IANA timezone](https://timeapi.io/documentation/iana-timezones) of the user, e.g. `America/Los_Angeles`. + pub timezone: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct WebSearchUserLocation { + // The type of location approximation. Always `approximate`. + pub r#type: WebSearchUserLocationType, + + pub approximate: WebSearchLocation, +} + +/// Options for the web search tool. +#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)] +pub struct WebSearchOptions { + /// High level guidance for the amount of context window space to use for the search. One of `low`, `medium`, or `high`. `medium` is the default. + pub search_context_size: Option, + + /// Approximate location parameters for the search. + pub user_location: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ServiceTier { + Auto, + Default, + Flex, + Scale, + Priority, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ServiceTierResponse { + Scale, + Default, + Flex, + Priority, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ReasoningEffort { + Minimal, + Low, + Medium, + High, +} + +/// Output types that you would like the model to generate for this request. +/// +/// Most models are capable of generating text, which is the default: `["text"]` +/// +/// The `gpt-4o-audio-preview` model can also be used to [generate +/// audio](https://platform.openai.com/docs/guides/audio). To request that this model generate both text and audio responses, you can use: `["text", "audio"]` +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ChatCompletionModalities { + Text, + Audio, +} + +/// The content that should be matched when generating a model response. If generated tokens would match this content, the entire model response can be returned much more quickly. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(untagged)] +pub enum PredictionContentContent { + /// The content used for a Predicted Output. This is often the text of a file you are regenerating with minor changes. + Text(String), + /// An array of content parts with a defined type. Supported options differ based on the [model](https://platform.openai.com/docs/models) being used to generate the response. Can contain text inputs. + Array(Vec), +} + +/// Static predicted output content, such as the content of a text file that is being regenerated. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type", rename_all = "lowercase", content = "content")] +pub enum PredictionContent { + /// The type of the predicted content you want to provide. This type is + /// currently always `content`. + Content(PredictionContentContent), +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ChatCompletionAudioVoice { + Alloy, + Ash, + Ballad, + Coral, + Echo, + Sage, + Shimmer, + Verse, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ChatCompletionAudioFormat { + Wav, + Mp3, + Flac, + Opus, + Pcm16, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct ChatCompletionAudio { + /// The voice the model uses to respond. Supported voices are `ash`, `ballad`, `coral`, `sage`, and `verse` (also supported but not recommended are `alloy`, `echo`, and `shimmer`; these voices are less expressive). + pub voice: ChatCompletionAudioVoice, + /// Specifies the output audio format. Must be one of `wav`, `mp3`, `flac`, `opus`, or `pcm16`. + pub format: ChatCompletionAudioFormat, +} + +#[derive(Clone, Serialize, Default, Debug, Builder, Deserialize, PartialEq)] +#[builder(name = "CreateChatCompletionRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateChatCompletionRequest { + /// A list of messages comprising the conversation so far. Depending on the [model](https://platform.openai.com/docs/models) you use, different message types (modalities) are supported, like [text](https://platform.openai.com/docs/guides/text-generation), [images](https://platform.openai.com/docs/guides/vision), and [audio](https://platform.openai.com/docs/guides/audio). + pub messages: Vec, // min: 1 + + /// ID of the model to use. + /// See the [model endpoint compatibility](https://platform.openai.com/docs/models#model-endpoint-compatibility) table for details on which models work with the Chat API. + pub model: String, + + /// Whether or not to store the output of this chat completion request + /// + /// for use in our [model distillation](https://platform.openai.com/docs/guides/distillation) or [evals](https://platform.openai.com/docs/guides/evals) products. + #[serde(skip_serializing_if = "Option::is_none")] + pub store: Option, // nullable: true, default: false + + /// **o1 models only** + /// + /// Constrains effort on reasoning for + /// [reasoning models](https://platform.openai.com/docs/guides/reasoning). + /// + /// Currently supported values are `low`, `medium`, and `high`. Reducing + /// + /// reasoning effort can result in faster responses and fewer tokens + /// used on reasoning in a response. + #[serde(skip_serializing_if = "Option::is_none")] + pub reasoning_effort: Option, + + /// Developer-defined tags and values used for filtering completions in the [dashboard](https://platform.openai.com/chat-completions). + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option, // nullable: true + + /// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. + #[serde(skip_serializing_if = "Option::is_none")] + pub frequency_penalty: Option, // min: -2.0, max: 2.0, default: 0 + + /// Modify the likelihood of specified tokens appearing in the completion. + /// + /// Accepts a json object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. + /// Mathematically, the bias is added to the logits generated by the model prior to sampling. + /// The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; + /// values like -100 or 100 should result in a ban or exclusive selection of the relevant token. + #[serde(skip_serializing_if = "Option::is_none")] + pub logit_bias: Option>, // default: null + + /// Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the `content` of `message`. + #[serde(skip_serializing_if = "Option::is_none")] + pub logprobs: Option, + + /// An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. `logprobs` must be set to `true` if this parameter is used. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_logprobs: Option, + + /// The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the chat completion. + /// + /// This value can be used to control [costs](https://openai.com/api/pricing/) for text generated via API. + /// This value is now deprecated in favor of `max_completion_tokens`, and is + /// not compatible with [o1 series models](https://platform.openai.com/docs/guides/reasoning). + #[deprecated] + #[serde(skip_serializing_if = "Option::is_none")] + pub max_tokens: Option, + + /// An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and [reasoning tokens](https://platform.openai.com/docs/guides/reasoning). + #[serde(skip_serializing_if = "Option::is_none")] + pub max_completion_tokens: Option, + + /// How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep `n` as `1` to minimize costs. + #[serde(skip_serializing_if = "Option::is_none")] + pub n: Option, // min:1, max: 128, default: 1 + + #[serde(skip_serializing_if = "Option::is_none")] + pub modalities: Option>, + + /// Configuration for a [Predicted Output](https://platform.openai.com/docs/guides/predicted-outputs),which can greatly improve response times when large parts of the model response are known ahead of time. This is most common when you are regenerating a file with only minor changes to most of the content. + #[serde(skip_serializing_if = "Option::is_none")] + pub prediction: Option, + + /// Parameters for audio output. Required when audio output is requested with `modalities: ["audio"]`. [Learn more](https://platform.openai.com/docs/guides/audio). + #[serde(skip_serializing_if = "Option::is_none")] + pub audio: Option, + + /// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. + #[serde(skip_serializing_if = "Option::is_none")] + pub presence_penalty: Option, // min: -2.0, max: 2.0, default 0 + + /// An object specifying the format that the model must output. Compatible with [GPT-4o](https://platform.openai.com/docs/models/gpt-4o), [GPT-4o mini](https://platform.openai.com/docs/models/gpt-4o-mini), [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. + /// + /// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which guarantees the model will match your supplied JSON schema. Learn more in the [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs). + /// + /// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. + /// + /// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. + #[serde(skip_serializing_if = "Option::is_none")] + pub response_format: Option, + + /// This feature is in Beta. + /// If specified, our system will make a best effort to sample deterministically, such that repeated requests + /// with the same `seed` and parameters should return the same result. + /// Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. + #[serde(skip_serializing_if = "Option::is_none")] + pub seed: Option, + + /// Specifies the latency tier to use for processing the request. This parameter is relevant for customers subscribed to the scale tier service: + /// - If set to 'auto', the system will utilize scale tier credits until they are exhausted. + /// - If set to 'default', the request will be processed using the default service tier with a lower uptime SLA and no latency guarentee. + /// - When not set, the default behavior is 'auto'. + /// + /// When this parameter is set, the response body will include the `service_tier` utilized. + #[serde(skip_serializing_if = "Option::is_none")] + pub service_tier: Option, + + /// Up to 4 sequences where the API will stop generating further tokens. + #[serde(skip_serializing_if = "Option::is_none")] + pub stop: Option, + + /// If set, partial message deltas will be sent, like in ChatGPT. + /// Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) + /// as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions). + #[serde(skip_serializing_if = "Option::is_none")] + pub stream: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub stream_options: Option, + + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, + /// while lower values like 0.2 will make it more focused and deterministic. + /// + /// We generally recommend altering this or `top_p` but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, // min: 0, max: 2, default: 1, + + /// An alternative to sampling with temperature, called nucleus sampling, + /// where the model considers the results of the tokens with top_p probability mass. + /// So 0.1 means only the tokens comprising the top 10% probability mass are considered. + /// + /// We generally recommend altering this or `temperature` but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, // min: 0, max: 1, default: 1 + + /// A list of tools the model may call. Currently, only functions are supported as a tool. + /// Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported. + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_choice: Option, + + /// 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, + + /// 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, + + /// This tool searches the web for relevant results to use in a response. + /// Learn more about the [web search tool](https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat). + #[serde(skip_serializing_if = "Option::is_none")] + pub web_search_options: Option, + + /// Deprecated in favor of `tool_choice`. + /// + /// Controls which (if any) function is called by the model. + /// `none` means the model will not call a function and instead generates a message. + /// `auto` means the model can pick between generating a message or calling a function. + /// Specifying a particular function via `{"name": "my_function"}` forces the model to call that function. + /// + /// `none` is the default when no functions are present. `auto` is the default if functions are present. + #[deprecated] + #[serde(skip_serializing_if = "Option::is_none")] + pub function_call: Option, + + /// Deprecated in favor of `tools`. + /// + /// A list of functions the model may generate JSON inputs for. + #[deprecated] + #[serde(skip_serializing_if = "Option::is_none")] + pub functions: Option>, +} + +/// Options for streaming response. Only set this when you set `stream: true`. +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +pub struct ChatCompletionStreamOptions { + /// If set, an additional chunk will be streamed before the `data: [DONE]` message. The `usage` field on this chunk shows the token usage statistics for the entire request, and the `choices` field will always be an empty array. All other chunks will also include a `usage` field, but with a null value. + pub include_usage: bool, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum FinishReason { + Stop, + Length, + ToolCalls, + ContentFilter, + FunctionCall, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct TopLogprobs { + /// The token. + pub token: String, + /// The log probability of this token. + pub logprob: f32, + /// A list of integers representing the UTF-8 bytes representation of the token. Useful in instances where characters are represented by multiple tokens and their byte representations must be combined to generate the correct text representation. Can be `null` if there is no bytes representation for the token. + pub bytes: Option>, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ChatCompletionTokenLogprob { + /// The token. + pub token: String, + /// The log probability of this token, if it is within the top 20 most likely tokens. Otherwise, the value `-9999.0` is used to signify that the token is very unlikely. + pub logprob: f32, + /// A list of integers representing the UTF-8 bytes representation of the token. Useful in instances where characters are represented by multiple tokens and their byte representations must be combined to generate the correct text representation. Can be `null` if there is no bytes representation for the token. + pub bytes: Option>, + /// List of the most likely tokens and their log probability, at this token position. In rare cases, there may be fewer than the number of requested `top_logprobs` returned. + pub top_logprobs: Vec, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ChatChoiceLogprobs { + /// A list of message content tokens with log probability information. + pub content: Option>, + pub refusal: Option>, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ChatChoice { + /// The index of the choice in the list of choices. + pub index: u32, + pub message: ChatCompletionResponseMessage, + /// The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence, + /// `length` if the maximum number of tokens specified in the request was reached, + /// `content_filter` if content was omitted due to a flag from our content filters, + /// `tool_calls` if the model called a tool, or `function_call` (deprecated) if the model called a function. + #[serde(skip_serializing_if = "Option::is_none")] + pub finish_reason: Option, + /// Log probability information for the choice. + #[serde(skip_serializing_if = "Option::is_none")] + pub logprobs: Option, +} + +/// Represents a chat completion response returned by model, based on the provided input. +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct CreateChatCompletionResponse { + /// A unique identifier for the chat completion. + pub id: String, + /// A list of chat completion choices. Can be more than one if `n` is greater than 1. + pub choices: Vec, + /// The Unix timestamp (in seconds) of when the chat completion was created. + pub created: u32, + /// The model used for the chat completion. + pub model: String, + /// The service tier used for processing the request. This field is only included if the `service_tier` parameter is specified in the request. + #[serde(skip_serializing_if = "Option::is_none")] + pub service_tier: Option, + /// This fingerprint represents the backend configuration that the model runs with. + /// + /// Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism. + #[serde(skip_serializing_if = "Option::is_none")] + pub system_fingerprint: Option, + + /// The object type, which is always `chat.completion`. + pub object: String, + pub usage: Option, +} + +/// Parsed server side events stream until an \[DONE\] is received from server. +pub type ChatCompletionResponseStream = + Pin> + Send>>; + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct FunctionCallStream { + /// The name of the function to call. + pub name: Option, + /// The arguments to call the function with, as generated by the model in JSON format. + /// Note that the model does not always generate valid JSON, and may hallucinate + /// parameters not defined by your function schema. Validate the arguments in your + /// code before calling your function. + pub arguments: Option, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ChatCompletionMessageToolCallChunk { + pub index: u32, + /// The ID of the tool call. + pub id: Option, + /// The type of the tool. Currently, only `function` is supported. + pub r#type: Option, + pub function: Option, +} + +/// A chat completion delta generated by streamed model responses. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ChatCompletionStreamResponseDelta { + /// The contents of the chunk message. + pub content: Option, + /// Deprecated and replaced by `tool_calls`. The name and arguments of a function that should be called, as generated by the model. + #[deprecated] + pub function_call: Option, + + pub tool_calls: Option>, + /// The role of the author of this message. + pub role: Option, + /// The refusal message generated by the model. + pub refusal: Option, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ChatChoiceStream { + /// The index of the choice in the list of choices. + pub index: u32, + pub delta: ChatCompletionStreamResponseDelta, + /// The reason the model stopped generating tokens. This will be + /// `stop` if the model hit a natural stop point or a provided + /// stop sequence, + /// + /// `length` if the maximum number of tokens specified in the + /// request was reached, + /// `content_filter` if content was omitted due to a flag from our + /// content filters, + /// `tool_calls` if the model called a tool, or `function_call` + /// (deprecated) if the model called a function. + #[serde(skip_serializing_if = "Option::is_none")] + pub finish_reason: Option, + /// Log probability information for the choice. + #[serde(skip_serializing_if = "Option::is_none")] + pub logprobs: Option, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +/// Represents a streamed chunk of a chat completion response returned by model, based on the provided input. +pub struct CreateChatCompletionStreamResponse { + /// A unique identifier for the chat completion. Each chunk has the same ID. + pub id: String, + /// A list of chat completion choices. Can contain more than one elements if `n` is greater than 1. Can also be empty for the last chunk if you set `stream_options: {"include_usage": true}`. + pub choices: Vec, + + /// The Unix timestamp (in seconds) of when the chat completion was created. Each chunk has the same timestamp. + pub created: u32, + /// The model to generate the completion. + pub model: String, + /// The service tier used for processing the request. This field is only included if the `service_tier` parameter is specified in the request. + pub service_tier: Option, + /// This fingerprint represents the backend configuration that the model runs with. + /// Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism. + pub system_fingerprint: Option, + /// The object type, which is always `chat.completion.chunk`. + pub object: String, + + /// An optional field that will only be present when you set `stream_options: {"include_usage": true}` in your request. + /// When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request. + pub usage: Option, +} diff --git a/async-openai/src/types/common.rs b/async-openai/src/types/common.rs new file mode 100644 index 00000000..1fc9017d --- /dev/null +++ b/async-openai/src/types/common.rs @@ -0,0 +1,18 @@ +use std::path::PathBuf; + +use bytes::Bytes; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq)] +pub enum InputSource { + Path { path: PathBuf }, + Bytes { filename: String, bytes: Bytes }, + VecU8 { filename: String, vec: Vec }, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum OrganizationRole { + Owner, + Reader, +} diff --git a/async-openai/src/types/completion.rs b/async-openai/src/types/completion.rs new file mode 100644 index 00000000..3c15dd6b --- /dev/null +++ b/async-openai/src/types/completion.rs @@ -0,0 +1,141 @@ +use std::{collections::HashMap, pin::Pin}; + +use derive_builder::Builder; +use futures::Stream; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +use super::{ChatCompletionStreamOptions, Choice, CompletionUsage, Prompt, Stop}; + +#[derive(Clone, Serialize, Deserialize, Default, Debug, Builder, PartialEq)] +#[builder(name = "CreateCompletionRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateCompletionRequest { + /// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them. + pub model: String, + + /// The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays. + /// + /// Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document. + pub prompt: Prompt, + + /// The suffix that comes after a completion of inserted text. + /// + /// This parameter is only supported for `gpt-3.5-turbo-instruct`. + #[serde(skip_serializing_if = "Option::is_none")] + pub suffix: Option, // default: null + + /// The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the completion. + /// + /// The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_tokens: Option, + + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + /// + /// We generally recommend altering this or `top_p` but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, // min: 0, max: 2, default: 1, + + /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + /// + /// We generally recommend altering this or `temperature` but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, // min: 0, max: 1, default: 1 + + /// How many completions to generate for each prompt. + + /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. + /// + #[serde(skip_serializing_if = "Option::is_none")] + pub n: Option, // min:1 max: 128, default: 1 + + /// Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) + /// as they become available, with the stream terminated by a `data: [DONE]` message. + #[serde(skip_serializing_if = "Option::is_none")] + pub stream: Option, // nullable: true + + #[serde(skip_serializing_if = "Option::is_none")] + pub stream_options: Option, + + /// Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response. + /// + /// The maximum value for `logprobs` is 5. + #[serde(skip_serializing_if = "Option::is_none")] + pub logprobs: Option, // min:0 , max: 5, default: null, nullable: true + + /// Echo back the prompt in addition to the completion + #[serde(skip_serializing_if = "Option::is_none")] + pub echo: Option, + + /// Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. + #[serde(skip_serializing_if = "Option::is_none")] + pub stop: Option, + + /// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. + /// + /// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details) + #[serde(skip_serializing_if = "Option::is_none")] + pub presence_penalty: Option, // min: -2.0, max: 2.0, default 0 + + /// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. + /// + /// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation/parameter-details) + #[serde(skip_serializing_if = "Option::is_none")] + pub frequency_penalty: Option, // min: -2.0, max: 2.0, default: 0 + + /// Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed. + /// + /// When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`. + /// + /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. + #[serde(skip_serializing_if = "Option::is_none")] + pub best_of: Option, //min: 0, max: 20, default: 1 + + /// Modify the likelihood of specified tokens appearing in the completion. + /// + /// Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](/tokenizer?view=bpe) (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. + /// + /// As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated. + #[serde(skip_serializing_if = "Option::is_none")] + pub logit_bias: Option>, // default: null + + /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids). + #[serde(skip_serializing_if = "Option::is_none")] + pub user: Option, + + /// If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result. + /// + /// Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. + #[serde(skip_serializing_if = "Option::is_none")] + pub seed: Option, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct CreateCompletionResponse { + /// A unique identifier for the completion. + pub id: String, + pub choices: Vec, + /// The Unix timestamp (in seconds) of when the completion was created. + pub created: u32, + + /// The model used for completion. + pub model: String, + /// This fingerprint represents the backend configuration that the model runs with. + /// + /// Can be used in conjunction with the `seed` request parameter to understand when backend changes have been + /// made that might impact determinism. + pub system_fingerprint: Option, + + /// The object type, which is always "text_completion" + pub object: String, + pub usage: Option, +} + +/// Parsed server side events stream until an \[DONE\] is received from server. +pub type CompletionResponseStream = + Pin> + Send>>; diff --git a/async-openai/src/types/embedding.rs b/async-openai/src/types/embedding.rs new file mode 100644 index 00000000..ea05ac3b --- /dev/null +++ b/async-openai/src/types/embedding.rs @@ -0,0 +1,122 @@ +use base64::engine::{general_purpose, Engine}; +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +#[derive(Debug, Serialize, Clone, PartialEq, Deserialize)] +#[serde(untagged)] +pub enum EmbeddingInput { + String(String), + StringArray(Vec), + // Minimum value is 0, maximum value is 100257 (inclusive). + IntegerArray(Vec), + ArrayOfIntegerArray(Vec>), +} + +#[derive(Debug, Serialize, Default, Clone, PartialEq, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum EncodingFormat { + #[default] + Float, + Base64, +} + +#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq, Deserialize)] +#[builder(name = "CreateEmbeddingRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateEmbeddingRequest { + /// ID of the model to use. You can use the + /// [List models](https://platform.openai.com/docs/api-reference/models/list) + /// API to see all of your available models, or see our + /// [Model overview](https://platform.openai.com/docs/models/overview) + /// for descriptions of them. + pub model: String, + + /// Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. The input must not exceed the max input tokens for the model (8192 tokens for `text-embedding-ada-002`), cannot be an empty string, and any array must be 2048 dimensions or less. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. + pub input: EmbeddingInput, + + /// The format to return the embeddings in. Can be either `float` or [`base64`](https://pypi.org/project/pybase64/). Defaults to float + #[serde(skip_serializing_if = "Option::is_none")] + pub encoding_format: Option, + + /// A unique identifier representing your end-user, which will help OpenAI + /// to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids). + #[serde(skip_serializing_if = "Option::is_none")] + pub user: Option, + + /// The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models. + #[serde(skip_serializing_if = "Option::is_none")] + pub dimensions: Option, +} + +/// Represents an embedding vector returned by embedding endpoint. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct Embedding { + /// The index of the embedding in the list of embeddings. + pub index: u32, + /// The object type, which is always "embedding". + pub object: String, + /// The embedding vector, which is a list of floats. The length of vector + /// depends on the model as listed in the [embedding guide](https://platform.openai.com/docs/guides/embeddings). + pub embedding: Vec, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct Base64EmbeddingVector(pub String); + +impl From for Vec { + fn from(value: Base64EmbeddingVector) -> Self { + let bytes = general_purpose::STANDARD + .decode(value.0) + .expect("openai base64 encoding to be valid"); + let chunks = bytes.chunks_exact(4); + chunks + .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]])) + .collect() + } +} + +/// Represents an base64-encoded embedding vector returned by embedding endpoint. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct Base64Embedding { + /// The index of the embedding in the list of embeddings. + pub index: u32, + /// The object type, which is always "embedding". + pub object: String, + /// The embedding vector, encoded in base64. + pub embedding: Base64EmbeddingVector, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct EmbeddingUsage { + /// The number of tokens used by the prompt. + pub prompt_tokens: u32, + /// The total number of tokens used by the request. + pub total_tokens: u32, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct CreateEmbeddingResponse { + pub object: String, + /// The name of the model used to generate the embedding. + pub model: String, + /// The list of embeddings generated by the model. + pub data: Vec, + /// The usage information for the request. + pub usage: EmbeddingUsage, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct CreateBase64EmbeddingResponse { + pub object: String, + /// The name of the model used to generate the embedding. + pub model: String, + /// The list of embeddings generated by the model. + pub data: Vec, + /// The usage information for the request. + pub usage: EmbeddingUsage, +} diff --git a/async-openai/src/types/file.rs b/async-openai/src/types/file.rs new file mode 100644 index 00000000..9a2e5090 --- /dev/null +++ b/async-openai/src/types/file.rs @@ -0,0 +1,90 @@ +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +use super::InputSource; + +#[derive(Debug, Default, Clone, PartialEq)] +pub struct FileInput { + pub source: InputSource, +} + +#[derive(Debug, Default, Clone, PartialEq)] +pub enum FilePurpose { + Assistants, + Batch, + #[default] + FineTune, + Vision, +} + +#[derive(Debug, Default, Clone, Builder, PartialEq)] +#[builder(name = "CreateFileRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateFileRequest { + /// The File object (not file name) to be uploaded. + pub file: FileInput, + + /// The intended purpose of the uploaded file. + /// + /// Use "assistants" for [Assistants](https://platform.openai.com/docs/api-reference/assistants) and [Message](https://platform.openai.com/docs/api-reference/messages) files, "vision" for Assistants image file inputs, "batch" for [Batch API](https://platform.openai.com/docs/guides/batch), and "fine-tune" for [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning). + pub purpose: FilePurpose, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct ListFilesResponse { + pub object: String, + pub data: Vec, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct DeleteFileResponse { + pub id: String, + pub object: String, + pub deleted: bool, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub enum OpenAIFilePurpose { + #[serde(rename = "assistants")] + Assistants, + #[serde(rename = "assistants_output")] + AssistantsOutput, + #[serde(rename = "batch")] + Batch, + #[serde(rename = "batch_output")] + BatchOutput, + #[serde(rename = "fine-tune")] + FineTune, + #[serde(rename = "fine-tune-results")] + FineTuneResults, + #[serde(rename = "vision")] + Vision, +} + +/// The `File` object represents a document that has been uploaded to OpenAI. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct OpenAIFile { + /// The file identifier, which can be referenced in the API endpoints. + pub id: String, + /// The object type, which is always "file". + pub object: String, + /// The size of the file in bytes. + pub bytes: u32, + /// The Unix timestamp (in seconds) for when the file was created. + pub created_at: u32, + /// The name of the file. + pub filename: String, + /// The intended purpose of the file. Supported values are `assistants`, `assistants_output`, `batch`, `batch_output`, `fine-tune`, `fine-tune-results` and `vision`. + pub purpose: OpenAIFilePurpose, + /// Deprecated. The current status of the file, which can be either `uploaded`, `processed`, or `error`. + #[deprecated] + pub status: Option, + /// Deprecated. For details on why a fine-tuning training file failed validation, see the `error` field on `fine_tuning.job`. + #[deprecated] + pub status_details: Option, // nullable: true +} diff --git a/async-openai/src/types/fine_tuning.rs b/async-openai/src/types/fine_tuning.rs new file mode 100644 index 00000000..a5c6d321 --- /dev/null +++ b/async-openai/src/types/fine_tuning.rs @@ -0,0 +1,348 @@ +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)] +#[serde(untagged)] +pub enum NEpochs { + NEpochs(u8), + #[default] + #[serde(rename = "auto")] + Auto, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)] +#[serde(untagged)] +pub enum BatchSize { + BatchSize(u16), + #[default] + #[serde(rename = "auto")] + Auto, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)] +#[serde(untagged)] +pub enum LearningRateMultiplier { + LearningRateMultiplier(f32), + #[default] + #[serde(rename = "auto")] + Auto, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)] +pub struct Hyperparameters { + /// Number of examples in each batch. A larger batch size means that model parameters + /// are updated less frequently, but with lower variance. + pub batch_size: BatchSize, + /// Scaling factor for the learning rate. A smaller learning rate may be useful to avoid + /// overfitting. + pub learning_rate_multiplier: LearningRateMultiplier, + /// The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset. + pub n_epochs: NEpochs, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)] +#[serde(untagged)] +pub enum Beta { + Beta(f32), + #[default] + #[serde(rename = "auto")] + Auto, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)] +pub struct DPOHyperparameters { + /// The beta value for the DPO method. A higher beta value will increase the weight of the penalty between the policy and reference model. + pub beta: Beta, + /// Number of examples in each batch. A larger batch size means that model parameters + /// are updated less frequently, but with lower variance. + pub batch_size: BatchSize, + /// Scaling factor for the learning rate. A smaller learning rate may be useful to avoid + /// overfitting. + pub learning_rate_multiplier: LearningRateMultiplier, + /// The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset. + pub n_epochs: NEpochs, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default, Builder, PartialEq)] +#[builder(name = "CreateFineTuningJobRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateFineTuningJobRequest { + /// The name of the model to fine-tune. You can select one of the + /// [supported models](https://platform.openai.com/docs/guides/fine-tuning#which-models-can-be-fine-tuned). + pub model: String, + + /// The ID of an uploaded file that contains training data. + /// + /// See [upload file](https://platform.openai.com/docs/api-reference/files/create) for how to upload a file. + /// + /// Your dataset must be formatted as a JSONL file. Additionally, you must upload your file with the purpose `fine-tune`. + /// + /// The contents of the file should differ depending on if the model uses the [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input), [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input) format, or if the fine-tuning method uses the [preference](https://platform.openai.com/docs/api-reference/fine-tuning/preference-input) format. + /// + /// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) for more details. + pub training_file: String, + + /// The hyperparameters used for the fine-tuning job. + /// This value is now deprecated in favor of `method`, and should be passed in under the `method` parameter. + #[deprecated] + pub hyperparameters: Option, + + /// A string of up to 64 characters that will be added to your fine-tuned model name. + /// + /// For example, a `suffix` of "custom-model-name" would produce a model name like `ft:gpt-4o-mini:openai:custom-model-name:7p4lURel`. + #[serde(skip_serializing_if = "Option::is_none")] + pub suffix: Option, // default: null, minLength:1, maxLength:40 + + /// The ID of an uploaded file that contains validation data. + /// + /// If you provide this file, the data is used to generate validation + /// metrics periodically during fine-tuning. These metrics can be viewed in + /// the fine-tuning results file. + /// The same data should not be present in both train and validation files. + /// + /// Your dataset must be formatted as a JSONL file. You must upload your file with the purpose `fine-tune`. + /// + /// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) for more details. + #[serde(skip_serializing_if = "Option::is_none")] + pub validation_file: Option, + + /// A list of integrations to enable for your fine-tuning job. + #[serde(skip_serializing_if = "Option::is_none")] + pub integrations: Option>, + + /// The seed controls the reproducibility of the job. Passing in the same seed and job parameters should produce the same results, but may differ in rare cases. + /// If a seed is not specified, one will be generated for you. + #[serde(skip_serializing_if = "Option::is_none")] + pub seed: Option, // min:0, max: 2147483647 + + #[serde(skip_serializing_if = "Option::is_none")] + pub method: Option, +} + +/// The method used for fine-tuning. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "lowercase")] +pub enum FineTuneMethod { + Supervised { + supervised: FineTuneSupervisedMethod, + }, + DPO { + dpo: FineTuneDPOMethod, + }, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct FineTuneSupervisedMethod { + pub hyperparameters: Hyperparameters, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct FineTuneDPOMethod { + pub hyperparameters: DPOHyperparameters, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize, Default)] +#[serde(rename_all = "lowercase")] +pub enum FineTuningJobIntegrationType { + #[default] + Wandb, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct FineTuningIntegration { + /// The type of integration to enable. Currently, only "wandb" (Weights and Biases) is supported. + pub r#type: FineTuningJobIntegrationType, + + /// The settings for your integration with Weights and Biases. This payload specifies the project that + /// metrics will be sent to. Optionally, you can set an explicit display name for your run, add tags + /// to your run, and set a default entity (team, username, etc) to be associated with your run. + pub wandb: WandB, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct WandB { + /// The name of the project that the new run will be created under. + pub project: String, + /// A display name to set for the run. If not set, we will use the Job ID as the name. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + /// The entity to use for the run. This allows you to set the team or username of the WandB user that you would + /// like associated with the run. If not set, the default entity for the registered WandB API key is used. + #[serde(skip_serializing_if = "Option::is_none")] + pub entity: Option, + /// A list of tags to be attached to the newly created run. These tags are passed through directly to WandB. Some + /// default tags are generated by OpenAI: "openai/finetune", "openai/{base-model}", "openai/{ftjob-abcdef}". + #[serde(skip_serializing_if = "Option::is_none")] + pub tags: Option>, +} + +/// For fine-tuning jobs that have `failed`, this will contain more information on the cause of the failure. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct FineTuneJobError { + /// A machine-readable error code. + pub code: String, + /// A human-readable error message. + pub message: String, + /// The parameter that was invalid, usually `training_file` or `validation_file`. + /// This field will be null if the failure was not parameter-specific. + pub param: Option, // nullable true +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum FineTuningJobStatus { + ValidatingFiles, + Queued, + Running, + Succeeded, + Failed, + Cancelled, +} + +/// The `fine_tuning.job` object represents a fine-tuning job that has been created through the API. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct FineTuningJob { + /// The object identifier, which can be referenced in the API endpoints. + pub id: String, + /// The Unix timestamp (in seconds) for when the fine-tuning job was created. + pub created_at: u32, + /// For fine-tuning jobs that have `failed`, this will contain more information on the cause of the failure. + pub error: Option, + /// The name of the fine-tuned model that is being created. + /// The value will be null if the fine-tuning job is still running. + pub fine_tuned_model: Option, // nullable: true + /// The Unix timestamp (in seconds) for when the fine-tuning job was finished. + /// The value will be null if the fine-tuning job is still running. + pub finished_at: Option, // nullable true + + /// The hyperparameters used for the fine-tuning job. + /// See the [fine-tuning guide](/docs/guides/fine-tuning) for more details. + pub hyperparameters: Hyperparameters, + + /// The base model that is being fine-tuned. + pub model: String, + + /// The object type, which is always "fine_tuning.job". + pub object: String, + /// The organization that owns the fine-tuning job. + pub organization_id: String, + + /// The compiled results file ID(s) for the fine-tuning job. + /// You can retrieve the results with the [Files API](https://platform.openai.com/docs/api-reference/files/retrieve-contents). + pub result_files: Vec, + + /// The current status of the fine-tuning job, which can be either + /// `validating_files`, `queued`, `running`, `succeeded`, `failed`, or `cancelled`. + pub status: FineTuningJobStatus, + + /// The total number of billable tokens processed by this fine-tuning job. The value will be null if the fine-tuning job is still running. + pub trained_tokens: Option, + + /// The file ID used for training. You can retrieve the training data with the [Files API](https://platform.openai.com/docs/api-reference/files/retrieve-contents). + pub training_file: String, + + /// The file ID used for validation. You can retrieve the validation results with the [Files API](https://platform.openai.com/docs/api-reference/files/retrieve-contents). + pub validation_file: Option, + + /// A list of integrations to enable for this fine-tuning job. + pub integrations: Option>, // maxItems: 5 + + /// The seed used for the fine-tuning job. + pub seed: u32, + + /// The Unix timestamp (in seconds) for when the fine-tuning job is estimated to finish. The value will be null if the fine-tuning job is not running. + pub estimated_finish: Option, + + pub method: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ListPaginatedFineTuningJobsResponse { + pub data: Vec, + pub has_more: bool, + pub object: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ListFineTuningJobEventsResponse { + pub data: Vec, + pub object: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ListFineTuningJobCheckpointsResponse { + pub data: Vec, + pub object: String, + pub first_id: Option, + pub last_id: Option, + pub has_more: bool, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum Level { + Info, + Warn, + Error, +} + +///Fine-tuning job event object +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct FineTuningJobEvent { + /// The object identifier. + pub id: String, + /// The Unix timestamp (in seconds) for when the fine-tuning job event was created. + pub created_at: u32, + /// The log level of the event. + pub level: Level, + /// The message of the event. + pub message: String, + /// The object type, which is always "fine_tuning.job.event". + pub object: String, + /// The type of event. + pub r#type: Option, + /// The data associated with the event. + pub data: Option, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum FineTuningJobEventType { + Message, + Metrics, +} + +/// The `fine_tuning.job.checkpoint` object represents a model checkpoint for a fine-tuning job that is ready to use. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct FineTuningJobCheckpoint { + /// The checkpoint identifier, which can be referenced in the API endpoints. + pub id: String, + /// The Unix timestamp (in seconds) for when the checkpoint was created. + pub created_at: u32, + /// The name of the fine-tuned checkpoint model that is created. + pub fine_tuned_model_checkpoint: String, + /// The step number that the checkpoint was created at. + pub step_number: u32, + /// Metrics at the step number during the fine-tuning job. + pub metrics: FineTuningJobCheckpointMetrics, + /// The name of the fine-tuning job that this checkpoint was created from. + pub fine_tuning_job_id: String, + /// The object type, which is always "fine_tuning.job.checkpoint". + pub object: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct FineTuningJobCheckpointMetrics { + pub step: u32, + pub train_loss: f32, + pub train_mean_token_accuracy: f32, + pub valid_loss: f32, + pub valid_mean_token_accuracy: f32, + pub full_valid_loss: f32, + pub full_valid_mean_token_accuracy: f32, +} diff --git a/async-openai/src/types/image.rs b/async-openai/src/types/image.rs new file mode 100644 index 00000000..b3c30d74 --- /dev/null +++ b/async-openai/src/types/image.rs @@ -0,0 +1,214 @@ +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +use super::InputSource; + +#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +pub enum ImageSize { + #[serde(rename = "256x256")] + S256x256, + #[serde(rename = "512x512")] + S512x512, + #[default] + #[serde(rename = "1024x1024")] + S1024x1024, + #[serde(rename = "1792x1024")] + S1792x1024, + #[serde(rename = "1024x1792")] + S1024x1792, +} + +#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +pub enum DallE2ImageSize { + #[serde(rename = "256x256")] + S256x256, + #[serde(rename = "512x512")] + S512x512, + #[default] + #[serde(rename = "1024x1024")] + S1024x1024, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageResponseFormat { + #[default] + Url, + #[serde(rename = "b64_json")] + B64Json, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +pub enum ImageModel { + #[default] + #[serde(rename = "dall-e-2")] + DallE2, + #[serde(rename = "dall-e-3")] + DallE3, + #[serde(untagged)] + Other(String), +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageQuality { + #[default] + Standard, + HD, + High, + Medium, + Low, + Auto, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageStyle { + #[default] + Vivid, + Natural, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageModeration { + #[default] + Auto, + Low, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder, PartialEq)] +#[builder(name = "CreateImageRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateImageRequest { + /// A text description of the desired image(s). The maximum length is 1000 characters for `dall-e-2` + /// and 4000 characters for `dall-e-3`. + pub prompt: String, + + /// The model to use for image generation. + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, + + /// The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported. + #[serde(skip_serializing_if = "Option::is_none")] + pub n: Option, // min:1 max:10 default:1 + + /// The quality of the image that will be generated. `hd` creates images with finer details and greater + /// consistency across the image. This param is only supported for `dall-e-3`. + #[serde(skip_serializing_if = "Option::is_none")] + pub quality: Option, + + /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated. + #[serde(skip_serializing_if = "Option::is_none")] + pub response_format: Option, + + /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`. + /// Must be one of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3` models. + #[serde(skip_serializing_if = "Option::is_none")] + pub size: Option, + + /// The style of the generated images. Must be one of `vivid` or `natural`. + /// Vivid causes the model to lean towards generating hyper-real and dramatic images. + /// Natural causes the model to produce more natural, less hyper-real looking images. + /// This param is only supported for `dall-e-3`. + #[serde(skip_serializing_if = "Option::is_none")] + pub style: Option, + + /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids). + #[serde(skip_serializing_if = "Option::is_none")] + pub user: Option, + + /// Control the content-moderation level for images generated by gpt-image-1. + /// Must be either `low` for less restrictive filtering or `auto` (default value). + #[serde(skip_serializing_if = "Option::is_none")] + pub moderation: Option, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum Image { + /// The URL of the generated image, if `response_format` is `url` (default). + Url { + url: String, + revised_prompt: Option, + }, + /// The base64-encoded JSON of the generated image, if `response_format` is `b64_json`. + B64Json { + b64_json: std::sync::Arc, + revised_prompt: Option, + }, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ImagesResponse { + pub created: u32, + pub data: Vec>, +} + +#[derive(Debug, Default, Clone, PartialEq)] +pub struct ImageInput { + pub source: InputSource, +} + +#[derive(Debug, Clone, Default, Builder, PartialEq)] +#[builder(name = "CreateImageEditRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateImageEditRequest { + /// The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask. + pub image: ImageInput, + + /// A text description of the desired image(s). The maximum length is 1000 characters. + pub prompt: String, + + /// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`. + pub mask: Option, + + /// The model to use for image generation. Only `dall-e-2` is supported at this time. + pub model: Option, + + /// The number of images to generate. Must be between 1 and 10. + pub n: Option, // min:1 max:10 default:1 + + /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`. + pub size: Option, + + /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. + pub response_format: Option, + + /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids). + pub user: Option, +} + +#[derive(Debug, Default, Clone, Builder, PartialEq)] +#[builder(name = "CreateImageVariationRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateImageVariationRequest { + /// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square. + pub image: ImageInput, + + /// The model to use for image generation. Only `dall-e-2` is supported at this time. + pub model: Option, + + /// The number of images to generate. Must be between 1 and 10. + pub n: Option, // min:1 max:10 default:1 + + /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`. + pub size: Option, + + /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. + pub response_format: Option, + + /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids). + pub user: Option, +} diff --git a/async-openai/src/types/impls.rs b/async-openai/src/types/impls.rs new file mode 100644 index 00000000..8646e8f9 --- /dev/null +++ b/async-openai/src/types/impls.rs @@ -0,0 +1,1038 @@ +use std::{ + fmt::Display, + path::{Path, PathBuf}, +}; + +use crate::{ + download::{download_url, save_b64}, + error::OpenAIError, + traits::AsyncTryFrom, + types::InputSource, + util::{create_all_dir, create_file_part}, +}; + +use bytes::Bytes; + +use super::{ + responses::{CodeInterpreterContainer, Input, InputContent, Role as ResponsesRole}, + AddUploadPartRequest, AudioInput, AudioResponseFormat, ChatCompletionFunctionCall, + ChatCompletionFunctions, ChatCompletionNamedToolChoice, ChatCompletionRequestAssistantMessage, + ChatCompletionRequestAssistantMessageContent, ChatCompletionRequestDeveloperMessage, + ChatCompletionRequestDeveloperMessageContent, ChatCompletionRequestFunctionMessage, + ChatCompletionRequestMessage, ChatCompletionRequestMessageContentPartAudio, + ChatCompletionRequestMessageContentPartImage, ChatCompletionRequestMessageContentPartText, + ChatCompletionRequestSystemMessage, ChatCompletionRequestSystemMessageContent, + ChatCompletionRequestToolMessage, ChatCompletionRequestToolMessageContent, + ChatCompletionRequestUserMessage, ChatCompletionRequestUserMessageContent, + ChatCompletionRequestUserMessageContentPart, ChatCompletionToolChoiceOption, CreateFileRequest, + CreateImageEditRequest, CreateImageVariationRequest, CreateMessageRequestContent, + CreateSpeechResponse, CreateTranscriptionRequest, CreateTranslationRequest, DallE2ImageSize, + EmbeddingInput, FileInput, FilePurpose, FunctionName, Image, ImageInput, ImageModel, + ImageResponseFormat, ImageSize, ImageUrl, ImagesResponse, ModerationInput, Prompt, Role, Stop, + TimestampGranularity, +}; + +/// for `impl_from!(T, Enum)`, implements +/// - `From` +/// - `From>` +/// - `From<&Vec>` +/// - `From<[T; N]>` +/// - `From<&[T; N]>` +/// +/// for `T: Into` and `Enum` having variants `String(String)` and `StringArray(Vec)` +macro_rules! impl_from { + ($from_typ:ty, $to_typ:ty) => { + // From -> String variant + impl From<$from_typ> for $to_typ { + fn from(value: $from_typ) -> Self { + <$to_typ>::String(value.into()) + } + } + + // From> -> StringArray variant + impl From> for $to_typ { + fn from(value: Vec<$from_typ>) -> Self { + <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect()) + } + } + + // From<&Vec> -> StringArray variant + impl From<&Vec<$from_typ>> for $to_typ { + fn from(value: &Vec<$from_typ>) -> Self { + <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect()) + } + } + + // From<[T; N]> -> StringArray variant + impl From<[$from_typ; N]> for $to_typ { + fn from(value: [$from_typ; N]) -> Self { + <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect()) + } + } + + // From<&[T; N]> -> StringArray variatn + impl From<&[$from_typ; N]> for $to_typ { + fn from(value: &[$from_typ; N]) -> Self { + <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect()) + } + } + }; +} + +// From String "family" to Prompt +impl_from!(&str, Prompt); +impl_from!(String, Prompt); +impl_from!(&String, Prompt); + +// From String "family" to Stop +impl_from!(&str, Stop); +impl_from!(String, Stop); +impl_from!(&String, Stop); + +// From String "family" to ModerationInput +impl_from!(&str, ModerationInput); +impl_from!(String, ModerationInput); +impl_from!(&String, ModerationInput); + +// From String "family" to EmbeddingInput +impl_from!(&str, EmbeddingInput); +impl_from!(String, EmbeddingInput); +impl_from!(&String, EmbeddingInput); + +/// for `impl_default!(Enum)`, implements `Default` for `Enum` as `Enum::String("")` where `Enum` has `String` variant +macro_rules! impl_default { + ($for_typ:ty) => { + impl Default for $for_typ { + fn default() -> Self { + Self::String("".into()) + } + } + }; +} + +impl_default!(Prompt); +impl_default!(ModerationInput); +impl_default!(EmbeddingInput); + +impl Default for InputSource { + fn default() -> Self { + InputSource::Path { + path: PathBuf::new(), + } + } +} + +/// for `impl_input!(Struct)` where +/// ```text +/// Struct { +/// source: InputSource +/// } +/// ``` +/// implements methods `from_bytes` and `from_vec_u8`, +/// and `From

` for `P: AsRef` +macro_rules! impl_input { + ($for_typ:ty) => { + impl $for_typ { + pub fn from_bytes(filename: String, bytes: Bytes) -> Self { + Self { + source: InputSource::Bytes { filename, bytes }, + } + } + + pub fn from_vec_u8(filename: String, vec: Vec) -> Self { + Self { + source: InputSource::VecU8 { filename, vec }, + } + } + } + + impl> From

for $for_typ { + fn from(path: P) -> Self { + let path_buf = path.as_ref().to_path_buf(); + Self { + source: InputSource::Path { path: path_buf }, + } + } + } + }; +} + +impl_input!(AudioInput); +impl_input!(FileInput); +impl_input!(ImageInput); + +impl Display for ImageSize { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::S256x256 => "256x256", + Self::S512x512 => "512x512", + Self::S1024x1024 => "1024x1024", + Self::S1792x1024 => "1792x1024", + Self::S1024x1792 => "1024x1792", + } + ) + } +} + +impl Display for DallE2ImageSize { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::S256x256 => "256x256", + Self::S512x512 => "512x512", + Self::S1024x1024 => "1024x1024", + } + ) + } +} + +impl Display for ImageModel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::DallE2 => "dall-e-2", + Self::DallE3 => "dall-e-3", + Self::Other(other) => other, + } + ) + } +} + +impl Display for ImageResponseFormat { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Url => "url", + Self::B64Json => "b64_json", + } + ) + } +} + +impl Display for AudioResponseFormat { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + AudioResponseFormat::Json => "json", + AudioResponseFormat::Srt => "srt", + AudioResponseFormat::Text => "text", + AudioResponseFormat::VerboseJson => "verbose_json", + AudioResponseFormat::Vtt => "vtt", + } + ) + } +} + +impl Display for TimestampGranularity { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + TimestampGranularity::Word => "word", + TimestampGranularity::Segment => "segment", + } + ) + } +} + +impl Display for Role { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Role::User => "user", + Role::System => "system", + Role::Assistant => "assistant", + Role::Function => "function", + Role::Tool => "tool", + } + ) + } +} + +impl Display for FilePurpose { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Assistants => "assistants", + Self::Batch => "batch", + Self::FineTune => "fine-tune", + Self::Vision => "vision", + } + ) + } +} + +impl ImagesResponse { + /// Save each image in a dedicated Tokio task and return paths to saved files. + /// For [ResponseFormat::Url] each file is downloaded in dedicated Tokio task. + pub async fn save>(&self, dir: P) -> Result, OpenAIError> { + create_all_dir(dir.as_ref())?; + + let mut handles = vec![]; + for id in self.data.clone() { + let dir_buf = PathBuf::from(dir.as_ref()); + handles.push(tokio::spawn(async move { id.save(dir_buf).await })); + } + + let results = futures::future::join_all(handles).await; + let mut errors = vec![]; + let mut paths = vec![]; + + for result in results { + match result { + Ok(inner) => match inner { + Ok(path) => paths.push(path), + Err(e) => errors.push(e), + }, + Err(e) => errors.push(OpenAIError::FileSaveError(e.to_string())), + } + } + + if errors.is_empty() { + Ok(paths) + } else { + Err(OpenAIError::FileSaveError( + errors + .into_iter() + .map(|e| e.to_string()) + .collect::>() + .join("; "), + )) + } + } +} + +impl CreateSpeechResponse { + pub async fn save>(&self, file_path: P) -> Result<(), OpenAIError> { + let dir = file_path.as_ref().parent(); + + if let Some(dir) = dir { + create_all_dir(dir)?; + } + + tokio::fs::write(file_path, &self.bytes) + .await + .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?; + + Ok(()) + } +} + +impl Image { + async fn save>(&self, dir: P) -> Result { + match self { + Image::Url { url, .. } => download_url(url, dir).await, + Image::B64Json { b64_json, .. } => save_b64(b64_json, dir).await, + } + } +} + +macro_rules! impl_from_for_integer_array { + ($from_typ:ty, $to_typ:ty) => { + impl From<[$from_typ; N]> for $to_typ { + fn from(value: [$from_typ; N]) -> Self { + Self::IntegerArray(value.to_vec()) + } + } + + impl From<&[$from_typ; N]> for $to_typ { + fn from(value: &[$from_typ; N]) -> Self { + Self::IntegerArray(value.to_vec()) + } + } + + impl From> for $to_typ { + fn from(value: Vec<$from_typ>) -> Self { + Self::IntegerArray(value) + } + } + + impl From<&Vec<$from_typ>> for $to_typ { + fn from(value: &Vec<$from_typ>) -> Self { + Self::IntegerArray(value.clone()) + } + } + }; +} + +impl_from_for_integer_array!(u32, EmbeddingInput); +impl_from_for_integer_array!(u32, Prompt); + +macro_rules! impl_from_for_array_of_integer_array { + ($from_typ:ty, $to_typ:ty) => { + impl From>> for $to_typ { + fn from(value: Vec>) -> Self { + Self::ArrayOfIntegerArray(value) + } + } + + impl From<&Vec>> for $to_typ { + fn from(value: &Vec>) -> Self { + Self::ArrayOfIntegerArray(value.clone()) + } + } + + impl From<[[$from_typ; N]; M]> for $to_typ { + fn from(value: [[$from_typ; N]; M]) -> Self { + Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect()) + } + } + + impl From<[&[$from_typ; N]; M]> for $to_typ { + fn from(value: [&[$from_typ; N]; M]) -> Self { + Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect()) + } + } + + impl From<&[[$from_typ; N]; M]> for $to_typ { + fn from(value: &[[$from_typ; N]; M]) -> Self { + Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect()) + } + } + + impl From<&[&[$from_typ; N]; M]> for $to_typ { + fn from(value: &[&[$from_typ; N]; M]) -> Self { + Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect()) + } + } + + impl From<[Vec<$from_typ>; N]> for $to_typ { + fn from(value: [Vec<$from_typ>; N]) -> Self { + Self::ArrayOfIntegerArray(value.to_vec()) + } + } + + impl From<&[Vec<$from_typ>; N]> for $to_typ { + fn from(value: &[Vec<$from_typ>; N]) -> Self { + Self::ArrayOfIntegerArray(value.to_vec()) + } + } + + impl From<[&Vec<$from_typ>; N]> for $to_typ { + fn from(value: [&Vec<$from_typ>; N]) -> Self { + Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.clone()).collect()) + } + } + + impl From<&[&Vec<$from_typ>; N]> for $to_typ { + fn from(value: &[&Vec<$from_typ>; N]) -> Self { + Self::ArrayOfIntegerArray( + value + .to_vec() + .into_iter() + .map(|inner| inner.clone()) + .collect(), + ) + } + } + + impl From> for $to_typ { + fn from(value: Vec<[$from_typ; N]>) -> Self { + Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect()) + } + } + + impl From<&Vec<[$from_typ; N]>> for $to_typ { + fn from(value: &Vec<[$from_typ; N]>) -> Self { + Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect()) + } + } + + impl From> for $to_typ { + fn from(value: Vec<&[$from_typ; N]>) -> Self { + Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect()) + } + } + + impl From<&Vec<&[$from_typ; N]>> for $to_typ { + fn from(value: &Vec<&[$from_typ; N]>) -> Self { + Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect()) + } + } + }; +} + +impl_from_for_array_of_integer_array!(u32, EmbeddingInput); +impl_from_for_array_of_integer_array!(u32, Prompt); + +impl From<&str> for ChatCompletionFunctionCall { + fn from(value: &str) -> Self { + match value { + "auto" => Self::Auto, + "none" => Self::None, + _ => Self::Function { name: value.into() }, + } + } +} + +impl From<&str> for FunctionName { + fn from(value: &str) -> Self { + Self { name: value.into() } + } +} + +impl From for FunctionName { + fn from(value: String) -> Self { + Self { name: value } + } +} + +impl From<&str> for ChatCompletionNamedToolChoice { + fn from(value: &str) -> Self { + Self { + r#type: super::ChatCompletionToolType::Function, + function: value.into(), + } + } +} + +impl From for ChatCompletionNamedToolChoice { + fn from(value: String) -> Self { + Self { + r#type: super::ChatCompletionToolType::Function, + function: value.into(), + } + } +} + +impl From<&str> for ChatCompletionToolChoiceOption { + fn from(value: &str) -> Self { + match value { + "auto" => Self::Auto, + "none" => Self::None, + _ => Self::Named(value.into()), + } + } +} + +impl From for ChatCompletionToolChoiceOption { + fn from(value: String) -> Self { + match value.as_str() { + "auto" => Self::Auto, + "none" => Self::None, + _ => Self::Named(value.into()), + } + } +} + +impl From<(String, serde_json::Value)> for ChatCompletionFunctions { + fn from(value: (String, serde_json::Value)) -> Self { + Self { + name: value.0, + description: None, + parameters: value.1, + } + } +} + +// todo: write macro for bunch of same looking From trait implementations below + +impl From for ChatCompletionRequestMessage { + fn from(value: ChatCompletionRequestUserMessage) -> Self { + Self::User(value) + } +} + +impl From for ChatCompletionRequestMessage { + fn from(value: ChatCompletionRequestSystemMessage) -> Self { + Self::System(value) + } +} + +impl From for ChatCompletionRequestMessage { + fn from(value: ChatCompletionRequestDeveloperMessage) -> Self { + Self::Developer(value) + } +} + +impl From for ChatCompletionRequestMessage { + fn from(value: ChatCompletionRequestAssistantMessage) -> Self { + Self::Assistant(value) + } +} + +impl From for ChatCompletionRequestMessage { + fn from(value: ChatCompletionRequestFunctionMessage) -> Self { + Self::Function(value) + } +} + +impl From for ChatCompletionRequestMessage { + fn from(value: ChatCompletionRequestToolMessage) -> Self { + Self::Tool(value) + } +} + +impl From for ChatCompletionRequestUserMessage { + fn from(value: ChatCompletionRequestUserMessageContent) -> Self { + Self { + content: value, + name: None, + } + } +} + +impl From for ChatCompletionRequestSystemMessage { + fn from(value: ChatCompletionRequestSystemMessageContent) -> Self { + Self { + content: value, + name: None, + } + } +} + +impl From for ChatCompletionRequestDeveloperMessage { + fn from(value: ChatCompletionRequestDeveloperMessageContent) -> Self { + Self { + content: value, + name: None, + } + } +} + +impl From for ChatCompletionRequestAssistantMessage { + fn from(value: ChatCompletionRequestAssistantMessageContent) -> Self { + Self { + content: Some(value), + ..Default::default() + } + } +} + +impl From<&str> for ChatCompletionRequestUserMessageContent { + fn from(value: &str) -> Self { + ChatCompletionRequestUserMessageContent::Text(value.into()) + } +} + +impl From for ChatCompletionRequestUserMessageContent { + fn from(value: String) -> Self { + ChatCompletionRequestUserMessageContent::Text(value) + } +} + +impl From<&str> for ChatCompletionRequestSystemMessageContent { + fn from(value: &str) -> Self { + ChatCompletionRequestSystemMessageContent::Text(value.into()) + } +} + +impl From for ChatCompletionRequestSystemMessageContent { + fn from(value: String) -> Self { + ChatCompletionRequestSystemMessageContent::Text(value) + } +} + +impl From<&str> for ChatCompletionRequestDeveloperMessageContent { + fn from(value: &str) -> Self { + ChatCompletionRequestDeveloperMessageContent::Text(value.into()) + } +} + +impl From for ChatCompletionRequestDeveloperMessageContent { + fn from(value: String) -> Self { + ChatCompletionRequestDeveloperMessageContent::Text(value) + } +} + +impl From<&str> for ChatCompletionRequestAssistantMessageContent { + fn from(value: &str) -> Self { + ChatCompletionRequestAssistantMessageContent::Text(value.into()) + } +} + +impl From for ChatCompletionRequestAssistantMessageContent { + fn from(value: String) -> Self { + ChatCompletionRequestAssistantMessageContent::Text(value) + } +} + +impl From<&str> for ChatCompletionRequestToolMessageContent { + fn from(value: &str) -> Self { + ChatCompletionRequestToolMessageContent::Text(value.into()) + } +} + +impl From for ChatCompletionRequestToolMessageContent { + fn from(value: String) -> Self { + ChatCompletionRequestToolMessageContent::Text(value) + } +} + +impl From<&str> for ChatCompletionRequestUserMessage { + fn from(value: &str) -> Self { + ChatCompletionRequestUserMessageContent::Text(value.into()).into() + } +} + +impl From for ChatCompletionRequestUserMessage { + fn from(value: String) -> Self { + value.as_str().into() + } +} + +impl From<&str> for ChatCompletionRequestSystemMessage { + fn from(value: &str) -> Self { + ChatCompletionRequestSystemMessageContent::Text(value.into()).into() + } +} + +impl From<&str> for ChatCompletionRequestDeveloperMessage { + fn from(value: &str) -> Self { + ChatCompletionRequestDeveloperMessageContent::Text(value.into()).into() + } +} + +impl From for ChatCompletionRequestSystemMessage { + fn from(value: String) -> Self { + value.as_str().into() + } +} + +impl From for ChatCompletionRequestDeveloperMessage { + fn from(value: String) -> Self { + value.as_str().into() + } +} + +impl From<&str> for ChatCompletionRequestAssistantMessage { + fn from(value: &str) -> Self { + ChatCompletionRequestAssistantMessageContent::Text(value.into()).into() + } +} + +impl From for ChatCompletionRequestAssistantMessage { + fn from(value: String) -> Self { + value.as_str().into() + } +} + +impl From> + for ChatCompletionRequestUserMessageContent +{ + fn from(value: Vec) -> Self { + ChatCompletionRequestUserMessageContent::Array(value) + } +} + +impl From + for ChatCompletionRequestUserMessageContentPart +{ + fn from(value: ChatCompletionRequestMessageContentPartText) -> Self { + ChatCompletionRequestUserMessageContentPart::Text(value) + } +} + +impl From + for ChatCompletionRequestUserMessageContentPart +{ + fn from(value: ChatCompletionRequestMessageContentPartImage) -> Self { + ChatCompletionRequestUserMessageContentPart::ImageUrl(value) + } +} + +impl From + for ChatCompletionRequestUserMessageContentPart +{ + fn from(value: ChatCompletionRequestMessageContentPartAudio) -> Self { + ChatCompletionRequestUserMessageContentPart::InputAudio(value) + } +} + +impl From<&str> for ChatCompletionRequestMessageContentPartText { + fn from(value: &str) -> Self { + ChatCompletionRequestMessageContentPartText { text: value.into() } + } +} + +impl From for ChatCompletionRequestMessageContentPartText { + fn from(value: String) -> Self { + ChatCompletionRequestMessageContentPartText { text: value } + } +} + +impl From<&str> for ImageUrl { + fn from(value: &str) -> Self { + Self { + url: value.into(), + detail: Default::default(), + } + } +} + +impl From for ImageUrl { + fn from(value: String) -> Self { + Self { + url: value, + detail: Default::default(), + } + } +} + +impl From for CreateMessageRequestContent { + fn from(value: String) -> Self { + Self::Content(value) + } +} + +impl From<&str> for CreateMessageRequestContent { + fn from(value: &str) -> Self { + Self::Content(value.to_string()) + } +} + +impl Default for ChatCompletionRequestUserMessageContent { + fn default() -> Self { + ChatCompletionRequestUserMessageContent::Text("".into()) + } +} + +impl Default for CreateMessageRequestContent { + fn default() -> Self { + Self::Content("".into()) + } +} + +impl Default for ChatCompletionRequestDeveloperMessageContent { + fn default() -> Self { + ChatCompletionRequestDeveloperMessageContent::Text("".into()) + } +} + +impl Default for ChatCompletionRequestSystemMessageContent { + fn default() -> Self { + ChatCompletionRequestSystemMessageContent::Text("".into()) + } +} + +impl Default for ChatCompletionRequestToolMessageContent { + fn default() -> Self { + ChatCompletionRequestToolMessageContent::Text("".into()) + } +} + +// start: types to multipart from + +impl AsyncTryFrom for reqwest::multipart::Form { + type Error = OpenAIError; + + async fn try_from(request: CreateTranscriptionRequest) -> Result { + let audio_part = create_file_part(request.file.source).await?; + + let mut form = reqwest::multipart::Form::new() + .part("file", audio_part) + .text("model", request.model); + + if let Some(prompt) = request.prompt { + form = form.text("prompt", prompt); + } + + if let Some(response_format) = request.response_format { + form = form.text("response_format", response_format.to_string()) + } + + if let Some(temperature) = request.temperature { + form = form.text("temperature", temperature.to_string()) + } + + if let Some(language) = request.language { + form = form.text("language", language); + } + + if let Some(timestamp_granularities) = request.timestamp_granularities { + for tg in timestamp_granularities { + form = form.text("timestamp_granularities[]", tg.to_string()); + } + } + + Ok(form) + } +} + +impl AsyncTryFrom for reqwest::multipart::Form { + type Error = OpenAIError; + + async fn try_from(request: CreateTranslationRequest) -> Result { + let audio_part = create_file_part(request.file.source).await?; + + let mut form = reqwest::multipart::Form::new() + .part("file", audio_part) + .text("model", request.model); + + if let Some(prompt) = request.prompt { + form = form.text("prompt", prompt); + } + + if let Some(response_format) = request.response_format { + form = form.text("response_format", response_format.to_string()) + } + + if let Some(temperature) = request.temperature { + form = form.text("temperature", temperature.to_string()) + } + Ok(form) + } +} + +impl AsyncTryFrom for reqwest::multipart::Form { + type Error = OpenAIError; + + async fn try_from(request: CreateImageEditRequest) -> Result { + let image_part = create_file_part(request.image.source).await?; + + let mut form = reqwest::multipart::Form::new() + .part("image", image_part) + .text("prompt", request.prompt); + + if let Some(mask) = request.mask { + let mask_part = create_file_part(mask.source).await?; + form = form.part("mask", mask_part); + } + + if let Some(model) = request.model { + form = form.text("model", model.to_string()) + } + + if request.n.is_some() { + form = form.text("n", request.n.unwrap().to_string()) + } + + if request.size.is_some() { + form = form.text("size", request.size.unwrap().to_string()) + } + + if request.response_format.is_some() { + form = form.text( + "response_format", + request.response_format.unwrap().to_string(), + ) + } + + if request.user.is_some() { + form = form.text("user", request.user.unwrap()) + } + Ok(form) + } +} + +impl AsyncTryFrom for reqwest::multipart::Form { + type Error = OpenAIError; + + async fn try_from(request: CreateImageVariationRequest) -> Result { + let image_part = create_file_part(request.image.source).await?; + + let mut form = reqwest::multipart::Form::new().part("image", image_part); + + if let Some(model) = request.model { + form = form.text("model", model.to_string()) + } + + if request.n.is_some() { + form = form.text("n", request.n.unwrap().to_string()) + } + + if request.size.is_some() { + form = form.text("size", request.size.unwrap().to_string()) + } + + if request.response_format.is_some() { + form = form.text( + "response_format", + request.response_format.unwrap().to_string(), + ) + } + + if request.user.is_some() { + form = form.text("user", request.user.unwrap()) + } + Ok(form) + } +} + +impl AsyncTryFrom for reqwest::multipart::Form { + type Error = OpenAIError; + + async fn try_from(request: CreateFileRequest) -> Result { + let file_part = create_file_part(request.file.source).await?; + let form = reqwest::multipart::Form::new() + .part("file", file_part) + .text("purpose", request.purpose.to_string()); + Ok(form) + } +} + +impl AsyncTryFrom for reqwest::multipart::Form { + type Error = OpenAIError; + + async fn try_from(request: AddUploadPartRequest) -> Result { + let file_part = create_file_part(request.data).await?; + let form = reqwest::multipart::Form::new().part("data", file_part); + Ok(form) + } +} + +// end: types to multipart form + +impl Default for Input { + fn default() -> Self { + Self::Text("".to_string()) + } +} + +impl Default for InputContent { + fn default() -> Self { + Self::TextInput("".to_string()) + } +} + +impl From for Input { + fn from(value: String) -> Self { + Input::Text(value) + } +} + +impl From<&str> for Input { + fn from(value: &str) -> Self { + Input::Text(value.to_owned()) + } +} + +impl Default for ResponsesRole { + fn default() -> Self { + Self::User + } +} + +impl From for InputContent { + fn from(value: String) -> Self { + Self::TextInput(value) + } +} + +impl From<&str> for InputContent { + fn from(value: &str) -> Self { + Self::TextInput(value.to_owned()) + } +} + +impl Default for CodeInterpreterContainer { + fn default() -> Self { + CodeInterpreterContainer::Id("".to_string()) + } +} diff --git a/async-openai/src/types/invites.rs b/async-openai/src/types/invites.rs new file mode 100644 index 00000000..282b3e02 --- /dev/null +++ b/async-openai/src/types/invites.rs @@ -0,0 +1,62 @@ +use crate::types::OpenAIError; +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use super::OrganizationRole; + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum InviteStatus { + Accepted, + Expired, + Pending, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Builder)] +#[builder(name = "InviteRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option))] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct InviteRequest { + pub email: String, + pub role: OrganizationRole, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct InviteListResponse { + pub object: String, + pub data: Vec, + pub first_id: Option, + pub last_id: Option, + pub has_more: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct InviteDeleteResponse { + /// The object type, which is always `organization.invite.deleted` + pub object: String, + pub id: String, + pub deleted: bool, +} + +/// Represents an individual `invite` to the organization. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct Invite { + /// The object type, which is always `organization.invite` + pub object: String, + /// The identifier, which can be referenced in API endpoints + pub id: String, + /// The email address of the individual to whom the invite was sent + pub email: String, + /// `owner` or `reader` + pub role: OrganizationRole, + /// `accepted`, `expired`, or `pending` + pub status: InviteStatus, + /// The Unix timestamp (in seconds) of when the invite was sent. + pub invited_at: u32, + /// The Unix timestamp (in seconds) of when the invite expires. + pub expires_at: u32, + /// The Unix timestamp (in seconds) of when the invite was accepted. + pub accepted_at: Option, +} diff --git a/async-openai/src/types/message.rs b/async-openai/src/types/message.rs new file mode 100644 index 00000000..af79ccc1 --- /dev/null +++ b/async-openai/src/types/message.rs @@ -0,0 +1,355 @@ +use std::collections::HashMap; + +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +use super::{ImageDetail, ImageUrl}; + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum MessageRole { + #[default] + User, + Assistant, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum MessageStatus { + InProgress, + Incomplete, + Completed, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum MessageIncompleteDetailsType { + ContentFilter, + MaxTokens, + RunCancelled, + RunExpired, + RunFailed, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageIncompleteDetails { + /// The reason the message is incomplete. + pub reason: MessageIncompleteDetailsType, +} + +/// Represents a message within a [thread](https://platform.openai.com/docs/api-reference/threads). +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageObject { + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `thread.message`. + pub object: String, + /// The Unix timestamp (in seconds) for when the message was created. + pub created_at: i32, + /// The [thread](https://platform.openai.com/docs/api-reference/threads) ID that this message belongs to. + pub thread_id: String, + + /// The status of the message, which can be either `in_progress`, `incomplete`, or `completed`. + pub status: Option, + + /// On an incomplete message, details about why the message is incomplete. + pub incomplete_details: Option, + + /// The Unix timestamp (in seconds) for when the message was completed. + pub completed_at: Option, + + /// The Unix timestamp (in seconds) for when the message was marked as incomplete. + pub incomplete_at: Option, + + /// The entity that produced the message. One of `user` or `assistant`. + pub role: MessageRole, + + /// The content of the message in array of text and/or images. + pub content: Vec, + + /// If applicable, the ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) that authored this message. + pub assistant_id: Option, + + /// The ID of the [run](https://platform.openai.com/docs/api-reference/runs) associated with the creation of this message. Value is `null` when messages are created manually using the create message or create thread endpoints. + pub run_id: Option, + + /// A list of files attached to the message, and the tools they were added to. + pub attachments: Option>, + + pub metadata: Option>, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageAttachment { + /// The ID of the file to attach to the message. + pub file_id: String, + /// The tools to add this file to. + pub tools: Vec, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum MessageAttachmentTool { + CodeInterpreter, + FileSearch, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum MessageContent { + Text(MessageContentTextObject), + ImageFile(MessageContentImageFileObject), + ImageUrl(MessageContentImageUrlObject), + Refusal(MessageContentRefusalObject), +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageContentRefusalObject { + pub refusal: String, +} + +/// The text content that is part of a message. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageContentTextObject { + pub text: TextData, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct TextData { + /// The data that makes up the text. + pub value: String, + pub annotations: Vec, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum MessageContentTextAnnotations { + /// A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "retrieval" tool to search files. + FileCitation(MessageContentTextAnnotationsFileCitationObject), + /// A URL for the file that's generated when the assistant used the `code_interpreter` tool to generate a file. + FilePath(MessageContentTextAnnotationsFilePathObject), +} + +/// A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "file_search" tool to search files. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageContentTextAnnotationsFileCitationObject { + /// The text in the message content that needs to be replaced. + pub text: String, + pub file_citation: FileCitation, + pub start_index: u32, + pub end_index: u32, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct FileCitation { + /// The ID of the specific File the citation is from. + pub file_id: String, + /// The specific quote in the file. + pub quote: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageContentTextAnnotationsFilePathObject { + /// The text in the message content that needs to be replaced. + pub text: String, + pub file_path: FilePath, + pub start_index: u32, + pub end_index: u32, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct FilePath { + /// The ID of the file that was generated. + pub file_id: String, +} + +/// References an image [File](https://platform.openai.com/docs/api-reference/files) in the content of a message. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageContentImageFileObject { + pub image_file: ImageFile, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct ImageFile { + /// The [File](https://platform.openai.com/docs/api-reference/files) ID of the image in the message content. Set `purpose="vision"` when uploading the File if you need to later display the file content. + pub file_id: String, + /// Specifies the detail level of the image if specified by the user. `low` uses fewer tokens, you can opt in to high resolution using `high`. + pub detail: Option, +} + +/// References an image URL in the content of a message. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageContentImageUrlObject { + pub image_url: ImageUrl, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageRequestContentTextObject { + /// Text content to be sent to the model + pub text: String, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(untagged)] +pub enum CreateMessageRequestContent { + /// The text contents of the message. + Content(String), + /// An array of content parts with a defined type, each can be of type `text` or images can be passed with `image_url` or `image_file`. Image types are only supported on [Vision-compatible models](https://platform.openai.com/docs/models/overview). + ContentArray(Vec), +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum MessageContentInput { + Text(MessageRequestContentTextObject), + ImageFile(MessageContentImageFileObject), + ImageUrl(MessageContentImageUrlObject), +} +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "CreateMessageRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateMessageRequest { + /// The role of the entity that is creating the message. Allowed values include: + /// - `user`: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages. + /// - `assistant`: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation. + pub role: MessageRole, + /// The content of the message. + pub content: CreateMessageRequestContent, + + /// A list of files attached to the message, and the tools they should be added to. + pub attachments: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct ModifyMessageRequest { + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct DeleteMessageResponse { + pub id: String, + pub deleted: bool, + pub object: String, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct ListMessagesResponse { + pub object: String, + pub data: Vec, + pub first_id: Option, + pub last_id: Option, + pub has_more: bool, +} + +/// Represents a message delta i.e. any changed fields on a message during streaming. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDeltaObject { + /// The identifier of the message, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `thread.message.delta`. + pub object: String, + /// The delta containing the fields that have changed on the Message. + pub delta: MessageDelta, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDelta { + /// The entity that produced the message. One of `user` or `assistant`. + pub role: Option, + /// The content of the message in array of text and/or images. + pub content: Option>, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum MessageDeltaContent { + ImageFile(MessageDeltaContentImageFileObject), + ImageUrl(MessageDeltaContentImageUrlObject), + Text(MessageDeltaContentTextObject), + Refusal(MessageDeltaContentRefusalObject), +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDeltaContentRefusalObject { + /// The index of the refusal part in the message. + pub index: i32, + pub refusal: Option, +} + +/// The text content that is part of a message. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDeltaContentTextObject { + /// The index of the content part in the message. + pub index: u32, + pub text: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDeltaContentText { + /// The data that makes up the text. + pub value: Option, + pub annotations: Option>, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum MessageDeltaContentTextAnnotations { + FileCitation(MessageDeltaContentTextAnnotationsFileCitationObject), + FilePath(MessageDeltaContentTextAnnotationsFilePathObject), +} + +/// A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "file_search" tool to search files. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDeltaContentTextAnnotationsFileCitationObject { + /// The index of the annotation in the text content part. + pub index: u32, + /// The text in the message content that needs to be replaced. + pub text: Option, + pub file_citation: Option, + pub start_index: Option, + pub end_index: Option, +} + +/// A URL for the file that's generated when the assistant used the `code_interpreter` tool to generate a file. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDeltaContentTextAnnotationsFilePathObject { + /// The index of the annotation in the text content part. + pub index: u32, + /// The text in the message content that needs to be replaced. + pub text: Option, + pub file_path: Option, + pub start_index: Option, + pub end_index: Option, +} + +/// References an image [File](https://platform.openai.com/docs/api-reference/files) in the content of a message. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDeltaContentImageFileObject { + /// The index of the content part in the message. + pub index: u32, + + pub image_file: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageDeltaContentImageUrlObject { + /// The index of the content part in the message. + pub index: u32, + + pub image_url: Option, +} diff --git a/async-openai/src/types/mod.rs b/async-openai/src/types/mod.rs new file mode 100644 index 00000000..4b8ccb6f --- /dev/null +++ b/async-openai/src/types/mod.rs @@ -0,0 +1,71 @@ +//! Types used in OpenAI API requests and responses. +//! These types are created from component schemas in the [OpenAPI spec](https://github.com/openai/openai-openapi) +mod assistant; +mod assistant_impls; +mod assistant_stream; +mod audio; +mod audit_log; +mod batch; +mod chat; +mod common; +mod completion; +mod embedding; +mod file; +mod fine_tuning; +mod image; +mod invites; +mod message; +mod model; +mod moderation; +mod project_api_key; +mod project_service_account; +mod project_users; +mod projects; +#[cfg_attr(docsrs, doc(cfg(feature = "realtime")))] +#[cfg(feature = "realtime")] +pub mod realtime; +pub mod responses; +mod run; +mod step; +mod thread; +mod upload; +mod users; +mod vector_store; + +pub use assistant::*; +pub use assistant_stream::*; +pub use audio::*; +pub use audit_log::*; +pub use batch::*; +pub use chat::*; +pub use common::*; +pub use completion::*; +pub use embedding::*; +pub use file::*; +pub use fine_tuning::*; +pub use image::*; +pub use invites::*; +pub use message::*; +pub use model::*; +pub use moderation::*; +pub use project_api_key::*; +pub use project_service_account::*; +pub use project_users::*; +pub use projects::*; +pub use run::*; +pub use step::*; +pub use thread::*; +pub use upload::*; +pub use users::*; +pub use vector_store::*; + +mod impls; +use derive_builder::UninitializedFieldError; + +use crate::error::OpenAIError; + +impl From for OpenAIError { + fn from(value: UninitializedFieldError) -> Self { + OpenAIError::InvalidArgument(value.to_string()) + } +} diff --git a/async-openai/src/types/model.rs b/async-openai/src/types/model.rs new file mode 100644 index 00000000..034213a6 --- /dev/null +++ b/async-openai/src/types/model.rs @@ -0,0 +1,27 @@ +use serde::{Deserialize, Serialize}; + +/// Describes an OpenAI model offering that can be used with the API. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct Model { + /// The model identifier, which can be referenced in the API endpoints. + pub id: String, + /// The object type, which is always "model". + pub object: String, + /// The Unix timestamp (in seconds) when the model was created. + pub created: u32, + /// The organization that owns the model. + pub owned_by: String, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct ListModelResponse { + pub object: String, + pub data: Vec, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct DeleteModelResponse { + pub id: String, + pub object: String, + pub deleted: bool, +} diff --git a/async-openai/src/types/moderation.rs b/async-openai/src/types/moderation.rs new file mode 100644 index 00000000..f8c1c0ff --- /dev/null +++ b/async-openai/src/types/moderation.rs @@ -0,0 +1,227 @@ +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +#[derive(Debug, Serialize, Clone, PartialEq, Deserialize)] +#[serde(untagged)] +pub enum ModerationInput { + /// A single string of text to classify for moderation + String(String), + + /// An array of strings to classify for moderation + StringArray(Vec), + + /// An array of multi-modal inputs to the moderation model + MultiModal(Vec), +} + +/// Content part for multi-modal moderation input +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type")] +pub enum ModerationContentPart { + /// An object describing text to classify + #[serde(rename = "text")] + Text { + /// A string of text to classify + text: String, + }, + + /// An object describing an image to classify + #[serde(rename = "image_url")] + ImageUrl { + /// Contains either an image URL or a data URL for a base64 encoded image + image_url: ModerationImageUrl, + }, +} + +/// Image URL configuration for image moderation +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ModerationImageUrl { + /// Either a URL of the image or the base64 encoded image data + pub url: String, +} + +#[derive(Debug, Default, Clone, Serialize, Builder, PartialEq, Deserialize)] +#[builder(name = "CreateModerationRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateModerationRequest { + /// Input (or inputs) to classify. Can be a single string, an array of strings, or + /// an array of multi-modal input objects similar to other models. + pub input: ModerationInput, + + /// The content moderation model you would like to use. Learn more in the + /// [moderation guide](https://platform.openai.com/docs/guides/moderation), and learn about + /// available models [here](https://platform.openai.com/docs/models/moderation). + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct Category { + /// Content that expresses, incites, or promotes hate based on race, gender, + /// ethnicity, religion, nationality, sexual orientation, disability status, or + /// caste. Hateful content aimed at non-protected groups (e.g., chess players) + /// is harrassment. + pub hate: bool, + #[serde(rename = "hate/threatening")] + /// Hateful content that also includes violence or serious harm towards the + /// targeted group based on race, gender, ethnicity, religion, nationality, + /// sexual orientation, disability status, or caste. + pub hate_threatening: bool, + /// Content that expresses, incites, or promotes harassing language towards any target. + pub harassment: bool, + /// Harassment content that also includes violence or serious harm towards any target. + #[serde(rename = "harassment/threatening")] + pub harassment_threatening: bool, + /// Content that includes instructions or advice that facilitate the planning or execution of wrongdoing, or that gives advice or instruction on how to commit illicit acts. For example, "how to shoplift" would fit this category. + pub illicit: bool, + /// Content that includes instructions or advice that facilitate the planning or execution of wrongdoing that also includes violence, or that gives advice or instruction on the procurement of any weapon. + #[serde(rename = "illicit/violent")] + pub illicit_violent: bool, + /// Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders. + #[serde(rename = "self-harm")] + pub self_harm: bool, + /// Content where the speaker expresses that they are engaging or intend to engage in acts of self-harm, such as suicide, cutting, and eating disorders. + #[serde(rename = "self-harm/intent")] + pub self_harm_intent: bool, + /// Content that encourages performing acts of self-harm, such as suicide, cutting, and eating disorders, or that gives instructions or advice on how to commit such acts. + #[serde(rename = "self-harm/instructions")] + pub self_harm_instructions: bool, + /// Content meant to arouse sexual excitement, such as the description of sexual activity, or that promotes sexual services (excluding sex education and wellness). + pub sexual: bool, + /// Sexual content that includes an individual who is under 18 years old. + #[serde(rename = "sexual/minors")] + pub sexual_minors: bool, + /// Content that depicts death, violence, or physical injury. + pub violence: bool, + /// Content that depicts death, violence, or physical injury in graphic detail. + #[serde(rename = "violence/graphic")] + pub violence_graphic: bool, +} + +/// A list of the categories along with their scores as predicted by model. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct CategoryScore { + /// The score for the category 'hate'. + pub hate: f32, + /// The score for the category 'hate/threatening'. + #[serde(rename = "hate/threatening")] + pub hate_threatening: f32, + /// The score for the category 'harassment'. + pub harassment: f32, + /// The score for the category 'harassment/threatening'. + #[serde(rename = "harassment/threatening")] + pub harassment_threatening: f32, + /// The score for the category 'illicit'. + pub illicit: f32, + /// The score for the category 'illicit/violent'. + #[serde(rename = "illicit/violent")] + pub illicit_violent: f32, + /// The score for the category 'self-harm'. + #[serde(rename = "self-harm")] + pub self_harm: f32, + /// The score for the category 'self-harm/intent'. + #[serde(rename = "self-harm/intent")] + pub self_harm_intent: f32, + /// The score for the category 'self-harm/instructions'. + #[serde(rename = "self-harm/instructions")] + pub self_harm_instructions: f32, + /// The score for the category 'sexual'. + pub sexual: f32, + /// The score for the category 'sexual/minors'. + #[serde(rename = "sexual/minors")] + pub sexual_minors: f32, + /// The score for the category 'violence'. + pub violence: f32, + /// The score for the category 'violence/graphic'. + #[serde(rename = "violence/graphic")] + pub violence_graphic: f32, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct ContentModerationResult { + /// Whether any of the below categories are flagged. + pub flagged: bool, + /// A list of the categories, and whether they are flagged or not. + pub categories: Category, + /// A list of the categories along with their scores as predicted by model. + pub category_scores: CategoryScore, + /// A list of the categories along with the input type(s) that the score applies to. + pub category_applied_input_types: CategoryAppliedInputTypes, +} + +/// Represents if a given text input is potentially harmful. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct CreateModerationResponse { + /// The unique identifier for the moderation request. + pub id: String, + /// The model used to generate the moderation results. + pub model: String, + /// A list of moderation objects. + pub results: Vec, +} + +/// A list of the categories along with the input type(s) that the score applies to. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct CategoryAppliedInputTypes { + /// The applied input type(s) for the category 'hate'. + pub hate: Vec, + + /// The applied input type(s) for the category 'hate/threatening'. + #[serde(rename = "hate/threatening")] + pub hate_threatening: Vec, + + /// The applied input type(s) for the category 'harassment'. + pub harassment: Vec, + + /// The applied input type(s) for the category 'harassment/threatening'. + #[serde(rename = "harassment/threatening")] + pub harassment_threatening: Vec, + + /// The applied input type(s) for the category 'illicit'. + pub illicit: Vec, + + /// The applied input type(s) for the category 'illicit/violent'. + #[serde(rename = "illicit/violent")] + pub illicit_violent: Vec, + + /// The applied input type(s) for the category 'self-harm'. + #[serde(rename = "self-harm")] + pub self_harm: Vec, + + /// The applied input type(s) for the category 'self-harm/intent'. + #[serde(rename = "self-harm/intent")] + pub self_harm_intent: Vec, + + /// The applied input type(s) for the category 'self-harm/instructions'. + #[serde(rename = "self-harm/instructions")] + pub self_harm_instructions: Vec, + + /// The applied input type(s) for the category 'sexual'. + pub sexual: Vec, + + /// The applied input type(s) for the category 'sexual/minors'. + #[serde(rename = "sexual/minors")] + pub sexual_minors: Vec, + + /// The applied input type(s) for the category 'violence'. + pub violence: Vec, + + /// The applied input type(s) for the category 'violence/graphic'. + #[serde(rename = "violence/graphic")] + pub violence_graphic: Vec, +} + +/// The type of input that was moderated +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ModInputType { + /// Text content that was moderated + Text, + /// Image content that was moderated + Image, +} diff --git a/async-openai/src/types/project_api_key.rs b/async-openai/src/types/project_api_key.rs new file mode 100644 index 00000000..96886581 --- /dev/null +++ b/async-openai/src/types/project_api_key.rs @@ -0,0 +1,64 @@ +use serde::{Deserialize, Serialize}; + +use super::{ProjectServiceAccount, ProjectUser}; + +/// Represents an individual API key in a project. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectApiKey { + /// The object type, which is always `organization.project.api_key`. + pub object: String, + /// The redacted value of the API key. + pub redacted_value: String, + /// The name of the API key. + pub name: String, + /// The Unix timestamp (in seconds) of when the API key was created. + pub created_at: u32, + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The owner of the API key. + pub owner: ProjectApiKeyOwner, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename = "snake_case")] +pub enum ProjectApiKeyOwnerType { + User, + ServiceAccount, +} + +/// Represents the owner of a project API key. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectApiKeyOwner { + /// The type of owner, which is either `user` or `service_account`. + pub r#type: ProjectApiKeyOwnerType, + /// The user owner of the API key, if applicable. + pub user: Option, + /// The service account owner of the API key, if applicable. + pub service_account: Option, +} + +/// Represents the response object for listing project API keys. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectApiKeyListResponse { + /// The object type, which is always `list`. + pub object: String, + /// The list of project API keys. + pub data: Vec, + /// The ID of the first project API key in the list. + pub first_id: String, + /// The ID of the last project API key in the list. + pub last_id: String, + /// Indicates if there are more project API keys available. + pub has_more: bool, +} + +/// Represents the response object for deleting a project API key. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectApiKeyDeleteResponse { + /// The object type, which is always `organization.project.api_key.deleted`. + pub object: String, + /// The ID of the deleted API key. + pub id: String, + /// Indicates if the API key was successfully deleted. + pub deleted: bool, +} diff --git a/async-openai/src/types/project_service_account.rs b/async-openai/src/types/project_service_account.rs new file mode 100644 index 00000000..4449ddf8 --- /dev/null +++ b/async-openai/src/types/project_service_account.rs @@ -0,0 +1,83 @@ +use serde::{Deserialize, Serialize}; + +use super::ProjectUserRole; + +/// Represents an individual service account in a project. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectServiceAccount { + /// The object type, which is always `organization.project.service_account`. + pub object: String, + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The name of the service account. + pub name: String, + /// `owner` or `member`. + pub role: ProjectUserRole, + /// The Unix timestamp (in seconds) of when the service account was created. + pub created_at: u32, +} + +/// Represents the response object for listing project service accounts. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectServiceAccountListResponse { + /// The object type, which is always `list`. + pub object: String, + /// The list of project service accounts. + pub data: Vec, + /// The ID of the first project service account in the list. + pub first_id: String, + /// The ID of the last project service account in the list. + pub last_id: String, + /// Indicates if there are more project service accounts available. + pub has_more: bool, +} + +/// Represents the request object for creating a project service account. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectServiceAccountCreateRequest { + /// The name of the service account being created. + pub name: String, +} + +/// Represents the response object for creating a project service account. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectServiceAccountCreateResponse { + /// The object type, which is always `organization.project.service_account`. + pub object: String, + /// The ID of the created service account. + pub id: String, + /// The name of the created service account. + pub name: String, + /// Service accounts can only have one role of type `member`. + pub role: String, + /// The Unix timestamp (in seconds) of when the service account was created. + pub created_at: u32, + /// The API key associated with the created service account. + pub api_key: ProjectServiceAccountApiKey, +} + +/// Represents the API key associated with a project service account. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectServiceAccountApiKey { + /// The object type, which is always `organization.project.service_account.api_key`. + pub object: String, + /// The value of the API key. + pub value: String, + /// The name of the API key. + pub name: String, + /// The Unix timestamp (in seconds) of when the API key was created. + pub created_at: u32, + /// The ID of the API key. + pub id: String, +} + +/// Represents the response object for deleting a project service account. +#[derive(Debug, Serialize, Deserialize)] +pub struct ProjectServiceAccountDeleteResponse { + /// The object type, which is always `organization.project.service_account.deleted`. + pub object: String, + /// The ID of the deleted service account. + pub id: String, + /// Indicates if the service account was successfully deleted. + pub deleted: bool, +} diff --git a/async-openai/src/types/project_users.rs b/async-openai/src/types/project_users.rs new file mode 100644 index 00000000..5bedd26a --- /dev/null +++ b/async-openai/src/types/project_users.rs @@ -0,0 +1,69 @@ +use crate::types::OpenAIError; +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +/// Represents an individual user in a project. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ProjectUser { + /// The object type, which is always `organization.project.user` + pub object: String, + /// The identifier, which can be referenced in API endpoints + pub id: String, + /// The name of the project + pub name: String, + /// The email address of the user + pub email: String, + /// `owner` or `member` + pub role: ProjectUserRole, + /// The Unix timestamp (in seconds) of when the project was added. + pub added_at: u32, +} + +/// `owner` or `member` +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ProjectUserRole { + Owner, + Member, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ProjectUserListResponse { + pub object: String, + pub data: Vec, + pub first_id: String, + pub last_id: String, + pub has_more: String, +} + +/// The project user create request payload. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Builder)] +#[builder(name = "ProjectUserCreateRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option))] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ProjectUserCreateRequest { + /// The ID of the user. + pub user_id: String, + /// `owner` or `member` + pub role: ProjectUserRole, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Builder)] +#[builder(name = "ProjectUserUpdateRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option))] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ProjectUserUpdateRequest { + /// `owner` or `member` + pub role: ProjectUserRole, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ProjectUserDeleteResponse { + pub object: String, + pub id: String, + pub deleted: bool, +} diff --git a/async-openai/src/types/projects.rs b/async-openai/src/types/projects.rs new file mode 100644 index 00000000..bb5ae3ff --- /dev/null +++ b/async-openai/src/types/projects.rs @@ -0,0 +1,62 @@ +use crate::types::OpenAIError; +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +/// `active` or `archived` +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ProjectStatus { + Active, + Archived, +} + +/// Represents an individual project. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct Project { + /// The identifier, which can be referenced in API endpoints + pub id: String, + /// The object type, which is always `organization.project` + pub object: String, + /// The name of the project. This appears in reporting. + pub name: String, + /// The Unix timestamp (in seconds) of when the project was created. + pub created_at: u32, + /// The Unix timestamp (in seconds) of when the project was archived or `null`. + pub archived_at: Option, + /// `active` or `archived` + pub status: ProjectStatus, +} + +/// A list of Project objects. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ProjectListResponse { + pub object: String, + pub data: Vec, + pub first_id: String, + pub last_id: String, + pub has_more: String, +} + +/// The project create request payload. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Builder)] +#[builder(name = "ProjectCreateRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option))] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ProjectCreateRequest { + /// The friendly name of the project, this name appears in reports. + pub name: String, +} + +/// The project update request payload. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Builder)] +#[builder(name = "ProjectUpdateRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option))] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ProjectUpdateRequest { + /// The updated name of the project, this name appears in reports. + pub name: String, +} diff --git a/async-openai/src/types/realtime/client_event.rs b/async-openai/src/types/realtime/client_event.rs new file mode 100644 index 00000000..87ff7010 --- /dev/null +++ b/async-openai/src/types/realtime/client_event.rs @@ -0,0 +1,220 @@ +use serde::{Deserialize, Serialize}; +use tokio_tungstenite::tungstenite::Message; + +use super::{item::Item, session_resource::SessionResource}; + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct SessionUpdateEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, + /// Session configuration to update. + pub session: SessionResource, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct InputAudioBufferAppendEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, + /// Base64-encoded audio bytes. + pub audio: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct InputAudioBufferCommitEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct InputAudioBufferClearEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ConversationItemCreateEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, + + /// The ID of the preceding item after which the new item will be inserted. + #[serde(skip_serializing_if = "Option::is_none")] + pub previous_item_id: Option, + + /// The item to add to the conversation. + pub item: Item, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct ConversationItemTruncateEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, + + /// The ID of the assistant message item to truncate. + pub item_id: String, + + /// The index of the content part to truncate. + pub content_index: u32, + + /// Inclusive duration up to which audio is truncated, in milliseconds. + pub audio_end_ms: u32, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct ConversationItemDeleteEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, + + /// The ID of the item to delete. + pub item_id: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct ResponseCreateEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, + + /// Configuration for the response. + pub response: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct ResponseCancelEvent { + /// Optional client-generated ID used to identify this event. + #[serde(skip_serializing_if = "Option::is_none")] + pub event_id: Option, +} + +/// These are events that the OpenAI Realtime WebSocket server will accept from the client. +#[derive(Debug, Serialize, Deserialize)] +#[serde(tag = "type")] +pub enum ClientEvent { + /// Send this event to update the session’s default configuration. + #[serde(rename = "session.update")] + SessionUpdate(SessionUpdateEvent), + + /// Send this event to append audio bytes to the input audio buffer. + #[serde(rename = "input_audio_buffer.append")] + InputAudioBufferAppend(InputAudioBufferAppendEvent), + + /// Send this event to commit audio bytes to a user message. + #[serde(rename = "input_audio_buffer.commit")] + InputAudioBufferCommit(InputAudioBufferCommitEvent), + + /// Send this event to clear the audio bytes in the buffer. + #[serde(rename = "input_audio_buffer.clear")] + InputAudioBufferClear(InputAudioBufferClearEvent), + + /// Send this event when adding an item to the conversation. + #[serde(rename = "conversation.item.create")] + ConversationItemCreate(ConversationItemCreateEvent), + + /// Send this event when you want to truncate a previous assistant message’s audio. + #[serde(rename = "conversation.item.truncate")] + ConversationItemTruncate(ConversationItemTruncateEvent), + + /// Send this event when you want to remove any item from the conversation history. + #[serde(rename = "conversation.item.delete")] + ConversationItemDelete(ConversationItemDeleteEvent), + + /// Send this event to trigger a response generation. + #[serde(rename = "response.create")] + ResponseCreate(ResponseCreateEvent), + + /// Send this event to cancel an in-progress response. + #[serde(rename = "response.cancel")] + ResponseCancel(ResponseCancelEvent), +} + +impl From<&ClientEvent> for String { + fn from(value: &ClientEvent) -> Self { + serde_json::to_string(value).unwrap() + } +} + +impl From for Message { + fn from(value: ClientEvent) -> Self { + Message::Text(String::from(&value).into()) + } +} + +macro_rules! message_from_event { + ($from_typ:ty, $evt_typ:ty) => { + impl From<$from_typ> for Message { + fn from(value: $from_typ) -> Self { + Self::from(<$evt_typ>::from(value)) + } + } + }; +} + +macro_rules! event_from { + ($from_typ:ty, $evt_typ:ty, $variant:ident) => { + impl From<$from_typ> for $evt_typ { + fn from(value: $from_typ) -> Self { + <$evt_typ>::$variant(value) + } + } + }; +} + +event_from!(SessionUpdateEvent, ClientEvent, SessionUpdate); +event_from!( + InputAudioBufferAppendEvent, + ClientEvent, + InputAudioBufferAppend +); +event_from!( + InputAudioBufferCommitEvent, + ClientEvent, + InputAudioBufferCommit +); +event_from!( + InputAudioBufferClearEvent, + ClientEvent, + InputAudioBufferClear +); +event_from!( + ConversationItemCreateEvent, + ClientEvent, + ConversationItemCreate +); +event_from!( + ConversationItemTruncateEvent, + ClientEvent, + ConversationItemTruncate +); +event_from!( + ConversationItemDeleteEvent, + ClientEvent, + ConversationItemDelete +); +event_from!(ResponseCreateEvent, ClientEvent, ResponseCreate); +event_from!(ResponseCancelEvent, ClientEvent, ResponseCancel); + +message_from_event!(SessionUpdateEvent, ClientEvent); +message_from_event!(InputAudioBufferAppendEvent, ClientEvent); +message_from_event!(InputAudioBufferCommitEvent, ClientEvent); +message_from_event!(InputAudioBufferClearEvent, ClientEvent); +message_from_event!(ConversationItemCreateEvent, ClientEvent); +message_from_event!(ConversationItemTruncateEvent, ClientEvent); +message_from_event!(ConversationItemDeleteEvent, ClientEvent); +message_from_event!(ResponseCreateEvent, ClientEvent); +message_from_event!(ResponseCancelEvent, ClientEvent); + +impl From for ConversationItemCreateEvent { + fn from(value: Item) -> Self { + Self { + event_id: None, + previous_item_id: None, + item: value, + } + } +} diff --git a/async-openai/src/types/realtime/content_part.rs b/async-openai/src/types/realtime/content_part.rs new file mode 100644 index 00000000..eec93ab3 --- /dev/null +++ b/async-openai/src/types/realtime/content_part.rs @@ -0,0 +1,18 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(tag = "type")] +pub enum ContentPart { + #[serde(rename = "text")] + Text { + /// The text content + text: String, + }, + #[serde(rename = "audio")] + Audio { + /// Base64-encoded audio data + audio: Option, + /// The transcript of the audio + transcript: String, + }, +} diff --git a/async-openai/src/types/realtime/conversation.rs b/async-openai/src/types/realtime/conversation.rs new file mode 100644 index 00000000..3ea43bd8 --- /dev/null +++ b/async-openai/src/types/realtime/conversation.rs @@ -0,0 +1,10 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Conversation { + /// The unique ID of the conversation. + pub id: String, + + /// The object type, must be "realtime.conversation". + pub object: String, +} diff --git a/async-openai/src/types/realtime/error.rs b/async-openai/src/types/realtime/error.rs new file mode 100644 index 00000000..6ce907c3 --- /dev/null +++ b/async-openai/src/types/realtime/error.rs @@ -0,0 +1,19 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct RealtimeAPIError { + /// The type of error (e.g., "invalid_request_error", "server_error"). + pub r#type: String, + + /// Error code, if any. + pub code: Option, + + /// A human-readable error message. + pub message: String, + + /// Parameter related to the error, if any. + pub param: Option, + + /// The event_id of the client event that caused the error, if applicable. + pub event_id: Option, +} diff --git a/async-openai/src/types/realtime/item.rs b/async-openai/src/types/realtime/item.rs new file mode 100644 index 00000000..3af7d0d9 --- /dev/null +++ b/async-openai/src/types/realtime/item.rs @@ -0,0 +1,99 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "snake_case")] +pub enum ItemType { + Message, + FunctionCall, + FunctionCallOutput, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "snake_case")] +pub enum ItemStatus { + Completed, + InProgress, + Incomplete, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "lowercase")] +pub enum ItemRole { + User, + Assistant, + System, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "snake_case")] +pub enum ItemContentType { + InputText, + InputAudio, + Text, + Audio, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ItemContent { + /// The content type ("input_text", "input_audio", "text", "audio"). + pub r#type: ItemContentType, + + /// The text content. + #[serde(skip_serializing_if = "Option::is_none")] + pub text: Option, + + /// Base64-encoded audio bytes. + #[serde(skip_serializing_if = "Option::is_none")] + pub audio: Option, + + /// The transcript of the audio. + #[serde(skip_serializing_if = "Option::is_none")] + pub transcript: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Item { + /// The unique ID of the item. + #[serde(skip_serializing_if = "Option::is_none")] + pub id: Option, + + /// The type of the item ("message", "function_call", "function_call_output"). + #[serde(skip_serializing_if = "Option::is_none")] + pub r#type: Option, + + /// The status of the item ("completed", "in_progress", "incomplete"). + #[serde(skip_serializing_if = "Option::is_none")] + pub status: Option, + + /// The role of the message sender ("user", "assistant", "system"). + #[serde(skip_serializing_if = "Option::is_none")] + pub role: Option, + + /// The content of the message. + #[serde(skip_serializing_if = "Option::is_none")] + pub content: Option>, + + /// The ID of the function call (for "function_call" items). + #[serde(skip_serializing_if = "Option::is_none")] + pub call_id: Option, + + /// The name of the function being called (for "function_call" items). + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + + /// The arguments of the function call (for "function_call" items). + #[serde(skip_serializing_if = "Option::is_none")] + pub arguments: Option, + + /// The output of the function call (for "function_call_output" items). + #[serde(skip_serializing_if = "Option::is_none")] + pub output: Option, +} + +impl TryFrom for Item { + type Error = serde_json::Error; + + fn try_from(value: serde_json::Value) -> Result { + serde_json::from_value(value) + } +} diff --git a/async-openai/src/types/realtime/mod.rs b/async-openai/src/types/realtime/mod.rs new file mode 100644 index 00000000..b47605f8 --- /dev/null +++ b/async-openai/src/types/realtime/mod.rs @@ -0,0 +1,19 @@ +mod client_event; +mod content_part; +mod conversation; +mod error; +mod item; +mod rate_limit; +mod response_resource; +mod server_event; +mod session_resource; + +pub use client_event::*; +pub use content_part::*; +pub use conversation::*; +pub use error::*; +pub use item::*; +pub use rate_limit::*; +pub use response_resource::*; +pub use server_event::*; +pub use session_resource::*; diff --git a/async-openai/src/types/realtime/rate_limit.rs b/async-openai/src/types/realtime/rate_limit.rs new file mode 100644 index 00000000..f3fc4aa6 --- /dev/null +++ b/async-openai/src/types/realtime/rate_limit.rs @@ -0,0 +1,13 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct RateLimit { + /// The name of the rate limit ("requests", "tokens", "input_tokens", "output_tokens"). + pub name: String, + /// The maximum allowed value for the rate limit. + pub limit: u32, + /// The remaining value before the limit is reached. + pub remaining: u32, + /// Seconds until the rate limit resets. + pub reset_seconds: f32, +} diff --git a/async-openai/src/types/realtime/response_resource.rs b/async-openai/src/types/realtime/response_resource.rs new file mode 100644 index 00000000..a6c6c32f --- /dev/null +++ b/async-openai/src/types/realtime/response_resource.rs @@ -0,0 +1,61 @@ +use serde::{Deserialize, Serialize}; + +use super::item::Item; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Usage { + pub total_tokens: u32, + pub input_tokens: u32, + pub output_tokens: u32, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "snake_case")] +pub enum ResponseStatus { + InProgress, + Completed, + Cancelled, + Failed, + Incomplete, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct FailedError { + pub code: String, + pub message: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "snake_case")] +pub enum IncompleteReason { + Interruption, + MaxOutputTokens, + ContentFilter, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(tag = "type")] +pub enum ResponseStatusDetail { + #[serde(rename = "incomplete")] + Incomplete { reason: IncompleteReason }, + #[serde(rename = "failed")] + Failed { error: Option }, + #[serde(rename = "cancelled")] + Cancelled { reason: String }, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseResource { + /// The unique ID of the response. + pub id: String, + /// The object type, must be "realtime.response". + pub object: String, + /// The status of the response + pub status: ResponseStatus, + /// Additional details about the status. + pub status_details: Option, + /// The list of output items generated by the response. + pub output: Vec, + /// Usage statistics for the response. + pub usage: Option, +} diff --git a/async-openai/src/types/realtime/server_event.rs b/async-openai/src/types/realtime/server_event.rs new file mode 100644 index 00000000..8795f6e4 --- /dev/null +++ b/async-openai/src/types/realtime/server_event.rs @@ -0,0 +1,489 @@ +use serde::{Deserialize, Serialize}; + +use super::{ + content_part::ContentPart, conversation::Conversation, error::RealtimeAPIError, item::Item, + rate_limit::RateLimit, response_resource::ResponseResource, session_resource::SessionResource, +}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ErrorEvent { + /// The unique ID of the server event. + pub event_id: String, + /// Details of the error. + pub error: RealtimeAPIError, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct SessionCreatedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The session resource. + pub session: SessionResource, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct SessionUpdatedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The updated session resource. + pub session: SessionResource, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ConversationCreatedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The conversation resource. + pub conversation: Conversation, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct InputAudioBufferCommitedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the preceding item after which the new item will be inserted. + pub previous_item_id: String, + /// The ID of the user message item that will be created. + pub item_id: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct InputAudioBufferClearedEvent { + /// The unique ID of the server event. + pub event_id: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct InputAudioBufferSpeechStartedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// Milliseconds since the session started when speech was detected. + pub audio_start_ms: u32, + /// The ID of the user message item that will be created when speech stops. + pub item_id: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct InputAudioBufferSpeechStoppedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// Milliseconds since the session started when speech stopped. + pub audio_end_ms: u32, + /// The ID of the user message item that will be created. + pub item_id: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ConversationItemCreatedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the preceding item. + pub previous_item_id: Option, + /// The item that was created. + pub item: Item, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +/// Log probability information for a transcribed token. +pub struct LogProb { + /// Raw UTF-8 bytes for the token. + pub bytes: Vec, + /// The log probability of the token. + pub logprob: f64, + /// The token string. + pub token: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ConversationItemInputAudioTranscriptionCompletedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the user message item. + pub item_id: String, + /// The index of the content part containing the audio. + pub content_index: u32, + /// The transcribed text. + pub transcript: String, + /// Optional per-token log probability data. + pub logprobs: Option>, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ConversationItemInputAudioTranscriptionDeltaEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the user message item. + pub item_id: String, + /// The index of the content part containing the audio. + pub content_index: u32, + /// The text delta. + pub delta: String, + /// Optional per-token log probability data. + pub logprobs: Option>, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ConversationItemInputAudioTranscriptionFailedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the user message item. + pub item_id: String, + /// The index of the content part containing the audio. + pub content_index: u32, + /// Details of the transcription error. + pub error: RealtimeAPIError, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ConversationItemTruncatedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the assistant message item that was truncated. + pub item_id: String, + /// The index of the content part that was truncated. + pub content_index: u32, + /// The duration up to which the audio was truncated, in milliseconds. + pub audio_end_ms: u32, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ConversationItemDeletedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the item that was deleted. + pub item_id: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseCreatedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The response resource. + pub response: ResponseResource, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseDoneEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The response resource. + pub response: ResponseResource, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseOutputItemAddedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response to which the item belongs. + pub response_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The item that was added. + pub item: Item, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseOutputItemDoneEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response to which the item belongs. + pub response_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The completed item. + pub item: Item, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseContentPartAddedEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the item to which the content part was added. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The index of the content part in the item's content array. + pub content_index: u32, + /// The content part that was added. + pub part: ContentPart, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseContentPartDoneEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the item to which the content part was added. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The index of the content part in the item's content array. + pub content_index: u32, + /// The content part that is done. + pub part: ContentPart, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseTextDeltaEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the item. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The index of the content part in the item's content array. + pub content_index: u32, + /// The text delta. + pub delta: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseTextDoneEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the item. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The index of the content part in the item's content array. + pub content_index: u32, + /// The final text content. + pub text: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseAudioTranscriptDeltaEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the item. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The index of the content part in the item's content array. + pub content_index: u32, + /// The text delta. + pub delta: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseAudioTranscriptDoneEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the item. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The index of the content part in the item's content array. + pub content_index: u32, + ///The final transcript of the audio. + pub transcript: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseAudioDeltaEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the item. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The index of the content part in the item's content array. + pub content_index: u32, + /// Base64-encoded audio data delta. + pub delta: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseAudioDoneEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the item. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The index of the content part in the item's content array. + pub content_index: u32, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseFunctionCallArgumentsDeltaEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the function call item. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The ID of the function call. + pub call_id: String, + /// The arguments delta as a JSON string. + pub delta: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct ResponseFunctionCallArgumentsDoneEvent { + /// The unique ID of the server event. + pub event_id: String, + /// The ID of the response. + pub response_id: String, + /// The ID of the function call item. + pub item_id: String, + /// The index of the output item in the response. + pub output_index: u32, + /// The ID of the function call. + pub call_id: String, + /// The final arguments as a JSON string. + pub arguments: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct RateLimitsUpdatedEvent { + /// The unique ID of the server event. + pub event_id: String, + pub rate_limits: Vec, +} + +/// These are events emitted from the OpenAI Realtime WebSocket server to the client. +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(tag = "type")] +pub enum ServerEvent { + /// Returned when an error occurs. + #[serde(rename = "error")] + Error(ErrorEvent), + + /// Returned when a session is created. Emitted automatically when a new connection is established. + #[serde(rename = "session.created")] + SessionCreated(SessionCreatedEvent), + + /// Returned when a session is updated. + #[serde(rename = "session.updated")] + SessionUpdated(SessionUpdatedEvent), + + /// Returned when a conversation is created. Emitted right after session creation. + #[serde(rename = "conversation.created")] + ConversationCreated(ConversationCreatedEvent), + + /// Returned when an input audio buffer is committed, either by the client or automatically in server VAD mode. + #[serde(rename = "input_audio_buffer.committed")] + InputAudioBufferCommited(InputAudioBufferCommitedEvent), + + /// Returned when the input audio buffer is cleared by the client. + #[serde(rename = "input_audio_buffer.cleared")] + InputAudioBufferCleared(InputAudioBufferClearedEvent), + + /// Returned in server turn detection mode when speech is detected. + #[serde(rename = "input_audio_buffer.speech_started")] + InputAudioBufferSpeechStarted(InputAudioBufferSpeechStartedEvent), + + /// Returned in server turn detection mode when speech stops. + #[serde(rename = "input_audio_buffer.speech_stopped")] + InputAudioBufferSpeechStopped(InputAudioBufferSpeechStoppedEvent), + + /// Returned when a conversation item is created. + #[serde(rename = "conversation.item.created")] + ConversationItemCreated(ConversationItemCreatedEvent), + + /// Returned when input audio transcription is enabled and a transcription succeeds. + #[serde(rename = "conversation.item.input_audio_transcription.completed")] + ConversationItemInputAudioTranscriptionCompleted( + ConversationItemInputAudioTranscriptionCompletedEvent, + ), + + #[serde(rename = "conversation.item.input_audio_transcription.delta")] + ConversationItemInputAudioTranscriptionDelta(ConversationItemInputAudioTranscriptionDeltaEvent), + + /// Returned when input audio transcription is configured, and a transcription request for a user message failed. + #[serde(rename = "conversation.item.input_audio_transcription.failed")] + ConversationItemInputAudioTranscriptionFailed( + ConversationItemInputAudioTranscriptionFailedEvent, + ), + + /// Returned when an earlier assistant audio message item is truncated by the client. + #[serde(rename = "conversation.item.truncated")] + ConversationItemTruncated(ConversationItemTruncatedEvent), + + /// Returned when an item in the conversation is deleted. + #[serde(rename = "conversation.item.deleted")] + ConversationItemDeleted(ConversationItemDeletedEvent), + + /// Returned when a new Response is created. The first event of response creation, where the response is in an initial state of "in_progress". + #[serde(rename = "response.created")] + ResponseCreated(ResponseCreatedEvent), + + /// Returned when a Response is done streaming. Always emitted, no matter the final state. + #[serde(rename = "response.done")] + ResponseDone(ResponseDoneEvent), + + /// Returned when a new Item is created during response generation. + #[serde(rename = "response.output_item.added")] + ResponseOutputItemAdded(ResponseOutputItemAddedEvent), + + /// Returned when an Item is done streaming. Also emitted when a Response is interrupted, incomplete, or cancelled. + #[serde(rename = "response.output_item.done")] + ResponseOutputItemDone(ResponseOutputItemDoneEvent), + + /// Returned when a new content part is added to an assistant message item during response generation. + #[serde(rename = "response.content_part.added")] + ResponseContentPartAdded(ResponseContentPartAddedEvent), + + /// Returned when a content part is done streaming in an assistant message item. + /// Also emitted when a Response is interrupted, incomplete, or cancelled. + #[serde(rename = "response.content_part.done")] + ResponseContentPartDone(ResponseContentPartDoneEvent), + + /// Returned when the text value of a "text" content part is updated. + #[serde(rename = "response.text.delta")] + ResponseTextDelta(ResponseTextDeltaEvent), + + /// Returned when the text value of a "text" content part is done streaming. + /// Also emitted when a Response is interrupted, incomplete, or cancelled. + #[serde(rename = "response.text.done")] + ResponseTextDone(ResponseTextDoneEvent), + + /// Returned when the model-generated transcription of audio output is updated. + #[serde(rename = "response.audio_transcript.delta")] + ResponseAudioTranscriptDelta(ResponseAudioTranscriptDeltaEvent), + + /// Returned when the model-generated transcription of audio output is done streaming. + /// Also emitted when a Response is interrupted, incomplete, or cancelled. + #[serde(rename = "response.audio_transcript.done")] + ResponseAudioTranscriptDone(ResponseAudioTranscriptDoneEvent), + + /// Returned when the model-generated audio is updated. + #[serde(rename = "response.audio.delta")] + ResponseAudioDelta(ResponseAudioDeltaEvent), + + /// Returned when the model-generated audio is done. + /// Also emitted when a Response is interrupted, incomplete, or cancelled. + #[serde(rename = "response.audio.done")] + ResponseAudioDone(ResponseAudioDoneEvent), + + /// Returned when the model-generated function call arguments are updated. + #[serde(rename = "response.function_call_arguments.delta")] + ResponseFunctionCallArgumentsDelta(ResponseFunctionCallArgumentsDeltaEvent), + + /// Returned when the model-generated function call arguments are done streaming. + /// Also emitted when a Response is interrupted, incomplete, or cancelled. + #[serde(rename = "response.function_call_arguments.done")] + ResponseFunctionCallArgumentsDone(ResponseFunctionCallArgumentsDoneEvent), + + /// Emitted after every "response.done" event to indicate the updated rate limits. + #[serde(rename = "rate_limits.updated")] + RateLimitsUpdated(RateLimitsUpdatedEvent), +} diff --git a/async-openai/src/types/realtime/session_resource.rs b/async-openai/src/types/realtime/session_resource.rs new file mode 100644 index 00000000..2fe1e5b1 --- /dev/null +++ b/async-openai/src/types/realtime/session_resource.rs @@ -0,0 +1,176 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum AudioFormat { + #[serde(rename = "pcm16")] + PCM16, + #[serde(rename = "g711_law")] + G711ULAW, + #[serde(rename = "g711_alaw")] + G711ALAW, +} + +#[derive(Debug, Default, Serialize, Deserialize, Clone)] +pub struct AudioTranscription { + /// The language of the input audio. Supplying the input language in ISO-639-1 (e.g. en) format will improve accuracy and latency. + #[serde(skip_serializing_if = "Option::is_none")] + pub language: Option, + /// The model to use for transcription, current options are gpt-4o-transcribe, gpt-4o-mini-transcribe, and whisper-1. + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, + /// An optional text to guide the model's style or continue a previous audio segment. + /// For whisper-1, the prompt is a list of keywords. For gpt-4o-transcribe models, + /// the prompt is a free text string, for example "expect words related to technology". + #[serde(skip_serializing_if = "Option::is_none")] + pub prompt: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(tag = "type")] +pub enum TurnDetection { + /// Type of turn detection, only "server_vad" is currently supported. + #[serde(rename = "server_vad")] + ServerVAD { + /// Activation threshold for VAD (0.0 to 1.0). + threshold: f32, + /// Amount of audio to include before speech starts (in milliseconds). + prefix_padding_ms: u32, + /// Duration of silence to detect speech stop (in milliseconds). + silence_duration_ms: u32, + + /// Whether or not to automatically generate a response when a VAD stop event occurs. + #[serde(skip_serializing_if = "Option::is_none")] + create_response: Option, + + /// Whether or not to automatically interrupt any ongoing response with output to + /// the default conversation (i.e. conversation of auto) when a VAD start event occurs. + #[serde(skip_serializing_if = "Option::is_none")] + interrupt_response: Option, + }, + + #[serde(rename = "semantic_vad")] + SemanticVAD { + /// The eagerness of the model to respond. + /// `low` will wait longer for the user to continue speaking, + /// `high`` will respond more quickly. `auto`` is the default and is equivalent to `medium` + eagerness: String, + + /// Whether or not to automatically generate a response when a VAD stop event occurs. + #[serde(skip_serializing_if = "Option::is_none", default)] + create_response: Option, + + /// Whether or not to automatically interrupt any ongoing response with output to + /// the default conversation (i.e. conversation of auto) when a VAD start event occurs. + #[serde(skip_serializing_if = "Option::is_none", default)] + interrupt_response: Option, + }, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum MaxResponseOutputTokens { + #[serde(rename = "inf")] + Inf, + #[serde(untagged)] + Num(u16), +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(tag = "type")] +pub enum ToolDefinition { + #[serde(rename = "function")] + Function { + /// The name of the function. + name: String, + /// The description of the function. + description: String, + /// Parameters of the function in JSON Schema. + parameters: serde_json::Value, + }, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "lowercase")] +pub enum FunctionType { + Function, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "lowercase")] +pub enum ToolChoice { + Auto, + None, + Required, + #[serde(untagged)] + Function { + r#type: FunctionType, + name: String, + }, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "lowercase")] +pub enum RealtimeVoice { + Alloy, + Ash, + Ballad, + Coral, + Echo, + Fable, + Onyx, + Nova, + Shimmer, + Verse, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Default)] +pub struct SessionResource { + /// The default model used for this session. + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, + + /// The set of modalities the model can respond with. To disable audio, set this to ["text"]. + #[serde(skip_serializing_if = "Option::is_none")] + pub modalities: Option>, + + //// The default system instructions prepended to model calls. + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + + /// The voice the model uses to respond. Cannot be changed once the model has responded with audio at least once. + #[serde(skip_serializing_if = "Option::is_none")] + pub voice: Option, + + /// The format of input audio. Options are "pcm16", "g711_ulaw", or "g711_alaw". + #[serde(skip_serializing_if = "Option::is_none")] + pub input_audio_format: Option, + + /// The format of output audio. Options are "pcm16", "g711_ulaw", or "g711_alaw". + #[serde(skip_serializing_if = "Option::is_none")] + pub output_audio_format: Option, + + /// Configuration for input audio transcription. Can be set to null to turn off. + #[serde(skip_serializing_if = "Option::is_none")] + pub input_audio_transcription: Option, + + /// Configuration for turn detection. Can be set to null to turn off. + #[serde(skip_serializing_if = "Option::is_none")] + pub turn_detection: Option, + + /// Tools (functions) available to the model. + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + /// How the model chooses tools. + pub tool_choice: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Sampling temperature for the model. + pub temperature: Option, + + /// Maximum number of output tokens for a single assistant response, inclusive of tool calls. + /// Provide an integer between 1 and 4096 to limit output tokens, or "inf" for the maximum available tokens for a given model. + /// Defaults to "inf". + #[serde(skip_serializing_if = "Option::is_none")] + pub max_response_output_tokens: Option, +} diff --git a/async-openai/src/types/responses.rs b/async-openai/src/types/responses.rs new file mode 100644 index 00000000..6b2762c3 --- /dev/null +++ b/async-openai/src/types/responses.rs @@ -0,0 +1,2173 @@ +use crate::error::OpenAIError; +pub use crate::types::{ + CompletionTokensDetails, ImageDetail, PromptTokensDetails, ReasoningEffort, + ResponseFormatJsonSchema, +}; +use derive_builder::Builder; +use futures::Stream; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::HashMap; +use std::pin::Pin; + +/// Role of messages in the API. +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum Role { + User, + Assistant, + System, + Developer, +} + +/// Status of input/output items. +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum OutputStatus { + InProgress, + Completed, + Incomplete, +} + +/// Input payload: raw text or structured context items. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum Input { + /// A text input to the model, equivalent to a text input with the user role. + Text(String), + /// A list of one or many input items to the model, containing different content types. + Items(Vec), +} + +/// A context item: currently only messages. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged, rename_all = "snake_case")] +pub enum InputItem { + Message(InputMessage), + Custom(serde_json::Value), +} + +/// A message to prime the model. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "InputMessageArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct InputMessage { + #[serde(default, rename = "type")] + pub kind: InputMessageType, + /// The role of the message input. + pub role: Role, + /// Text, image, or audio input to the model, used to generate a response. Can also contain + /// previous assistant responses. + pub content: InputContent, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "snake_case")] +pub enum InputMessageType { + #[default] + Message, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum InputContent { + /// A text input to the model. + TextInput(String), + /// A list of one or many input items to the model, containing different content types. + InputItemContentList(Vec), +} + +/// Parts of a message: text, image, file, or audio. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum ContentType { + /// A text input to the model. + InputText(InputText), + /// An image input to the model. + InputImage(InputImage), + /// A file input to the model. + InputFile(InputFile), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct InputText { + text: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "InputImageArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct InputImage { + /// The detail level of the image to be sent to the model. + detail: ImageDetail, + /// The ID of the file to be sent to the model. + #[serde(skip_serializing_if = "Option::is_none")] + file_id: Option, + /// The URL of the image to be sent to the model. A fully qualified URL or base64 encoded image + /// in a data URL. + #[serde(skip_serializing_if = "Option::is_none")] + image_url: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "InputFileArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct InputFile { + /// The content of the file to be sent to the model. + #[serde(skip_serializing_if = "Option::is_none")] + file_data: Option, + /// The ID of the file to be sent to the model. + #[serde(skip_serializing_if = "Option::is_none")] + file_id: Option, + /// The name of the file to be sent to the model. + #[serde(skip_serializing_if = "Option::is_none")] + filename: Option, +} + +/// Builder for a Responses API request. +#[derive(Clone, Serialize, Deserialize, Debug, Default, Builder, PartialEq)] +#[builder( + name = "CreateResponseArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateResponse { + /// Text, image, or file inputs to the model, used to generate a response. + pub input: Input, + + /// Model ID used to generate the response, like `gpt-4o`. + /// OpenAI offers a wide range of models with different capabilities, + /// performance characteristics, and price points. + pub model: String, + + /// Whether to run the model response in the background. + /// boolean or null. + #[serde(skip_serializing_if = "Option::is_none")] + pub background: Option, + + /// Specify additional output data to include in the model response. + /// + /// Supported values: + /// - `file_search_call.results` + /// Include the search results of the file search tool call. + /// - `message.input_image.image_url` + /// Include image URLs from the input message. + /// - `computer_call_output.output.image_url` + /// Include image URLs from the computer call output. + /// - `reasoning.encrypted_content` + /// Include an encrypted version of reasoning tokens in reasoning item outputs. + /// This enables reasoning items to be used in multi-turn conversations when + /// using the Responses API statelessly (for example, when the `store` parameter + /// is set to `false`, or when an organization is enrolled in the zero-data- + /// retention program). + /// + /// If `None`, no additional data is returned. + #[serde(skip_serializing_if = "Option::is_none")] + pub include: Option>, + + /// Inserts a system (or developer) message as the first item in the model's context. + /// + /// When using along with previous_response_id, the instructions from a previous response will + /// not be carried over to the next response. This makes it simple to swap out system + /// (or developer) messages in new responses. + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + + /// An upper bound for the number of tokens that can be generated for a + /// response, including visible output tokens and reasoning tokens. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_output_tokens: Option, + + /// The maximum number of total calls to built-in tools that can be processed in a response. + /// This maximum number applies across all built-in tool calls, not per individual tool. + /// Any further attempts to call a tool by the model will be ignored. + pub max_tool_calls: Option, + + /// Set of 16 key-value pairs that can be attached to an object. This can be + /// useful for storing additional information about the object in a structured + /// format, and querying for objects via API or the dashboard. + /// + /// Keys are strings with a maximum length of 64 characters. Values are + /// strings with a maximum length of 512 characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, + + /// Whether to allow the model to run tool calls in parallel. + #[serde(skip_serializing_if = "Option::is_none")] + pub parallel_tool_calls: Option, + + /// The unique ID of the previous response to the model. Use this to create + /// multi-turn conversations. + #[serde(skip_serializing_if = "Option::is_none")] + pub previous_response_id: Option, + + /// Reference to a prompt template and its variables. + #[serde(skip_serializing_if = "Option::is_none")] + pub prompt: Option, + + /// **o-series models only**: Configuration options for reasoning models. + #[serde(skip_serializing_if = "Option::is_none")] + pub reasoning: Option, + + /// Specifies the latency tier to use for processing the request. + /// + /// This parameter is relevant for customers subscribed to the Scale tier service. + /// + /// Supported values: + /// - `auto` + /// - If the Project is Scale tier enabled, the system will utilize Scale tier credits until + /// they are exhausted. + /// - If the Project is not Scale tier enabled, the request will be processed using the + /// default service tier with a lower uptime SLA and no latency guarantee. + /// - `default` + /// The request will be processed using the default service tier with a lower uptime SLA and + /// no latency guarantee. + /// - `flex` + /// The request will be processed with the Flex Processing service tier. Learn more. + /// + /// When not set, the default behavior is `auto`. + /// + /// When this parameter is set, the response body will include the `service_tier` utilized. + #[serde(skip_serializing_if = "Option::is_none")] + pub service_tier: Option, + + /// Whether to store the generated model response for later retrieval via API. + #[serde(skip_serializing_if = "Option::is_none")] + pub store: Option, + + /// If set to true, the model response data will be streamed to the client as it is + /// generated using server-sent events. + #[serde(skip_serializing_if = "Option::is_none")] + pub stream: Option, + + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 + /// will make the output more random, while lower values like 0.2 will make it + /// more focused and deterministic. We generally recommend altering this or + /// `top_p` but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, + + /// Configuration options for a text response from the model. Can be plain text + /// or structured JSON data. + #[serde(skip_serializing_if = "Option::is_none")] + pub text: Option, + + /// How the model should select which tool (or tools) to use when generating + /// a response. + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_choice: Option, + + /// An array of tools the model may call while generating a response. + /// Can include built-in tools (file_search, web_search_preview, + /// computer_use_preview) or custom function definitions. + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + + /// An integer between 0 and 20 specifying the number of most likely tokens to return + /// at each token position, each with an associated log probability. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_logprobs: Option, // TODO add validation of range + + /// An alternative to sampling with temperature, called nucleus sampling, + /// where the model considers the results of the tokens with top_p probability + /// mass. So 0.1 means only the tokens comprising the top 10% probability mass + /// are considered. We generally recommend altering this or `temperature` but + /// not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, + + /// The truncation strategy to use for the model response: + /// - `auto`: drop items in the middle to fit context window. + /// - `disabled`: error if exceeding context window. + #[serde(skip_serializing_if = "Option::is_none")] + pub truncation: Option, + + /// A unique identifier representing your end-user, which can help OpenAI to + /// monitor and detect abuse. + #[serde(skip_serializing_if = "Option::is_none")] + pub user: Option, +} + +/// Service tier request options. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct PromptConfig { + /// The unique identifier of the prompt template to use. + pub id: String, + + /// Optional version of the prompt template. + #[serde(skip_serializing_if = "Option::is_none")] + pub version: Option, + + /// Optional map of values to substitute in for variables in your prompt. The substitution + /// values can either be strings, or other Response input types like images or files. + /// For now only supporting Strings. + #[serde(skip_serializing_if = "Option::is_none")] + pub variables: Option>, +} + +/// Service tier request options. +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ServiceTier { + Auto, + Default, + Flex, + Scale, + Priority, +} + +/// Truncation strategies. +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum Truncation { + Auto, + Disabled, +} + +/// o-series reasoning settings. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "ReasoningConfigArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ReasoningConfig { + /// Constrain effort on reasoning. + #[serde(skip_serializing_if = "Option::is_none")] + pub effort: Option, + /// Summary mode for reasoning. + #[serde(skip_serializing_if = "Option::is_none")] + pub summary: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ReasoningSummary { + Auto, + Concise, + Detailed, +} + +/// Configuration for text response format. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct TextConfig { + /// Defines the format: plain text, JSON object, or JSON schema. + pub format: TextResponseFormat, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum TextResponseFormat { + /// The type of response format being defined: `text` + Text, + /// The type of response format being defined: `json_object` + JsonObject, + /// The type of response format being defined: `json_schema` + JsonSchema(ResponseFormatJsonSchema), +} + +/// Definitions for model-callable tools. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum ToolDefinition { + /// File search tool. + FileSearch(FileSearch), + /// Custom function call. + Function(Function), + /// Web search preview tool. + WebSearchPreview(WebSearchPreview), + /// Virtual computer control tool. + ComputerUsePreview(ComputerUsePreview), + /// Remote Model Context Protocol server. + Mcp(Mcp), + /// Python code interpreter tool. + CodeInterpreter(CodeInterpreter), + /// Image generation tool. + ImageGeneration(ImageGeneration), + /// Local shell command execution tool. + LocalShell, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "FileSearchArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct FileSearch { + /// The IDs of the vector stores to search. + pub vector_store_ids: Vec, + /// The maximum number of results to return. This number should be between 1 and 50 inclusive. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_num_results: Option, + /// A filter to apply. + #[serde(skip_serializing_if = "Option::is_none")] + pub filters: Option, + /// Ranking options for search. + #[serde(skip_serializing_if = "Option::is_none")] + pub ranking_options: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "FunctionArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +pub struct Function { + /// The name of the function to call. + pub name: String, + /// A JSON schema object describing the parameters of the function. + pub parameters: serde_json::Value, + /// Whether to enforce strict parameter validation. + pub strict: bool, + /// A description of the function. Used by the model to determine whether or not to call the + /// function. + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "WebSearchPreviewArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +pub struct WebSearchPreview { + /// The user's location. + #[serde(skip_serializing_if = "Option::is_none")] + pub user_location: Option, + /// High level guidance for the amount of context window space to use for the search. + #[serde(skip_serializing_if = "Option::is_none")] + pub search_context_size: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "lowercase")] +pub enum WebSearchContextSize { + Low, + Medium, + High, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "ComputerUsePreviewArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +pub struct ComputerUsePreview { + /// The type of computer environment to control. + environment: String, + /// The width of the computer display. + display_width: u32, + /// The height of the computer display. + display_height: u32, +} + +/// Options for search result ranking. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct RankingOptions { + /// The ranker to use for the file search. + pub ranker: String, + /// The score threshold for the file search, a number between 0 and 1. Numbers closer to 1 will + /// attempt to return only the most relevant results, but may return fewer results. + #[serde(skip_serializing_if = "Option::is_none")] + pub score_threshold: Option, +} + +/// Filters for file search. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum Filter { + /// A filter used to compare a specified attribute key to a given value using a defined + /// comparison operation. + Comparison(ComparisonFilter), + /// Combine multiple filters using and or or. + Compound(CompoundFilter), +} + +/// Single comparison filter. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ComparisonFilter { + /// Specifies the comparison operator + #[serde(rename = "type")] + pub op: ComparisonType, + /// The key to compare against the value. + pub key: String, + /// The value to compare against the attribute key; supports string, number, or boolean types. + pub value: serde_json::Value, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +pub enum ComparisonType { + #[serde(rename = "eq")] + Equals, + #[serde(rename = "ne")] + NotEquals, + #[serde(rename = "gt")] + GreaterThan, + #[serde(rename = "gte")] + GreaterThanOrEqualTo, + #[serde(rename = "lt")] + LessThan, + #[serde(rename = "lte")] + LessThanOrEqualTo, +} + +/// Combine multiple filters. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct CompoundFilter { + /// Type of operation + #[serde(rename = "type")] + pub op: CompoundType, + /// Array of filters to combine. Items can be ComparisonFilter or CompoundFilter. + pub filters: Vec, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum CompoundType { + And, + Or, +} + +/// Approximate user location for web search. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "LocationArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct Location { + /// The type of location approximation. Always approximate. + #[serde(rename = "type")] + pub kind: String, + /// Free text input for the city of the user, e.g. San Francisco. + #[serde(skip_serializing_if = "Option::is_none")] + pub city: Option, + /// The two-letter ISO country code of the user, e.g. US. + #[serde(skip_serializing_if = "Option::is_none")] + pub country: Option, + /// Free text input for the region of the user, e.g. California. + #[serde(skip_serializing_if = "Option::is_none")] + pub region: Option, + /// The IANA timezone of the user, e.g. America/Los_Angeles. + #[serde(skip_serializing_if = "Option::is_none")] + pub timezone: Option, +} + +/// MCP (Model Context Protocol) tool configuration. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "McpArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct Mcp { + /// A label for this MCP server. + pub server_label: String, + /// The URL for the MCP server. + pub server_url: String, + /// List of allowed tool names or filter object. + #[serde(skip_serializing_if = "Option::is_none")] + pub allowed_tools: Option, + /// Optional HTTP headers for the MCP server. + #[serde(skip_serializing_if = "Option::is_none")] + pub headers: Option, + /// Approval policy or filter for tools. + #[serde(skip_serializing_if = "Option::is_none")] + pub require_approval: Option, +} + +/// Allowed tools configuration for MCP. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum AllowedTools { + /// A flat list of allowed tool names. + List(Vec), + /// A filter object specifying allowed tools. + Filter(McpAllowedToolsFilter), +} + +/// Filter object for MCP allowed tools. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct McpAllowedToolsFilter { + /// Names of tools in the filter + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_names: Option>, +} + +/// Approval policy or filter for MCP tools. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum RequireApproval { + /// A blanket policy: "always" or "never". + Policy(RequireApprovalPolicy), + /// A filter object specifying which tools require approval. + Filter(McpApprovalFilter), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum RequireApprovalPolicy { + Always, + Never, +} + +/// Filter object for MCP tool approval. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct McpApprovalFilter { + /// A list of tools that always require approval. + #[serde(skip_serializing_if = "Option::is_none")] + pub always: Option, + /// A list of tools that never require approval. + #[serde(skip_serializing_if = "Option::is_none")] + pub never: Option, +} + +/// Container configuration for a code interpreter. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum CodeInterpreterContainer { + /// A simple container ID. + Id(String), + /// Auto-configured container with optional files. + Container(CodeInterpreterContainerKind), +} + +/// Auto configuration for code interpreter container. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum CodeInterpreterContainerKind { + Auto { + /// Optional list of uploaded file IDs. + #[serde(skip_serializing_if = "Option::is_none")] + file_ids: Option>, + }, +} + +/// Code interpreter tool definition. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "CodeInterpreterArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CodeInterpreter { + /// Container configuration for running code. + pub container: CodeInterpreterContainer, +} + +/// Mask image input for image generation. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct InputImageMask { + /// Base64-encoded mask image. + #[serde(skip_serializing_if = "Option::is_none")] + pub image_url: Option, + /// File ID for the mask image. + #[serde(skip_serializing_if = "Option::is_none")] + pub file_id: Option, +} + +/// Image generation tool definition. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default, Builder)] +#[builder( + name = "ImageGenerationArgs", + pattern = "mutable", + setter(into, strip_option), + default +)] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ImageGeneration { + /// Background type: transparent, opaque, or auto. + #[serde(skip_serializing_if = "Option::is_none")] + pub background: Option, + /// Optional mask for inpainting. + #[serde(skip_serializing_if = "Option::is_none")] + pub input_image_mask: Option, + /// Model to use (default: gpt-image-1). + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, + /// Moderation level (default: auto). + #[serde(skip_serializing_if = "Option::is_none")] + pub moderation: Option, + /// Compression level (0-100). + #[serde(skip_serializing_if = "Option::is_none")] + pub output_compression: Option, + /// Output format: png, webp, or jpeg. + #[serde(skip_serializing_if = "Option::is_none")] + pub output_format: Option, + /// Number of partial images (0-3). + #[serde(skip_serializing_if = "Option::is_none")] + pub partial_images: Option, + /// Quality: low, medium, high, or auto. + #[serde(skip_serializing_if = "Option::is_none")] + pub quality: Option, + /// Size: e.g. "1024x1024" or auto. + #[serde(skip_serializing_if = "Option::is_none")] + pub size: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageGenerationBackground { + Transparent, + Opaque, + Auto, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageGenerationOutputFormat { + Png, + Webp, + Jpeg, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageGenerationQuality { + Low, + Medium, + High, + Auto, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ImageGenerationSize { + Auto, + #[serde(rename = "1024x1024")] + Size1024x1024, + #[serde(rename = "1024x1536")] + Size1024x1536, + #[serde(rename = "1536x1024")] + Size1536x1024, +} + +/// Control how the model picks or is forced to pick a tool. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum ToolChoice { + /// Controls which (if any) tool is called by the model. + Mode(ToolChoiceMode), + /// Indicates that the model should use a built-in tool to generate a response. + Hosted { + /// The type of hosted tool the model should to use. + #[serde(rename = "type")] + kind: HostedToolType, + }, + /// Use this option to force the model to call a specific function. + Function { + /// The name of the function to call. + name: String, + }, +} + +/// Simple tool-choice modes. +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ToolChoiceMode { + /// The model will not call any tool and instead generates a message. + None, + /// The model can pick between generating a message or calling one or more tools. + Auto, + /// The model must call one or more tools. + Required, +} + +/// Hosted tool type identifiers. +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum HostedToolType { + FileSearch, + WebSearchPreview, + ComputerUsePreview, +} + +/// Error returned by the API when a request fails. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ErrorObject { + /// The error code for the response. + pub code: String, + /// A human-readable description of the error. + pub message: String, +} + +/// Details about an incomplete response. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct IncompleteDetails { + /// The reason why the response is incomplete. + pub reason: String, +} + +/// A simple text output from the model. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct OutputText { + /// The annotations of the text output. + pub annotations: Vec, + /// The text output from the model. + pub text: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum Annotation { + /// A citation to a file. + FileCitation(FileCitation), + /// A citation for a web resource used to generate a model response. + UrlCitation(UrlCitation), + /// A path to a file. + FilePath(FilePath), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct FileCitation { + /// The ID of the file. + file_id: String, + /// The index of the file in the list of files. + index: u32, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct UrlCitation { + /// The index of the last character of the URL citation in the message. + end_index: u32, + /// The index of the first character of the URL citation in the message. + start_index: u32, + /// The title of the web resource. + title: String, + /// The URL of the web resource. + url: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct FilePath { + /// The ID of the file. + file_id: String, + /// The index of the file in the list of files. + index: u32, +} + +/// A refusal explanation from the model. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct Refusal { + /// The refusal explanationfrom the model. + pub refusal: String, +} + +/// A message generated by the model. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct OutputMessage { + /// The content of the output message. + pub content: Vec, + /// The unique ID of the output message. + pub id: String, + /// The role of the output message. Always assistant. + pub role: Role, + /// The status of the message input. + pub status: OutputStatus, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum Content { + /// A text output from the model. + OutputText(OutputText), + /// A refusal from the model. + Refusal(Refusal), +} + +/// Nested content within an output message. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum OutputContent { + /// An output message from the model. + Message(OutputMessage), + /// The results of a file search tool call. + FileSearchCall(FileSearchCallOutput), + /// A tool call to run a function. + FunctionCall(FunctionCall), + /// The results of a web search tool call. + WebSearchCall(WebSearchCallOutput), + /// A tool call to a computer use tool. + ComputerCall(ComputerCallOutput), + /// A description of the chain of thought used by a reasoning model while generating a response. + /// Be sure to include these items in your input to the Responses API for subsequent turns of a + /// conversation if you are manually managing context. + Reasoning(ReasoningItem), + /// Image generation tool call output. + ImageGenerationCall(ImageGenerationCallOutput), + /// Code interpreter tool call output. + CodeInterpreterCall(CodeInterpreterCallOutput), + /// Local shell tool call output. + LocalShellCall(LocalShellCallOutput), + /// MCP tool invocation output. + McpCall(McpCallOutput), + /// MCP list-tools output. + McpListTools(McpListToolsOutput), + /// MCP approval request output. + McpApprovalRequest(McpApprovalRequestOutput), +} + +/// A reasoning item representing the model's chain of thought, including summary paragraphs. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ReasoningItem { + /// Unique identifier of the reasoning content. + pub id: String, + /// The summarized chain-of-thought paragraphs. + pub summary: Vec, + /// The encrypted content of the reasoning item - populated when a response is generated with + /// `reasoning.encrypted_content` in the `include` parameter. + #[serde(skip_serializing_if = "Option::is_none")] + pub encrypted_content: Option, + /// The status of the reasoning item. + #[serde(skip_serializing_if = "Option::is_none")] + pub status: Option, +} + +/// A single summary text fragment from reasoning. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct SummaryText { + /// A short summary of the reasoning used by the model. + pub text: String, +} + +/// File search tool call output. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct FileSearchCallOutput { + /// The unique ID of the file search tool call. + pub id: String, + /// The queries used to search for files. + pub queries: Vec, + /// The status of the file search tool call. + pub status: FileSearchCallOutputStatus, + /// The results of the file search tool call. + #[serde(skip_serializing_if = "Option::is_none")] + pub results: Option>, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum FileSearchCallOutputStatus { + InProgress, + Searching, + Incomplete, + Failed, + Completed, +} + +/// A single result from a file search. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct FileSearchResult { + /// The unique ID of the file. + pub file_id: String, + /// The name of the file. + pub filename: String, + /// The relevance score of the file - a value between 0 and 1. + pub score: f32, + /// The text that was retrieved from the file. + pub text: String, + /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing + /// additional information about the object in a structured format, and querying for objects + /// API or the dashboard. Keys are strings with a maximum length of 64 characters + /// . Values are strings with a maximum length of 512 characters, booleans, or numbers. + pub attributes: HashMap, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct SafetyCheck { + /// The ID of the safety check. + pub id: String, + /// The type/code of the pending safety check. + pub code: String, + /// Details about the pending safety check. + pub message: String, +} + +/// Web search tool call output. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct WebSearchCallOutput { + /// The unique ID of the web search tool call. + pub id: String, + /// The status of the web search tool call. + pub status: String, +} + +/// Output from a computer tool call. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ComputerCallOutput { + pub action: ComputerCallAction, + /// An identifier used when responding to the tool call with output. + pub call_id: String, + /// The unique ID of the computer call. + pub id: String, + /// The pending safety checks for the computer call. + pub pending_safety_checks: Vec, + /// The status of the item. + pub status: OutputStatus, +} + +/// A point in 2D space. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Point { + pub x: i32, + pub y: i32, +} + +/// Represents all user‐triggered actions. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum ComputerCallAction { + /// A click action. + Click(Click), + + /// A double-click action. + DoubleClick(DoubleClick), + + /// A drag action. + Drag(Drag), + + /// A keypress action. + KeyPress(KeyPress), + + /// A mouse move action. + Move(MoveAction), + + /// A screenshot action. + Screenshot, + + /// A scroll action. + Scroll(Scroll), + + /// A type (text entry) action. + Type(TypeAction), + + /// A wait (no-op) action. + Wait, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ButtonPress { + Left, + Right, + Wheel, + Back, + Forward, +} + +/// A click action. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Click { + /// Which mouse button was pressed. + pub button: ButtonPress, + /// X‐coordinate of the click. + pub x: i32, + /// Y‐coordinate of the click. + pub y: i32, +} + +/// A double click action. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct DoubleClick { + /// X‐coordinate of the double click. + pub x: i32, + /// Y‐coordinate of the double click. + pub y: i32, +} + +/// A drag action. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Drag { + /// The path of points the cursor drags through. + pub path: Vec, + /// X‐coordinate at the end of the drag. + pub x: i32, + /// Y‐coordinate at the end of the drag. + pub y: i32, +} + +/// A keypress action. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct KeyPress { + /// The list of keys to press (e.g. `["Control", "C"]`). + pub keys: Vec, +} + +/// A mouse move action. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct MoveAction { + /// X‐coordinate to move to. + pub x: i32, + /// Y‐coordinate to move to. + pub y: i32, +} + +/// A scroll action. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Scroll { + /// Horizontal scroll distance. + pub scroll_x: i32, + /// Vertical scroll distance. + pub scroll_y: i32, + /// X‐coordinate where the scroll began. + pub x: i32, + /// Y‐coordinate where the scroll began. + pub y: i32, +} + +/// A typing (text entry) action. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct TypeAction { + /// The text to type. + pub text: String, +} + +/// Metadata for a function call request. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct FunctionCall { + /// The unique ID of the function tool call. + pub id: String, + /// The unique ID of the function tool call generated by the model. + pub call_id: String, + /// The name of the function to run. + pub name: String, + /// A JSON string of the arguments to pass to the function. + pub arguments: String, + /// The status of the item. + pub status: OutputStatus, +} + +/// Output of an image generation request. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ImageGenerationCallOutput { + /// Unique ID of the image generation call. + pub id: String, + /// Base64-encoded generated image, or null. + pub result: Option, + /// Status of the image generation call. + pub status: String, +} + +/// Output of a code interpreter request. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct CodeInterpreterCallOutput { + /// The code that was executed. + #[serde(skip_serializing_if = "Option::is_none")] + pub code: Option, + /// Unique ID of the call. + pub id: String, + /// Status of the tool call. + pub status: String, + /// ID of the container used to run the code. + pub container_id: String, + /// The outputs of the execution: logs or files. + #[serde(skip_serializing_if = "Option::is_none")] + pub outputs: Option>, +} + +/// Individual result from a code interpreter: either logs or files. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum CodeInterpreterResult { + /// Text logs from the execution. + Logs(CodeInterpreterTextOutput), + /// File outputs from the execution. + Files(CodeInterpreterFileOutput), +} + +/// The output containing execution logs. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct CodeInterpreterTextOutput { + /// The logs of the code interpreter tool call. + pub logs: String, +} + +/// The output containing file references. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct CodeInterpreterFileOutput { + /// List of file IDs produced. + pub files: Vec, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct CodeInterpreterFile { + /// The ID of the file. + file_id: String, + /// The MIME type of the file. + mime_type: String, +} + +/// Output of a local shell command request. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct LocalShellCallOutput { + /// Details of the exec action. + pub action: LocalShellAction, + /// Unique call identifier for responding to the tool call. + pub call_id: String, + /// Unique ID of the local shell call. + pub id: String, + /// Status of the local shell call. + pub status: String, +} + +/// Define the shape of a local shell action (exec). +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct LocalShellAction { + /// The command to run. + pub command: Vec, + /// Environment variables to set for the command. + pub env: HashMap, + /// Optional timeout for the command (ms). + pub timeout_ms: Option, + /// Optional user to run the command as. + pub user: Option, + /// Optional working directory for the command. + pub working_directory: Option, +} + +/// Output of an MCP server tool invocation. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct McpCallOutput { + /// JSON string of the arguments passed. + pub arguments: String, + /// Unique ID of the MCP call. + pub id: String, + /// Name of the tool invoked. + pub name: String, + /// Label of the MCP server. + pub server_label: String, + /// Error message from the call, if any. + pub error: Option, + /// Output from the call, if any. + pub output: Option, +} + +/// Output listing tools available on an MCP server. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct McpListToolsOutput { + /// Unique ID of the list request. + pub id: String, + /// Label of the MCP server. + pub server_label: String, + /// Tools available on the server with metadata. + pub tools: Vec, + /// Error message if listing failed. + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option, +} + +/// Information about a single tool on an MCP server. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct McpToolInfo { + /// The name of the tool. + pub name: String, + /// The JSON schema describing the tool's input. + pub input_schema: Value, + /// Additional annotations about the tool. + #[serde(skip_serializing_if = "Option::is_none")] + pub annotations: Option, + /// The description of the tool. + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, +} + +/// Output representing a human approval request for an MCP tool. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct McpApprovalRequestOutput { + /// JSON string of arguments for the tool. + pub arguments: String, + /// Unique ID of the approval request. + pub id: String, + /// Name of the tool requiring approval. + pub name: String, + /// Label of the MCP server making the request. + pub server_label: String, +} + +/// Usage statistics for a response. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct Usage { + /// The number of input tokens. + pub input_tokens: u32, + /// A detailed breakdown of the input tokens. + pub input_tokens_details: PromptTokensDetails, + /// The number of output tokens. + pub output_tokens: u32, + /// A detailed breakdown of the output tokens. + pub output_tokens_details: CompletionTokensDetails, + /// The total number of tokens used. + pub total_tokens: u32, +} + +/// The complete response returned by the Responses API. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct Response { + /// Unix timestamp (in seconds) when this Response was created. + pub created_at: u64, + + /// Error object if the API failed to generate a response. + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option, + + /// Unique identifier for this response. + pub id: String, + + /// Details about why the response is incomplete, if any. + #[serde(skip_serializing_if = "Option::is_none")] + pub incomplete_details: Option, + + /// Instructions that were inserted as the first item in context. + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + + /// The value of `max_output_tokens` that was honored. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_output_tokens: Option, + + /// Metadata tags/values that were attached to this response. + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, + + /// Model ID used to generate the response. + pub model: String, + + /// The object type – always `response`. + pub object: String, + + /// The array of content items generated by the model. + pub output: Vec, + + /// SDK-only convenience property that contains the aggregated text output from all + /// `output_text` items in the `output` array, if any are present. + /// Supported in the Python and JavaScript SDKs. + #[serde(skip_serializing_if = "Option::is_none")] + pub output_text: Option, + + /// Whether parallel tool calls were enabled. + #[serde(skip_serializing_if = "Option::is_none")] + pub parallel_tool_calls: Option, + + /// Previous response ID, if creating part of a multi-turn conversation. + #[serde(skip_serializing_if = "Option::is_none")] + pub previous_response_id: Option, + + /// Reasoning configuration echoed back (effort, summary settings). + #[serde(skip_serializing_if = "Option::is_none")] + pub reasoning: Option, + + /// Whether to store the generated model response for later retrieval via API. + #[serde(skip_serializing_if = "Option::is_none")] + pub store: Option, + + /// The service tier that actually processed this response. + #[serde(skip_serializing_if = "Option::is_none")] + pub service_tier: Option, + + /// The status of the response generation. + pub status: Status, + + /// Sampling temperature that was used. + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, + + /// Text format configuration echoed back (plain, json_object, json_schema). + #[serde(skip_serializing_if = "Option::is_none")] + pub text: Option, + + /// How the model chose or was forced to choose a tool. + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_choice: Option, + + /// Tool definitions that were provided. + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + + /// Nucleus sampling cutoff that was used. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, + + /// Truncation strategy that was applied. + #[serde(skip_serializing_if = "Option::is_none")] + pub truncation: Option, + + /// Token usage statistics for this request. + #[serde(skip_serializing_if = "Option::is_none")] + pub usage: Option, + + /// End-user ID for which this response was generated. + #[serde(skip_serializing_if = "Option::is_none")] + pub user: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum Status { + Completed, + Failed, + InProgress, + Incomplete, +} + +/// Event types for streaming responses from the Responses API +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type")] +#[non_exhaustive] // Future-proof against breaking changes +pub enum ResponseEvent { + /// Response creation started + #[serde(rename = "response.created")] + ResponseCreated(ResponseCreated), + /// Processing in progress + #[serde(rename = "response.in_progress")] + ResponseInProgress(ResponseInProgress), + /// Response completed (different from done) + #[serde(rename = "response.completed")] + ResponseCompleted(ResponseCompleted), + /// Response failed + #[serde(rename = "response.failed")] + ResponseFailed(ResponseFailed), + /// Response incomplete + #[serde(rename = "response.incomplete")] + ResponseIncomplete(ResponseIncomplete), + /// Response queued + #[serde(rename = "response.queued")] + ResponseQueued(ResponseQueued), + /// Output item added + #[serde(rename = "response.output_item.added")] + ResponseOutputItemAdded(ResponseOutputItemAdded), + /// Content part added + #[serde(rename = "response.content_part.added")] + ResponseContentPartAdded(ResponseContentPartAdded), + /// Text delta update + #[serde(rename = "response.output_text.delta")] + ResponseOutputTextDelta(ResponseOutputTextDelta), + /// Text output completed + #[serde(rename = "response.output_text.done")] + ResponseOutputTextDone(ResponseOutputTextDone), + /// Refusal delta update + #[serde(rename = "response.refusal.delta")] + ResponseRefusalDelta(ResponseRefusalDelta), + /// Refusal completed + #[serde(rename = "response.refusal.done")] + ResponseRefusalDone(ResponseRefusalDone), + /// Content part completed + #[serde(rename = "response.content_part.done")] + ResponseContentPartDone(ResponseContentPartDone), + /// Output item completed + #[serde(rename = "response.output_item.done")] + ResponseOutputItemDone(ResponseOutputItemDone), + /// Function call arguments delta + #[serde(rename = "response.function_call_arguments.delta")] + ResponseFunctionCallArgumentsDelta(ResponseFunctionCallArgumentsDelta), + /// Function call arguments completed + #[serde(rename = "response.function_call_arguments.done")] + ResponseFunctionCallArgumentsDone(ResponseFunctionCallArgumentsDone), + /// File search call in progress + #[serde(rename = "response.file_search_call.in_progress")] + ResponseFileSearchCallInProgress(ResponseFileSearchCallInProgress), + /// File search call searching + #[serde(rename = "response.file_search_call.searching")] + ResponseFileSearchCallSearching(ResponseFileSearchCallSearching), + /// File search call completed + #[serde(rename = "response.file_search_call.completed")] + ResponseFileSearchCallCompleted(ResponseFileSearchCallCompleted), + /// Web search call in progress + #[serde(rename = "response.web_search_call.in_progress")] + ResponseWebSearchCallInProgress(ResponseWebSearchCallInProgress), + /// Web search call searching + #[serde(rename = "response.web_search_call.searching")] + ResponseWebSearchCallSearching(ResponseWebSearchCallSearching), + /// Web search call completed + #[serde(rename = "response.web_search_call.completed")] + ResponseWebSearchCallCompleted(ResponseWebSearchCallCompleted), + /// Reasoning summary part added + #[serde(rename = "response.reasoning_summary_part.added")] + ResponseReasoningSummaryPartAdded(ResponseReasoningSummaryPartAdded), + /// Reasoning summary part done + #[serde(rename = "response.reasoning_summary_part.done")] + ResponseReasoningSummaryPartDone(ResponseReasoningSummaryPartDone), + /// Reasoning summary text delta + #[serde(rename = "response.reasoning_summary_text.delta")] + ResponseReasoningSummaryTextDelta(ResponseReasoningSummaryTextDelta), + /// Reasoning summary text done + #[serde(rename = "response.reasoning_summary_text.done")] + ResponseReasoningSummaryTextDone(ResponseReasoningSummaryTextDone), + /// Reasoning summary delta + #[serde(rename = "response.reasoning_summary.delta")] + ResponseReasoningSummaryDelta(ResponseReasoningSummaryDelta), + /// Reasoning summary done + #[serde(rename = "response.reasoning_summary.done")] + ResponseReasoningSummaryDone(ResponseReasoningSummaryDone), + /// Image generation call in progress + #[serde(rename = "response.image_generation_call.in_progress")] + ResponseImageGenerationCallInProgress(ResponseImageGenerationCallInProgress), + /// Image generation call generating + #[serde(rename = "response.image_generation_call.generating")] + ResponseImageGenerationCallGenerating(ResponseImageGenerationCallGenerating), + /// Image generation call partial image + #[serde(rename = "response.image_generation_call.partial_image")] + ResponseImageGenerationCallPartialImage(ResponseImageGenerationCallPartialImage), + /// Image generation call completed + #[serde(rename = "response.image_generation_call.completed")] + ResponseImageGenerationCallCompleted(ResponseImageGenerationCallCompleted), + /// MCP call arguments delta + #[serde(rename = "response.mcp_call_arguments.delta")] + ResponseMcpCallArgumentsDelta(ResponseMcpCallArgumentsDelta), + /// MCP call arguments done + #[serde(rename = "response.mcp_call_arguments.done")] + ResponseMcpCallArgumentsDone(ResponseMcpCallArgumentsDone), + /// MCP call completed + #[serde(rename = "response.mcp_call.completed")] + ResponseMcpCallCompleted(ResponseMcpCallCompleted), + /// MCP call failed + #[serde(rename = "response.mcp_call.failed")] + ResponseMcpCallFailed(ResponseMcpCallFailed), + /// MCP call in progress + #[serde(rename = "response.mcp_call.in_progress")] + ResponseMcpCallInProgress(ResponseMcpCallInProgress), + /// MCP list tools completed + #[serde(rename = "response.mcp_list_tools.completed")] + ResponseMcpListToolsCompleted(ResponseMcpListToolsCompleted), + /// MCP list tools failed + #[serde(rename = "response.mcp_list_tools.failed")] + ResponseMcpListToolsFailed(ResponseMcpListToolsFailed), + /// MCP list tools in progress + #[serde(rename = "response.mcp_list_tools.in_progress")] + ResponseMcpListToolsInProgress(ResponseMcpListToolsInProgress), + /// Code interpreter call in progress + #[serde(rename = "response.code_interpreter_call.in_progress")] + ResponseCodeInterpreterCallInProgress(ResponseCodeInterpreterCallInProgress), + /// Code interpreter call interpreting + #[serde(rename = "response.code_interpreter_call.interpreting")] + ResponseCodeInterpreterCallInterpreting(ResponseCodeInterpreterCallInterpreting), + /// Code interpreter call completed + #[serde(rename = "response.code_interpreter_call.completed")] + ResponseCodeInterpreterCallCompleted(ResponseCodeInterpreterCallCompleted), + /// Code interpreter call code delta + #[serde(rename = "response.code_interpreter_call_code.delta")] + ResponseCodeInterpreterCallCodeDelta(ResponseCodeInterpreterCallCodeDelta), + /// Code interpreter call code done + #[serde(rename = "response.code_interpreter_call_code.done")] + ResponseCodeInterpreterCallCodeDone(ResponseCodeInterpreterCallCodeDone), + /// Output text annotation added + #[serde(rename = "response.output_text.annotation.added")] + ResponseOutputTextAnnotationAdded(ResponseOutputTextAnnotationAdded), + /// Error occurred + #[serde(rename = "error")] + ResponseError(ResponseError), + + /// Unknown event type + #[serde(untagged)] + Unknown(serde_json::Value), +} + +/// Stream of response events +pub type ResponseStream = Pin> + Send>>; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseCreated { + pub sequence_number: u64, + pub response: ResponseMetadata, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseInProgress { + pub sequence_number: u64, + pub response: ResponseMetadata, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseOutputItemAdded { + pub sequence_number: u64, + pub output_index: u32, + pub item: OutputItem, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseContentPartAdded { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub content_index: u32, + pub part: ContentPart, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseOutputTextDelta { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub content_index: u32, + pub delta: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub logprobs: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseContentPartDone { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub content_index: u32, + pub part: ContentPart, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseOutputItemDone { + pub sequence_number: u64, + pub output_index: u32, + pub item: OutputItem, +} + +/// Response completed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseCompleted { + pub sequence_number: u64, + pub response: ResponseMetadata, +} + +/// Response failed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseFailed { + pub sequence_number: u64, + pub response: ResponseMetadata, +} + +/// Response incomplete event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseIncomplete { + pub sequence_number: u64, + pub response: ResponseMetadata, +} + +/// Response queued event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseQueued { + pub sequence_number: u64, + pub response: ResponseMetadata, +} + +/// Text output completed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseOutputTextDone { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub content_index: u32, + pub text: String, + pub logprobs: Option>, +} + +/// Refusal delta event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseRefusalDelta { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub content_index: u32, + pub delta: String, +} + +/// Refusal done event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseRefusalDone { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub content_index: u32, + pub refusal: String, +} + +/// Function call arguments delta event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseFunctionCallArgumentsDelta { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub delta: String, +} + +/// Function call arguments done event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseFunctionCallArgumentsDone { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub arguments: String, +} + +/// Error event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseError { + pub sequence_number: u64, + pub code: Option, + pub message: String, + pub param: Option, +} + +/// File search call in progress event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseFileSearchCallInProgress { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// File search call searching event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseFileSearchCallSearching { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// File search call completed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseFileSearchCallCompleted { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Web search call in progress event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseWebSearchCallInProgress { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Web search call searching event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseWebSearchCallSearching { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Web search call completed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseWebSearchCallCompleted { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Reasoning summary part added event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseReasoningSummaryPartAdded { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub summary_index: u32, + pub part: serde_json::Value, // Could be more specific but using Value for flexibility +} + +/// Reasoning summary part done event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseReasoningSummaryPartDone { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub summary_index: u32, + pub part: serde_json::Value, +} + +/// Reasoning summary text delta event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseReasoningSummaryTextDelta { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub summary_index: u32, + pub delta: String, +} + +/// Reasoning summary text done event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseReasoningSummaryTextDone { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub summary_index: u32, + pub text: String, +} + +/// Reasoning summary delta event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseReasoningSummaryDelta { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub summary_index: u32, + pub delta: serde_json::Value, +} + +/// Reasoning summary done event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseReasoningSummaryDone { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub summary_index: u32, + pub text: String, +} + +/// Image generation call in progress event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseImageGenerationCallInProgress { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Image generation call generating event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseImageGenerationCallGenerating { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Image generation call partial image event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseImageGenerationCallPartialImage { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, + pub partial_image_index: u32, + pub partial_image_b64: String, +} + +/// Image generation call completed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseImageGenerationCallCompleted { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// MCP call arguments delta event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMcpCallArgumentsDelta { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, + pub delta: String, +} + +/// MCP call arguments done event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMcpCallArgumentsDone { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, + pub arguments: String, +} + +/// MCP call completed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMcpCallCompleted { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// MCP call failed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMcpCallFailed { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// MCP call in progress event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMcpCallInProgress { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// MCP list tools completed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMcpListToolsCompleted { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// MCP list tools failed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMcpListToolsFailed { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// MCP list tools in progress event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMcpListToolsInProgress { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Code interpreter call in progress event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseCodeInterpreterCallInProgress { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Code interpreter call interpreting event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseCodeInterpreterCallInterpreting { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Code interpreter call completed event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseCodeInterpreterCallCompleted { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, +} + +/// Code interpreter call code delta event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseCodeInterpreterCallCodeDelta { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, + pub delta: String, +} + +/// Code interpreter call code done event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseCodeInterpreterCallCodeDone { + pub sequence_number: u64, + pub output_index: u32, + pub item_id: String, + pub code: String, +} + +/// Response metadata +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseMetadata { + pub id: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub object: Option, + pub created_at: u64, + pub status: Status, + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub usage: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub incomplete_details: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub input: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub max_output_tokens: Option, + /// Whether the model was run in background mode + #[serde(skip_serializing_if = "Option::is_none")] + pub background: Option, + /// The service tier that was actually used + #[serde(skip_serializing_if = "Option::is_none")] + pub service_tier: Option, + /// The effective value of top_logprobs parameter + #[serde(skip_serializing_if = "Option::is_none")] + pub top_logprobs: Option, + /// The effective value of max_tool_calls parameter + #[serde(skip_serializing_if = "Option::is_none")] + pub max_tool_calls: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub output: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + pub parallel_tool_calls: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub previous_response_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub reasoning: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub store: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub text: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_choice: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub truncation: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub user: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, + /// Prompt cache key for improved performance + #[serde(skip_serializing_if = "Option::is_none")] + pub prompt_cache_key: Option, + /// Safety identifier for content filtering + #[serde(skip_serializing_if = "Option::is_none")] + pub safety_identifier: Option, +} + +/// Output item +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +#[non_exhaustive] +pub enum OutputItem { + Message(OutputMessage), + FileSearchCall(FileSearchCallOutput), + FunctionCall(FunctionCall), + WebSearchCall(WebSearchCallOutput), + ComputerCall(ComputerCallOutput), + Reasoning(ReasoningItem), + ImageGenerationCall(ImageGenerationCallOutput), + CodeInterpreterCall(CodeInterpreterCallOutput), + LocalShellCall(LocalShellCallOutput), + McpCall(McpCallOutput), + McpListTools(McpListToolsOutput), + McpApprovalRequest(McpApprovalRequestOutput), + CustomToolCall(CustomToolCallOutput), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct CustomToolCallOutput { + pub call_id: String, + pub input: String, + pub name: String, + pub id: String, +} + +/// Content part +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ContentPart { + #[serde(rename = "type")] + pub part_type: String, + pub text: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub annotations: Option>, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub logprobs: Option>, +} + +// ===== RESPONSE COLLECTOR ===== + +/// Collects streaming response events into a complete response + +/// Output text annotation added event +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct ResponseOutputTextAnnotationAdded { + pub sequence_number: u64, + pub item_id: String, + pub output_index: u32, + pub content_index: u32, + pub annotation_index: u32, + pub annotation: TextAnnotation, +} + +/// Text annotation object for output text +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[non_exhaustive] +pub struct TextAnnotation { + #[serde(rename = "type")] + pub annotation_type: String, + pub text: String, + pub start: u32, + pub end: u32, +} diff --git a/async-openai/src/types/run.rs b/async-openai/src/types/run.rs new file mode 100644 index 00000000..8be4ad99 --- /dev/null +++ b/async-openai/src/types/run.rs @@ -0,0 +1,285 @@ +use std::collections::HashMap; + +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::{error::OpenAIError, types::FunctionCall}; + +use super::{ + AssistantTools, AssistantsApiResponseFormatOption, AssistantsApiToolChoiceOption, + CreateMessageRequest, +}; + +/// Represents an execution run on a [thread](https://platform.openai.com/docs/api-reference/threads). +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunObject { + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `thread.run`. + pub object: String, + /// The Unix timestamp (in seconds) for when the run was created. + pub created_at: i32, + ///The ID of the [thread](https://platform.openai.com/docs/api-reference/threads) that was executed on as a part of this run. + pub thread_id: String, + + /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for execution of this run. + pub assistant_id: Option, + + /// The status of the run, which can be either `queued`, `in_progress`, `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`, `incomplete`, or `expired`. + pub status: RunStatus, + + /// Details on the action required to continue the run. Will be `null` if no action is required. + pub required_action: Option, + + /// The last error associated with this run. Will be `null` if there are no errors. + pub last_error: Option, + + /// The Unix timestamp (in seconds) for when the run will expire. + pub expires_at: Option, + /// The Unix timestamp (in seconds) for when the run was started. + pub started_at: Option, + /// The Unix timestamp (in seconds) for when the run was cancelled. + pub cancelled_at: Option, + /// The Unix timestamp (in seconds) for when the run failed. + pub failed_at: Option, + ///The Unix timestamp (in seconds) for when the run was completed. + pub completed_at: Option, + + /// Details on why the run is incomplete. Will be `null` if the run is not incomplete. + pub incomplete_details: Option, + + /// The model that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run. + pub model: String, + + /// The instructions that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run. + pub instructions: String, + + /// The list of tools that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run. + pub tools: Vec, + + pub metadata: Option>, + + /// Usage statistics related to the run. This value will be `null` if the run is not in a terminal state (i.e. `in_progress`, `queued`, etc.). + pub usage: Option, + + /// The sampling temperature used for this run. If not set, defaults to 1. + pub temperature: Option, + + /// The nucleus sampling value used for this run. If not set, defaults to 1. + pub top_p: Option, + + /// The maximum number of prompt tokens specified to have been used over the course of the run. + pub max_prompt_tokens: Option, + + /// The maximum number of completion tokens specified to have been used over the course of the run. + pub max_completion_tokens: Option, + + /// Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run. + pub truncation_strategy: Option, + + pub tool_choice: Option, + + /// 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, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)] +#[serde(rename_all = "snake_case")] +pub enum TruncationObjectType { + #[default] + Auto, + LastMessages, +} + +/// Thread Truncation Controls +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct TruncationObject { + /// The truncation strategy to use for the thread. The default is `auto`. If set to `last_messages`, the thread will be truncated to the n most recent messages in the thread. When set to `auto`, messages in the middle of the thread will be dropped to fit the context length of the model, `max_prompt_tokens`. + pub r#type: TruncationObjectType, + /// The number of most recent messages from the thread when constructing the context for the run. + pub last_messages: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunObjectIncompleteDetails { + /// The reason why the run is incomplete. This will point to which specific token limit was reached over the course of the run. + pub reason: RunObjectIncompleteDetailsReason, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum RunObjectIncompleteDetailsReason { + MaxCompletionTokens, + MaxPromptTokens, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum RunStatus { + Queued, + InProgress, + RequiresAction, + Cancelling, + Cancelled, + Failed, + Completed, + Incomplete, + Expired, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RequiredAction { + /// For now, this is always `submit_tool_outputs`. + pub r#type: String, + + pub submit_tool_outputs: SubmitToolOutputs, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct SubmitToolOutputs { + pub tool_calls: Vec, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunToolCallObject { + /// The ID of the tool call. This ID must be referenced when you submit the tool outputs in using the [Submit tool outputs to run](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) endpoint. + pub id: String, + /// The type of tool call the output is required for. For now, this is always `function`. + pub r#type: String, + /// The function definition. + pub function: FunctionCall, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct LastError { + /// One of `server_error`, `rate_limit_exceeded`, or `invalid_prompt`. + pub code: LastErrorCode, + /// A human-readable description of the error. + pub message: String, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum LastErrorCode { + ServerError, + RateLimitExceeded, + InvalidPrompt, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunCompletionUsage { + /// Number of completion tokens used over the course of the run. + pub completion_tokens: u32, + /// Number of prompt tokens used over the course of the run. + pub prompt_tokens: u32, + /// Total number of tokens used (prompt + completion). + pub total_tokens: u32, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "CreateRunRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateRunRequest { + /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to execute this run. + pub assistant_id: String, + + /// The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used. + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, + + /// Overrides the [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant) of the assistant. This is useful for modifying the behavior on a per-run basis. + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + + /// Appends additional instructions at the end of the instructions for the run. This is useful for modifying the behavior on a per-run basis without overriding other instructions. + #[serde(skip_serializing_if = "Option::is_none")] + pub additional_instructions: Option, + + /// Adds additional messages to the thread before creating the run. + #[serde(skip_serializing_if = "Option::is_none")] + pub additional_messages: Option>, + + /// Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis. + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, + + /// The sampling temperature used for this run. If not set, defaults to 1. + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, + + /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + /// + /// We generally recommend altering this or temperature but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, + + /// If `true`, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a `data: [DONE]` message. + #[serde(skip_serializing_if = "Option::is_none")] + pub stream: Option, + + /// The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_prompt_tokens: Option, + + /// The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_completion_tokens: Option, + + /// Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run. + #[serde(skip_serializing_if = "Option::is_none")] + pub truncation_strategy: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_choice: Option, + + /// 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, + + #[serde(skip_serializing_if = "Option::is_none")] + pub response_format: Option, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct ModifyRunRequest { + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct ListRunsResponse { + pub object: String, + pub data: Vec, + pub first_id: Option, + pub last_id: Option, + pub has_more: bool, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct SubmitToolOutputsRunRequest { + /// A list of tools for which the outputs are being submitted. + pub tool_outputs: Vec, + /// If `true`, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a `data: [DONE]` message. + pub stream: Option, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "ToolsOutputsArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct ToolsOutputs { + /// The ID of the tool call in the `required_action` object within the run object the output is being submitted for. + pub tool_call_id: Option, + /// The output of the tool call to be submitted to continue the run. + pub output: Option, +} diff --git a/async-openai/src/types/step.rs b/async-openai/src/types/step.rs new file mode 100644 index 00000000..d95b3c18 --- /dev/null +++ b/async-openai/src/types/step.rs @@ -0,0 +1,334 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +use super::{FileSearchRankingOptions, ImageFile, LastError, RunStatus}; + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum RunStepType { + MessageCreation, + ToolCalls, +} + +/// Represents a step in execution of a run. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepObject { + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `thread.run.step`. + pub object: String, + /// The Unix timestamp (in seconds) for when the run step was created. + pub created_at: i32, + + /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) associated with the run step. + pub assistant_id: Option, + + /// The ID of the [thread](https://platform.openai.com/docs/api-reference/threads) that was run. + pub thread_id: String, + + /// The ID of the [run](https://platform.openai.com/docs/api-reference/runs) that this run step is a part of. + pub run_id: String, + + /// The type of run step, which can be either `message_creation` or `tool_calls`. + pub r#type: RunStepType, + + /// The status of the run step, which can be either `in_progress`, `cancelled`, `failed`, `completed`, or `expired`. + pub status: RunStatus, + + /// The details of the run step. + pub step_details: StepDetails, + + /// The last error associated with this run. Will be `null` if there are no errors. + pub last_error: Option, + + ///The Unix timestamp (in seconds) for when the run step expired. A step is considered expired if the parent run is expired. + pub expires_at: Option, + + /// The Unix timestamp (in seconds) for when the run step was cancelled. + pub cancelled_at: Option, + + /// The Unix timestamp (in seconds) for when the run step failed. + pub failed_at: Option, + + /// The Unix timestamp (in seconds) for when the run step completed. + pub completed_at: Option, + + pub metadata: Option>, + + /// Usage statistics related to the run step. This value will be `null` while the run step's status is `in_progress`. + pub usage: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepCompletionUsage { + /// Number of completion tokens used over the course of the run step. + pub completion_tokens: u32, + /// Number of prompt tokens used over the course of the run step. + pub prompt_tokens: u32, + /// Total number of tokens used (prompt + completion). + pub total_tokens: u32, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum StepDetails { + MessageCreation(RunStepDetailsMessageCreationObject), + ToolCalls(RunStepDetailsToolCallsObject), +} + +/// Details of the message creation by the run step. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsMessageCreationObject { + pub message_creation: MessageCreation, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct MessageCreation { + /// The ID of the message that was created by this run step. + pub message_id: String, +} + +/// Details of the tool call. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsObject { + /// An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `file_search`, or `function`. + pub tool_calls: Vec, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum RunStepDetailsToolCalls { + /// Details of the Code Interpreter tool call the run step was involved in. + CodeInterpreter(RunStepDetailsToolCallsCodeObject), + FileSearch(RunStepDetailsToolCallsFileSearchObject), + Function(RunStepDetailsToolCallsFunctionObject), +} + +/// Code interpreter tool call +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsCodeObject { + /// The ID of the tool call. + pub id: String, + + /// The Code Interpreter tool call definition. + pub code_interpreter: CodeInterpreter, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct CodeInterpreter { + /// The input to the Code Interpreter tool call. + pub input: String, + /// The outputs from the Code Interpreter tool call. Code Interpreter can output one or more items, including text (`logs`) or images (`image`). Each of these are represented by a different object type. + pub outputs: Vec, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "lowercase")] +pub enum CodeInterpreterOutput { + /// Code interpreter log output + Logs(RunStepDetailsToolCallsCodeOutputLogsObject), + /// Code interpreter image output + Image(RunStepDetailsToolCallsCodeOutputImageObject), +} + +/// Text output from the Code Interpreter tool call as part of a run step. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsCodeOutputLogsObject { + /// The text output from the Code Interpreter tool call. + pub logs: String, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsCodeOutputImageObject { + /// The [file](https://platform.openai.com/docs/api-reference/files) ID of the image. + pub image: ImageFile, +} + +/// File search tool call +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsFileSearchObject { + /// The ID of the tool call object. + pub id: String, + /// For now, this is always going to be an empty object. + pub file_search: RunStepDetailsToolCallsFileSearchObjectFileSearch, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsFileSearchObjectFileSearch { + pub ranking_options: Option, + /// The results of the file search. + pub results: Option>, +} + +/// A result instance of the file search. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsFileSearchResultObject { + /// The ID of the file that result was found in. + pub file_id: String, + /// The name of the file that result was found in. + pub file_name: String, + /// The score of the result. All values must be a floating point number between 0 and 1. + pub score: f32, + /// The content of the result that was found. The content is only included if requested via the include query parameter. + pub content: Option>, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsFileSearchResultObjectContent { + // note: type is text hence omitted from struct + /// The text content of the file. + pub text: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDetailsToolCallsFunctionObject { + /// The ID of the tool call object. + pub id: String, + /// he definition of the function that was called. + pub function: RunStepFunctionObject, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepFunctionObject { + /// The name of the function. + pub name: String, + /// The arguments passed to the function. + pub arguments: String, + /// The output of the function. This will be `null` if the outputs have not been [submitted](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) yet. + pub output: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepFunctionObjectDelta { + /// The name of the function. + pub name: Option, + /// The arguments passed to the function. + pub arguments: Option, + /// The output of the function. This will be `null` if the outputs have not been [submitted](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) yet. + pub output: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct ListRunStepsResponse { + pub object: String, + pub data: Vec, + pub first_id: Option, + pub last_id: Option, + pub has_more: bool, +} + +/// Represents a run step delta i.e. any changed fields on a run step during streaming. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDeltaObject { + /// The identifier of the run step, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `thread.run.step.delta`. + pub object: String, + /// The delta containing the fields that have changed on the run step. + pub delta: RunStepDelta, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDelta { + pub step_details: DeltaStepDetails, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum DeltaStepDetails { + MessageCreation(RunStepDeltaStepDetailsMessageCreationObject), + ToolCalls(RunStepDeltaStepDetailsToolCallsObject), +} + +/// Details of the message creation by the run step. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDeltaStepDetailsMessageCreationObject { + pub message_creation: Option, +} + +/// Details of the tool call. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDeltaStepDetailsToolCallsObject { + /// An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `file_search`, or `function`. + pub tool_calls: Option>, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum RunStepDeltaStepDetailsToolCalls { + CodeInterpreter(RunStepDeltaStepDetailsToolCallsCodeObject), + FileSearch(RunStepDeltaStepDetailsToolCallsFileSearchObject), + Function(RunStepDeltaStepDetailsToolCallsFunctionObject), +} + +/// Details of the Code Interpreter tool call the run step was involved in. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDeltaStepDetailsToolCallsCodeObject { + /// The index of the tool call in the tool calls array. + pub index: u32, + /// The ID of the tool call. + pub id: Option, + /// The Code Interpreter tool call definition. + pub code_interpreter: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct DeltaCodeInterpreter { + /// The input to the Code Interpreter tool call. + pub input: Option, + /// The outputs from the Code Interpreter tool call. Code Interpreter can output one or more items, including text (`logs`) or images (`image`). Each of these are represented by a different object type. + pub outputs: Option>, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +#[serde(tag = "type")] +#[serde(rename_all = "lowercase")] +pub enum DeltaCodeInterpreterOutput { + Logs(RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject), + Image(RunStepDeltaStepDetailsToolCallsCodeOutputImageObject), +} + +/// Text output from the Code Interpreter tool call as part of a run step. +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject { + /// The index of the output in the outputs array. + pub index: u32, + /// The text output from the Code Interpreter tool call. + pub logs: Option, +} + +/// Code interpreter image output +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDeltaStepDetailsToolCallsCodeOutputImageObject { + /// The index of the output in the outputs array. + pub index: u32, + + pub image: Option, +} + +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDeltaStepDetailsToolCallsFileSearchObject { + /// The index of the tool call in the tool calls array. + pub index: u32, + /// The ID of the tool call object. + pub id: Option, + /// For now, this is always going to be an empty object. + pub file_search: Option, +} + +/// Function tool call +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct RunStepDeltaStepDetailsToolCallsFunctionObject { + /// The index of the tool call in the tool calls array. + pub index: u32, + /// The ID of the tool call object. + pub id: Option, + /// The definition of the function that was called. + pub function: Option, +} diff --git a/async-openai/src/types/thread.rs b/async-openai/src/types/thread.rs new file mode 100644 index 00000000..199d8a46 --- /dev/null +++ b/async-openai/src/types/thread.rs @@ -0,0 +1,134 @@ +use std::collections::HashMap; + +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +use super::{ + AssistantToolResources, AssistantTools, AssistantsApiResponseFormatOption, + AssistantsApiToolChoiceOption, CreateAssistantToolResources, CreateMessageRequest, + TruncationObject, +}; + +/// Represents a thread that contains [messages](https://platform.openai.com/docs/api-reference/messages). +#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)] +pub struct ThreadObject { + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `thread`. + pub object: String, + /// The Unix timestamp (in seconds) for when the thread was created. + pub created_at: i32, + + /// A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + pub tool_resources: Option, + + pub metadata: Option>, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "CreateThreadRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateThreadRequest { + /// A list of [messages](https://platform.openai.com/docs/api-reference/messages) to start the thread with. + #[serde(skip_serializing_if = "Option::is_none")] + pub messages: Option>, + + /// A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_resources: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct ModifyThreadRequest { + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, + + /// A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_resources: Option, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)] +pub struct DeleteThreadResponse { + pub id: String, + pub deleted: bool, + pub object: String, +} + +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "CreateThreadAndRunRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateThreadAndRunRequest { + /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to execute this run. + pub assistant_id: String, + + /// If no thread is provided, an empty thread will be created. + #[serde(skip_serializing_if = "Option::is_none")] + pub thread: Option, + + /// The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used. + #[serde(skip_serializing_if = "Option::is_none")] + pub model: Option, + + /// Override the default system message of the assistant. This is useful for modifying the behavior on a per-run basis. + #[serde(skip_serializing_if = "Option::is_none")] + pub instructions: Option, + + /// Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis. + #[serde(skip_serializing_if = "Option::is_none")] + pub tools: Option>, + + /// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_resources: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, + + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + #[serde(skip_serializing_if = "Option::is_none")] + pub temperature: Option, + + /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + /// + /// We generally recommend altering this or temperature but not both. + #[serde(skip_serializing_if = "Option::is_none")] + pub top_p: Option, + + /// If `true`, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a `data: [DONE]` message. + #[serde(skip_serializing_if = "Option::is_none")] + pub stream: Option, + + /// The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_prompt_tokens: Option, + + /// The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_completion_tokens: Option, + + /// Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run. + #[serde(skip_serializing_if = "Option::is_none")] + pub truncation_strategy: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub tool_choice: Option, + + /// 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, + + #[serde(skip_serializing_if = "Option::is_none")] + pub response_format: Option, +} diff --git a/async-openai/src/types/upload.rs b/async-openai/src/types/upload.rs new file mode 100644 index 00000000..eb91c0e1 --- /dev/null +++ b/async-openai/src/types/upload.rs @@ -0,0 +1,126 @@ +use crate::error::OpenAIError; +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use super::{InputSource, OpenAIFile}; + +/// Request to create an upload object that can accept byte chunks in the form of Parts. +#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)] +#[builder(name = "CreateUploadRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateUploadRequest { + /// The name of the file to upload. + pub filename: String, + + /// The intended purpose of the uploaded file. + /// + /// See the [documentation on File purposes](https://platform.openai.com/docs/api-reference/files/create#files-create-purpose). + pub purpose: UploadPurpose, + + /// The number of bytes in the file you are uploading. + pub bytes: u64, + + /// The MIME type of the file. + /// + /// This must fall within the supported MIME types for your file purpose. See the supported MIME + /// types for assistants and vision. + pub mime_type: String, +} + +/// The intended purpose of the uploaded file. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)] +#[serde(rename_all = "snake_case")] +pub enum UploadPurpose { + /// For use with Assistants and Message files + Assistants, + /// For Assistants image file inputs + Vision, + /// For use with the Batch API + Batch, + /// For use with Fine-tuning + #[default] + FineTune, +} + +/// The Upload object can accept byte chunks in the form of Parts. +#[derive(Debug, Serialize, Deserialize)] +pub struct Upload { + /// The Upload unique identifier, which can be referenced in API endpoints + pub id: String, + + /// The Unix timestamp (in seconds) for when the Upload was created + pub created_at: u32, + + /// The name of the file to be uploaded + pub filename: String, + + /// The intended number of bytes to be uploaded + pub bytes: u64, + + /// The intended purpose of the file. [Pelase refer here]([Please refer here](/docs/api-reference/files/object#files/object-purpose) for acceptable values.) + pub purpose: UploadPurpose, + + /// The status of the Upload. + pub status: UploadStatus, + + /// The Unix timestamp (in seconds) for when the Upload was created + pub expires_at: u32, + + /// The object type, which is always "upload" + pub object: String, + + /// The ready File object after the Upload is completed + #[serde(skip_serializing_if = "Option::is_none")] + pub file: Option, +} + +/// The status of an upload +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum UploadStatus { + /// Upload is pending + Pending, + /// Upload has completed successfully + Completed, + /// Upload was cancelled + Cancelled, + /// Upload has expired + Expired, +} + +/// The upload Part represents a chunk of bytes we can add to an Upload object. +#[derive(Debug, Serialize, Deserialize)] +pub struct UploadPart { + /// The upload Part unique identifier, which can be referenced in API endpoints + pub id: String, + + /// The Unix timestamp (in seconds) for when the Part was created + pub created_at: u32, + + /// The ID of the Upload object that this Part was added to + pub upload_id: String, + + /// The object type, which is always `upload.part` + pub object: String, +} + +/// Request parameters for adding a part to an Upload +#[derive(Debug, Clone)] +pub struct AddUploadPartRequest { + /// The chunk of bytes for this Part + pub data: InputSource, +} + +/// Request parameters for completing an Upload +#[derive(Debug, Serialize)] +pub struct CompleteUploadRequest { + /// The ordered list of Part IDs + pub part_ids: Vec, + + /// The optional md5 checksum for the file contents to verify if the bytes uploaded matches what you expect + #[serde(skip_serializing_if = "Option::is_none")] + pub md5: Option, +} diff --git a/async-openai/src/types/users.rs b/async-openai/src/types/users.rs new file mode 100644 index 00000000..5fd0760c --- /dev/null +++ b/async-openai/src/types/users.rs @@ -0,0 +1,51 @@ +use crate::types::OpenAIError; +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use super::OrganizationRole; + +/// Represents an individual `user` within an organization. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct User { + /// The object type, which is always `organization.user` + pub object: String, + /// The identifier, which can be referenced in API endpoints + pub id: String, + /// The name of the user + pub name: String, + /// The email address of the user + pub email: String, + /// `owner` or `reader` + pub role: OrganizationRole, + /// The Unix timestamp (in seconds) of when the users was added. + pub added_at: u32, +} + +/// A list of `User` objects. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct UserListResponse { + pub object: String, + pub data: Vec, + pub first_id: String, + pub last_id: String, + pub has_more: bool, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Builder)] +#[builder(name = "UserRoleUpdateRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option))] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct UserRoleUpdateRequest { + /// `owner` or `reader` + pub role: OrganizationRole, +} + +/// Confirmation of the deleted user +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct UserDeleteResponse { + pub object: String, + pub id: String, + pub deleted: bool, +} diff --git a/async-openai/src/types/vector_store.rs b/async-openai/src/types/vector_store.rs new file mode 100644 index 00000000..b1682633 --- /dev/null +++ b/async-openai/src/types/vector_store.rs @@ -0,0 +1,518 @@ +use std::collections::HashMap; + +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; + +use crate::error::OpenAIError; + +use super::StaticChunkingStrategy; + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "CreateVectorStoreRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateVectorStoreRequest { + /// A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that the vector store should use. Useful for tools like `file_search` that can access files. + #[serde(skip_serializing_if = "Option::is_none")] + pub file_ids: Option>, + /// The name of the vector store. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + + /// The expiration policy for a vector store. + #[serde(skip_serializing_if = "Option::is_none")] + pub expires_after: Option, + + /// The chunking strategy used to chunk the file(s). If not set, will use the `auto` strategy. Only applicable if `file_ids` is non-empty. + #[serde(skip_serializing_if = "Option::is_none")] + pub chunking_strategy: Option, + + /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long. + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +#[serde(tag = "type")] +pub enum VectorStoreChunkingStrategy { + /// The default strategy. This strategy currently uses a `max_chunk_size_tokens` of `800` and `chunk_overlap_tokens` of `400`. + #[default] + #[serde(rename = "auto")] + Auto, + #[serde(rename = "static")] + Static { + #[serde(rename = "static")] + config: StaticChunkingStrategy, + }, +} + +/// Vector store expiration policy +#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)] +pub struct VectorStoreExpirationAfter { + /// Anchor timestamp after which the expiration policy applies. Supported anchors: `last_active_at`. + pub anchor: String, + /// The number of days after the anchor time that the vector store will expire. + pub days: u16, // min: 1, max: 365 +} + +/// A vector store is a collection of processed files can be used by the `file_search` tool. +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreObject { + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `vector_store`. + pub object: String, + /// The Unix timestamp (in seconds) for when the vector store was created. + pub created_at: u32, + /// The name of the vector store. + pub name: Option, + /// The total number of bytes used by the files in the vector store. + pub usage_bytes: u64, + pub file_counts: VectorStoreFileCounts, + /// The status of the vector store, which can be either `expired`, `in_progress`, or `completed`. A status of `completed` indicates that the vector store is ready for use. + pub status: VectorStoreStatus, + pub expires_after: Option, + /// The Unix timestamp (in seconds) for when the vector store will expire. + pub expires_at: Option, + /// The Unix timestamp (in seconds) for when the vector store was last active. + pub last_active_at: Option, + + /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long. + pub metadata: Option>, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum VectorStoreStatus { + Expired, + InProgress, + Completed, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreFileCounts { + /// The number of files that are currently being processed. + pub in_progress: u32, + /// The number of files that have been successfully processed. + pub completed: u32, + /// The number of files that have failed to process. + pub failed: u32, + /// The number of files that were cancelled. + pub cancelled: u32, + /// The total number of files. + pub total: u32, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct ListVectorStoresResponse { + pub object: String, + pub data: Vec, + pub first_id: Option, + pub last_id: Option, + pub has_more: bool, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct DeleteVectorStoreResponse { + pub id: String, + pub object: String, + pub deleted: bool, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "UpdateVectorStoreRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct UpdateVectorStoreRequest { + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub expires_after: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option>, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct ListVectorStoreFilesResponse { + pub object: String, + pub data: Vec, + pub first_id: Option, + pub last_id: Option, + pub has_more: bool, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreFileObject { + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `vector_store.file`. + pub object: String, + /// The total vector store usage in bytes. Note that this may be different from the original file size. + pub usage_bytes: u64, + /// The Unix timestamp (in seconds) for when the vector store file was created. + pub created_at: u32, + /// The ID of the [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) that the [File](https://platform.openai.com/docs/api-reference/files) is attached to. + pub vector_store_id: String, + /// The status of the vector store file, which can be either `in_progress`, `completed`, `cancelled`, or `failed`. The status `completed` indicates that the vector store file is ready for use. + pub status: VectorStoreFileStatus, + /// The last error associated with this vector store file. Will be `null` if there are no errors. + pub last_error: Option, + /// The strategy used to chunk the file. + pub chunking_strategy: Option, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum VectorStoreFileStatus { + InProgress, + Completed, + Cancelled, + Failed, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreFileError { + pub code: VectorStoreFileErrorCode, + /// A human-readable description of the error. + pub message: String, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum VectorStoreFileErrorCode { + ServerError, + UnsupportedFile, + InvalidFile, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +#[serde(tag = "type")] +#[serde(rename_all = "lowercase")] +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, + }, +} + +#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)] +#[builder(name = "CreateVectorStoreFileRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateVectorStoreFileRequest { + /// A [File](https://platform.openai.com/docs/api-reference/files) ID that the vector store should use. Useful for tools like `file_search` that can access files. + pub file_id: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub chunking_strategy: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub attributes: Option>, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct DeleteVectorStoreFileResponse { + pub id: String, + pub object: String, + pub deleted: bool, +} + +#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq, Deserialize)] +#[builder(name = "CreateVectorStoreFileBatchRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct CreateVectorStoreFileBatchRequest { + /// A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that the vector store should use. Useful for tools like `file_search` that can access files. + pub file_ids: Vec, // minItems: 1, maxItems: 500 + pub chunking_strategy: Option, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum VectorStoreFileBatchStatus { + InProgress, + Completed, + Cancelled, + Failed, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreFileBatchCounts { + /// The number of files that are currently being processed. + pub in_progress: u32, + /// The number of files that have been successfully processed. + pub completed: u32, + /// The number of files that have failed to process. + pub failed: u32, + /// The number of files that were cancelled. + pub cancelled: u32, + /// The total number of files. + pub total: u32, +} + +/// A batch of files attached to a vector store. +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreFileBatchObject { + /// The identifier, which can be referenced in API endpoints. + pub id: String, + /// The object type, which is always `vector_store.file_batch`. + pub object: String, + /// The Unix timestamp (in seconds) for when the vector store files batch was created. + pub created_at: u32, + /// The ID of the [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) that the [File](https://platform.openai.com/docs/api-reference/files) is attached to. + pub vector_store_id: String, + /// The status of the vector store files batch, which can be either `in_progress`, `completed`, `cancelled` or `failed`. + pub status: VectorStoreFileBatchStatus, + pub file_counts: VectorStoreFileBatchCounts, +} + +/// Represents the parsed content of a vector store file. +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreFileContentResponse { + /// The object type, which is always `vector_store.file_content.page` + pub object: String, + + /// Parsed content of the file. + pub data: Vec, + + /// Indicates if there are more content pages to fetch. + pub has_more: bool, + + /// The token for the next page, if any. + pub next_page: Option, +} + +/// Represents the parsed content of a vector store file. +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreFileContentObject { + /// The content type (currently only `"text"`) + pub r#type: String, + + /// The text content + pub text: String, +} + +#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq, Deserialize)] +#[builder(name = "VectorStoreSearchRequestArgs")] +#[builder(pattern = "mutable")] +#[builder(setter(into, strip_option), default)] +#[builder(derive(Debug))] +#[builder(build_fn(error = "OpenAIError"))] +pub struct VectorStoreSearchRequest { + /// A query string for a search. + pub query: VectorStoreSearchQuery, + + /// Whether to rewrite the natural language query for vector search. + #[serde(skip_serializing_if = "Option::is_none")] + pub rewrite_query: Option, + + /// The maximum number of results to return. This number should be between 1 and 50 inclusive. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_num_results: Option, + + /// A filter to apply based on file attributes. + #[serde(skip_serializing_if = "Option::is_none")] + pub filters: Option, + + /// Ranking options for search. + #[serde(skip_serializing_if = "Option::is_none")] + pub ranking_options: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum VectorStoreSearchQuery { + /// A single query to search for. + Text(String), + /// A list of queries to search for. + Array(Vec), +} + +impl Default for VectorStoreSearchQuery { + fn default() -> Self { + Self::Text(String::new()) + } +} + +impl From for VectorStoreSearchQuery { + fn from(query: String) -> Self { + Self::Text(query) + } +} + +impl From<&str> for VectorStoreSearchQuery { + fn from(query: &str) -> Self { + Self::Text(query.to_string()) + } +} + +impl From> for VectorStoreSearchQuery { + fn from(query: Vec) -> Self { + Self::Array(query) + } +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum VectorStoreSearchFilter { + Comparison(ComparisonFilter), + Compound(CompoundFilter), +} + +impl From for VectorStoreSearchFilter { + fn from(filter: ComparisonFilter) -> Self { + Self::Comparison(filter) + } +} + +impl From for VectorStoreSearchFilter { + fn from(filter: CompoundFilter) -> Self { + Self::Compound(filter) + } +} + +/// A filter used to compare a specified attribute key to a given value using a defined comparison operation. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct ComparisonFilter { + /// Specifies the comparison operator: `eq`, `ne`, `gt`, `gte`, `lt`, `lte`. + pub r#type: ComparisonType, + + /// The key to compare against the value. + pub key: String, + + /// The value to compare against the attribute key; supports string, number, or boolean types. + pub value: AttributeValue, +} + +/// Specifies the comparison operator: `eq`, `ne`, `gt`, `gte`, `lt`, `lte`. +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ComparisonType { + Eq, + Ne, + Gt, + Gte, + Lt, + Lte, +} + +/// The value to compare against the attribute key; supports string, number, or boolean types. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(untagged)] +pub enum AttributeValue { + String(String), + Number(i64), + Boolean(bool), +} + +impl From for AttributeValue { + fn from(value: String) -> Self { + Self::String(value) + } +} + +impl From for AttributeValue { + fn from(value: i64) -> Self { + Self::Number(value) + } +} + +impl From for AttributeValue { + fn from(value: bool) -> Self { + Self::Boolean(value) + } +} + +impl From<&str> for AttributeValue { + fn from(value: &str) -> Self { + Self::String(value.to_string()) + } +} + +/// Ranking options for search. +#[derive(Debug, Serialize, Default, Deserialize, Clone, PartialEq)] +pub struct RankingOptions { + #[serde(skip_serializing_if = "Option::is_none")] + pub ranker: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub score_threshold: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub enum Ranker { + #[serde(rename = "auto")] + Auto, + #[serde(rename = "default-2024-11-15")] + Default20241115, +} + +/// Combine multiple filters using `and` or `or`. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct CompoundFilter { + /// Type of operation: `and` or `or`. + pub r#type: CompoundFilterType, + + /// Array of filters to combine. Items can be `ComparisonFilter` or `CompoundFilter` + pub filters: Vec, +} + +/// Type of operation: `and` or `or`. +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum CompoundFilterType { + And, + Or, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreSearchResultsPage { + /// The object type, which is always `vector_store.search_results.page`. + pub object: String, + + /// The query used for this search. + pub search_query: Vec, + + /// The list of search result items. + pub data: Vec, + + /// Indicates if there are more results to fetch. + pub has_more: bool, + + /// The token for the next page, if any. + pub next_page: Option, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreSearchResultItem { + /// The ID of the vector store file. + pub file_id: String, + + /// The name of the vector store file. + pub filename: String, + + /// The similarity score for the result. + pub score: f32, // minimum: 0, maximum: 1 + + /// Attributes of the vector store file. + pub attributes: HashMap, + + /// Content chunks from the file. + pub content: Vec, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct VectorStoreSearchResultContentObject { + /// The type of content + pub r#type: String, + + /// The text content returned from search. + pub text: String, +} diff --git a/async-openai/src/uploads.rs b/async-openai/src/uploads.rs new file mode 100644 index 00000000..ba3cced1 --- /dev/null +++ b/async-openai/src/uploads.rs @@ -0,0 +1,90 @@ +use crate::{ + config::Config, + error::OpenAIError, + types::{AddUploadPartRequest, CompleteUploadRequest, CreateUploadRequest, Upload, UploadPart}, + Client, +}; + +/// Allows you to upload large files in multiple parts. +pub struct Uploads<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Uploads<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Creates an intermediate [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object that + /// you can add [Parts](https://platform.openai.com/docs/api-reference/uploads/part-object) to. Currently, + /// an Upload can accept at most 8 GB in total and expires after an hour after you create it. + /// + /// Once you complete the Upload, we will create a [File](https://platform.openai.com/docs/api-reference/files/object) + /// object that contains all the parts you uploaded. This File is usable in the rest of our platform as a regular File object. + /// + /// For certain `purpose`s, the correct `mime_type` must be specified. Please refer to documentation for the + /// supported MIME types for your use case: + /// - [Assistants](https://platform.openai.com/docs/assistants/tools/file-search/supported-files) + /// + /// For guidance on the proper filename extensions for each purpose, please follow the documentation on + /// [creating a File](https://platform.openai.com/docs/api-reference/files/create). + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create(&self, request: CreateUploadRequest) -> Result { + self.client.post("/uploads", request).await + } + + /// Adds a [Part](https://platform.openai.com/docs/api-reference/uploads/part-object) to an + /// [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object. + /// A Part represents a chunk of bytes from the file you are trying to upload. + /// + /// Each Part can be at most 64 MB, and you can add Parts until you hit the Upload maximum of 8 GB. + /// + /// It is possible to add multiple Parts in parallel. You can decide the intended order of the Parts + /// when you [complete the Upload](https://platform.openai.com/docs/api-reference/uploads/complete). + #[crate::byot( + T0 = std::fmt::Display, + T1 = Clone, + R = serde::de::DeserializeOwned, + where_clause = "reqwest::multipart::Form: crate::traits::AsyncTryFrom")] + pub async fn add_part( + &self, + upload_id: &str, + request: AddUploadPartRequest, + ) -> Result { + self.client + .post_form(&format!("/uploads/{upload_id}/parts"), request) + .await + } + + /// Completes the [Upload](https://platform.openai.com/docs/api-reference/uploads/object). + /// + /// Within the returned Upload object, there is a nested [File](https://platform.openai.com/docs/api-reference/files/object) + /// object that is ready to use in the rest of the platform. + /// + /// You can specify the order of the Parts by passing in an ordered list of the Part IDs. + /// + /// The number of bytes uploaded upon completion must match the number of bytes initially specified + /// when creating the Upload object. No Parts may be added after an Upload is completed. + + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn complete( + &self, + upload_id: &str, + request: CompleteUploadRequest, + ) -> Result { + self.client + .post(&format!("/uploads/{upload_id}/complete"), request) + .await + } + + /// Cancels the Upload. No Parts may be added after an Upload is cancelled. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn cancel(&self, upload_id: &str) -> Result { + self.client + .post( + &format!("/uploads/{upload_id}/cancel"), + serde_json::json!({}), + ) + .await + } +} diff --git a/async-openai/src/users.rs b/async-openai/src/users.rs new file mode 100644 index 00000000..727d3962 --- /dev/null +++ b/async-openai/src/users.rs @@ -0,0 +1,58 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{User, UserDeleteResponse, UserListResponse, UserRoleUpdateRequest}, + Client, +}; + +/// Manage users and their role in an organization. Users will be automatically added to the Default project. +pub struct Users<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> Users<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// Lists all of the users in the organization. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query("/organization/users", &query) + .await + } + + /// Modifies a user's role in the organization. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn modify( + &self, + user_id: &str, + request: UserRoleUpdateRequest, + ) -> Result { + self.client + .post(format!("/organization/users/{user_id}").as_str(), request) + .await + } + + /// Retrieve a user by their identifier + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, user_id: &str) -> Result { + self.client + .get(format!("/organization/users/{user_id}").as_str()) + .await + } + + /// Deletes a user from the organization. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete(&self, user_id: &str) -> Result { + self.client + .delete(format!("/organizations/users/{user_id}").as_str()) + .await + } +} diff --git a/async-openai/src/util.rs b/async-openai/src/util.rs index 695855d1..eab55caf 100644 --- a/async-openai/src/util.rs +++ b/async-openai/src/util.rs @@ -1,40 +1,72 @@ use std::path::Path; use reqwest::Body; +use tokio::fs::File; use tokio_util::codec::{BytesCodec, FramedRead}; use crate::error::OpenAIError; +use crate::types::InputSource; -pub(crate) async fn file_stream_body>(path: P) -> Result { - let file = tokio::fs::File::open(path.as_ref()) - .await - .map_err(|e| OpenAIError::FileReadError(e.to_string()))?; - let stream = FramedRead::new(file, BytesCodec::new()); - let body = Body::wrap_stream(stream); +pub(crate) async fn file_stream_body(source: InputSource) -> Result { + let body = match source { + InputSource::Path { path } => { + let file = File::open(path) + .await + .map_err(|e| OpenAIError::FileReadError(e.to_string()))?; + let stream = FramedRead::new(file, BytesCodec::new()); + Body::wrap_stream(stream) + } + _ => { + return Err(OpenAIError::FileReadError( + "Cannot create stream from non-file source".to_string(), + )) + } + }; Ok(body) } -/// Creates the part for the given image file for multipart upload. -pub(crate) async fn create_file_part>( - path: P, +/// Creates the part for the given file for multipart upload. +pub(crate) async fn create_file_part( + source: InputSource, ) -> Result { - let file_name = path - .as_ref() - .file_name() - .ok_or_else(|| { - OpenAIError::FileReadError(format!( - "cannot extract file name from {}", - path.as_ref().display() - )) - })? - .to_str() - .unwrap() - .to_string(); + let (stream, file_name) = match source { + InputSource::Path { path } => { + let file_name = path + .file_name() + .ok_or_else(|| { + OpenAIError::FileReadError(format!( + "cannot extract file name from {}", + path.display() + )) + })? + .to_str() + .unwrap() + .to_string(); - let file_part = reqwest::multipart::Part::stream(file_stream_body(path.as_ref()).await?) - .file_name(file_name) - .mime_str("application/octet-stream") - .unwrap(); + ( + file_stream_body(InputSource::Path { path }).await?, + file_name, + ) + } + InputSource::Bytes { filename, bytes } => (Body::from(bytes), filename), + InputSource::VecU8 { filename, vec } => (Body::from(vec), filename), + }; + + let file_part = reqwest::multipart::Part::stream(stream).file_name(file_name); Ok(file_part) } + +pub(crate) fn create_all_dir>(dir: P) -> Result<(), OpenAIError> { + let exists = match Path::try_exists(dir.as_ref()) { + Ok(exists) => exists, + Err(e) => return Err(OpenAIError::FileSaveError(e.to_string())), + }; + + if !exists { + std::fs::create_dir_all(dir.as_ref()) + .map_err(|e| OpenAIError::FileSaveError(e.to_string()))?; + } + + Ok(()) +} diff --git a/async-openai/src/vector_store_file_batches.rs b/async-openai/src/vector_store_file_batches.rs new file mode 100644 index 00000000..8e1384a9 --- /dev/null +++ b/async-openai/src/vector_store_file_batches.rs @@ -0,0 +1,90 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + CreateVectorStoreFileBatchRequest, ListVectorStoreFilesResponse, VectorStoreFileBatchObject, + }, + Client, +}; + +/// Vector store file batches represent operations to add multiple files to a vector store. +/// +/// Related guide: [File Search](https://platform.openai.com/docs/assistants/tools/file-search) +pub struct VectorStoreFileBatches<'c, C: Config> { + client: &'c Client, + pub vector_store_id: String, +} + +impl<'c, C: Config> VectorStoreFileBatches<'c, C> { + pub fn new(client: &'c Client, vector_store_id: &str) -> Self { + Self { + client, + vector_store_id: vector_store_id.into(), + } + } + + /// Create vector store file batch + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: CreateVectorStoreFileBatchRequest, + ) -> Result { + self.client + .post( + &format!("/vector_stores/{}/file_batches", &self.vector_store_id), + request, + ) + .await + } + + /// Retrieves a vector store file batch. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve( + &self, + batch_id: &str, + ) -> Result { + self.client + .get(&format!( + "/vector_stores/{}/file_batches/{batch_id}", + &self.vector_store_id + )) + .await + } + + /// Cancel a vector store file batch. This attempts to cancel the processing of files in this batch as soon as possible. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn cancel(&self, batch_id: &str) -> Result { + self.client + .post( + &format!( + "/vector_stores/{}/file_batches/{batch_id}/cancel", + &self.vector_store_id + ), + serde_json::json!({}), + ) + .await + } + + /// Returns a list of vector store files in a batch. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list( + &self, + batch_id: &str, + query: &Q, + ) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query( + &format!( + "/vector_stores/{}/file_batches/{batch_id}/files", + &self.vector_store_id + ), + &query, + ) + .await + } +} diff --git a/async-openai/src/vector_store_files.rs b/async-openai/src/vector_store_files.rs new file mode 100644 index 00000000..5ecaac06 --- /dev/null +++ b/async-openai/src/vector_store_files.rs @@ -0,0 +1,148 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + CreateVectorStoreFileRequest, DeleteVectorStoreFileResponse, ListVectorStoreFilesResponse, + VectorStoreFileContentResponse, VectorStoreFileObject, + }, + Client, +}; + +/// Vector store files represent files inside a vector store. +/// +/// Related guide: [File Search](https://platform.openai.com/docs/assistants/tools/file-search) +pub struct VectorStoreFiles<'c, C: Config> { + client: &'c Client, + pub vector_store_id: String, +} + +impl<'c, C: Config> VectorStoreFiles<'c, C> { + pub fn new(client: &'c Client, vector_store_id: &str) -> Self { + Self { + client, + vector_store_id: vector_store_id.into(), + } + } + + /// Create a vector store file by attaching a [File](https://platform.openai.com/docs/api-reference/files) to a [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object). + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: CreateVectorStoreFileRequest, + ) -> Result { + self.client + .post( + &format!("/vector_stores/{}/files", &self.vector_store_id), + request, + ) + .await + } + + /// Retrieves a vector store file. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, file_id: &str) -> Result { + self.client + .get(&format!( + "/vector_stores/{}/files/{file_id}", + &self.vector_store_id + )) + .await + } + + /// Delete a vector store file. This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the [delete file](https://platform.openai.com/docs/api-reference/files/delete) endpoint. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete( + &self, + file_id: &str, + ) -> Result { + self.client + .delete(&format!( + "/vector_stores/{}/files/{file_id}", + &self.vector_store_id + )) + .await + } + + /// Returns a list of vector store files. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client + .get_with_query( + &format!("/vector_stores/{}/files", &self.vector_store_id), + &query, + ) + .await + } + + /// Retrieve the parsed contents of a vector store file. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve_file_content( + &self, + file_id: &str, + ) -> Result { + self.client + .get(&format!( + "/vector_stores/{}/files/{file_id}/content", + &self.vector_store_id + )) + .await + } +} + +#[cfg(test)] +mod tests { + use crate::types::{CreateFileRequest, CreateVectorStoreRequest, FileInput, FilePurpose}; + use crate::Client; + + #[tokio::test] + async fn vector_store_file_creation_and_deletion( + ) -> Result<(), Box> { + let client = Client::new(); + + // Create a file + let file_handle = client + .files() + .create(CreateFileRequest { + file: FileInput::from_vec_u8( + String::from("meow.txt"), + String::from(":3").into_bytes(), + ), + purpose: FilePurpose::Assistants, + }) + .await?; + + // Create a vector store + let vector_store_handle = client + .vector_stores() + .create(CreateVectorStoreRequest { + file_ids: Some(vec![file_handle.id.clone()]), + name: None, + expires_after: None, + chunking_strategy: None, + metadata: None, + }) + .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); + // Delete the vector store + client + .vector_stores() + .delete(&vector_store_handle.id) + .await?; + + // Delete the file + client.files().delete(&file_handle.id).await?; + + Ok(()) + } +} diff --git a/async-openai/src/vector_stores.rs b/async-openai/src/vector_stores.rs new file mode 100644 index 00000000..64821cb4 --- /dev/null +++ b/async-openai/src/vector_stores.rs @@ -0,0 +1,94 @@ +use serde::Serialize; + +use crate::{ + config::Config, + error::OpenAIError, + types::{ + CreateVectorStoreRequest, DeleteVectorStoreResponse, ListVectorStoresResponse, + UpdateVectorStoreRequest, VectorStoreObject, VectorStoreSearchRequest, + VectorStoreSearchResultsPage, + }, + vector_store_file_batches::VectorStoreFileBatches, + Client, VectorStoreFiles, +}; + +pub struct VectorStores<'c, C: Config> { + client: &'c Client, +} + +impl<'c, C: Config> VectorStores<'c, C> { + pub fn new(client: &'c Client) -> Self { + Self { client } + } + + /// [VectorStoreFiles] API group + pub fn files(&self, vector_store_id: &str) -> VectorStoreFiles { + VectorStoreFiles::new(self.client, vector_store_id) + } + + /// [VectorStoreFileBatches] API group + pub fn file_batches(&self, vector_store_id: &str) -> VectorStoreFileBatches { + VectorStoreFileBatches::new(self.client, vector_store_id) + } + + /// Create a vector store. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn create( + &self, + request: CreateVectorStoreRequest, + ) -> Result { + self.client.post("/vector_stores", request).await + } + + /// Retrieves a vector store. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn retrieve(&self, vector_store_id: &str) -> Result { + self.client + .get(&format!("/vector_stores/{vector_store_id}")) + .await + } + + /// Returns a list of vector stores. + #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn list(&self, query: &Q) -> Result + where + Q: Serialize + ?Sized, + { + self.client.get_with_query("/vector_stores", &query).await + } + + /// Delete a vector store. + #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)] + pub async fn delete( + &self, + vector_store_id: &str, + ) -> Result { + self.client + .delete(&format!("/vector_stores/{vector_store_id}")) + .await + } + + /// Modifies a vector store. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn update( + &self, + vector_store_id: &str, + request: UpdateVectorStoreRequest, + ) -> Result { + self.client + .post(&format!("/vector_stores/{vector_store_id}"), request) + .await + } + + /// Searches a vector store. + #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)] + pub async fn search( + &self, + vector_store_id: &str, + request: VectorStoreSearchRequest, + ) -> Result { + self.client + .post(&format!("/vector_stores/{vector_store_id}/search"), request) + .await + } +} diff --git a/async-openai/tests/boxed_future.rs b/async-openai/tests/boxed_future.rs new file mode 100644 index 00000000..fa03914a --- /dev/null +++ b/async-openai/tests/boxed_future.rs @@ -0,0 +1,43 @@ +use futures::future::{BoxFuture, FutureExt}; +use futures::StreamExt; + +use async_openai::types::{CompletionResponseStream, CreateCompletionRequestArgs}; +use async_openai::Client; + +#[tokio::test] +async fn boxed_future_test() { + fn interpret_bool(token_stream: &mut CompletionResponseStream) -> BoxFuture<'_, bool> { + async move { + while let Some(response) = token_stream.next().await { + match response { + Ok(response) => { + let token_str = &response.choices[0].text.trim(); + if !token_str.is_empty() { + return token_str.contains("yes") || token_str.contains("Yes"); + } + } + Err(e) => eprintln!("Error: {e}"), + } + } + false + } + .boxed() + } + + let client = Client::new(); + + let request = CreateCompletionRequestArgs::default() + .model("gpt-3.5-turbo-instruct") + .n(1) + .prompt("does 2 and 2 add to four? (yes/no):\n") + .stream(true) + .logprobs(3) + .max_tokens(64_u32) + .build() + .unwrap(); + + let mut stream = client.completions().create_stream(request).await.unwrap(); + + let result = interpret_bool(&mut stream).await; + assert!(result); +} diff --git a/async-openai/tests/bring-your-own-type.rs b/async-openai/tests/bring-your-own-type.rs new file mode 100644 index 00000000..001a43c4 --- /dev/null +++ b/async-openai/tests/bring-your-own-type.rs @@ -0,0 +1,444 @@ +#![allow(dead_code)] +//! The purpose of this test to make sure that all _byot methods compiles with custom types. +use std::pin::Pin; + +use async_openai::{error::OpenAIError, Client}; +use futures::Stream; +use serde_json::{json, Value}; + +impl async_openai::traits::AsyncTryFrom for reqwest::multipart::Form { + type Error = OpenAIError; + async fn try_from(_value: MyJson) -> Result { + Ok(reqwest::multipart::Form::new()) + } +} + +#[derive(Clone)] +pub struct MyJson(Value); + +type MyStreamingType = Pin> + Send>>; + +#[tokio::test] +async fn test_byot_files() { + let client = Client::new(); + + let _r: Result = client.files().create_byot(MyJson(json!({}))).await; + let _r: Result = client.files().list_byot([("limit", "2")]).await; + let _r: Result = client.files().retrieve_byot("file_id").await; + let _r: Result = client.files().delete_byot("file_id").await; +} + +#[tokio::test] +async fn test_byot_assistants() { + let client = Client::new(); + + let _r: Result = client.assistants().create_byot(json!({})).await; + let _r: Result = client.assistants().retrieve_byot("aid").await; + let _r: Result = client.assistants().update_byot("aid", json!({})).await; + let _r: Result = client.assistants().list_byot([("limit", 2)]).await; +} + +#[tokio::test] +async fn test_byot_models() { + let client = Client::new(); + + let _r: Result = client.models().list_byot().await; + let _r: Result = client.models().retrieve_byot("").await; + let _r: Result = client.models().delete_byot(String::new()).await; +} + +#[tokio::test] +async fn test_byot_moderations() { + let client = Client::new(); + + let _r: Result = client.moderations().create_byot(json!({})).await; +} + +#[tokio::test] +async fn test_byot_images() { + let client = Client::new(); + + let _r: Result = client.images().create_byot(json!({})).await; + let _r: Result = client.images().create_edit_byot(MyJson(json!({}))).await; + let _r: Result = client + .images() + .create_variation_byot(MyJson(json!({}))) + .await; +} + +#[tokio::test] +async fn test_byot_chat() { + let client = Client::new(); + + let _r: Result = client.chat().create_byot(json!({})).await; + let _r: Result = + client.chat().create_stream_byot(json!({})).await; +} + +#[tokio::test] +async fn test_byot_completions() { + let client = Client::new(); + + let _r: Result = client.completions().create_byot(json!({})).await; + let _r: Result = + client.completions().create_stream_byot(json!({})).await; +} + +#[tokio::test] +async fn test_byot_audio() { + let client = Client::new(); + + let _r: Result = client.audio().transcribe_byot(MyJson(json!({}))).await; + let _r: Result = client + .audio() + .transcribe_verbose_json_byot(MyJson(json!({}))) + .await; + let _r: Result = client.audio().translate_byot(MyJson(json!({}))).await; + let _r: Result = client + .audio() + .translate_verbose_json_byot(MyJson(json!({}))) + .await; +} + +#[tokio::test] +async fn test_byot_embeddings() { + let client = Client::new(); + + let _r: Result = client.embeddings().create_byot(json!({})).await; + let _r: Result = client.embeddings().create_base64_byot(json!({})).await; +} + +#[tokio::test] +async fn test_byot_fine_tunning() { + let client = Client::new(); + + let _r: Result = client.fine_tuning().create_byot(json!({})).await; + let _r: Result = client + .fine_tuning() + .list_paginated_byot([("limit", "2")]) + .await; + let _r: Result = client + .fine_tuning() + .retrieve_byot("fine_tunning_job_id") + .await; + let _r: Result = + client.fine_tuning().cancel_byot("fine_tuning_job_id").await; + let _r: Result = client + .fine_tuning() + .list_events_byot("fine_tuning_job_id", [("limit", "2")]) + .await; + let _r: Result = client + .fine_tuning() + .list_checkpoints_byot("fine_tuning_job_id", [("limit", "2")]) + .await; +} + +#[derive(Clone, serde::Deserialize)] +pub struct MyThreadJson(Value); + +impl TryFrom for MyThreadJson { + type Error = OpenAIError; + fn try_from(_value: eventsource_stream::Event) -> Result { + Ok(MyThreadJson(json!({}))) + } +} + +type MyThreadStreamingType = Pin> + Send>>; + +#[tokio::test] +async fn test_byot_threads() { + let client = Client::new(); + + let _r: Result = client.threads().create_and_run_byot(json!({})).await; + let _r: Result = + client.threads().create_and_run_stream_byot(json!({})).await; + let _r: Result = client.threads().create_byot(json!({})).await; + let _r: Result = client.threads().retrieve_byot("thread_id").await; + let _r: Result = client.threads().update_byot("thread_id", json!({})).await; + let _r: Result = client.threads().delete_byot("thread_id").await; +} + +#[tokio::test] +async fn test_byot_messages() { + let client = Client::new(); + + let _r: Result = client + .threads() + .messages("thread_id") + .create_byot(json!({})) + .await; + let _r: Result = client + .threads() + .messages("thread_id") + .retrieve_byot("message_id") + .await; + let _r: Result = client + .threads() + .messages("thread_id") + .update_byot("message_id", json!({})) + .await; + let _r: Result = client + .threads() + .messages("thread_id") + .list_byot([("limit", "2")]) + .await; + let _r: Result = client + .threads() + .messages("thread_id") + .delete_byot("message_id") + .await; +} + +#[tokio::test] +async fn test_byot_runs() { + let client = Client::new(); + + let _r: Result = client + .threads() + .runs("thread_id") + .create_byot(json!({})) + .await; + let _r: Result = client + .threads() + .runs("thread_id") + .create_stream_byot(json!({})) + .await; + let _r: Result = client + .threads() + .runs("thread_id") + .retrieve_byot("run_id") + .await; + let _r: Result = client + .threads() + .runs("thread_id") + .update_byot("run_id", json!({})) + .await; + let _r: Result = client + .threads() + .runs("thread_id") + .list_byot([("limit", "2")]) + .await; + let _r: Result = client + .threads() + .runs("thread_id") + .submit_tool_outputs_byot("run_id", json!({})) + .await; + let _r: Result = client + .threads() + .runs("thread_id") + .submit_tool_outputs_stream_byot("run_id", json!({})) + .await; + let _r: Result = client + .threads() + .runs("thread_id") + .cancel_byot("run_id") + .await; +} + +#[tokio::test] +async fn test_byot_run_steps() { + let client = Client::new(); + + let _r: Result = client + .threads() + .runs("thread_id") + .steps("run_id") + .retrieve_byot("step_id") + .await; + let _r: Result = client + .threads() + .runs("thread_id") + .steps("run_id") + .list_byot([("limit", "2")]) + .await; +} + +#[tokio::test] +async fn test_byot_vector_store_files() { + let client = Client::new(); + let _r: Result = client + .vector_stores() + .files("vector_store_id") + .create_byot(json!({})) + .await; + let _r: Result = client + .vector_stores() + .files("vector_store_id") + .retrieve_byot("file_id") + .await; + let _r: Result = client + .vector_stores() + .files("vector_store_id") + .delete_byot("file_id") + .await; + let _r: Result = client + .vector_stores() + .files("vector_store_id") + .list_byot([("limit", "2")]) + .await; +} + +#[tokio::test] +async fn test_byot_vector_store_file_batches() { + let client = Client::new(); + let _r: Result = client + .vector_stores() + .file_batches("vector_store_id") + .create_byot(json!({})) + .await; + let _r: Result = client + .vector_stores() + .file_batches("vector_store_id") + .retrieve_byot("file_id") + .await; + let _r: Result = client + .vector_stores() + .file_batches("vector_store_id") + .cancel_byot("file_id") + .await; + let _r: Result = client + .vector_stores() + .file_batches("vector_store_id") + .list_byot("batch_id", [("limit", "2")]) + .await; +} + +#[tokio::test] +async fn test_byot_batches() { + let client = Client::new(); + let _r: Result = client.batches().create_byot(json!({})).await; + let _r: Result = client.batches().list_byot([("limit", "2")]).await; + let _r: Result = client.batches().retrieve_byot("batch_id").await; + let _r: Result = client.batches().cancel_byot("batch_id").await; +} + +#[tokio::test] +async fn test_byot_audit_logs() { + let client = Client::new(); + let _r: Result = client.audit_logs().get_byot([("limit", "2")]).await; +} + +#[tokio::test] +async fn test_byot_invites() { + let client = Client::new(); + let _r: Result = client.invites().create_byot(json!({})).await; + let _r: Result = client.invites().retrieve_byot("invite_id").await; + let _r: Result = client.invites().delete_byot("invite_id").await; + let _r: Result = client.invites().list_byot([("limit", "2")]).await; +} + +#[tokio::test] +async fn test_byot_projects() { + let client = Client::new(); + + let _r: Result = client.projects().list_byot([("limit", "2")]).await; + let _r: Result = client.projects().create_byot(json!({})).await; + let _r: Result = client.projects().retrieve_byot("project_id").await; + let _r: Result = + client.projects().modify_byot("project_id", json!({})).await; + let _r: Result = client.projects().archive_byot("project_id").await; +} + +#[tokio::test] +async fn test_byot_project_api_keys() { + let client = Client::new(); + + let _r: Result = client + .projects() + .api_keys("project_id") + .list_byot([("query", "2")]) + .await; + + let _r: Result = client + .projects() + .api_keys("project_id") + .retrieve_byot("api_key") + .await; + + let _r: Result = client + .projects() + .api_keys("project_id") + .delete_byot("api_key") + .await; +} + +#[tokio::test] +async fn test_byot_project_service_accounts() { + let client = Client::new(); + + let _r: Result = client + .projects() + .service_accounts("project_id") + .create_byot(json!({})) + .await; + + let _r: Result = client + .projects() + .service_accounts("project_id") + .delete_byot("service_account_id") + .await; + + let _r: Result = client + .projects() + .service_accounts("project_id") + .retrieve_byot("service_account_id") + .await; + + let _r: Result = client + .projects() + .service_accounts("project_id") + .list_byot([("limit", "2")]) + .await; +} + +#[tokio::test] +async fn test_byot_project_users() { + let client = Client::new(); + + let _r: Result = client + .projects() + .users("project_id") + .create_byot(json!({})) + .await; + let _r: Result = client + .projects() + .users("project_id") + .delete_byot("user_id") + .await; + + let _r: Result = client + .projects() + .users("project_id") + .list_byot([("limit", "2")]) + .await; + + let _r: Result = client + .projects() + .users("project_id") + .retrieve_byot("user_id") + .await; +} + +#[tokio::test] +async fn test_byot_uploads() { + let client = Client::new(); + + let _r: Result = client.uploads().create_byot(json!({})).await; + let _r: Result = client + .uploads() + .add_part_byot("upload_id", MyJson(json!({}))) + .await; + let _r: Result = + client.uploads().complete_byot("upload_id", json!({})).await; + let _r: Result = client.uploads().cancel_byot("upload_id").await; +} + +#[tokio::test] +async fn test_byot_users() { + let client = Client::new(); + + let _r: Result = client.users().list_byot([("limit", "2")]).await; + let _r: Result = client.users().modify_byot("user_id", json!({})).await; + let _r: Result = client.users().retrieve_byot("user_id").await; + let _r: Result = client.users().delete_byot("user_id").await; +} diff --git a/async-openai/tests/completion.rs b/async-openai/tests/completion.rs new file mode 100644 index 00000000..e3ccc392 --- /dev/null +++ b/async-openai/tests/completion.rs @@ -0,0 +1,47 @@ +//! This test is primarily to make sure that macros_rules for From traits are correct. +use async_openai::types::Prompt; + +fn prompt_input(input: T) -> Prompt +where + Prompt: From, +{ + input.into() +} + +#[test] +fn create_prompt_input() { + let prompt = "This is &str prompt"; + let _ = prompt_input(prompt); + + let prompt = "This is String".to_string(); + let _ = prompt_input(&prompt); + let _ = prompt_input(prompt); + + let prompt = vec!["This is first", "This is second"]; + let _ = prompt_input(&prompt); + let _ = prompt_input(prompt); + + let prompt = vec!["First string".to_string(), "Second string".to_string()]; + let _ = prompt_input(&prompt); + let _ = prompt_input(prompt); + + let first = "First".to_string(); + let second = "Second".to_string(); + let prompt = vec![&first, &second]; + let _ = prompt_input(&prompt); + let _ = prompt_input(prompt); + + let prompt = ["first", "second"]; + let _ = prompt_input(&prompt); + let _ = prompt_input(prompt); + + let prompt = ["first".to_string(), "second".to_string()]; + let _ = prompt_input(&prompt); + let _ = prompt_input(prompt); + + let first = "First".to_string(); + let second = "Second".to_string(); + let prompt = [&first, &second]; + let _ = prompt_input(&prompt); + let _ = prompt_input(prompt); +} diff --git a/async-openai/tests/embeddings.rs b/async-openai/tests/embeddings.rs new file mode 100644 index 00000000..6bc1bacf --- /dev/null +++ b/async-openai/tests/embeddings.rs @@ -0,0 +1,46 @@ +//! This test is primarily to make sure that macros_rules for From traits are correct. +use async_openai::types::EmbeddingInput; + +fn embedding_input(input: T) -> EmbeddingInput +where + EmbeddingInput: From, +{ + input.into() +} + +#[test] +fn create_embedding_input() { + let input = [1, 2, 3]; + let _ = embedding_input(&input); + let _ = embedding_input(input); + + let input = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; + let _ = embedding_input(&input); + let _ = embedding_input(input); + + let (s1, s2, s3) = ([1, 2, 3], [4, 5, 6], [7, 8, 9]); + let input = [&s1, &s2, &s3]; + let _ = embedding_input(&input); + let _ = embedding_input(input); + + let input = vec![1, 2, 3]; + let _ = embedding_input(&input); + let _ = embedding_input(input); + + let input = vec![[1, 2, 3], [4, 5, 6], [7, 8, 9]]; + let _ = embedding_input(&input); + let _ = embedding_input(input); + + let input = vec![vec![1, 2, 3], vec![4, 5, 6, 7], vec![8, 9, 10, 11, 12]]; + let _ = embedding_input(&input); + let _ = embedding_input(input); + + let input = [vec![1, 2, 3], vec![4, 5, 6, 7], vec![8, 9, 10, 11, 12]]; + let _ = embedding_input(&input); + let _ = embedding_input(input); + + let (v1, v2, v3) = (vec![1, 2, 3], vec![4, 5, 6, 7], vec![8, 9, 10, 11, 12]); + let input = [&v1, &v2, &v3]; + let _ = embedding_input(&input); + let _ = embedding_input(input); +} diff --git a/async-openai/tests/ser_de.rs b/async-openai/tests/ser_de.rs new file mode 100644 index 00000000..6f1a4cff --- /dev/null +++ b/async-openai/tests/ser_de.rs @@ -0,0 +1,28 @@ +use async_openai::types::{ + ChatCompletionRequestSystemMessageArgs, ChatCompletionRequestUserMessageArgs, + CreateChatCompletionRequest, CreateChatCompletionRequestArgs, +}; + +#[tokio::test] +async fn chat_types_serde() { + let request: CreateChatCompletionRequest = CreateChatCompletionRequestArgs::default() + .messages([ + ChatCompletionRequestSystemMessageArgs::default() + .content("your are a calculator") + .build() + .unwrap() + .into(), + ChatCompletionRequestUserMessageArgs::default() + .content("what is the result of 1+1") + .build() + .unwrap() + .into(), + ]) + .build() + .unwrap(); + // serialize the request + let serialized = serde_json::to_string(&request).unwrap(); + // deserialize the request + let deserialized: CreateChatCompletionRequest = serde_json::from_str(&serialized).unwrap(); + assert_eq!(request, deserialized); +} diff --git a/async-openai/tests/whisper.rs b/async-openai/tests/whisper.rs new file mode 100644 index 00000000..ee17a05f --- /dev/null +++ b/async-openai/tests/whisper.rs @@ -0,0 +1,57 @@ +use async_openai::types::CreateTranslationRequestArgs; +use async_openai::{types::CreateTranscriptionRequestArgs, Client}; +use tokio_test::assert_err; + +#[tokio::test] +async fn transcribe_test() { + let client = Client::new(); + + let request = CreateTranscriptionRequestArgs::default().build().unwrap(); + + let response = client.audio().transcribe(request).await; + + assert_err!(response); // FileReadError("cannot extract file name from ") +} + +#[tokio::test] +async fn transcribe_sendable_test() { + let client = Client::new(); + + // https://github.com/64bit/async-openai/issues/140 + let transcribe = tokio::spawn(async move { + let request = CreateTranscriptionRequestArgs::default().build().unwrap(); + + client.audio().transcribe(request).await + }); + + let response = transcribe.await.unwrap(); + + assert_err!(response); // FileReadError("cannot extract file name from ") +} + +#[tokio::test] +async fn translate_test() { + let client = Client::new(); + + let request = CreateTranslationRequestArgs::default().build().unwrap(); + + let response = client.audio().translate(request).await; + + assert_err!(response); // FileReadError("cannot extract file name from ") +} + +#[tokio::test] +async fn translate_sendable_test() { + let client = Client::new(); + + // https://github.com/64bit/async-openai/issues/140 + let translate = tokio::spawn(async move { + let request = CreateTranslationRequestArgs::default().build().unwrap(); + + client.audio().translate(request).await + }); + + let response = translate.await.unwrap(); + + assert_err!(response); // FileReadError("cannot extract file name from ") +} diff --git a/examples/Cargo.toml b/examples/Cargo.toml deleted file mode 100644 index 7c01a22b..00000000 --- a/examples/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[workspace] -members = [ - "codex", - "create-edit", - "completions", - "completions-stream", - "create-image", - "create-image-b64-json", - "create-image-edit", - "create-image-variation", - "models", - "moderations", - "rate-limit-completions", -] -resolver = "2" diff --git a/examples/assistants-code-interpreter/Cargo.toml b/examples/assistants-code-interpreter/Cargo.toml new file mode 100644 index 00000000..c53112ae --- /dev/null +++ b/examples/assistants-code-interpreter/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "assistants-code-interpreter" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = { path = "../../async-openai" } +tokio = { version = "1.43.0", features = ["full"] } +tracing-subscriber = { version = "0.3.19", features = ["env-filter"]} diff --git a/examples/assistants-code-interpreter/README.md b/examples/assistants-code-interpreter/README.md new file mode 100644 index 00000000..0d937368 --- /dev/null +++ b/examples/assistants-code-interpreter/README.md @@ -0,0 +1,22 @@ +## Data + +Data source: https://fred.stlouisfed.org/series/CASTHPI + + +## Output + +![price_index_vs_year_graph.png](./output/price_index_vs_year_graph.png) + +``` +> Run Queued +> In Progress ... +> In Progress ... +Retrieving image file_id: file-cEMuokypFSVuH3bCZ9NR6s2D +Graph file: ./output/price_index_vs_year_graph.pngThe graph of the average price index vs. year has been generated and saved as a PNG file. You can download it using the link below: + +[Download the graph](sandbox:/mnt/data/price_index_vs_year.png) +annotation: file path: MessageContentTextAnnotationsFilePathObject { text: "sandbox:/mnt/data/price_index_vs_year.png", file_path: FilePath { file_id: "file-V7gqRBnMUAQ6K3p1fHDCVByf" }, start_index: 154, end_index: 195 } +It appears the file contains a column `DATE` and a column `CASTHPI` (which seems to represent the price index). The next step is to generate a graph of the price index (`CASTHPI`) vs. year extracted from the `DATE` column. Let's proceed with that. +To generate a graph of the price index vs. year, we need to first inspect the uploaded file and extract the relevant data. Let's begin by loading and examining the content of the file. +Generate a graph of price index vs year in png format +``` diff --git a/examples/assistants-code-interpreter/input/CASTHPI.csv b/examples/assistants-code-interpreter/input/CASTHPI.csv new file mode 100644 index 00000000..577e9477 --- /dev/null +++ b/examples/assistants-code-interpreter/input/CASTHPI.csv @@ -0,0 +1,198 @@ +DATE,CASTHPI +1975-01-01,41.69 +1975-04-01,42.76 +1975-07-01,44.33 +1975-10-01,45.79 +1976-01-01,47.82 +1976-04-01,50.28 +1976-07-01,53.58 +1976-10-01,55.59 +1977-01-01,58.02 +1977-04-01,63.01 +1977-07-01,67.97 +1977-10-01,70.71 +1978-01-01,73.23 +1978-04-01,75.99 +1978-07-01,78.63 +1978-10-01,81.82 +1979-01-01,84.89 +1979-04-01,88.67 +1979-07-01,93.12 +1979-10-01,96.4 +1980-01-01,100.00 +1980-04-01,102.85 +1980-07-01,106.54 +1980-10-01,108.38 +1981-01-01,110.45 +1981-04-01,112.91 +1981-07-01,116.29 +1981-10-01,117.6 +1982-01-01,104.11 +1982-04-01,107.35 +1982-07-01,104.8 +1982-10-01,115.86 +1983-01-01,116.69 +1983-04-01,117.43 +1983-07-01,117.24 +1983-10-01,117.74 +1984-01-01,119.48 +1984-04-01,120.1 +1984-07-01,121.7 +1984-10-01,122.76 +1985-01-01,124.5 +1985-04-01,126.29 +1985-07-01,129.16 +1985-10-01,130.56 +1986-01-01,132.25 +1986-04-01,134.79 +1986-07-01,137.74 +1986-10-01,141.1 +1987-01-01,144.71 +1987-04-01,148.27 +1987-07-01,152.93 +1987-10-01,157.24 +1988-01-01,163.01 +1988-04-01,169.72 +1988-07-01,177.76 +1988-10-01,187.49 +1989-01-01,196.26 +1989-04-01,205.92 +1989-07-01,217.83 +1989-10-01,224.68 +1990-01-01,227.44 +1990-04-01,228.19 +1990-07-01,230.12 +1990-10-01,228.59 +1991-01-01,228.51 +1991-04-01,227.09 +1991-07-01,227.1 +1991-10-01,228.77 +1992-01-01,227.56 +1992-04-01,225.2 +1992-07-01,224.76 +1992-10-01,222.67 +1993-01-01,219.22 +1993-04-01,217.34 +1993-07-01,215.5 +1993-10-01,214.22 +1994-01-01,212.1 +1994-04-01,206.95 +1994-07-01,203.9 +1994-10-01,201.01 +1995-01-01,199.69 +1995-04-01,201.58 +1995-07-01,203.92 +1995-10-01,203.6 +1996-01-01,203.58 +1996-04-01,200.98 +1996-07-01,200.61 +1996-10-01,201.65 +1997-01-01,202.6 +1997-04-01,204.63 +1997-07-01,208.68 +1997-10-01,212.33 +1998-01-01,216.96 +1998-04-01,221.7 +1998-07-01,227.55 +1998-10-01,231.94 +1999-01-01,235.43 +1999-04-01,239.43 +1999-07-01,244.93 +1999-10-01,250.11 +2000-01-01,260.98 +2000-04-01,268.01 +2000-07-01,277.0 +2000-10-01,285.57 +2001-01-01,296.6 +2001-04-01,305.68 +2001-07-01,312.11 +2001-10-01,316.49 +2002-01-01,324.95 +2002-04-01,335.98 +2002-07-01,348.63 +2002-10-01,358.82 +2003-01-01,367.11 +2003-04-01,373.98 +2003-07-01,386.67 +2003-10-01,411.38 +2004-01-01,424.65 +2004-04-01,451.25 +2004-07-01,497.12 +2004-10-01,516.25 +2005-01-01,538.8 +2005-04-01,570.4 +2005-07-01,598.87 +2005-10-01,625.59 +2006-01-01,638.6 +2006-04-01,644.47 +2006-07-01,646.56 +2006-10-01,642.86 +2007-01-01,634.09 +2007-04-01,623.45 +2007-07-01,600.14 +2007-10-01,575.65 +2008-01-01,543.36 +2008-04-01,498.47 +2008-07-01,460.62 +2008-10-01,444.72 +2009-01-01,440.74 +2009-04-01,420.64 +2009-07-01,411.34 +2009-10-01,412.66 +2010-01-01,409.6 +2010-04-01,408.38 +2010-07-01,410.63 +2010-10-01,406.1 +2011-01-01,391.58 +2011-04-01,385.3 +2011-07-01,386.9 +2011-10-01,385.72 +2012-01-01,381.42 +2012-04-01,382.07 +2012-07-01,391.0 +2012-10-01,399.3 +2013-01-01,407.71 +2013-04-01,426.44 +2013-07-01,447.4 +2013-10-01,462.24 +2014-01-01,471.87 +2014-04-01,484.65 +2014-07-01,494.78 +2014-10-01,500.4 +2015-01-01,505.64 +2015-04-01,516.97 +2015-07-01,530.16 +2015-10-01,537.47 +2016-01-01,544.51 +2016-04-01,554.17 +2016-07-01,566.29 +2016-10-01,573.11 +2017-01-01,579.06 +2017-04-01,591.07 +2017-07-01,602.85 +2017-10-01,611.49 +2018-01-01,621.95 +2018-04-01,633.81 +2018-07-01,641.81 +2018-10-01,641.73 +2019-01-01,645.88 +2019-04-01,652.47 +2019-07-01,659.37 +2019-10-01,663.65 +2020-01-01,669.9 +2020-04-01,677.58 +2020-07-01,685.51 +2020-10-01,699.66 +2021-01-01,719.24 +2021-04-01,761.95 +2021-07-01,801.58 +2021-10-01,834.11 +2022-01-01,868.25 +2022-04-01,922.13 +2022-07-01,916.73 +2022-10-01,894.75 +2023-01-01,891.74 +2023-04-01,914.89 +2023-07-01,928.36 +2023-10-01,929.62 +2024-01-01,942.11 diff --git a/examples/assistants-code-interpreter/output/price_index_vs_year_graph.png b/examples/assistants-code-interpreter/output/price_index_vs_year_graph.png new file mode 100644 index 00000000..bbcb8beb Binary files /dev/null and b/examples/assistants-code-interpreter/output/price_index_vs_year_graph.png differ diff --git a/examples/assistants-code-interpreter/src/main.rs b/examples/assistants-code-interpreter/src/main.rs new file mode 100644 index 00000000..bee370dc --- /dev/null +++ b/examples/assistants-code-interpreter/src/main.rs @@ -0,0 +1,172 @@ +use std::error::Error; + +use async_openai::{ + types::{ + AssistantToolCodeInterpreterResources, AssistantTools, CreateAssistantRequestArgs, + CreateFileRequest, CreateMessageRequestArgs, CreateRunRequest, CreateThreadRequest, + FilePurpose, MessageContent, MessageContentTextAnnotations, MessageRole, RunStatus, + }, + Client, +}; +use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + std::env::set_var("RUST_LOG", "ERROR"); + + // Setup tracing subscriber so that library can log the errors + tracing_subscriber::registry() + .with(fmt::layer()) + .with(EnvFilter::from_default_env()) + .init(); + + let client = Client::new(); + + // Upload data file with "assistants" purpose + let data_file = client + .files() + .create(CreateFileRequest { + file: "./input/CASTHPI.csv".into(), + purpose: FilePurpose::Assistants, + }) + .await?; + + // Create an assistant with code_interpreter tool with the uploaded file + let create_assistant_request = CreateAssistantRequestArgs::default() + .instructions("You are a data processor. When asked a question about data in a file, write and run code to answer the question.") + .model("gpt-4o") + .tools(vec![ + AssistantTools::CodeInterpreter + ]) + .tool_resources( + AssistantToolCodeInterpreterResources { file_ids: vec![data_file.id.clone()] } + ) + .build()?; + + let assistant = client.assistants().create(create_assistant_request).await?; + + // create a thread + let create_message_request = CreateMessageRequestArgs::default() + .role(MessageRole::User) + .content("Generate a graph of price index vs year in png format") + .build()?; + + let create_thread_request = CreateThreadRequest { + messages: Some(vec![create_message_request]), + ..Default::default() + }; + + let thread = client.threads().create(create_thread_request).await?; + + // create run and check the output + let create_run_request = CreateRunRequest { + assistant_id: assistant.id.clone(), + ..Default::default() + }; + + let mut run = client + .threads() + .runs(&thread.id) + .create(create_run_request) + .await?; + + let mut generated_file_ids: Vec = vec![]; + + // poll the status of run until its in a terminal state + loop { + //check the status of the run + match run.status { + RunStatus::Completed => { + let messages = client + .threads() + .messages(&thread.id) + .list(&[("limit", "10")]) + .await?; + + for message_obj in messages.data { + let message_contents = message_obj.content; + for message_content in message_contents { + match message_content { + MessageContent::Text(text) => { + let text_data = text.text; + let annotations = text_data.annotations; + println!("{}", text_data.value); + for annotation in annotations { + match annotation { + MessageContentTextAnnotations::FileCitation(object) => { + println!("annotation: file citation : {object:?}"); + } + MessageContentTextAnnotations::FilePath(object) => { + println!("annotation: file path: {object:?}"); + generated_file_ids.push(object.file_path.file_id); + } + } + } + } + MessageContent::ImageFile(object) => { + let file_id = object.image_file.file_id; + println!("Retrieving image file_id: {}", file_id); + let contents = client.files().content(&file_id).await?; + let path = "./output/price_index_vs_year_graph.png"; + tokio::fs::write(path, contents).await?; + print!("Graph file: {path}"); + generated_file_ids.push(file_id); + } + MessageContent::ImageUrl(object) => { + eprintln!("Got Image URL instead: {object:?}"); + } + MessageContent::Refusal(refusal) => { + println!("{refusal:?}"); + } + } + } + } + + break; + } + RunStatus::Failed => { + println!("> Run Failed: {:#?}", run); + break; + } + RunStatus::Queued => { + println!("> Run Queued"); + } + RunStatus::Cancelling => { + println!("> Run Cancelling"); + } + RunStatus::Cancelled => { + println!("> Run Cancelled"); + break; + } + RunStatus::Expired => { + println!("> Run Expired"); + break; + } + RunStatus::RequiresAction => { + println!("> Run Requires Action"); + } + RunStatus::InProgress => { + println!("> In Progress ..."); + } + RunStatus::Incomplete => { + println!("> Run Incomplete"); + } + } + + // wait for 1 sec before polling run object again + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + + //retrieve the run + run = client.threads().runs(&thread.id).retrieve(&run.id).await?; + } + + // clean up + client.threads().delete(&thread.id).await?; + client.files().delete(&data_file.id).await?; + for file_id in generated_file_ids { + client.files().delete(&file_id).await?; + } + client.assistants().delete(&assistant.id).await?; + + Ok(()) +} diff --git a/examples/assistants-file-search/Cargo.toml b/examples/assistants-file-search/Cargo.toml new file mode 100644 index 00000000..7c94c891 --- /dev/null +++ b/examples/assistants-file-search/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "assistants-file-search" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = { path = "../../async-openai" } +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/assistants-file-search/README.md b/examples/assistants-file-search/README.md new file mode 100644 index 00000000..3b31187d --- /dev/null +++ b/examples/assistants-file-search/README.md @@ -0,0 +1,47 @@ +## Intro + +This example is based on https://platform.openai.com/docs/assistants/tools/file-search + + +## Data + +Uber Annual Report obtained from https://investor.uber.com/financials/ + +Lyft Annual Report obtained from https://investor.lyft.com/financials-and-reports/annual-reports/default.aspx + + +## Output + +``` +> Run Queued +> In Progress ... +> In Progress ... +> In Progress ... +> In Progress ... +> In Progress ... +> In Progress ... +> In Progress ... +> In Progress ... +> In Progress ... +> In Progress ... +### Total Annual Profit of Uber and Lyft for 2023 + +#### Uber +For the year ended December 31, 2023, Uber Technologies, Inc. reported: +- **Net Income:** $1.887 billion. +- **Adjusted EBITDA:** $4.052 billion【4:2†source】 . + +#### Lyft +For the year ended December 31, 2023, Lyft, Inc. reported: +- **Net Loss:** $340.3 million. +- **Adjusted EBITDA:** $222.4 million【4:1†source】 . + +### Summary +- Uber reported a net income of $1.887 billion for 2023. +- Lyft reported a net loss of $340.3 million for 2023. + +Uber was profitable in 2023, while Lyft incurred a significant loss. +[FileCitation(MessageContentTextAnnotationsFileCitationObject { text: "【4:2†source】", file_citation: FileCitation { file_id: "file-YHlpVPi1RIr6jTjlCG54wsHq", quote: None }, start_index: 204, end_index: 216 }), FileCitation(MessageContentTextAnnotationsFileCitationObject { text: "【4:1†source】", file_citation: FileCitation { file_id: "file-2zGaN3VzwqRd9c3ZHa6mGk38", quote: None }, start_index: 358, end_index: 370 })] +What was the total annual profit of Uber and Lyft? +[] +``` diff --git a/examples/assistants-file-search/input/lyft-10k.pdf b/examples/assistants-file-search/input/lyft-10k.pdf new file mode 100644 index 00000000..7e28d3c4 Binary files /dev/null and b/examples/assistants-file-search/input/lyft-10k.pdf differ diff --git a/examples/assistants-file-search/input/uber-10k.pdf b/examples/assistants-file-search/input/uber-10k.pdf new file mode 100644 index 00000000..8b2298b4 Binary files /dev/null and b/examples/assistants-file-search/input/uber-10k.pdf differ diff --git a/examples/assistants-file-search/src/main.rs b/examples/assistants-file-search/src/main.rs new file mode 100644 index 00000000..11770d6b --- /dev/null +++ b/examples/assistants-file-search/src/main.rs @@ -0,0 +1,200 @@ +use std::error::Error; + +use async_openai::{ + types::{ + AssistantToolFileSearchResources, AssistantToolsFileSearch, CreateAssistantRequestArgs, + CreateFileRequest, CreateMessageRequestArgs, CreateRunRequest, CreateThreadRequest, + CreateVectorStoreRequest, FilePurpose, MessageAttachment, MessageAttachmentTool, + MessageContent, MessageRole, ModifyAssistantRequest, RunStatus, + }, + Client, +}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + // + // Step 1: Create a new Assistant with File Search Enabled + // + + let create_assistant_request = CreateAssistantRequestArgs::default() + .name("Financial Analyst Assistant") + .instructions("You are an expert financial analyst. Use you knowledge base to answer questions about audited financial statements.") + .model("gpt-4o") + .tools(vec![ + AssistantToolsFileSearch::default().into() + ]) + .build()?; + + let assistant = client.assistants().create(create_assistant_request).await?; + + // + // Step 2: Upload files and add them to a Vector Store + // + + // upload file to add to vector store + let openai_file = client + .files() + .create(CreateFileRequest { + file: "./input/uber-10k.pdf".into(), + purpose: FilePurpose::Assistants, + }) + .await?; + + // Create a vector store called "Financial Statements" + // add uploaded file to vector store + let vector_store = client + .vector_stores() + .create(CreateVectorStoreRequest { + name: Some("Financial Statements".into()), + file_ids: Some(vec![openai_file.id.clone()]), + ..Default::default() + }) + .await?; + + // + // Step 3: Update the assistant to to use the new Vector Store + // + + let assistant = client + .assistants() + .update( + &assistant.id, + ModifyAssistantRequest { + tool_resources: Some( + AssistantToolFileSearchResources { + vector_store_ids: vec![vector_store.id.clone()], + } + .into(), + ), + ..Default::default() + }, + ) + .await?; + + // + // Step 4: Create a thread + // + + // You can also attach files as Message attachments on your thread. Doing so will create another vector_store associated with the thread, or, if there is already a vector store attached to this thread, attach the new files to the existing thread vector store. When you create a Run on this thread, the file search tool will query both the vector_store from your assistant and the vector_store on the thread. + + // Upload user provided file to OpenAI + let message_file = client + .files() + .create(CreateFileRequest { + file: "./input/lyft-10k.pdf".into(), + purpose: FilePurpose::Assistants, + }) + .await?; + + // Create a thread and attach the file to the message + + let create_message_request = CreateMessageRequestArgs::default() + .role(MessageRole::User) + .content("What was the total annual profit of Uber and Lyft?") + .attachments(vec![MessageAttachment { + file_id: message_file.id.clone(), + tools: vec![MessageAttachmentTool::FileSearch], + }]) + .build()?; + + let create_thread_request = CreateThreadRequest { + messages: Some(vec![create_message_request]), + ..Default::default() + }; + + let thread = client.threads().create(create_thread_request).await?; + + // + // Step 5: Create a run and check the output + // + + let create_run_request = CreateRunRequest { + assistant_id: assistant.id.clone(), + ..Default::default() + }; + + let mut run = client + .threads() + .runs(&thread.id) + .create(create_run_request) + .await?; + + // poll the status of run until its in a terminal state + loop { + //check the status of the run + match run.status { + RunStatus::Completed => { + let messages = client + .threads() + .messages(&thread.id) + .list(&[("limit", "10")]) + .await?; + + for message_obj in messages.data { + let message_contents = message_obj.content; + for message_content in message_contents { + match message_content { + MessageContent::Text(text) => { + let text_data = text.text; + let annotations = text_data.annotations; + println!("{}", text_data.value); + println!("{annotations:?}"); + } + MessageContent::ImageFile(_) | MessageContent::ImageUrl(_) => { + eprintln!("Images not supported on terminal"); + } + MessageContent::Refusal(refusal) => { + println!("{refusal:?}"); + } + } + } + } + + break; + } + RunStatus::Failed => { + println!("> Run Failed: {:#?}", run); + break; + } + RunStatus::Queued => { + println!("> Run Queued"); + } + RunStatus::Cancelling => { + println!("> Run Cancelling"); + } + RunStatus::Cancelled => { + println!("> Run Cancelled"); + break; + } + RunStatus::Expired => { + println!("> Run Expired"); + break; + } + RunStatus::RequiresAction => { + println!("> Run Requires Action"); + } + RunStatus::InProgress => { + println!("> In Progress ..."); + } + RunStatus::Incomplete => { + println!("> Run Incomplete"); + } + } + + // wait for 1 sec before polling run object again + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + + //retrieve the run + run = client.threads().runs(&thread.id).retrieve(&run.id).await?; + } + + // clean up + client.threads().delete(&thread.id).await?; + client.vector_stores().delete(&vector_store.id).await?; + client.files().delete(&openai_file.id).await?; + client.files().delete(&message_file.id).await?; + client.assistants().delete(&assistant.id).await?; + + Ok(()) +} diff --git a/examples/assistants-func-call-stream/Cargo.toml b/examples/assistants-func-call-stream/Cargo.toml new file mode 100644 index 00000000..9767e2b0 --- /dev/null +++ b/examples/assistants-func-call-stream/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "assistants-func-call-stream" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = { path = "../../async-openai" } +tokio = { version = "1.43.0", features = ["full"] } +serde_json = "1.0.135" +futures = "0.3.31" +tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } diff --git a/examples/assistants-func-call-stream/README.md b/examples/assistants-func-call-stream/README.md new file mode 100644 index 00000000..b4f43cf6 --- /dev/null +++ b/examples/assistants-func-call-stream/README.md @@ -0,0 +1,91 @@ +## Intro + +Based on https://platform.openai.com/docs/assistants/tools/function-calling/quickstart + +## Output + +``` +Event: ThreadRunCreated(RunObject { id: "run_Bb5cE5w7m1Dl4kptwbCje4ZW", object: "thread.run", created_at: 1717545495, thread_id: "thread_T6ExzoafsAFhYPCIDqdg02yA", assistant_id: Some("asst_ukwQANH3emMUxQnG24Lb8Rph"), status: Queued, required_action: None, last_error: None, expires_at: Some(1717546095), started_at: None, cancelled_at: None, failed_at: None, completed_at: None, incomplete_details: None, model: "gpt-4o", instructions: "You are a weather bot. Use the provided functions to answer questions.", tools: [Function(AssistantToolsFunction { function: FunctionObject { name: "get_current_temperature", description: Some("Get the current temperature for a specific location"), parameters: Some(Object {"properties": Object {"location": Object {"description": String("The city and state, e.g., San Francisco, CA"), "type": String("string")}, "unit": Object {"description": String("The temperature unit to use. Infer this from the user's location."), "enum": Array [String("Celsius"), String("Fahrenheit")], "type": String("string")}}, "required": Array [String("location"), String("unit")], "type": String("object")}) } }), Function(AssistantToolsFunction { function: FunctionObject { name: "get_rain_probability", description: Some("Get the probability of rain for a specific location"), parameters: Some(Object {"properties": Object {"location": Object {"description": String("The city and state, e.g., San Francisco, CA"), "type": String("string")}}, "required": Array [String("location")], "type": String("object")}) } })], metadata: Some({}), usage: None, temperature: Some(1.0), top_p: Some(1.0), max_prompt_tokens: None, max_completion_tokens: None, truncation_strategy: Some(TruncationObject { type: Auto, last_messages: None }), tool_choice: Some(Auto), response_format: Some(Auto) }) + + +Event: ThreadRunQueued(RunObject { id: "run_Bb5cE5w7m1Dl4kptwbCje4ZW", object: "thread.run", created_at: 1717545495, thread_id: "thread_T6ExzoafsAFhYPCIDqdg02yA", assistant_id: Some("asst_ukwQANH3emMUxQnG24Lb8Rph"), status: Queued, required_action: None, last_error: None, expires_at: Some(1717546095), started_at: None, cancelled_at: None, failed_at: None, completed_at: None, incomplete_details: None, model: "gpt-4o", instructions: "You are a weather bot. Use the provided functions to answer questions.", tools: [Function(AssistantToolsFunction { function: FunctionObject { name: "get_current_temperature", description: Some("Get the current temperature for a specific location"), parameters: Some(Object {"properties": Object {"location": Object {"description": String("The city and state, e.g., San Francisco, CA"), "type": String("string")}, "unit": Object {"description": String("The temperature unit to use. Infer this from the user's location."), "enum": Array [String("Celsius"), String("Fahrenheit")], "type": String("string")}}, "required": Array [String("location"), String("unit")], "type": String("object")}) } }), Function(AssistantToolsFunction { function: FunctionObject { name: "get_rain_probability", description: Some("Get the probability of rain for a specific location"), parameters: Some(Object {"properties": Object {"location": Object {"description": String("The city and state, e.g., San Francisco, CA"), "type": String("string")}}, "required": Array [String("location")], "type": String("object")}) } })], metadata: Some({}), usage: None, temperature: Some(1.0), top_p: Some(1.0), max_prompt_tokens: None, max_completion_tokens: None, truncation_strategy: Some(TruncationObject { type: Auto, last_messages: None }), tool_choice: Some(Auto), response_format: Some(Auto) }) + + +Event: ThreadRunInProgress(RunObject { id: "run_Bb5cE5w7m1Dl4kptwbCje4ZW", object: "thread.run", created_at: 1717545495, thread_id: "thread_T6ExzoafsAFhYPCIDqdg02yA", assistant_id: Some("asst_ukwQANH3emMUxQnG24Lb8Rph"), status: InProgress, required_action: None, last_error: None, expires_at: Some(1717546095), started_at: Some(1717545496), cancelled_at: None, failed_at: None, completed_at: None, incomplete_details: None, model: "gpt-4o", instructions: "You are a weather bot. Use the provided functions to answer questions.", tools: [Function(AssistantToolsFunction { function: FunctionObject { name: "get_current_temperature", description: Some("Get the current temperature for a specific location"), parameters: Some(Object {"properties": Object {"location": Object {"description": String("The city and state, e.g., San Francisco, CA"), "type": String("string")}, "unit": Object {"description": String("The temperature unit to use. Infer this from the user's location."), "enum": Array [String("Celsius"), String("Fahrenheit")], "type": String("string")}}, "required": Array [String("location"), String("unit")], "type": String("object")}) } }), Function(AssistantToolsFunction { function: FunctionObject { name: "get_rain_probability", description: Some("Get the probability of rain for a specific location"), parameters: Some(Object {"properties": Object {"location": Object {"description": String("The city and state, e.g., San Francisco, CA"), "type": String("string")}}, "required": Array [String("location")], "type": String("object")}) } })], metadata: Some({}), usage: None, temperature: Some(1.0), top_p: Some(1.0), max_prompt_tokens: None, max_completion_tokens: None, truncation_strategy: Some(TruncationObject { type: Auto, last_messages: None }), tool_choice: Some(Auto), response_format: Some(Auto) }) + + +Event: ThreadRunStepCreated(RunStepObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step", created_at: 1717545497, assistant_id: Some("asst_ukwQANH3emMUxQnG24Lb8Rph"), thread_id: "thread_T6ExzoafsAFhYPCIDqdg02yA", run_id: "run_Bb5cE5w7m1Dl4kptwbCje4ZW", type: ToolCalls, status: InProgress, step_details: ToolCalls(RunStepDetailsToolCallsObject { tool_calls: [] }), last_error: None, expires_at: Some(1717546095), cancelled_at: None, failed_at: None, completed_at: None, metadata: None, usage: None }) + + +Event: ThreadRunStepInProgress(RunStepObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step", created_at: 1717545497, assistant_id: Some("asst_ukwQANH3emMUxQnG24Lb8Rph"), thread_id: "thread_T6ExzoafsAFhYPCIDqdg02yA", run_id: "run_Bb5cE5w7m1Dl4kptwbCje4ZW", type: ToolCalls, status: InProgress, step_details: ToolCalls(RunStepDetailsToolCallsObject { tool_calls: [] }), last_error: None, expires_at: Some(1717546095), cancelled_at: None, failed_at: None, completed_at: None, metadata: None, usage: None }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: Some("call_mmNiY0Y3qBciehXXfuKoWKDS"), function: Some(RunStepFunctionObjectDelta { name: Some("get_current_temperature"), arguments: Some(""), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("{\"lo"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("catio"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("n\": \"S"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("an F"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("ranci"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("sco, C"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("A\", "), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("\"unit"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("\": \"Fa"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("hren"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("heit\""), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 0, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("}"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 1, id: Some("call_wbeW7n9EXmCZfzwFjEYMVUVW"), function: Some(RunStepFunctionObjectDelta { name: Some("get_rain_probability"), arguments: Some(""), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 1, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("{\"lo"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 1, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("catio"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 1, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("n\": \"S"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 1, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("an F"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 1, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("ranci"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 1, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("sco, C"), output: None }) })]) }) } }) + + +Event: ThreadRunStepDelta(RunStepDeltaObject { id: "step_tH9R7o3ylttzyT7Cx09lPa9Y", object: "thread.run.step.delta", delta: RunStepDelta { step_details: ToolCalls(RunStepDeltaStepDetailsToolCallsObject { tool_calls: Some([Function(RunStepDeltaStepDetailsToolCallsFunctionObject { index: 1, id: None, function: Some(RunStepFunctionObjectDelta { name: None, arguments: Some("A\"}"), output: None }) })]) }) } }) + +thread.run.requires_action: run_id:run_Bb5cE5w7m1Dl4kptwbCje4ZW + +Event: Done("[DONE]") + +The current temperature in San Francisco is 57°F, and there is a 6% chance of rain today. + +``` diff --git a/examples/assistants-func-call-stream/src/main.rs b/examples/assistants-func-call-stream/src/main.rs new file mode 100644 index 00000000..a745292f --- /dev/null +++ b/examples/assistants-func-call-stream/src/main.rs @@ -0,0 +1,210 @@ +use std::error::Error; + +use async_openai::{ + config::OpenAIConfig, + types::{ + AssistantStreamEvent, CreateAssistantRequestArgs, CreateMessageRequest, CreateRunRequest, + CreateThreadRequest, FunctionObject, MessageDeltaContent, MessageRole, RunObject, + SubmitToolOutputsRunRequest, ToolsOutputs, + }, + Client, +}; +use futures::StreamExt; +use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + std::env::set_var("RUST_LOG", "ERROR"); + + // Setup tracing subscriber so that library can log the errors + tracing_subscriber::registry() + .with(fmt::layer()) + .with(EnvFilter::from_default_env()) + .init(); + + let client = Client::new(); + + // + // Step 1: Define functions + // + + let create_assistant_request = CreateAssistantRequestArgs::default() + .instructions("You are a weather bot. Use the provided functions to answer questions.") + .model("gpt-4o") + .tools(vec![ + FunctionObject { + name: "get_current_temperature".into(), + description: Some("Get the current temperature for a specific location".into()), + parameters: Some(serde_json::json!( + { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g., San Francisco, CA" + }, + "unit": { + "type": "string", + "enum": ["Celsius", "Fahrenheit"], + "description": "The temperature unit to use. Infer this from the user's location." + } + }, + "required": ["location", "unit"] + } + )), + strict: None, + }.into(), + + FunctionObject { + name: "get_rain_probability".into(), + description: Some("Get the probability of rain for a specific location".into()), + parameters: Some(serde_json::json!( + { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g., San Francisco, CA" + } + }, + "required": ["location"] + } + )), + strict: None, + }.into() + ]).build()?; + + let assistant = client.assistants().create(create_assistant_request).await?; + + // + // Step 2: Create a Thread and add Messages + // + let thread = client + .threads() + .create(CreateThreadRequest::default()) + .await?; + + let _message = client + .threads() + .messages(&thread.id) + .create(CreateMessageRequest { + role: MessageRole::User, + content: "What's the weather in San Francisco today and the likelihood it'll rain?" + .into(), + ..Default::default() + }) + .await?; + + // + // Step 3: Initiate a Run + // + let mut event_stream = client + .threads() + .runs(&thread.id) + .create_stream(CreateRunRequest { + assistant_id: assistant.id.clone(), + stream: Some(true), + ..Default::default() + }) + .await?; + + let mut task_handle = None; + + while let Some(event) = event_stream.next().await { + match event { + Ok(event) => match event { + AssistantStreamEvent::ThreadRunRequiresAction(run_object) => { + println!("thread.run.requires_action: run_id:{}", run_object.id); + let client = client.clone(); + task_handle = Some(tokio::spawn(async move { + handle_requires_action(client, run_object).await + })); + } + _ => println!("\nEvent: {event:?}\n"), + }, + Err(e) => { + eprintln!("Error: {e}"); + } + } + } + + // wait for task to handle required action and submit tool outputs + if let Some(task_handle) = task_handle { + let _ = tokio::join!(task_handle); + } + + // clean up + client.threads().delete(&thread.id).await?; + client.assistants().delete(&assistant.id).await?; + + Ok(()) +} + +async fn handle_requires_action(client: Client, run_object: RunObject) { + let mut tool_outputs: Vec = vec![]; + if let Some(ref required_action) = run_object.required_action { + for tool in &required_action.submit_tool_outputs.tool_calls { + if tool.function.name == "get_current_temperature" { + tool_outputs.push(ToolsOutputs { + tool_call_id: Some(tool.id.clone()), + output: Some("57".into()), + }) + } + + if tool.function.name == "get_rain_probability" { + tool_outputs.push(ToolsOutputs { + tool_call_id: Some(tool.id.clone()), + output: Some("0.06".into()), + }) + } + } + + if let Err(e) = submit_tool_outputs(client, run_object, tool_outputs).await { + eprintln!("Error on submitting tool outputs: {e}"); + } + } +} + +async fn submit_tool_outputs( + client: Client, + run_object: RunObject, + tool_outputs: Vec, +) -> Result<(), Box> { + let mut event_stream = client + .threads() + .runs(&run_object.thread_id) + .submit_tool_outputs_stream( + &run_object.id, + SubmitToolOutputsRunRequest { + tool_outputs, + stream: Some(true), + }, + ) + .await?; + + while let Some(event) = event_stream.next().await { + match event { + Ok(event) => { + if let AssistantStreamEvent::ThreadMessageDelta(delta) = event { + if let Some(contents) = delta.delta.content { + for content in contents { + // only text is expected here and no images + if let MessageDeltaContent::Text(text) = content { + if let Some(text) = text.text { + if let Some(text) = text.value { + print!("{}", text); + } + } + } + } + } + } + } + Err(e) => { + eprintln!("Error: {e}"); + } + } + } + + Ok(()) +} diff --git a/examples/assistants/Cargo.toml b/examples/assistants/Cargo.toml new file mode 100644 index 00000000..d7595fd4 --- /dev/null +++ b/examples/assistants/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "assistants" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +tokio = { version = "1.43.0", features = ["full"] } +tracing-subscriber = { version = "0.3.19", features = ["env-filter"]} diff --git a/examples/assistants/README.md b/examples/assistants/README.md new file mode 100644 index 00000000..124c6828 --- /dev/null +++ b/examples/assistants/README.md @@ -0,0 +1,79 @@ +# Assistants +This example allows you to chat with an assistant, and walks through the process of: +- creating threads, messages, and run requests +- appending messages to and retrieving messages from the thread +- running threads and retrieving their responses + +When you run the example you will be prompted to create an assistant by giving it a name and instruction set. +``` +--- Enter the name of your assistant +test bot +--- Enter the instruction set for your new assistant +you are a bot that writes simple rust programs +``` +Then you will be able to query the assistant. +``` +--- How can I help you? +write a hello world program +--- Waiting for response... +--- Response: Sure! Here's a simple "Hello, World!" program in Rust: +```rust +fn main() { + println!("Hello, World!"); +} +``` +To exit the assistant type: +``` +exit() +``` +# Generalized Steps +in order to interact with an assistant we will generally follow these steps for creating a query and retrieving a response. +1. We can either set our assistant by hard coding our assistant id, or by creating an assistant dynamically in our code. This example does the former. +``` +//build our assistant +let assistant_request = CreateAssistantRequestArgs::default() + .name(&assistant_name) //assistant name + .instructions(&instructions) //instruction set + .model("gpt-3.5-turbo-1106") //model + .build()?; +//create the assistant in the client +let assistant = client.assistants().create(assistant_request).await?; +``` +2. We create a thread in which the conversation will run. +``` +//create a thread for the conversation +let thread_request = CreateThreadRequestArgs::default().build()?; +let thread = client.threads().create(thread_request.clone()).await?; +``` +3. we post messages to the thread. +``` +//create a message for the thread +let message = CreateMessageRequestArgs::default() + .role("user") + .content(input.clone()) + .build()?; + +//attach message to the thread +let _message_obj = client + .threads() + .messages(&thread.id) + .create(message) + .await?; +``` +4. we create a run on the thread and await its completion. +``` +//create a run for the thread +let run_request = CreateRunRequestArgs::default() + .assistant_id(assistant_id) + .build()?; +let run = client + .threads() + .runs(&thread.id) + .create(run_request) + .await?; + +``` +5. we retrieve and display the response +``` +this step does not fit in a clean code snippet, take a look at the source code for a better understanding +``` diff --git a/examples/assistants/src/main.rs b/examples/assistants/src/main.rs new file mode 100644 index 00000000..eaebde24 --- /dev/null +++ b/examples/assistants/src/main.rs @@ -0,0 +1,159 @@ +use async_openai::{ + types::{ + CreateAssistantRequestArgs, CreateMessageRequestArgs, CreateRunRequestArgs, + CreateThreadRequestArgs, MessageContent, MessageRole, RunStatus, + }, + Client, +}; +use std::error::Error; +use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + std::env::set_var("RUST_LOG", "ERROR"); + + // Setup tracing subscriber so that library can log the errors + tracing_subscriber::registry() + .with(fmt::layer()) + .with(EnvFilter::from_default_env()) + .init(); + + let query = [("limit", "1")]; //limit the list responses to 1 message + + //create a client + let client = Client::new(); + + //create a thread for the conversation + let thread_request = CreateThreadRequestArgs::default().build()?; + let thread = client.threads().create(thread_request.clone()).await?; + + //ask the user for the name of the assistant + println!("--- Enter the name of your assistant"); + //get user input + let mut assistant_name = String::new(); + std::io::stdin().read_line(&mut assistant_name).unwrap(); + + //ask the user for the instruction set for the assistant + println!("--- Enter the instruction set for your new assistant"); + //get user input + let mut instructions = String::new(); + std::io::stdin().read_line(&mut instructions).unwrap(); + + //create the assistant + let assistant_request = CreateAssistantRequestArgs::default() + .name(&assistant_name) + .instructions(&instructions) + .model("gpt-3.5-turbo-1106") + .build()?; + let assistant = client.assistants().create(assistant_request).await?; + //get the id of the assistant + let assistant_id = &assistant.id; + + loop { + println!("--- How can I help you?"); + //get user input + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + + //break out of the loop if the user enters exit() + if input.trim() == "exit()" { + break; + } + + //create a message for the thread + let message = CreateMessageRequestArgs::default() + .role(MessageRole::User) + .content(input.clone()) + .build()?; + + //attach message to the thread + let _message_obj = client + .threads() + .messages(&thread.id) + .create(message) + .await?; + + //create a run for the thread + let run_request = CreateRunRequestArgs::default() + .assistant_id(assistant_id) + .build()?; + let run = client + .threads() + .runs(&thread.id) + .create(run_request) + .await?; + + //wait for the run to complete + let mut awaiting_response = true; + while awaiting_response { + //retrieve the run + let run = client.threads().runs(&thread.id).retrieve(&run.id).await?; + //check the status of the run + match run.status { + RunStatus::Completed => { + awaiting_response = false; + // once the run is completed we + // get the response from the run + // which will be the first message + // in the thread + + //retrieve the response from the run + let response = client.threads().messages(&thread.id).list(&query).await?; + //get the message id from the response + let message_id = response.data.first().unwrap().id.clone(); + //get the message from the response + let message = client + .threads() + .messages(&thread.id) + .retrieve(&message_id) + .await?; + //get the content from the message + let content = message.content.first().unwrap(); + //get the text from the content + let text = match content { + MessageContent::Text(text) => text.text.value.clone(), + MessageContent::ImageFile(_) | MessageContent::ImageUrl(_) => { + panic!("imaged are not expected in this example"); + } + MessageContent::Refusal(refusal) => refusal.refusal.clone(), + }; + //print the text + println!("--- Response: {}\n", text); + } + RunStatus::Failed => { + awaiting_response = false; + println!("--- Run Failed: {:#?}", run); + } + RunStatus::Queued => { + println!("--- Run Queued"); + } + RunStatus::Cancelling => { + println!("--- Run Cancelling"); + } + RunStatus::Cancelled => { + println!("--- Run Cancelled"); + } + RunStatus::Expired => { + println!("--- Run Expired"); + } + RunStatus::RequiresAction => { + println!("--- Run Requires Action"); + } + RunStatus::InProgress => { + println!("--- In Progress ..."); + } + RunStatus::Incomplete => { + println!("--- Run Incomplete"); + } + } + //wait for 1 second before checking the status again + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + } + } + + //once we have broken from the main loop we can delete the assistant and thread + client.assistants().delete(assistant_id).await?; + client.threads().delete(&thread.id).await?; + + Ok(()) +} diff --git a/examples/audio-speech/.gitignore b/examples/audio-speech/.gitignore new file mode 100644 index 00000000..cbf36313 --- /dev/null +++ b/examples/audio-speech/.gitignore @@ -0,0 +1 @@ +audio.mp3 diff --git a/examples/codex/Cargo.toml b/examples/audio-speech/Cargo.toml similarity index 62% rename from examples/codex/Cargo.toml rename to examples/audio-speech/Cargo.toml index be0ae476..32c19367 100644 --- a/examples/codex/Cargo.toml +++ b/examples/audio-speech/Cargo.toml @@ -1,9 +1,9 @@ [package] -name = "codex" +name = "audio-speech" version = "0.1.0" edition = "2021" publish = false [dependencies] async-openai = {path = "../../async-openai"} -tokio = {version = "1.22.0", features = ["full"]} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/audio-speech/README.md b/examples/audio-speech/README.md new file mode 100644 index 00000000..ef30a3c7 --- /dev/null +++ b/examples/audio-speech/README.md @@ -0,0 +1,3 @@ +### Output (as an mp3 file) + +> Today is a wonderful day to build something people love! diff --git a/examples/audio-speech/src/main.rs b/examples/audio-speech/src/main.rs new file mode 100644 index 00000000..ebbcf62b --- /dev/null +++ b/examples/audio-speech/src/main.rs @@ -0,0 +1,22 @@ +use async_openai::{ + types::{CreateSpeechRequestArgs, SpeechModel, Voice}, + Client, +}; +use std::error::Error; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let request = CreateSpeechRequestArgs::default() + .input("Today is a wonderful day to build something people love!") + .voice(Voice::Alloy) + .model(SpeechModel::Tts1) + .build()?; + + let response = client.audio().speech(request).await?; + + response.save("./data/audio.mp3").await?; + + Ok(()) +} diff --git a/examples/audio-transcribe/Cargo.toml b/examples/audio-transcribe/Cargo.toml new file mode 100644 index 00000000..e5112626 --- /dev/null +++ b/examples/audio-transcribe/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "audio-transcribe" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/audio-transcribe/README.md b/examples/audio-transcribe/README.md new file mode 100644 index 00000000..3c0c8927 --- /dev/null +++ b/examples/audio-transcribe/README.md @@ -0,0 +1,3 @@ +### Output + +> Hello, I'm David Attenborough. I'm speaking to you from my home because, like many of you, I've spent much of the last year indoors, away from friends, family, and access to the natural world. It's been a challenging few months for many of us, but the reaction to these extraordinary times has proved that when we work together, there is no limit to what we can accomplish. Today, we are experiencing environmental change as never before. And the need to take action has never been more urgent. This year, the world will gather in Glasgow for the United Nations Climate Change Conference. It's a crucial moment in our history. This could be a year for positive change for ourselves, for our planet, and for the wonderful creatures with which we share it. A year the world could remember proudly and say, we made a difference. As we make our New Year's resolutions, let's think about what each of us could do. What positive changes could we make in our own lives? So here's to a brighter year ahead. Let's make 2021 a happy New Year for all the inhabitants of our perfect planet. diff --git a/examples/audio-transcribe/audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3 b/examples/audio-transcribe/audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3 new file mode 100644 index 00000000..42f2cb40 Binary files /dev/null and b/examples/audio-transcribe/audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3 differ diff --git a/examples/audio-transcribe/src/main.rs b/examples/audio-transcribe/src/main.rs new file mode 100644 index 00000000..8cc610db --- /dev/null +++ b/examples/audio-transcribe/src/main.rs @@ -0,0 +1,71 @@ +use async_openai::{ + types::{AudioResponseFormat, CreateTranscriptionRequestArgs, TimestampGranularity}, + Client, +}; +use std::error::Error; + +#[tokio::main] +async fn main() -> Result<(), Box> { + transcribe_json().await?; + transcribe_verbose_json().await?; + transcribe_srt().await?; + Ok(()) +} + +async fn transcribe_json() -> Result<(), Box> { + let client = Client::new(); + // Credits and Source for audio: https://www.youtube.com/watch?v=oQnDVqGIv4s + let request = CreateTranscriptionRequestArgs::default() + .file( + "./audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3", + ) + .model("whisper-1") + .response_format(AudioResponseFormat::Json) + .build()?; + + let response = client.audio().transcribe(request).await?; + println!("{}", response.text); + Ok(()) +} + +async fn transcribe_verbose_json() -> Result<(), Box> { + let client = Client::new(); + let request = CreateTranscriptionRequestArgs::default() + .file( + "./audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3", + ) + .model("whisper-1") + .response_format(AudioResponseFormat::VerboseJson) + .timestamp_granularities(vec![ + TimestampGranularity::Word, + TimestampGranularity::Segment, + ]) + .build()?; + + let response = client.audio().transcribe_verbose_json(request).await?; + + println!("{}", response.text); + if let Some(words) = &response.words { + println!("- {} words", words.len()); + } + if let Some(segments) = &response.segments { + println!("- {} segments", segments.len()); + } + + Ok(()) +} + +async fn transcribe_srt() -> Result<(), Box> { + let client = Client::new(); + let request = CreateTranscriptionRequestArgs::default() + .file( + "./audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3", + ) + .model("whisper-1") + .response_format(AudioResponseFormat::Srt) + .build()?; + + let response = client.audio().transcribe_raw(request).await?; + println!("{}", String::from_utf8_lossy(response.as_ref())); + Ok(()) +} diff --git a/examples/audio-translate/Cargo.toml b/examples/audio-translate/Cargo.toml new file mode 100644 index 00000000..ba64e89a --- /dev/null +++ b/examples/audio-translate/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "audio-translate" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/audio-translate/README.md b/examples/audio-translate/README.md new file mode 100644 index 00000000..65c0b179 --- /dev/null +++ b/examples/audio-translate/README.md @@ -0,0 +1,3 @@ +### Output + +> The boat does not cross the waves in fear. Those who try never lose. When the tiny ant walks with the grain, It slips on the walls a hundred times. The faith in the mind fills the veins with courage. It does not fall and rise. After all, its hard work is not in vain. Those who try never lose. The tiny ant dives in the vermillion. It comes back empty-handed. The pearls do not meet in the deep water. Its fist is not empty every time. Those who try never lose. And the last verse is, Failure is a challenge, accept it. See what is missing and improve. Until you are unsuccessful, give up sleep and peace. Do not run away from the battlefield of struggle. You will not win without doing anything. Those who try never lose. diff --git a/examples/audio-translate/audio/koshish karne walon ki haar nahi hoti by amitabh bachchan_320kbps.mp3 b/examples/audio-translate/audio/koshish karne walon ki haar nahi hoti by amitabh bachchan_320kbps.mp3 new file mode 100644 index 00000000..c802391b Binary files /dev/null and b/examples/audio-translate/audio/koshish karne walon ki haar nahi hoti by amitabh bachchan_320kbps.mp3 differ diff --git a/examples/audio-translate/src/main.rs b/examples/audio-translate/src/main.rs new file mode 100644 index 00000000..acc59389 --- /dev/null +++ b/examples/audio-translate/src/main.rs @@ -0,0 +1,43 @@ +use async_openai::{ + types::{AudioResponseFormat, CreateTranslationRequestArgs}, + Client, +}; +use std::error::Error; + +async fn translate_srt() -> Result<(), Box> { + 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> { + let client = Client::new(); + // Credits and Source for audio: https://www.youtube.com/watch?v=bHWmzQ4HTS0 + let request = CreateTranslationRequestArgs::default() + .file("./audio/koshish karne walon ki haar nahi hoti by amitabh bachchan_320kbps.mp3") + .model("whisper-1") + .build()?; + + let response = client.audio().translate(request).await?; + + println!("translate_verbose_json:"); + println!("{}", response.text); + + Ok(()) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + translate_verbose_json().await?; + translate_srt().await?; + Ok(()) +} diff --git a/examples/azure-openai-service/Cargo.toml b/examples/azure-openai-service/Cargo.toml new file mode 100644 index 00000000..ec8f0b41 --- /dev/null +++ b/examples/azure-openai-service/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "azure-openai-service" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +tokio = { version = "1.43.0", features = ["full"] } +futures = "0.3.31" diff --git a/examples/azure-openai-service/README.md b/examples/azure-openai-service/README.md new file mode 100644 index 00000000..229cc5ab --- /dev/null +++ b/examples/azure-openai-service/README.md @@ -0,0 +1,3 @@ +## Overview + +Please note: before running this example configure api_base, api_key, deploy_id and api_version in main.rs. diff --git a/examples/azure-openai-service/src/main.rs b/examples/azure-openai-service/src/main.rs new file mode 100644 index 00000000..8137cc18 --- /dev/null +++ b/examples/azure-openai-service/src/main.rs @@ -0,0 +1,103 @@ +use std::error::Error; + +use async_openai::{ + config::AzureConfig, + types::{ + ChatCompletionRequestSystemMessageArgs, ChatCompletionRequestUserMessageArgs, + CreateChatCompletionRequestArgs, CreateEmbeddingRequestArgs, + }, + Client, +}; + +async fn chat_completion_example(client: &Client) -> Result<(), Box> { + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-3.5-turbo") + .messages([ + ChatCompletionRequestSystemMessageArgs::default() + .content("You are a helpful assistant.") + .build()? + .into(), + ChatCompletionRequestUserMessageArgs::default() + .content("How does large language model work?") + .build()? + .into(), + ]) + .build()?; + + let response = client.chat().create(request).await?; + + println!("\nResponse:\n"); + for choice in response.choices { + println!( + "{}: Role: {} Content: {:?}", + choice.index, choice.message.role, choice.message.content + ); + } + Ok(()) +} + +// Bug (help wanted): https://github.com/64bit/async-openai/pull/67#issuecomment-1555165805 +// async fn completions_stream_example(client: &Client) -> Result<(), Box> { +// let request = CreateCompletionRequestArgs::default() +// .model("text-davinci-003") +// .n(1) +// .prompt("Tell me a short bedtime story about Optimus Prime and Bumblebee in Sir David Attenborough voice") +// .stream(true) +// .max_tokens(512_u32) +// .build()?; + +// let mut stream = client.completions().create_stream(request).await?; + +// while let Some(response) = stream.next().await { +// match response { +// Ok(ccr) => ccr.choices.iter().for_each(|c| { +// print!("{}", c.text); +// }), +// Err(e) => eprintln!("{}", e), +// } +// } +// Ok(()) +// } + +async fn embedding_example(client: &Client) -> Result<(), Box> { + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-ada-002") + .input("Why do programmers hate nature? It has too many bugs.") + .build()?; + + let response = client.embeddings().create(request).await?; + + for data in response.data { + println!( + "[{}]: has embedding of length {}", + data.index, + data.embedding.len() + ) + } + + Ok(()) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let config = AzureConfig::new() + .with_api_base("https://your-resource-name.openai.azure.com") + .with_api_key("...") + .with_deployment_id("deployment-id") + .with_api_version("2023-03-15-preview"); + + let client = Client::with_config(config); + + // Run embedding Example + embedding_example(&client).await?; + + // Run completions stream Example + // Bug (help wanted): https://github.com/64bit/async-openai/pull/67#issuecomment-1555165805 + //completions_stream_example(&client).await?; + + // Run chat completion example + chat_completion_example(&client).await?; + + Ok(()) +} diff --git a/examples/bring-your-own-type/Cargo.toml b/examples/bring-your-own-type/Cargo.toml new file mode 100644 index 00000000..e99a7454 --- /dev/null +++ b/examples/bring-your-own-type/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "bring-your-own-type" +version = "0.1.0" +edition = "2021" +rust-version.workspace = true +publish = false + +[dependencies] +async-openai = {path = "../../async-openai", features = ["byot"]} +tokio = { version = "1.43.0", features = ["full"] } +serde_json = "1" +futures-core = "0.3" +futures = "0.3" diff --git a/examples/bring-your-own-type/README.md b/examples/bring-your-own-type/README.md new file mode 100644 index 00000000..c9452a7f --- /dev/null +++ b/examples/bring-your-own-type/README.md @@ -0,0 +1,3 @@ +## Bring your own types + +This example demonstrate a custom request and response type that's not part of the async-openai library. \ No newline at end of file diff --git a/examples/bring-your-own-type/src/main.rs b/examples/bring-your-own-type/src/main.rs new file mode 100644 index 00000000..e4226514 --- /dev/null +++ b/examples/bring-your-own-type/src/main.rs @@ -0,0 +1,79 @@ +use std::{ + error::Error, + io::{stdout, Write}, + pin::Pin, +}; + +use async_openai::{config::OpenAIConfig, error::OpenAIError, Client}; +use futures::StreamExt; +use futures_core::Stream; + +use serde_json::{json, Value}; + +async fn chat(client: &Client) -> Result<(), Box> { + let response: Value = client + .chat() + .create_byot(json!({ + "messages": [ + { + "role": "developer", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "What do you think about life?" + } + ], + "model": "gpt-4o", + "store": false + })) + .await?; + + if let Some(content) = response["choices"][0]["message"]["content"].as_str() { + println!("{}", content); + } + + Ok(()) +} + +type MyStreamingType = Pin> + Send>>; + +async fn chat_stream(client: &Client) -> Result<(), Box> { + let mut stream: MyStreamingType = client + .chat() + .create_stream_byot(json!({ + "messages": [ + { + "role": "developer", + "content": "You are a helpful assistant" + }, + { + "role": "user", + "content": "What do you think about life?" + } + ], + "model": "gpt-4o", + "stream": true + })) + .await?; + + let mut lock = stdout().lock(); + while let Some(result) = stream.next().await { + if let Ok(chunk) = result { + if let Some(content) = chunk["choices"][0]["delta"]["content"].as_str() { + write!(lock, "{}", content).unwrap(); + } + } + stdout().flush()?; + } + + Ok(()) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + chat(&client).await?; + chat_stream(&client).await?; + Ok(()) +} diff --git a/examples/chat-store/Cargo.toml b/examples/chat-store/Cargo.toml new file mode 100644 index 00000000..0bf77f5f --- /dev/null +++ b/examples/chat-store/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "chat-store" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/chat-store/README.md b/examples/chat-store/README.md new file mode 100644 index 00000000..b933bb7d --- /dev/null +++ b/examples/chat-store/README.md @@ -0,0 +1,5 @@ +### Output + +> Response: +> +> 0: Role: assistant Content: To hide the dock on a Mac, you can follow these steps:\n\n1. Click on the Apple logo in the top-left corner of the screen.\n2. Select \"System Preferences\" from the drop-down menu.\n3. Click on \"Dock & Menu Bar\".\n4. Under the \"Dock\" section, you will see an option to \"Automatically hide and show the Dock\". Check the box next to this option.\n5. The dock will now be hidden until you move your cursor to the bottom of the screen, at which point it will slide back into view.\n\nYou can also change the size and position of the dock in the Dock preferences \ No newline at end of file diff --git a/examples/chat-store/src/main.rs b/examples/chat-store/src/main.rs new file mode 100644 index 00000000..0b611b3e --- /dev/null +++ b/examples/chat-store/src/main.rs @@ -0,0 +1,49 @@ +use async_openai::{ + types::{ + ChatCompletionRequestSystemMessageArgs, ChatCompletionRequestUserMessageArgs, + CreateChatCompletionRequestArgs, + }, + Client, +}; +use serde_json::json; +use std::error::Error; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-3.5-turbo") + .store(true) + .metadata(json!({ + "role": "manager", + "department": "accounting", + "source": "homepage", + })) + .messages([ + ChatCompletionRequestSystemMessageArgs::default() + .content("You are a corporate IT support expert.") + .build()? + .into(), + ChatCompletionRequestUserMessageArgs::default() + .content("How can I hide the dock on my Mac?") + .build()? + .into(), + ]) + .build()?; + + println!("{}", serde_json::to_string(&request).unwrap()); + + let response = client.chat().create(request).await?; + + println!("\nResponse:\n"); + for choice in response.choices { + println!( + "{}: Role: {} Content: {:?}", + choice.index, choice.message.role, choice.message.content + ); + } + + Ok(()) +} diff --git a/examples/chat-stream/Cargo.toml b/examples/chat-stream/Cargo.toml new file mode 100644 index 00000000..85b4cf43 --- /dev/null +++ b/examples/chat-stream/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "chat-stream" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +tokio = { version = "1.43.0", features = ["full"] } +futures = "0.3.31" diff --git a/examples/chat-stream/README.md b/examples/chat-stream/README.md new file mode 100644 index 00000000..c3a61e2a --- /dev/null +++ b/examples/chat-stream/README.md @@ -0,0 +1,107 @@ +## Output + +Content: "write a marketing blog praising and introducing Rust library async-openai" + + + + + +## Other Examples + +Content: "write a song if Coldplay and AR Rahman collaborated together" + +### Output 1 + +``` +Verse 1 (Chris Martin): +The sun rises over the horizon +I feel a new day has come +The warmth on my face, I'm alive +I hear the sound of a new song + +Chorus (Chris Martin and AR Rahman): +So come let's sing together +With our hearts and our souls +Our voices will unite forever +In a harmony we'll never let go + +Verse 2 (AR Rahman): +We come from different worlds +But music knows no bounds +Our melodies will intertwine +In a rhythm that astounds + +Chorus (Chris Martin and AR Rahman): +So come let's sing together +With our hearts and our souls +Our voices will unite forever +In a harmony we'll never let go + +Bridge (AR Rahman): +We'll dance beneath the stars +And let our spirits soar +Our hearts will beat as one +As we sing forevermore + +Chorus (Chris Martin and AR Rahman): +So come let's sing together +With our hearts and our souls +Our voices will unite forever +In a harmony we'll never let go + +Outro (Chris Martin and AR Rahman): +Our love for music is our bond +And it will never fade +Together we will journey on +In a symphony that's never played. + +``` + + +### Output 2 + +``` +Verse 1: +In the streets of Mumbai +Where the colors come alive +I see a boy with a dream +He's searching for the light + +Then the music starts to play +And the world begins to change +The rhythm of his heartbeat +Will guide him to his fate + +Chorus: +So don't give up tonight +Hold on to your dreams, don't hide +Let the music lead the way +And take you to a brand new day + +Verse 2: +In the misty hills of England +Where the silence greets the dawn +I see a girl with a voice +She's searching for a song + +Then the melody fills the air +And the words begin to flow +The power of her fire +Will guide her to her home + +(Chorus) + +Bridge: +Every note, every beat +Takes us to a different street +And we'll find our way +Through the darkness and the grey + +(Chorus) + +Outro: +So let's dance to the sunrise +With our hearts and souls alive +The world is waiting for us +To make our dreams come to life +``` diff --git a/examples/chat-stream/src/main.rs b/examples/chat-stream/src/main.rs new file mode 100644 index 00000000..2241e9ee --- /dev/null +++ b/examples/chat-stream/src/main.rs @@ -0,0 +1,50 @@ +use std::error::Error; +use std::io::{stdout, Write}; + +use async_openai::types::ChatCompletionRequestUserMessageArgs; +use async_openai::{types::CreateChatCompletionRequestArgs, Client}; +use futures::StreamExt; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let request = CreateChatCompletionRequestArgs::default() + .model("gpt-3.5-turbo") + .max_tokens(512u32) + .messages([ChatCompletionRequestUserMessageArgs::default() + .content("Write a marketing blog praising and introducing Rust library async-openai") + .build()? + .into()]) + .build()?; + + let mut stream = client.chat().create_stream(request).await?; + + // From Rust docs on print: https://doc.rust-lang.org/std/macro.print.html + // + // Note that stdout is frequently line-buffered by default so it may be necessary + // to use io::stdout().flush() to ensure the output is emitted immediately. + // + // The print! macro will lock the standard output on each call. + // If you call print! within a hot loop, this behavior may be the bottleneck of the loop. + // To avoid this, lock stdout with io::stdout().lock(): + + let mut lock = stdout().lock(); + while let Some(result) = stream.next().await { + match result { + Ok(response) => { + response.choices.iter().for_each(|chat_choice| { + if let Some(ref content) = chat_choice.delta.content { + write!(lock, "{}", content).unwrap(); + } + }); + } + Err(err) => { + writeln!(lock, "error: {err}").unwrap(); + } + } + stdout().flush()?; + } + + Ok(()) +} diff --git a/examples/chat/Cargo.toml b/examples/chat/Cargo.toml new file mode 100644 index 00000000..b7251e36 --- /dev/null +++ b/examples/chat/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "chat" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/chat/README.md b/examples/chat/README.md new file mode 100644 index 00000000..e080c6c0 --- /dev/null +++ b/examples/chat/README.md @@ -0,0 +1,5 @@ +### Output + +> Response: +> +> 0: Role: assistant Content: The 2020 World Series was played at Globe Life Field in Arlington, Texas. diff --git a/examples/chat/src/main.rs b/examples/chat/src/main.rs new file mode 100644 index 00000000..4ca389ef --- /dev/null +++ b/examples/chat/src/main.rs @@ -0,0 +1,51 @@ +use std::error::Error; + +use async_openai::{ + types::{ + ChatCompletionRequestAssistantMessageArgs, ChatCompletionRequestSystemMessageArgs, + ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs, + }, + Client, +}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-3.5-turbo") + .messages([ + ChatCompletionRequestSystemMessageArgs::default() + .content("You are a helpful assistant.") + .build()? + .into(), + ChatCompletionRequestUserMessageArgs::default() + .content("Who won the world series in 2020?") + .build()? + .into(), + ChatCompletionRequestAssistantMessageArgs::default() + .content("The Los Angeles Dodgers won the World Series in 2020.") + .build()? + .into(), + ChatCompletionRequestUserMessageArgs::default() + .content("Where was it played?") + .build()? + .into(), + ]) + .build()?; + + println!("{}", serde_json::to_string(&request).unwrap()); + + let response = client.chat().create(request).await?; + + println!("\nResponse:\n"); + for choice in response.choices { + println!( + "{}: Role: {} Content: {:?}", + choice.index, choice.message.role, choice.message.content + ); + } + + Ok(()) +} diff --git a/examples/codex/README.md b/examples/codex/README.md deleted file mode 100644 index 5cd30a81..00000000 --- a/examples/codex/README.md +++ /dev/null @@ -1,30 +0,0 @@ -## codex - -### Output - -The output generated by this code was: - -```rust -/// Rust language -/// Function to download a url and save it to disk. -/// -/// # Arguments -/// -/// * `url` - The url to download -/// * `path` - The path to save the file to -/// -/// # Example -/// -/// ``` -/// use download_file::download_file; -/// -/// download_file("https://www.rust-lang.org/logos/rust-logo-512x512.png", "./rust-logo.png"); -/// ``` -pub fn download_file(url: &str, path: &str) -> Result<(), Box> { - let mut resp = reqwest::get(url)?; - let mut out = File::create(path)?; - io::copy(&mut resp, &mut out)?; - Ok(()) -} - -``` diff --git a/examples/codex/src/main.rs b/examples/codex/src/main.rs deleted file mode 100644 index b3a69302..00000000 --- a/examples/codex/src/main.rs +++ /dev/null @@ -1,34 +0,0 @@ -use std::error::Error; - -use async_openai as openai; -use openai::{types::CreateCompletionRequest, Client, Completion}; - -#[tokio::main] -async fn main() -> Result<(), Box> { - let prompt = "/// Rust language -/// Function to download a url and save it to disk."; - - let client = Client::new(); - - let request = CreateCompletionRequest { - model: "code-davinci-002".to_owned(), - prompt: Some(prompt.to_owned()), - max_tokens: Some(256), - temperature: Some(0.0), - top_p: Some(1.0), - best_of: Some(1), - logprobs: Some(1), - echo: Some(true), - frequency_penalty: Some(0.0), - presence_penalty: Some(0.0), - ..Default::default() - }; - - let response = Completion::create(&client, request).await?; - - let choice = response.choices.iter().nth(0).unwrap(); - - println!("{}", choice.text); - - Ok(()) -} diff --git a/examples/completions-stream/Cargo.toml b/examples/completions-stream/Cargo.toml index 196e1b6b..fd6d5e59 100644 --- a/examples/completions-stream/Cargo.toml +++ b/examples/completions-stream/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "completions-stream" -version = "0.1.1" +version = "0.1.0" edition = "2021" publish = false [dependencies] async-openai = {path = "../../async-openai"} -futures = "0.3.25" -tokio = { version = "1.22.0", features = ["full"] } +futures = "0.3.30" +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/completions-stream/src/main.rs b/examples/completions-stream/src/main.rs index fb8bc0e5..70b9f5bc 100644 --- a/examples/completions-stream/src/main.rs +++ b/examples/completions-stream/src/main.rs @@ -1,21 +1,19 @@ -use async_openai as openai; +use async_openai::{types::CreateCompletionRequestArgs, Client}; use futures::StreamExt; -use openai::{types::CreateCompletionRequest, Client, Completion}; #[tokio::main] async fn main() -> Result<(), Box> { let client = Client::new(); - let completion_request = CreateCompletionRequest { - model: "text-davinci-003".to_owned(), - n: Some(1), - prompt: Some("Tell me a bedtime story about Optimus Prime and Bumblebee".to_owned()), - max_tokens: Some(1024), - stream: Some(true), - ..Default::default() - }; + let request = CreateCompletionRequestArgs::default() + .model("gpt-3.5-turbo-instruct") + .n(1) + .prompt("Tell me a bedtime story about Optimus Prime and Bumblebee") + .stream(true) + .max_tokens(1024_u32) + .build()?; - let mut stream = Completion::create_stream(&client, completion_request).await?; + let mut stream = client.completions().create_stream(request).await?; while let Some(response) = stream.next().await { match response { diff --git a/examples/completions-web-search/Cargo.toml b/examples/completions-web-search/Cargo.toml new file mode 100644 index 00000000..f58d4f8b --- /dev/null +++ b/examples/completions-web-search/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "completions-web-search" +version = "0.1.0" +edition = "2021" +publish = false + + +[dependencies] +async-openai = {path = "../../async-openai"} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/completions-web-search/src/main.rs b/examples/completions-web-search/src/main.rs new file mode 100644 index 00000000..6839895e --- /dev/null +++ b/examples/completions-web-search/src/main.rs @@ -0,0 +1,46 @@ +use async_openai::types::{ + ChatCompletionRequestUserMessageArgs, WebSearchContextSize, WebSearchLocation, + WebSearchOptions, WebSearchUserLocation, WebSearchUserLocationType, +}; +use async_openai::{types::CreateChatCompletionRequestArgs, Client}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + let user_prompt = "What is the weather like today? Be concise."; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(256u32) + .model("gpt-4o-mini-search-preview") + .messages([ChatCompletionRequestUserMessageArgs::default() + .content(user_prompt) + .build()? + .into()]) + .web_search_options(WebSearchOptions { + search_context_size: Some(WebSearchContextSize::Low), + user_location: Some(WebSearchUserLocation { + r#type: WebSearchUserLocationType::Approximate, + approximate: WebSearchLocation { + city: Some("Paris".to_string()), + ..Default::default() + }, + }), + }) + .build()?; + + let response_message = client + .chat() + .create(request) + .await? + .choices + .first() + .unwrap() + .message + .clone(); + + if let Some(content) = response_message.content { + println!("Response: {}", content); + } + + Ok(()) +} diff --git a/examples/completions/Cargo.toml b/examples/completions/Cargo.toml index 6d51a039..0574060f 100644 --- a/examples/completions/Cargo.toml +++ b/examples/completions/Cargo.toml @@ -1,9 +1,9 @@ [package] -name = "example-completions" +name = "completions" version = "0.1.0" edition = "2021" publish = false [dependencies] async-openai = {path = "../../async-openai"} -tokio = { version = "1.22.0", features = ["full"] } +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/completions/src/main.rs b/examples/completions/src/main.rs index 24cb0687..d54b4837 100644 --- a/examples/completions/src/main.rs +++ b/examples/completions/src/main.rs @@ -1,17 +1,41 @@ -use async_openai as openai; -use openai::{types::CreateCompletionRequest, Client, Completion}; +use std::error::Error; + +use async_openai::{types::CreateCompletionRequestArgs, Client}; #[tokio::main] -async fn main() { +async fn main() -> Result<(), Box> { let client = Client::new(); - let completion_request = CreateCompletionRequest { - model: "text-davinci-003".to_owned(), - prompt: Some("Tell me a joke about universe".to_owned()), - ..Default::default() - }; + // single + let request = CreateCompletionRequestArgs::default() + .model("gpt-3.5-turbo-instruct") + .prompt("Tell me a joke about the universe") + .max_tokens(40_u32) + .build()?; + + let response = client.completions().create(request).await?; + + println!("\nResponse (single):\n"); + for choice in response.choices { + println!("{}", choice.text); + } + + // multiple + let request = CreateCompletionRequestArgs::default() + .model("gpt-3.5-turbo-instruct") + .prompt([ + "How old is the human civilization?", + "How old is the Earth?", + ]) + .max_tokens(40_u32) + .build()?; + + let response = client.completions().create(request).await?; - let completion_response = Completion::create(&client, completion_request).await; + println!("\nResponse (multiple):\n"); + for choice in response.choices { + println!("{}", choice.text); + } - println!("{:#?}", completion_response); + Ok(()) } diff --git a/examples/create-edit/src/main.rs b/examples/create-edit/src/main.rs deleted file mode 100644 index 0f787893..00000000 --- a/examples/create-edit/src/main.rs +++ /dev/null @@ -1,29 +0,0 @@ -use std::error::Error; - -use async_openai as openai; -use openai::{types::CreateEditRequest, Client, Edit}; - -#[tokio::main] -async fn main() -> Result<(), Box> { - let client = Client::new(); - - let request = CreateEditRequest { - model: "text-davinci-edit-001".to_string(), - input: Some( - "It's surely our responsibility to do everything within our power - to create a planet that provides a home not just for us, - but for all life on Earth." - .into(), - ), - instruction: "Add a new paragraph in Sir David Attenborough voice".to_string(), - n: Some(2), - temperature: Some(0.9), // recommended altering this or top_p but not both. - top_p: None, - }; - - let response = Edit::create(&client, request).await?; - - println!("response: {:#?}", response); - - Ok(()) -} diff --git a/examples/create-image-b64-json/Cargo.toml b/examples/create-image-b64-json/Cargo.toml index a87487d7..4d5e1c94 100644 --- a/examples/create-image-b64-json/Cargo.toml +++ b/examples/create-image-b64-json/Cargo.toml @@ -6,4 +6,4 @@ publish = false [dependencies] async-openai = {path = "../../async-openai"} -tokio = {version = "1.22.0", features = ["full"]} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/create-image-b64-json/README.md b/examples/create-image-b64-json/README.md index ccef5cdb..9f03b206 100644 --- a/examples/create-image-b64-json/README.md +++ b/examples/create-image-b64-json/README.md @@ -1,8 +1,5 @@ ## create-image-b64-json -Its recommended to use the `ResponseFormat` to be `Url` as the files can be downloaded and saved in dedicated Tokio task. -In case of b64 json - the API response contains full images but files are still saved in separate Tokio task. - ### Input

diff --git a/examples/create-image-b64-json/src/main.rs b/examples/create-image-b64-json/src/main.rs index 4acfdb47..3cae2396 100644 --- a/examples/create-image-b64-json/src/main.rs +++ b/examples/create-image-b64-json/src/main.rs @@ -1,30 +1,32 @@ -use std::error::Error; - -use async_openai as openai; -use openai::{ - types::{CreateImageRequest, ImageSize, ResponseFormat}, - Client, Image, +use async_openai::{ + types::{CreateImageRequestArgs, ImageResponseFormat, ImageSize}, + Client, }; +use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { // create client, reads OPENAI_API_KEY environment variable for API key. let client = Client::new(); - let request = CreateImageRequest { - prompt: "Generate a logo for github repository async-openai".to_owned(), - n: Some(2), - response_format: Some(ResponseFormat::B64Json), - size: Some(ImageSize::S256x256), - user: Some("async-openai".to_owned()), - }; + let request = CreateImageRequestArgs::default() + .prompt("Generate a logo for github repository async-openai") + .n(2) + .response_format(ImageResponseFormat::B64Json) + .size(ImageSize::S256x256) + .user("async-openai") + .build()?; - let response = Image::create(&client, request).await?; + let response = client.images().create(request).await?; // Response already contains image data in base64 format. - // Save images to ./data directory, each save happens in dedicated Tokio task - // (creates directory when it doesn't exist) - response.save("./data").await?; + // Save each image to ./data directory in dedicated Tokio task. + // Directory is created if it doesn't exist. + let paths = response.save("./data").await?; + + paths + .iter() + .for_each(|path| println!("Image file path: {}", path.display())); Ok(()) } diff --git a/examples/create-image-edit/Cargo.toml b/examples/create-image-edit/Cargo.toml index d732c7b7..a18f6dd1 100644 --- a/examples/create-image-edit/Cargo.toml +++ b/examples/create-image-edit/Cargo.toml @@ -6,4 +6,4 @@ publish = false [dependencies] async-openai = {path = "../../async-openai"} -tokio = {version = "1.22.0", features = ["full"]} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/create-image-edit/src/main.rs b/examples/create-image-edit/src/main.rs index 1eca3537..20e96735 100644 --- a/examples/create-image-edit/src/main.rs +++ b/examples/create-image-edit/src/main.rs @@ -1,28 +1,30 @@ -use std::error::Error; - -use async_openai as openai; -use openai::{ - types::{CreateImageEditRequest, ImageInput, ImageSize, ResponseFormat}, - Client, Image, +use async_openai::{ + types::{CreateImageEditRequestArgs, DallE2ImageSize, ImageResponseFormat}, + Client, }; +use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { let client = Client::new(); - let request = CreateImageEditRequest { - image: ImageInput::new("./images/sunlit_lounge.png"), - mask: ImageInput::new("./images/mask.png"), - prompt: "A sunlit indoor lounge area with a duck in the pool".to_string(), - n: Some(1), - size: Some(ImageSize::S1024x1024), - response_format: Some(ResponseFormat::Url), - user: Some("async-openai".to_string()), - }; + let request = CreateImageEditRequestArgs::default() + .image("./images/sunlit_lounge.png") + .mask("./images/mask.png") + .prompt("A sunlit indoor lounge area with a duck in the pool") + .n(1) + .size(DallE2ImageSize::S1024x1024) + .response_format(ImageResponseFormat::Url) + .user("async-openai") + .build()?; + + let response = client.images().create_edit(request).await?; - let response = Image::create_edit(&client, request).await?; + let paths = response.save("./data").await?; - response.save("./data").await?; + paths + .iter() + .for_each(|path| println!("image saved at {}", path.display())); Ok(()) } diff --git a/examples/create-image-variation/Cargo.toml b/examples/create-image-variation/Cargo.toml index f4e5e6ee..12de47cb 100644 --- a/examples/create-image-variation/Cargo.toml +++ b/examples/create-image-variation/Cargo.toml @@ -6,4 +6,4 @@ publish = false [dependencies] async-openai = {path = "../../async-openai"} -tokio = {version = "1.22.0", features = ["full"]} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/create-image-variation/src/main.rs b/examples/create-image-variation/src/main.rs index d42cabe7..e0244fec 100644 --- a/examples/create-image-variation/src/main.rs +++ b/examples/create-image-variation/src/main.rs @@ -1,26 +1,28 @@ -use std::error::Error; - -use async_openai as openai; -use openai::{ - types::{CreateImageVariationRequest, ImageInput, ImageSize, ResponseFormat}, - Client, Image, +use async_openai::{ + types::{CreateImageVariationRequestArgs, DallE2ImageSize, ImageResponseFormat}, + Client, }; +use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { let client = Client::new(); - let request = CreateImageVariationRequest { - image: ImageInput::new("./images/cake.png"), - n: Some(1), - size: Some(ImageSize::S512x512), - response_format: Some(ResponseFormat::Url), - user: Some("async-openai".to_string()), - }; + let request = CreateImageVariationRequestArgs::default() + .image("./images/cake.png") + .n(1) + .size(DallE2ImageSize::S512x512) + .response_format(ImageResponseFormat::Url) + .user("async-openai") + .build()?; + + let response = client.images().create_variation(request).await?; - let response = Image::create_variation(&client, request).await?; + let paths = response.save("./data").await?; - response.save("./data").await?; + paths + .iter() + .for_each(|path| println!("image saved at {}", path.display())); Ok(()) } diff --git a/examples/create-image/Cargo.toml b/examples/create-image/Cargo.toml index 73c7d383..4c48adac 100644 --- a/examples/create-image/Cargo.toml +++ b/examples/create-image/Cargo.toml @@ -1,9 +1,9 @@ [package] -name = "example-create-image" +name = "create-image" version = "0.1.0" edition = "2021" publish = false [dependencies] async-openai = {path = "../../async-openai"} -tokio = {version = "1.22.0", features = ["full"]} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/create-image/src/main.rs b/examples/create-image/src/main.rs index 493936dc..5de3467a 100644 --- a/examples/create-image/src/main.rs +++ b/examples/create-image/src/main.rs @@ -1,30 +1,32 @@ -use std::error::Error; - -use async_openai as openai; -use openai::{ - types::{CreateImageRequest, ImageSize, ResponseFormat}, - Client, Image, +use async_openai::{ + types::{CreateImageRequestArgs, ImageResponseFormat, ImageSize}, + Client, }; +use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box> { // create client, reads OPENAI_API_KEY environment variable for API key. let client = Client::new(); - let request = CreateImageRequest { - prompt: "cats on sofa and carpet in living room".to_owned(), - n: Some(2), - response_format: Some(ResponseFormat::Url), - size: Some(ImageSize::S256x256), - user: Some("async-openai".to_owned()), - }; + let request = CreateImageRequestArgs::default() + .prompt("cats on sofa and carpet in living room") + .n(2) + .response_format(ImageResponseFormat::Url) + .size(ImageSize::S256x256) + .user("async-openai") + .build()?; + + let response = client.images().create(request).await?; - let response = Image::create(&client, request).await?; + // Download and save images to ./data directory. + // Each url is downloaded and saved in dedicated Tokio task. + // Directory is created if it doesn't exist. + let paths = response.save("./data").await?; - // download and save images to ./data directory - // Each url download and save happens in dedicated Tokio task - // (creates directory when it doesn't exist) - response.save("./data").await?; + paths + .iter() + .for_each(|path| println!("Image file path: {}", path.display())); Ok(()) } diff --git a/examples/create-edit/Cargo.toml b/examples/embeddings/Cargo.toml similarity index 62% rename from examples/create-edit/Cargo.toml rename to examples/embeddings/Cargo.toml index c03936fe..49855f24 100644 --- a/examples/create-edit/Cargo.toml +++ b/examples/embeddings/Cargo.toml @@ -1,9 +1,9 @@ [package] -name = "create-edit" +name = "embeddings" version = "0.1.0" edition = "2021" publish = false [dependencies] async-openai = {path = "../../async-openai"} -tokio = {version = "1.22.0", features = ["full"]} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/embeddings/src/main.rs b/examples/embeddings/src/main.rs new file mode 100644 index 00000000..f10a8630 --- /dev/null +++ b/examples/embeddings/src/main.rs @@ -0,0 +1,32 @@ +use std::error::Error; + +use async_openai::{types::CreateEmbeddingRequestArgs, Client}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + // An embedding is a vector (list) of floating point numbers. + // The distance between two vectors measures their relatedness. + // Small distances suggest high relatedness and large distances suggest low relatedness. + + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-ada-002") + .input([ + "Why do programmers hate nature? It has too many bugs.", + "Why was the computer cold? It left its Windows open.", + ]) + .build()?; + + let response = client.embeddings().create(request).await?; + + for data in response.data { + println!( + "[{}]: has embedding of length {}", + data.index, + data.embedding.len() + ) + } + + Ok(()) +} diff --git a/examples/function-call-stream/Cargo.toml b/examples/function-call-stream/Cargo.toml new file mode 100644 index 00000000..00153331 --- /dev/null +++ b/examples/function-call-stream/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "function-call-stream" +version = "0.1.0" +edition = "2021" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } +futures = "0.3.31" diff --git a/examples/function-call-stream/src/main.rs b/examples/function-call-stream/src/main.rs new file mode 100644 index 00000000..02282971 --- /dev/null +++ b/examples/function-call-stream/src/main.rs @@ -0,0 +1,153 @@ +use std::collections::HashMap; +use std::error::Error; +use std::io::{stdout, Write}; + +use async_openai::types::{ + ChatCompletionRequestFunctionMessageArgs, ChatCompletionRequestUserMessageArgs, FinishReason, +}; +use async_openai::{ + types::{ChatCompletionFunctionsArgs, CreateChatCompletionRequestArgs}, + Client, +}; + +use async_openai::config::OpenAIConfig; +use futures::StreamExt; +use serde_json::json; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let model = "gpt-4o-mini"; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model(model) + .messages([ChatCompletionRequestUserMessageArgs::default() + .content("What's the weather like in Boston?") + .build()? + .into()]) + .functions([ChatCompletionFunctionsArgs::default() + .name("get_current_weather") + .description("Get the current weather in a given location") + .parameters(json!({ + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }, + }, + "required": ["location"], + })) + .build()?]) + .function_call("auto") + .build()?; + + let mut stream = client.chat().create_stream(request).await?; + + let mut fn_name = String::new(); + let mut fn_args = String::new(); + + let mut lock = stdout().lock(); + while let Some(result) = stream.next().await { + match result { + Ok(response) => { + for chat_choice in response.choices { + if let Some(fn_call) = &chat_choice.delta.function_call { + writeln!(lock, "function_call: {:?}", fn_call).unwrap(); + if let Some(name) = &fn_call.name { + fn_name.clone_from(name); + } + if let Some(args) = &fn_call.arguments { + fn_args.push_str(args); + } + } + if let Some(finish_reason) = &chat_choice.finish_reason { + if matches!(finish_reason, FinishReason::FunctionCall) { + call_fn(&client, &fn_name, &fn_args).await?; + } + } else if let Some(content) = &chat_choice.delta.content { + write!(lock, "{}", content).unwrap(); + } + } + } + Err(err) => { + writeln!(lock, "error: {err}").unwrap(); + } + } + stdout().flush()?; + } + + Ok(()) +} + +async fn call_fn( + client: &Client, + name: &str, + args: &str, +) -> Result<(), Box> { + let mut available_functions: HashMap<&str, fn(&str, &str) -> serde_json::Value> = + HashMap::new(); + available_functions.insert("get_current_weather", get_current_weather); + + let function_args: serde_json::Value = args.parse().unwrap(); + + let model = "gpt-4o-mini"; + let location = function_args["location"].as_str().unwrap(); + let unit = function_args["unit"].as_str().unwrap_or("fahrenheit"); + let function = available_functions.get(name).unwrap(); + let function_response = function(location, unit); // call the function + + let message = vec![ + ChatCompletionRequestUserMessageArgs::default() + .content("What's the weather like in Boston?") + .build()? + .into(), + ChatCompletionRequestFunctionMessageArgs::default() + .content(function_response.to_string()) + .name(name) + .build()? + .into(), + ]; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model(model) + .messages(message) + .build()?; + + // Now stream received response from model, which essentially formats the function response + let mut stream = client.chat().create_stream(request).await?; + + let mut lock = stdout().lock(); + while let Some(result) = stream.next().await { + match result { + Ok(response) => { + response.choices.iter().for_each(|chat_choice| { + if let Some(ref content) = chat_choice.delta.content { + write!(lock, "{}", content).unwrap(); + } + }); + } + Err(err) => { + writeln!(lock, "error: {err}").unwrap(); + } + } + stdout().flush()?; + } + println!("\n"); + Ok(()) +} + +fn get_current_weather(location: &str, unit: &str) -> serde_json::Value { + let weather_info = json!({ + "location": location, + "temperature": "72", + "unit": unit, + "forecast": ["sunny", "windy"] + }); + + weather_info +} diff --git a/examples/function-call/Cargo.toml b/examples/function-call/Cargo.toml new file mode 100644 index 00000000..c1ff6094 --- /dev/null +++ b/examples/function-call/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "function-call" +version = "0.1.0" +edition = "2021" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } +tracing-subscriber = { version = "0.3.19", features = ["env-filter"]} diff --git a/examples/function-call/README.md b/examples/function-call/README.md new file mode 100644 index 00000000..9b86dd8d --- /dev/null +++ b/examples/function-call/README.md @@ -0,0 +1,5 @@ +### Output + +> Response: +> +> 0: Role: assistant Content: Some("The current weather in Boston is sunny and windy with a temperature of 72 degrees Fahrenheit.") diff --git a/examples/function-call/src/main.rs b/examples/function-call/src/main.rs new file mode 100644 index 00000000..d85aa3a8 --- /dev/null +++ b/examples/function-call/src/main.rs @@ -0,0 +1,118 @@ +use async_openai::{ + types::{ + ChatCompletionFunctionsArgs, ChatCompletionRequestFunctionMessageArgs, + ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs, + }, + Client, +}; +use serde_json::json; +use std::collections::HashMap; +use std::error::Error; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + // This should come from env var outside the program + std::env::set_var("RUST_LOG", "warn"); + + // Setup tracing subscriber so that library can log the rate limited message + tracing_subscriber::registry() + .with(fmt::layer()) + .with(EnvFilter::from_default_env()) + .init(); + + let client = Client::new(); + + let model = "gpt-4o-mini"; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model(model) + .messages([ChatCompletionRequestUserMessageArgs::default() + .content("What's the weather like in Boston?") + .build()? + .into()]) + .functions([ChatCompletionFunctionsArgs::default() + .name("get_current_weather") + .description("Get the current weather in a given location") + .parameters(json!({ + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }, + }, + "required": ["location"], + })) + .build()?]) + .function_call("auto") + .build()?; + + let response_message = client + .chat() + .create(request) + .await? + .choices + .first() + .unwrap() + .message + .clone(); + + if let Some(function_call) = response_message.function_call { + let mut available_functions: HashMap<&str, fn(&str, &str) -> serde_json::Value> = + HashMap::new(); + available_functions.insert("get_current_weather", get_current_weather); + let function_name = function_call.name; + let function_args: serde_json::Value = function_call.arguments.parse().unwrap(); + + let location = function_args["location"].as_str().unwrap(); + let unit = "fahrenheit"; + let function = available_functions.get(function_name.as_str()).unwrap(); + let function_response = function(location, unit); + + let message = vec![ + ChatCompletionRequestUserMessageArgs::default() + .content("What's the weather like in Boston?") + .build()? + .into(), + ChatCompletionRequestFunctionMessageArgs::default() + .content(function_response.to_string()) + .name(function_name) + .build()? + .into(), + ]; + + println!("{}", serde_json::to_string(&message).unwrap()); + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model(model) + .messages(message) + .build()?; + + let response = client.chat().create(request).await?; + + println!("\nResponse:\n"); + for choice in response.choices { + println!( + "{}: Role: {} Content: {:?}", + choice.index, choice.message.role, choice.message.content + ); + } + } + + Ok(()) +} + +fn get_current_weather(location: &str, unit: &str) -> serde_json::Value { + let weather_info = json!({ + "location": location, + "temperature": "72", + "unit": unit, + "forecast": ["sunny", "windy"] + }); + + weather_info +} diff --git a/examples/gemini-openai-compatibility/Cargo.toml b/examples/gemini-openai-compatibility/Cargo.toml new file mode 100644 index 00000000..fefe9f7f --- /dev/null +++ b/examples/gemini-openai-compatibility/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "gemini-openai-compatibility" +version = "0.1.0" +edition = "2021" +rust-version.workspace = true + +[dependencies] +async-openai = {path = "../../async-openai", features = ["byot"]} +tokio = { version = "1.43.0", features = ["full"] } +tracing-subscriber = { version = "0.3.19", features = ["env-filter"]} +dotenv = "0.15.0" +futures = "0.3.31" +serde_json = "1.0.100" +serde = { version = "1.0", features = ["derive"] } +base64 = "0.22.1" diff --git a/examples/gemini-openai-compatibility/README.md b/examples/gemini-openai-compatibility/README.md new file mode 100644 index 00000000..5a168707 --- /dev/null +++ b/examples/gemini-openai-compatibility/README.md @@ -0,0 +1,65 @@ +# Gemini's OpenAI Compatibility Example + +This example demonstrates how to use OpenAI's `async_openai` Rust library with Google's Gemini API. By modifying a few lines of configuration, you can integrate Gemini models while maintaining OpenAI compatibility. + +## Features +- **List Available Models**: Fetch a list of supported Gemini models. +- **Retrieve Model Details**: Get detailed information about the `gemini-1.5-flash` model. +- **Chat Completion**: Perform chat completions using Gemini's API. +- **Stream Chat Messages**: Receive streaming responses for chat queries in real-time. +- **Generate Images**: Leverage Gemini's image generation capabilities. +- **Understand Images**: Analyze and extract information from images. +- **Understand Audio**: Process and interpret audio inputs. +- **Structured Output Response**: Generate structured outputs for complex queries. +- **Function Calling**: Invoke functions dynamically based on input prompts. +- **Create Embeddings**: Generate embeddings for text or other data types. +- **Bring Your Own Type (BYOT)**: Use custom Gemini response types defined in `gemini_type.rs`. + +## Prerequisites +- Rust installed (`rustc` and `cargo`) +- Set up your Google Gemini API key from [Google AI Studio](https://aistudio.google.com/) +- Create a `.env` file with: + ```plaintext + GEMINI_API_KEY=your_api_key_here + ``` +- Install dependencies: + ```sh + cargo add async-openai dotenv futures tokio + ``` + +## Enabling BYOT Feature +To enable the BYOT (Bring Your Own Type) feature in `async-openai`, modify your `Cargo.toml` as follows: +```toml +async-openai = {version = '{{version}}', features = ["byot"]} +``` + +## Usage +This example now uses the `byot` (Bring Your Own Type) feature to define custom types for Gemini responses. The Gemini types are defined in `gemini_type.rs`, and methods using these types have the `_byot` suffix. + +### Running the Example +To run the example: +```sh +cargo run +``` +This will: +1. List available models +2. Retrieve details of `gemini-1.5-flash` +3. Generate chat completion responses +4. Stream chat messages +5. Generate an image +6. Understanding an image +7. Understanding an audio +8. Structured output response +9. Function calling +10. Create Embeddings + +## Data +Sample Image obtained from Unsplash - https://unsplash.com/photos/an-elderly-couple-walks-through-a-park-Mpf6IQpiq3A + +Sample Audio extracted from "How to Stop Holding Yourself Back | Simon Sinek" obtained from https://www.youtube.com/watch?v=W05FYkqv7hM + + + +## References +- [Google Gemini's OpenAI compatibility](https://ai.google.dev/gemini-api/docs/openai) + diff --git a/examples/gemini-openai-compatibility/sample_data/How to Stop Holding Yourself Back Simon Sinek.mp3 b/examples/gemini-openai-compatibility/sample_data/How to Stop Holding Yourself Back Simon Sinek.mp3 new file mode 100644 index 00000000..fec418b8 Binary files /dev/null and b/examples/gemini-openai-compatibility/sample_data/How to Stop Holding Yourself Back Simon Sinek.mp3 differ diff --git a/examples/gemini-openai-compatibility/sample_data/gavin-allanwood-Mpf6IQpiq3A-unsplash.jpg b/examples/gemini-openai-compatibility/sample_data/gavin-allanwood-Mpf6IQpiq3A-unsplash.jpg new file mode 100644 index 00000000..22033dee Binary files /dev/null and b/examples/gemini-openai-compatibility/sample_data/gavin-allanwood-Mpf6IQpiq3A-unsplash.jpg differ diff --git a/examples/gemini-openai-compatibility/src/gemini_types.rs b/examples/gemini-openai-compatibility/src/gemini_types.rs new file mode 100644 index 00000000..6a245432 --- /dev/null +++ b/examples/gemini-openai-compatibility/src/gemini_types.rs @@ -0,0 +1,84 @@ +use std::pin::Pin; + +/// Gemini types (Generally user defined types) for Gemini API +use async_openai::{ + error::OpenAIError, + types::{ChatChoice, ChatChoiceStream, CompletionUsage, Image}, +}; +use futures::Stream; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct GeminiModel { + pub id: String, + pub object: String, + pub owned_by: String, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct ListGeminiModelResponse { + pub data: Vec, + pub object: String, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +/// Represents a streamed chunk of a chat completion response returned by model, based on the provided input. +pub struct GeminiCreateChatCompletionStreamResponse { + /// A list of chat completion choices. Can contain more than one elements if `n` is greater than 1. Can also be empty for the last chunk if you set `stream_options: {"include_usage": true}`. + pub choices: Vec, + + /// The Unix timestamp (in seconds) of when the chat completion was created. Each chunk has the same timestamp. + pub created: u32, + /// The model to generate the completion. + pub model: String, + + /// The object type, which is always `chat.completion.chunk`. + pub object: String, + + /// An optional field that will only be present when you set `stream_options: {"include_usage": true}` in your request. + /// When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request. + pub usage: Option, +} + +/// A stream of chat completion responses. +pub type GeminiChatCompletionResponseStream = Pin< + Box> + Send>, +>; + +/// Represents a chat completion response returned by model, based on the provided input. +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct GeminiCreateChatCompletionResponse { + /// A list of chat completion choices. Can be more than one if `n` is greater than 1. + pub choices: Vec, + /// The Unix timestamp (in seconds) of when the chat completion was created. + pub created: u32, + /// The model used for the chat completion. + pub model: String, + /// The object type, which is always `chat.completion`. + pub object: String, + /// usage statistics for the entire request. + pub usage: Option, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct GeminiImagesResponse { + pub data: Vec>, +} + +/// Represents an embedding vector returned by embedding endpoint. +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct GeminiEmbedding { + /// The object type, which is always "embedding". + pub object: String, + /// The embedding vector, which is a list of floats. The length of vector + pub embedding: Vec, +} + +#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] +pub struct GeminiCreateEmbeddingResponse { + pub object: String, + /// The name of the model used to generate the embedding. + pub model: String, + /// The list of embeddings generated by the model. + pub data: Vec, +} diff --git a/examples/gemini-openai-compatibility/src/main.rs b/examples/gemini-openai-compatibility/src/main.rs new file mode 100644 index 00000000..1bac59fa --- /dev/null +++ b/examples/gemini-openai-compatibility/src/main.rs @@ -0,0 +1,382 @@ +use async_openai::{ + config::OpenAIConfig, + types::{ + ChatCompletionRequestMessage, ChatCompletionRequestUserMessage, + ChatCompletionRequestUserMessageContentPart, CreateChatCompletionRequestArgs, + CreateEmbeddingRequestArgs, CreateImageRequestArgs, Image, ImageModel, ImageResponseFormat, + InputAudio, ResponseFormat, ResponseFormatJsonSchema, + }, + Client, +}; +use base64::Engine; +use dotenv::dotenv; +use futures::StreamExt; +use gemini_types::{ + GeminiChatCompletionResponseStream, GeminiCreateChatCompletionResponse, + GeminiCreateEmbeddingResponse, GeminiImagesResponse, GeminiModel, ListGeminiModelResponse, +}; +use serde_json::json; +use std::error::Error; +use std::fs; +mod gemini_types; + +/// Initializes the OpenAI client with Gemini API compatibility +fn get_gemini_client() -> Client { + let base_url = "https://generativelanguage.googleapis.com/v1beta/openai"; + let api_key = std::env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set"); + let config = OpenAIConfig::new() + .with_api_base(base_url) + .with_api_key(api_key); + Client::with_config(config) +} + +/// Lists available models from the Gemini API +async fn list_models() -> Result<(), Box> { + let client = get_gemini_client(); + let models: ListGeminiModelResponse = client.models().list_byot().await?; + + println!("Available Models:"); + for model in models.data { + println!("ID: {}", model.id); + println!("Object: {}", model.object); + println!("Owned By: {}", model.owned_by); + println!(); + } + Ok(()) +} + +/// Retrieves details of a specific model +async fn retrieve_model(model_id: &str) -> Result<(), Box> { + let client = get_gemini_client(); + let model: GeminiModel = client.models().retrieve_byot(model_id).await?; + + println!("Model: {:?}", model); + Ok(()) +} + +/// Streams a chat response using Gemini API +async fn stream_chat() -> Result<(), Box> { + let client = get_gemini_client(); + let request = CreateChatCompletionRequestArgs::default() + //Usage of gemini model + .model("gemini-2.0-flash") + .messages(vec![ChatCompletionRequestMessage::User( + ChatCompletionRequestUserMessage { + content: async_openai::types::ChatCompletionRequestUserMessageContent::Text( + "What is the meaning of life?".to_string(), + ), + ..Default::default() + }, + )]) + .n(1) + .stream(true) + .max_tokens(500_u32) + .build()?; + + let mut stream: GeminiChatCompletionResponseStream = + client.chat().create_stream_byot(request).await?; + + while let Some(response) = stream.next().await { + match response { + Ok(ccr) => ccr.choices.iter().for_each(|c| { + print!("{}", c.delta.content.clone().unwrap()); + }), + Err(e) => eprintln!("{}", e), + } + } + + Ok(()) +} + +async fn chat_completion() -> Result<(), Box> { + let client = get_gemini_client(); + let request = CreateChatCompletionRequestArgs::default() + //Usage of gemini model + .model("gemini-2.0-flash") + .messages([ChatCompletionRequestMessage::User( + "How old is the human civilization?".into(), + )]) + // .max_tokens(40_u32) + .build()?; + + let response: GeminiCreateChatCompletionResponse = client.chat().create_byot(request).await?; + + // if let Ok(response) = response { + println!("\nResponse (single):\n"); + for choice in response.choices { + println!("{}", choice.message.content.unwrap()); + } + Ok(()) +} + +async fn function_call() -> Result<(), Box> { + let client = get_gemini_client(); + + let response: serde_json::Value = client + .chat() + .create_byot(serde_json::json!({ + + "model": "gemini-2.0-flash", + "messages": [ + { + "role": "user", + "content": "What'\''s the weather like in Chicago today?" + } + ], + "tools": [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. Chicago, IL" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"] + } + }, + "required": ["location"] + } + } + } + ], + "tool_choice": "auto" + + })) + .await?; + + println!("\nResponse (function call):\n"); + println!("{}", response); + Ok(()) +} + +async fn image_understanding() -> Result<(), Box> { + // ref: https://unsplash.com/photos/an-elderly-couple-walks-through-a-park-Mpf6IQpiq3A + let image_file = "./sample_data/gavin-allanwood-Mpf6IQpiq3A-unsplash.jpg"; + let image_data = fs::read(image_file)?; + let image_base64 = base64::engine::general_purpose::STANDARD.encode(image_data); + let client = get_gemini_client(); + + let request = CreateChatCompletionRequestArgs::default() + .model("gemini-2.0-flash") + .messages([ + ChatCompletionRequestMessage::User("What do you see in this image?".into()), + ChatCompletionRequestMessage::User(ChatCompletionRequestUserMessage { + content: async_openai::types::ChatCompletionRequestUserMessageContent::Array(vec![ + ChatCompletionRequestUserMessageContentPart::ImageUrl( + async_openai::types::ChatCompletionRequestMessageContentPartImage { + image_url: ("data:image/jpg;base64,".to_string() + &image_base64) + .into(), + }, + ), + ]), + ..Default::default() + }), + ]) + .build()?; + + let response: GeminiCreateChatCompletionResponse = client.chat().create_byot(request).await?; + + println!("\nResponse (image understanding):\n"); + for choice in response.choices { + println!("{}", choice.message.content.unwrap()); + } + Ok(()) +} + +/// Generates an image based on a text prompt +async fn generate_image(prompt: &str) -> Result<(), Box> { + let client = get_gemini_client(); + + let request = CreateImageRequestArgs::default() + .prompt(prompt) + //Usage of gemini model + .model(ImageModel::Other("imagen-3.0-generate-002".to_string())) + .n(1) + .response_format(ImageResponseFormat::B64Json) + .build()?; + + let response: GeminiImagesResponse = client.images().create_byot(request).await?; + + let images = response.data; + + println!("\nResponse (image):\n"); + for image in images { + if let Image::B64Json { + b64_json, + revised_prompt: _, + } = &*image + { + println!("Image b64_json: {}", b64_json); + } else if let Image::Url { + url, + revised_prompt: _, + } = &*image + { + println!("Image URL: {}", url); + } + } + + Ok(()) +} + +async fn audio_understanding() -> Result<(), Box> { + let client = get_gemini_client(); + + // Credits and Source for audio: https://www.youtube.com/watch?v=W05FYkqv7hM + let audio_file = "./sample_data/How to Stop Holding Yourself Back Simon Sinek.mp3"; + let audio_data = fs::read(audio_file)?; + let audio_base64 = base64::engine::general_purpose::STANDARD.encode(audio_data); + + let request = CreateChatCompletionRequestArgs::default() + .model("gemini-2.0-flash") + .messages([ + ChatCompletionRequestMessage::User("Transcribe this audio file.".into()), + ChatCompletionRequestMessage::User(ChatCompletionRequestUserMessage { + content: async_openai::types::ChatCompletionRequestUserMessageContent::Array(vec![ + ChatCompletionRequestUserMessageContentPart::InputAudio( + async_openai::types::ChatCompletionRequestMessageContentPartAudio { + input_audio: InputAudio { + data: audio_base64, + format: async_openai::types::InputAudioFormat::Mp3, + }, + }, + ), + ]), + ..Default::default() + }), + ]) + .build()?; + + let response: GeminiCreateChatCompletionResponse = client.chat().create_byot(request).await?; + + println!("\nResponse (audio understanding):\n"); + + for choice in response.choices { + println!("{}", choice.message.content.unwrap()); + } + + Ok(()) +} + +async fn structured_output() -> Result<(), Box> { + let client = get_gemini_client(); + + let schema = json!({ + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": false + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": false + }); + + let request = CreateChatCompletionRequestArgs::default() + .model("gemini-2.0-flash") + .messages([ChatCompletionRequestMessage::User( + ChatCompletionRequestUserMessage { + content: async_openai::types::ChatCompletionRequestUserMessageContent::Text( + "How can I solve 8x + 7 = -23?".to_string(), + ), + ..Default::default() + }, + )]) + .response_format(ResponseFormat::JsonSchema { + json_schema: ResponseFormatJsonSchema { + schema: Some(schema), + description: None, + name: "math_reasoning".into(), + strict: Some(true), + }, + }) + .build()?; + + let response: GeminiCreateChatCompletionResponse = client.chat().create_byot(request).await?; + + println!("\nResponse (structured output):\n"); + for choice in response.choices { + println!("{}", choice.message.content.unwrap()); + } + + Ok(()) +} + +async fn create_embeddings() -> Result<(), Box> { + let client = get_gemini_client(); + + let request = CreateEmbeddingRequestArgs::default() + .model("text-embedding-004") + .input("The food was delicious and the waiter...") + .build()?; + + let response: GeminiCreateEmbeddingResponse = client.embeddings().create_byot(request).await?; + + println!("\nResponse (embedding):\n"); + for embedding in response.data { + println!("Embedding: {:?}", embedding.embedding); + } + + Ok(()) +} + +#[tokio::main] +async fn main() { + dotenv().ok(); // Load environment variables + + if let Err(e) = list_models().await { + eprintln!("Error: {}", e); + } + + if let Err(e) = retrieve_model("gemini-2.0-flash").await { + eprintln!("Error: {}", e); + } + if let Err(e) = chat_completion().await { + eprintln!("Error: {}", e); + } + + if let Err(e) = function_call().await { + eprintln!("Error: {}", e); + } + + if let Err(e) = stream_chat().await { + eprintln!("Error: {}", e); + } + + if let Err(e) = image_understanding().await { + eprintln!("Error: {}", e); + } + + if let Err(e) = generate_image("a futuristic city at night").await { + eprintln!("Error: {}", e); + } + + if let Err(e) = audio_understanding().await { + eprintln!("Error: {}", e); + } + + if let Err(e) = structured_output().await { + eprintln!("Error: {}", e); + } + + if let Err(e) = create_embeddings().await { + eprintln!("Error: {}", e); + } +} diff --git a/examples/in-memory-file/Cargo.toml b/examples/in-memory-file/Cargo.toml new file mode 100644 index 00000000..1d5c4458 --- /dev/null +++ b/examples/in-memory-file/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "in-memory-file" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +tokio = { version = "1.43.0", features = ["full"] } +bytes = "1.9.0" diff --git a/examples/in-memory-file/README.md b/examples/in-memory-file/README.md new file mode 100644 index 00000000..3c0c8927 --- /dev/null +++ b/examples/in-memory-file/README.md @@ -0,0 +1,3 @@ +### Output + +> Hello, I'm David Attenborough. I'm speaking to you from my home because, like many of you, I've spent much of the last year indoors, away from friends, family, and access to the natural world. It's been a challenging few months for many of us, but the reaction to these extraordinary times has proved that when we work together, there is no limit to what we can accomplish. Today, we are experiencing environmental change as never before. And the need to take action has never been more urgent. This year, the world will gather in Glasgow for the United Nations Climate Change Conference. It's a crucial moment in our history. This could be a year for positive change for ourselves, for our planet, and for the wonderful creatures with which we share it. A year the world could remember proudly and say, we made a difference. As we make our New Year's resolutions, let's think about what each of us could do. What positive changes could we make in our own lives? So here's to a brighter year ahead. Let's make 2021 a happy New Year for all the inhabitants of our perfect planet. diff --git a/examples/in-memory-file/audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3 b/examples/in-memory-file/audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3 new file mode 100644 index 00000000..42f2cb40 Binary files /dev/null and b/examples/in-memory-file/audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3 differ diff --git a/examples/in-memory-file/src/main.rs b/examples/in-memory-file/src/main.rs new file mode 100644 index 00000000..ec769aad --- /dev/null +++ b/examples/in-memory-file/src/main.rs @@ -0,0 +1,29 @@ +use async_openai::types::AudioInput; +use async_openai::{types::CreateTranscriptionRequestArgs, Client}; +use std::error::Error; +use std::fs; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let filename = + "A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3".to_string(); + let file_contents = fs::read(format!("./audio/{}", filename))?; + + let bytes = bytes::Bytes::from(file_contents); + + // To pass in in-memory files, you can pass either bytes::Bytes or vec[u8] to AudioInputs, FileInputs, and ImageInputs. + let audio_input = AudioInput::from_bytes(filename, bytes); + + let client = Client::new(); + // Credits and Source for audio: https://www.youtube.com/watch?v=oQnDVqGIv4s + let request = CreateTranscriptionRequestArgs::default() + .file(audio_input) + .model("whisper-1") + .build()?; + + let response = client.audio().transcribe(request).await?; + + println!("{}", response.text); + + Ok(()) +} diff --git a/examples/models/Cargo.toml b/examples/models/Cargo.toml index 931a4210..bc38c36f 100644 --- a/examples/models/Cargo.toml +++ b/examples/models/Cargo.toml @@ -6,4 +6,4 @@ publish = false [dependencies] async-openai = {path = "../../async-openai"} -tokio = {version = "1.22.0", features = ["full"]} +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/models/src/main.rs b/examples/models/src/main.rs index 91b34185..abb4501e 100644 --- a/examples/models/src/main.rs +++ b/examples/models/src/main.rs @@ -1,13 +1,17 @@ -use async_openai as openai; -use openai::{Client, Models}; +use std::error::Error; + +use async_openai::Client; #[tokio::main] -async fn main() { +async fn main() -> Result<(), Box> { let client = Client::new(); - let response = Models::list(&client).await; - println!("Models::list:\n {:#?}", response); + let model_list = client.models().list().await?; + + println!("List of models:\n {:#?}", model_list.data); + + let model = client.models().retrieve("gpt-3.5-turbo-instruct").await?; + println!("gpt-3.5-turbo-instruct model id: {}", model.id); - let response = Models::retrieve(&client, "text-davinci-003").await; - println!("Retrieved text-davinci-003: {response:#?}"); + Ok(()) } diff --git a/examples/moderations/Cargo.toml b/examples/moderations/Cargo.toml index 079519a5..12cf62eb 100644 --- a/examples/moderations/Cargo.toml +++ b/examples/moderations/Cargo.toml @@ -5,5 +5,5 @@ edition = "2021" publish = false [dependencies] -async-openai = {path = "../../async-openai"} -tokio = {version = "1.22.0", features = ["full"]} +async-openai = { path = "../../async-openai" } +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/moderations/src/main.rs b/examples/moderations/src/main.rs index af0c5ab4..3e37916c 100644 --- a/examples/moderations/src/main.rs +++ b/examples/moderations/src/main.rs @@ -1,35 +1,29 @@ +use async_openai::{types::CreateModerationRequestArgs, Client}; use std::error::Error; -use async_openai as openai; -use openai::{ - types::{CreateModerationRequest, Input, TextModerationModel}, - Client, Moderation, -}; - #[tokio::main] async fn main() -> Result<(), Box> { let client = Client::new(); + let model = "omni-moderation-latest"; + // single - let request = CreateModerationRequest { - input: Input::Single("Lions want to kill".to_string()), - model: Some(TextModerationModel::Latest), - }; + let request = CreateModerationRequestArgs::default() + .input("Lions want to kill") + .model(model) + .build()?; - let response = Moderation::create(&client, request).await?; + let response = client.moderations().create(request).await?; println!("Response (single): {response:#?}"); // multiple - let request = CreateModerationRequest { - input: Input::Array(vec![ - "Lions want to kill".to_string(), - "I hate them".to_string(), - ]), - model: Some(TextModerationModel::Latest), - }; - - let response = Moderation::create(&client, request).await?; + let request = CreateModerationRequestArgs::default() + .input(["Lions want to kill", "I hate them"]) + .model(model) + .build()?; + + let response = client.moderations().create(request).await?; println!("Response (multiple): {response:#?}"); diff --git a/examples/ollama-chat/.gitignore b/examples/ollama-chat/.gitignore new file mode 100644 index 00000000..2bab6cb2 --- /dev/null +++ b/examples/ollama-chat/.gitignore @@ -0,0 +1 @@ +volumes/* \ No newline at end of file diff --git a/examples/ollama-chat/Cargo.toml b/examples/ollama-chat/Cargo.toml new file mode 100644 index 00000000..cbdd7cc2 --- /dev/null +++ b/examples/ollama-chat/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ollama-chat" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/ollama-chat/README.md b/examples/ollama-chat/README.md new file mode 100644 index 00000000..763b9770 --- /dev/null +++ b/examples/ollama-chat/README.md @@ -0,0 +1,38 @@ +## Setup + +A docker compose file is provided to run a dockerized version of Ollama and download a default model. You will need the Ollama container to be up and running _before_ you can run the Rust example code. + +You can check the container status with `docker ps` or check the container's logs with `docker container logs {CONTAINER NAME} -f`. E.g. `docker container logs ollama -f`. + +## Running the Example + +```sh +# Bring ollama up with model and wait for it to be healthy. +docker compose up -d + +# Once model is downloaded and Ollama is up, run the Rust code. +cargo run +``` + +## Docker Notes + +- Since Ollama requires you to pull a model before first use, a custom entrypoint script is used. See [Stack Overflow discussion](https://stackoverflow.com/a/78501628). + - The model will be cached in the volumes dir. + - Depending on your network connection, the healthcheck may need to be adjusted to allow more time for the model to download. +- [llama3.2:1b](https://ollama.com/library/llama3.2:1b) is used in the example as it is a smaller model and will download more quickly compared to larger models. + - A larger model will provide better responses, but be slower to download. + - Also, using the default CPU inference, smaller models will have better tokens / second performance. +- The GPU mapping is written but commented out. This means it will default to CPU inference which is slower, but should run without any additional setup. + - If you have a GPU and the proper container support, feel free to uncomment / adapt. + +## Ollama OpenAI Compatibility + +**NOTE: an api key parameter is used for compatibility with OpenAI's API spec, but it is ignored by Ollama (it can be any value).** + +See the [Ollama OpenAI Compatibility docs](https://github.com/ollama/ollama/blob/main/docs/openai.md) for more details on what Ollama supports. + +## Response + +> Response: +> +> 0: Role: assistant Content: Some("The 2020 World Series was played at Globe Life Field in Arlington, Texas, as part of Major League Baseball's (MLB) move to play its season without spectators due to the COVID-19 pandemic. The Dodgers defeated the Tampa Bay Rays in 6 games.") diff --git a/examples/ollama-chat/docker-compose.yml b/examples/ollama-chat/docker-compose.yml new file mode 100644 index 00000000..8381d8fe --- /dev/null +++ b/examples/ollama-chat/docker-compose.yml @@ -0,0 +1,27 @@ +services: + ollama: + container_name: ollama + image: ollama/ollama:0.5.12 + entrypoint: ["/usr/bin/bash", "/ollama_entrypoint.sh"] + environment: + MODEL: "llama3.2:1b" + volumes: + - ./volumes/ollama:/root/.ollama + - ./ollama_entrypoint.sh:/ollama_entrypoint.sh + restart: unless-stopped + ports: + - "11434:11434" + healthcheck: + test: ["CMD", "bash", "-c", "ollama list | grep -q llama3.2:1b"] + interval: 15s + retries: 30 + start_period: 5s + timeout: 5s + # Uncomment if you have NVIDIA container toolkit, CUDA, etc. + # deploy: + # resources: + # reservations: + # devices: + # - capabilities: [gpu] + # driver: nvidia + # count: all diff --git a/examples/ollama-chat/ollama_entrypoint.sh b/examples/ollama-chat/ollama_entrypoint.sh new file mode 100755 index 00000000..a1a5cfdb --- /dev/null +++ b/examples/ollama-chat/ollama_entrypoint.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +# Start Ollama in the background. +/bin/ollama serve & +# Record Process ID. +pid=$! + +# Pause for Ollama to start. +sleep 5 + +echo "Retrieving model $MODEL..." +ollama pull $MODEL +echo "Done!" + +# Wait for Ollama process to finish. +wait $pid diff --git a/examples/ollama-chat/src/main.rs b/examples/ollama-chat/src/main.rs new file mode 100644 index 00000000..831fddac --- /dev/null +++ b/examples/ollama-chat/src/main.rs @@ -0,0 +1,65 @@ +use std::error::Error; + +use async_openai::{ + config::OpenAIConfig, + types::{ + ChatCompletionRequestAssistantMessageArgs, ChatCompletionRequestSystemMessageArgs, + ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs, + }, + Client, +}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + // This is the default host:port for Ollama's OpenAI endpoint. + // Should match the config in docker-compose.yml. + let api_base = "http://localhost:11434/v1"; + // Required but ignored + let api_key = "ollama"; + + let client = Client::with_config( + OpenAIConfig::new() + .with_api_key(api_key) + .with_api_base(api_base), + ); + + // This should match whatever model is downloaded in Ollama docker container. + let model = "llama3.2:1b"; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model(model) + .messages([ + ChatCompletionRequestSystemMessageArgs::default() + .content("You are a helpful assistant.") + .build()? + .into(), + ChatCompletionRequestUserMessageArgs::default() + .content("Who won the world series in 2020?") + .build()? + .into(), + ChatCompletionRequestAssistantMessageArgs::default() + .content("The Los Angeles Dodgers won the World Series in 2020.") + .build()? + .into(), + ChatCompletionRequestUserMessageArgs::default() + .content("Where was it played?") + .build()? + .into(), + ]) + .build()?; + + println!("{}", serde_json::to_string(&request).unwrap()); + + let response = client.chat().create(request).await?; + + println!("\nResponse:\n"); + for choice in response.choices { + println!( + "{}: Role: {} Content: {:?}", + choice.index, choice.message.role, choice.message.content + ); + } + + Ok(()) +} diff --git a/examples/rate-limit-completions/Cargo.toml b/examples/rate-limit-completions/Cargo.toml deleted file mode 100644 index eef3b862..00000000 --- a/examples/rate-limit-completions/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "rate-limit-completions" -version = "0.1.0" -edition = "2021" -publish = false - -[dependencies] -async-openai = {path = "../../async-openai"} -backoff = { version = "0.4.0", features = ["tokio"] } -tokio = {version = "1.22.0", features = ["full"]} diff --git a/examples/rate-limit-completions/src/main.rs b/examples/rate-limit-completions/src/main.rs deleted file mode 100644 index a77c99d9..00000000 --- a/examples/rate-limit-completions/src/main.rs +++ /dev/null @@ -1,38 +0,0 @@ -use async_openai as openai; -use openai::{error::OpenAIError, types::CreateCompletionRequest, Client, Completion}; - -async fn joke(client: &Client) -> Result { - let request = CreateCompletionRequest { - model: "text-davinci-003".to_owned(), - prompt: Some("Tell me a joke".to_owned()), - max_tokens: Some(30), - ..Default::default() - }; - - let response = Completion::create(&client, request).await?; - - Ok(response.choices.first().unwrap().text.to_string()) -} - -#[tokio::main] -async fn main() { - let backoff = backoff::ExponentialBackoffBuilder::new() - .with_max_elapsed_time(Some(std::time::Duration::from_secs(60))) - .build(); - - let client = Client::new().with_backoff(backoff); - let mut count = 100; - - // Make back to back requests in a loop to trigger rate limits - // which will be retried by exponential backoff - while count > 0 { - match joke(&client).await { - Ok(joke) => println!("{joke}"), - Err(e) => { - eprintln!("{e}"); - break; - } - } - count -= 1; - } -} diff --git a/examples/realtime/Cargo.toml b/examples/realtime/Cargo.toml new file mode 100644 index 00000000..e97882a6 --- /dev/null +++ b/examples/realtime/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "realtime" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = { path = "../../async-openai", features = ["realtime"] } +futures-channel = "0.3.31" +futures-util = { version = "0.3.31", features = ["sink", "std"] } +serde = { version = "1.0.217", features = ["derive"] } +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = [ + "io-std", + "io-util", + "macros", + "rt-multi-thread", +] } +tokio-tungstenite = { version = "0.26.1", features = ["connect", "native-tls"] } diff --git a/examples/realtime/README.md b/examples/realtime/README.md new file mode 100644 index 00000000..89670385 --- /dev/null +++ b/examples/realtime/README.md @@ -0,0 +1,138 @@ +## Overview + +The example takes input from stdin, for every input two client events are sent: +1. "conversation.item.create" with content of type "input_text" +2. "response.create" + +All the output happens on stderr, so conversation can be continued on stdin. To stop type "quit" and press enter. + +Code is based on https://github.com/snapview/tokio-tungstenite/blob/master/examples/client.rs + +## Sample Output + +``` +WebSocket handshake complete +session.created | +age of sun? +conversation.item.created | +response.created | +response.output_item.added | +conversation.item.created | +response.content_part.added | +response.audio_transcript.delta | The +response.audio_transcript.delta | Sun +response.audio_transcript.delta | is +response.audio_transcript.delta | about +response.audio_transcript.delta | +response.audio_transcript.delta | 4 +response.audio_transcript.delta | . +response.audio.delta | +response.audio.delta | +response.audio_transcript.delta | 6 +response.audio.delta | +response.audio_transcript.delta | billion +response.audio.delta | +response.audio.delta | +response.audio_transcript.delta | years +response.audio_transcript.delta | old +response.audio_transcript.delta | . +response.audio_transcript.delta | It +response.audio.delta | +response.audio.delta | +response.audio_transcript.delta | formed +response.audio.delta | +response.audio_transcript.delta | from +response.audio_transcript.delta | the +response.audio_transcript.delta | gravitational +response.audio_transcript.delta | collapse +response.audio_transcript.delta | of +response.audio_transcript.delta | a +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio_transcript.delta | region +response.audio.delta | +response.audio_transcript.delta | within +response.audio_transcript.delta | a +response.audio_transcript.delta | large +response.audio_transcript.delta | molecular +response.audio_transcript.delta | cloud +response.audio_transcript.delta | . +response.audio_transcript.delta | It +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio_transcript.delta | 's +response.audio.delta | +response.audio_transcript.delta | currently +response.audio_transcript.delta | in +response.audio_transcript.delta | the +response.audio_transcript.delta | middle +response.audio_transcript.delta | of +response.audio_transcript.delta | its +response.audio_transcript.delta | life +response.audio_transcript.delta | cycle +response.audio_transcript.delta | , +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio_transcript.delta | expected +response.audio.delta | +response.audio_transcript.delta | to +response.audio_transcript.delta | last +response.audio_transcript.delta | for +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio_transcript.delta | another +response.audio.delta | +response.audio_transcript.delta | +response.audio_transcript.delta | 5 +response.audio_transcript.delta | billion +response.audio_transcript.delta | years +response.audio_transcript.delta | or +response.audio_transcript.delta | so +response.audio_transcript.delta | . +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.delta | +response.audio.done | +response.audio_transcript.done | +response.content_part.done | +response.output_item.done | [Some(Assistant)]: The Sun is about 4.6 billion years old. It formed from the gravitational collapse of a region within a large molecular cloud. It's currently in the middle of its life cycle, expected to last for another 5 billion years or so. + +response.done | +rate_limits.updated | +quit +``` diff --git a/examples/realtime/src/main.rs b/examples/realtime/src/main.rs new file mode 100644 index 00000000..141fefa3 --- /dev/null +++ b/examples/realtime/src/main.rs @@ -0,0 +1,148 @@ +use std::process::exit; + +use async_openai::types::realtime::{ + ConversationItemCreateEvent, Item, ResponseCreateEvent, ServerEvent, +}; +use futures_util::{future, pin_mut, StreamExt}; + +use tokio::io::AsyncReadExt; +use tokio_tungstenite::{ + connect_async, + tungstenite::{client::IntoClientRequest, protocol::Message}, +}; + +#[tokio::main] +async fn main() { + let url = "wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-12-17"; + let api_key = std::env::var("OPENAI_API_KEY").expect("Please provide OPENAPI_API_KEY env var"); + + let (stdin_tx, stdin_rx) = futures_channel::mpsc::unbounded(); + tokio::spawn(read_stdin(stdin_tx)); + + // create request from url and add required headers + let mut request = url.into_client_request().unwrap(); + request.headers_mut().insert( + "Authorization", + format!("Bearer {api_key}").parse().unwrap(), + ); + request + .headers_mut() + .insert("OpenAI-Beta", "realtime=v1".parse().unwrap()); + + // connect to WebSocket endpoint + let (ws_stream, _) = connect_async(request).await.expect("Failed to connect"); + + // output everything to stderr, for rest of the program stdin is used to send items of type "input_text" + eprintln!("WebSocket handshake complete"); + + let (write, read) = ws_stream.split(); + + let stdin_to_ws = stdin_rx.map(Ok).forward(write); + + let ws_to_stdout = { + read.for_each(|message| async { + let message = message.unwrap(); + + match message { + Message::Text(_) => { + let data = message.clone().into_data(); + let server_event: Result = + serde_json::from_slice(&data); + match server_event { + Ok(server_event) => { + let value = serde_json::to_value(&server_event).unwrap(); + let event_type = value["type"].clone(); + + eprint!("{:32} | ", event_type.as_str().unwrap()); + + match server_event { + ServerEvent::ResponseOutputItemDone(event) => { + event.item.content.unwrap_or(vec![]).iter().for_each( + |content| { + if let Some(ref transcript) = content.transcript { + eprintln!( + "[{:?}]: {}", + event.item.role, + transcript.trim(), + ); + } + }, + ); + } + ServerEvent::ResponseAudioTranscriptDelta(event) => { + eprint!("{}", event.delta.trim()); + } + ServerEvent::Error(e) => { + eprint!("{e:?}"); + } + _ => {} + } + } + Err(error) => { + eprintln!("failed to deserialize: {error:?}"); + eprintln!("{message:?}"); + } + } + } + Message::Binary(_) => eprintln!("Binary"), + Message::Frame(_) => eprintln!("Frame"), + Message::Ping(_) => eprintln!("Ping"), + Message::Pong(_) => eprintln!("Pong"), + Message::Close(_) => { + eprintln!("Close"); + exit(0); + } + } + + // after every message add newline + eprint!("\n"); + }) + }; + + pin_mut!(stdin_to_ws, ws_to_stdout); + future::select(stdin_to_ws, ws_to_stdout).await; +} + +// Read from stdin and send "conversation.item.create" and "response.create" client events. +// type "quit" to stop +async fn read_stdin(tx: futures_channel::mpsc::UnboundedSender) { + let mut stdin = tokio::io::stdin(); + loop { + let mut buf = vec![0; 1024]; + let n = match stdin.read(&mut buf).await { + Err(_) | Ok(0) => break, + Ok(n) => n, + }; + buf.truncate(n); + + let text = String::from_utf8_lossy(&buf).into_owned(); + + if text.trim() == "quit" { + tx.close_channel(); + return; + } + + // Create item from json representation + let item = Item::try_from(serde_json::json!({ + "type": "message", + "role": "user", + "content": [ + { + "type": "input_text", + "text": String::from_utf8_lossy(&buf).into_owned() + } + ] + })) + .unwrap(); + + // Create event of type "conversation.item.create" + let event: ConversationItemCreateEvent = item.into(); + // Create WebSocket message from client event + let message: Message = event.into(); + // send WebSocket message containing event of type "conversation.item.create" to server + tx.unbounded_send(message).unwrap(); + // send WebSocket message containing event of type "response.create" to server + tx.unbounded_send(ResponseCreateEvent::default().into()) + .unwrap(); + } +} diff --git a/examples/responses-function-call/Cargo.toml b/examples/responses-function-call/Cargo.toml new file mode 100644 index 00000000..b576a1f2 --- /dev/null +++ b/examples/responses-function-call/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "responses-function-call" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } +serde = { version = "1.0.219", features = ["derive"] } diff --git a/examples/responses-function-call/src/main.rs b/examples/responses-function-call/src/main.rs new file mode 100644 index 00000000..3e2083e8 --- /dev/null +++ b/examples/responses-function-call/src/main.rs @@ -0,0 +1,123 @@ +use async_openai::{ + types::responses::{ + CreateResponseArgs, FunctionArgs, FunctionCall, Input, InputItem, InputMessageArgs, + OutputContent, Role, ToolDefinition, + }, + Client, +}; +use serde::Deserialize; +use std::error::Error; + +#[derive(Debug, Deserialize)] +struct WeatherFunctionArgs { + location: String, + units: String, +} + +fn check_weather(location: String, units: String) -> String { + format!("The weather in {location} is 25 {units}") +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let tools = vec![ToolDefinition::Function( + FunctionArgs::default() + .name("get_weather") + .description("Retrieves current weather for the given location") + .parameters(serde_json::json!( + { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "City and country e.g. Bogotá, Colombia" + }, + "units": { + "type": "string", + "enum": [ + "celsius", + "fahrenheit" + ], + "description": "Units the temperature will be returned in." + } + }, + "required": [ + "location", + "units" + ], + "additionalProperties": false + } + )) + .build()?, + )]; + + let mut input_messages = vec![InputItem::Message( + InputMessageArgs::default() + .role(Role::User) + .content("What's the weather like in Paris today?") + .build()?, + )]; + + let request = CreateResponseArgs::default() + .max_output_tokens(512u32) + .model("gpt-4.1") + .input(Input::Items(input_messages.clone())) + .tools(tools.clone()) + .build()?; + + println!("{}", serde_json::to_string(&request).unwrap()); + + let response = client.responses().create(request).await?; + + // the model might ask for us to do a function call + let function_call_request: Option = + response.output.into_iter().find_map(|output_content| { + if let OutputContent::FunctionCall(inner) = output_content { + Some(inner) + } else { + None + } + }); + + let Some(function_call_request) = function_call_request else { + println!("No function_call request found"); + return Ok(()); + }; + + let function_result = match function_call_request.name.as_str() { + "get_weather" => { + let args: WeatherFunctionArgs = serde_json::from_str(&function_call_request.arguments)?; + check_weather(args.location, args.units) + } + _ => { + println!("Unknown function {}", function_call_request.name); + return Ok(()); + } + }; + + input_messages.push(InputItem::Custom(serde_json::to_value( + &OutputContent::FunctionCall(function_call_request.clone()), + )?)); + input_messages.push(InputItem::Custom(serde_json::json!({ + "type": "function_call_output", + "call_id": function_call_request.call_id, + "output": function_result, + }))); + + let request = CreateResponseArgs::default() + .max_output_tokens(512u32) + .model("gpt-4.1") + .input(Input::Items(input_messages)) + .tools(tools) + .build()?; + + println!("request 2 {}", serde_json::to_string(&request).unwrap()); + + let response = client.responses().create(request).await?; + + println!("{}", serde_json::to_string(&response).unwrap()); + + Ok(()) +} diff --git a/examples/responses-stream/Cargo.toml b/examples/responses-stream/Cargo.toml new file mode 100644 index 00000000..82eb90a7 --- /dev/null +++ b/examples/responses-stream/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "responses-stream" +version = "0.1.0" +edition = "2024" + +[dependencies] +async-openai = { path = "../../async-openai" } +tokio = { version = "1.0", features = ["full"] } +futures = "0.3" +serde_json = "1.0" diff --git a/examples/responses-stream/src/main.rs b/examples/responses-stream/src/main.rs new file mode 100644 index 00000000..5b565cd8 --- /dev/null +++ b/examples/responses-stream/src/main.rs @@ -0,0 +1,51 @@ +use async_openai::{ + Client, + types::responses::{ + CreateResponseArgs, Input, InputContent, InputItem, InputMessageArgs, ResponseEvent, Role, + }, +}; +use futures::StreamExt; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let request = CreateResponseArgs::default() + .model("gpt-4.1") + .stream(true) + .input(Input::Items(vec![InputItem::Message( + InputMessageArgs::default() + .role(Role::User) + .content(InputContent::TextInput( + "Write a haiku about programming.".to_string(), + )) + .build()?, + )])) + .build()?; + + let mut stream = client.responses().create_stream(request).await?; + + while let Some(result) = stream.next().await { + match result { + Ok(response_event) => match &response_event { + ResponseEvent::ResponseOutputTextDelta(delta) => { + print!("{}", delta.delta); + } + ResponseEvent::ResponseCompleted(_) + | ResponseEvent::ResponseIncomplete(_) + | ResponseEvent::ResponseFailed(_) => { + break; + } + _ => { println!("{response_event:#?}"); } + }, + Err(e) => { + eprintln!("{e:#?}"); + // When a stream ends, it returns Err(OpenAIError::StreamError("Stream ended")) + // Without this, the stream will never end + break; + } + } + } + + Ok(()) +} diff --git a/examples/responses/Cargo.toml b/examples/responses/Cargo.toml new file mode 100644 index 00000000..5de7c1e4 --- /dev/null +++ b/examples/responses/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "responses" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/responses/src/main.rs b/examples/responses/src/main.rs new file mode 100644 index 00000000..158bfe5b --- /dev/null +++ b/examples/responses/src/main.rs @@ -0,0 +1,46 @@ +use std::error::Error; + +use async_openai::{ + types::responses::{ + AllowedTools, CreateResponseArgs, Input, InputItem, InputMessageArgs, McpArgs, + RequireApproval, RequireApprovalPolicy, Role, + ToolDefinition::{Mcp, WebSearchPreview}, + WebSearchPreviewArgs, + }, + Client, +}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let request = CreateResponseArgs::default() + .max_output_tokens(512u32) + .model("gpt-4.1") + .input(Input::Items(vec![InputItem::Message( + InputMessageArgs::default() + .role(Role::User) + .content("What transport protocols does the 2025-03-26 version of the MCP spec (modelcontextprotocol/modelcontextprotocol) support?") + .build()?, + )])) + .tools(vec![ + WebSearchPreview(WebSearchPreviewArgs::default().build()?), + Mcp(McpArgs::default() + .server_label("deepwiki") + .server_url("https://mcp.deepwiki.com/mcp") + .require_approval(RequireApproval::Policy(RequireApprovalPolicy::Never)) + .allowed_tools(AllowedTools::List(vec!["ask_question".to_string()])) + .build()?), + ]) + .build()?; + + println!("{}", serde_json::to_string(&request).unwrap()); + + let response = client.responses().create(request).await?; + + for output in response.output { + println!("\nOutput: {:?}\n", output); + } + + Ok(()) +} diff --git a/examples/structured-outputs-schemars/Cargo.toml b/examples/structured-outputs-schemars/Cargo.toml new file mode 100644 index 00000000..029298c1 --- /dev/null +++ b/examples/structured-outputs-schemars/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "structured-outputs-schemars" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.127" +tokio = { version = "1.39.3", features = ["full"] } +schemars = "0.8.21" +serde = "1.0.130" diff --git a/examples/structured-outputs-schemars/README.md b/examples/structured-outputs-schemars/README.md new file mode 100644 index 00000000..735ca2b9 --- /dev/null +++ b/examples/structured-outputs-schemars/README.md @@ -0,0 +1,39 @@ +## Intro + +Based on the 'Chain of thought' example from https://platform.openai.com/docs/guides/structured-outputs/introduction?lang=curl + +Using `schemars` and `serde` reduces coding effort. + +## Output + +``` +cargo run | jq . +``` + +``` +{ + "final_answer": "x = -3.75", + "steps": [ + { + "explanation": "Start with the equation given in the problem.", + "output": "8x + 7 = -23" + }, + { + "explanation": "Subtract 7 from both sides to begin isolating the term with the variable x.", + "output": "8x + 7 - 7 = -23 - 7" + }, + { + "explanation": "Simplify both sides. On the left-hand side, 7 - 7 equals 0, cancelling out, leaving the equation as follows.", + "output": "8x = -30" + }, + { + "explanation": "Now, divide both sides by 8 to fully isolate x.", + "output": "8x/8 = -30/8" + }, + { + "explanation": "Simplify the right side by performing the division. -30 divided by 8 is -3.75.", + "output": "x = -3.75" + } + ] +} +``` diff --git a/examples/structured-outputs-schemars/src/main.rs b/examples/structured-outputs-schemars/src/main.rs new file mode 100644 index 00000000..f1e77db4 --- /dev/null +++ b/examples/structured-outputs-schemars/src/main.rs @@ -0,0 +1,97 @@ +use std::error::Error; + +use async_openai::{ + types::{ + ChatCompletionRequestMessage, ChatCompletionRequestSystemMessage, + ChatCompletionRequestUserMessage, CreateChatCompletionRequestArgs, ResponseFormat, + ResponseFormatJsonSchema, + }, + Client, +}; +use schemars::{schema_for, JsonSchema}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct Step { + pub output: String, + pub explanation: String, +} + +#[derive(Debug, Serialize, Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct MathReasoningResponse { + pub final_answer: String, + pub steps: Vec, +} + +pub async fn structured_output( + messages: Vec, +) -> Result, Box> { + let schema = schema_for!(T); + let schema_value = serde_json::to_value(&schema)?; + let response_format = ResponseFormat::JsonSchema { + json_schema: ResponseFormatJsonSchema { + description: None, + name: "math_reasoning".into(), + schema: Some(schema_value), + strict: Some(true), + }, + }; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-4o-mini") + .messages(messages) + .response_format(response_format) + .build()?; + + let client = Client::new(); + let response = client.chat().create(request).await?; + + for choice in response.choices { + if let Some(content) = choice.message.content { + return Ok(Some(serde_json::from_str::(&content)?)); + } + } + + Ok(None) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + // Expecting output schema + // let schema = json!({ + // "type": "object", + // "properties": { + // "steps": { + // "type": "array", + // "items": { + // "type": "object", + // "properties": { + // "explanation": { "type": "string" }, + // "output": { "type": "string" } + // }, + // "required": ["explanation", "output"], + // "additionalProperties": false + // } + // }, + // "final_answer": { "type": "string" } + // }, + // "required": ["steps", "final_answer"], + // "additionalProperties": false + // }); + if let Some(response) = structured_output::(vec![ + ChatCompletionRequestSystemMessage::from( + "You are a helpful math tutor. Guide the user through the solution step by step.", + ) + .into(), + ChatCompletionRequestUserMessage::from("how can I solve 8x + 7 = -23").into(), + ]) + .await? + { + println!("{}", serde_json::to_string(&response).unwrap()); + } + + Ok(()) +} diff --git a/examples/structured-outputs/Cargo.toml b/examples/structured-outputs/Cargo.toml new file mode 100644 index 00000000..0005568b --- /dev/null +++ b/examples/structured-outputs/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "structured-outputs" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = {path = "../../async-openai"} +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/structured-outputs/README.md b/examples/structured-outputs/README.md new file mode 100644 index 00000000..f445d4a9 --- /dev/null +++ b/examples/structured-outputs/README.md @@ -0,0 +1,37 @@ +## Intro + +Based on the 'Chain of thought' example from https://platform.openai.com/docs/guides/structured-outputs/introduction?lang=curl + +## Output + +``` +cargo run | jq . +``` + +``` +{ + "final_answer": "x = -3.75", + "steps": [ + { + "explanation": "Start with the equation given in the problem.", + "output": "8x + 7 = -23" + }, + { + "explanation": "Subtract 7 from both sides to begin isolating the term with the variable x.", + "output": "8x + 7 - 7 = -23 - 7" + }, + { + "explanation": "Simplify both sides. On the left-hand side, 7 - 7 equals 0, cancelling out, leaving the equation as follows.", + "output": "8x = -30" + }, + { + "explanation": "Now, divide both sides by 8 to fully isolate x.", + "output": "8x/8 = -30/8" + }, + { + "explanation": "Simplify the right side by performing the division. -30 divided by 8 is -3.75.", + "output": "x = -3.75" + } + ] +} +``` diff --git a/examples/structured-outputs/src/main.rs b/examples/structured-outputs/src/main.rs new file mode 100644 index 00000000..3948308d --- /dev/null +++ b/examples/structured-outputs/src/main.rs @@ -0,0 +1,68 @@ +use std::error::Error; + +use async_openai::{ + types::{ + ChatCompletionRequestSystemMessage, ChatCompletionRequestUserMessage, + CreateChatCompletionRequestArgs, ResponseFormat, ResponseFormatJsonSchema, + }, + Client, +}; +use serde_json::json; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let schema = json!({ + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": false + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": false + }); + + let response_format = ResponseFormat::JsonSchema { + json_schema: ResponseFormatJsonSchema { + description: None, + name: "math_reasoning".into(), + schema: Some(schema), + strict: Some(true), + }, + }; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-4o-2024-08-06") + .messages([ + ChatCompletionRequestSystemMessage::from( + "You are a helpful math tutor. Guide the user through the solution step by step.", + ) + .into(), + ChatCompletionRequestUserMessage::from("how can I solve 8x + 7 = -23").into(), + ]) + .response_format(response_format) + .build()?; + + let response = client.chat().create(request).await?; + + for choice in response.choices { + if let Some(content) = choice.message.content { + print!("{content}") + } + } + + Ok(()) +} diff --git a/examples/tool-call-stream/Cargo.toml b/examples/tool-call-stream/Cargo.toml new file mode 100644 index 00000000..6d68ba36 --- /dev/null +++ b/examples/tool-call-stream/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "tool-call-stream" +version = "0.1.0" +edition = "2021" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-openai = {path = "../../async-openai"} +rand = "0.8.5" +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } +futures = "0.3.31" diff --git a/examples/tool-call-stream/src/main.rs b/examples/tool-call-stream/src/main.rs new file mode 100644 index 00000000..230ee9a3 --- /dev/null +++ b/examples/tool-call-stream/src/main.rs @@ -0,0 +1,253 @@ +use std::collections::HashMap; +use std::error::Error; +use std::io::{stdout, Write}; +use std::sync::Arc; + +use async_openai::types::{ + ChatCompletionMessageToolCall, ChatCompletionRequestAssistantMessageArgs, + ChatCompletionRequestMessage, ChatCompletionRequestToolMessageArgs, + ChatCompletionRequestUserMessageArgs, ChatCompletionToolArgs, ChatCompletionToolType, + FinishReason, FunctionCall, FunctionObjectArgs, +}; +use async_openai::{types::CreateChatCompletionRequestArgs, Client}; +use futures::StreamExt; +use rand::seq::SliceRandom; +use rand::{thread_rng, Rng}; +use serde_json::{json, Value}; +use tokio::sync::Mutex; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + let user_prompt = "What's the weather like in Boston and Atlanta?"; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-4-1106-preview") + .messages([ChatCompletionRequestUserMessageArgs::default() + .content(user_prompt) + .build()? + .into()]) + .tools(vec![ChatCompletionToolArgs::default() + .r#type(ChatCompletionToolType::Function) + .function( + FunctionObjectArgs::default() + .name("get_current_weather") + .description("Get the current weather in a given location") + .parameters(json!({ + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }, + }, + "required": ["location"], + })) + .build()?, + ) + .build()?]) + .build()?; + + let mut stream = client.chat().create_stream(request).await?; + + let tool_call_states: Arc>> = + Arc::new(Mutex::new(HashMap::new())); + + while let Some(result) = stream.next().await { + match result { + Ok(response) => { + for chat_choice in response.choices { + let function_responses: Arc< + Mutex>, + > = Arc::new(Mutex::new(Vec::new())); + if let Some(tool_calls) = chat_choice.delta.tool_calls { + for tool_call_chunk in tool_calls.into_iter() { + let key = (chat_choice.index, tool_call_chunk.index); + let states = tool_call_states.clone(); + let tool_call_data = tool_call_chunk.clone(); + + let mut states_lock = states.lock().await; + let state = states_lock.entry(key).or_insert_with(|| { + ChatCompletionMessageToolCall { + id: tool_call_data.id.clone().unwrap_or_default(), + r#type: ChatCompletionToolType::Function, + function: FunctionCall { + name: tool_call_data + .function + .as_ref() + .and_then(|f| f.name.clone()) + .unwrap_or_default(), + arguments: tool_call_data + .function + .as_ref() + .and_then(|f| f.arguments.clone()) + .unwrap_or_default(), + }, + } + }); + if let Some(arguments) = tool_call_chunk + .function + .as_ref() + .and_then(|f| f.arguments.as_ref()) + { + state.function.arguments.push_str(arguments); + } + } + } + if let Some(finish_reason) = &chat_choice.finish_reason { + if matches!(finish_reason, FinishReason::ToolCalls) { + let tool_call_states_clone = tool_call_states.clone(); + + let tool_calls_to_process = { + let states_lock = tool_call_states_clone.lock().await; + states_lock + .iter() + .map(|(_key, tool_call)| { + let name = tool_call.function.name.clone(); + let args = tool_call.function.arguments.clone(); + let tool_call_clone = tool_call.clone(); + (name, args, tool_call_clone) + }) + .collect::>() + }; + + let mut handles = Vec::new(); + for (name, args, tool_call_clone) in tool_calls_to_process { + let response_content_clone = function_responses.clone(); + let handle = tokio::spawn(async move { + let response_content = call_fn(&name, &args).await.unwrap(); + let mut function_responses_lock = + response_content_clone.lock().await; + function_responses_lock + .push((tool_call_clone, response_content)); + }); + handles.push(handle); + } + + for handle in handles { + handle.await.unwrap(); + } + + let function_responses_clone = function_responses.clone(); + let function_responses_lock = function_responses_clone.lock().await; + let mut messages: Vec = + vec![ChatCompletionRequestUserMessageArgs::default() + .content(user_prompt) + .build()? + .into()]; + + let tool_calls: Vec = + function_responses_lock + .iter() + .map(|tc| tc.0.clone()) + .collect(); + + let assistant_messages: ChatCompletionRequestMessage = + ChatCompletionRequestAssistantMessageArgs::default() + .tool_calls(tool_calls) + .build() + .map_err(|e| Box::new(e) as Box) + .unwrap() + .into(); + + let tool_messages: Vec = + function_responses_lock + .iter() + .map(|tc| { + ChatCompletionRequestToolMessageArgs::default() + .content(tc.1.to_string()) + .tool_call_id(tc.0.id.clone()) + .build() + .map_err(|e| Box::new(e) as Box) + .unwrap() + .into() + }) + .collect(); + + messages.push(assistant_messages); + messages.extend(tool_messages); + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-4-1106-preview") + .messages(messages) + .build() + .map_err(|e| Box::new(e) as Box)?; + + let mut stream = client.chat().create_stream(request).await?; + + let mut response_content = String::new(); + let mut lock = stdout().lock(); + while let Some(result) = stream.next().await { + match result { + Ok(response) => { + for chat_choice in response.choices.iter() { + if let Some(ref content) = chat_choice.delta.content { + write!(lock, "{}", content).unwrap(); + response_content.push_str(content); + } + } + } + Err(err) => { + return Err(Box::new(err) as Box); + } + } + } + } + } + + if let Some(content) = &chat_choice.delta.content { + let mut lock = stdout().lock(); + write!(lock, "{}", content).unwrap(); + } + } + } + Err(err) => { + let mut lock = stdout().lock(); + writeln!(lock, "error: {err}").unwrap(); + } + } + stdout() + .flush() + .map_err(|e| Box::new(e) as Box)?; + } + + Ok(()) +} + +async fn call_fn(name: &str, args: &str) -> Result> { + let mut available_functions: HashMap<&str, fn(&str, &str) -> serde_json::Value> = + HashMap::new(); + available_functions.insert("get_current_weather", get_current_weather); + + let function_args: serde_json::Value = args.parse().unwrap(); + + let location = function_args["location"].as_str().unwrap(); + let unit = function_args["unit"].as_str().unwrap_or("fahrenheit"); + let function = available_functions.get(name).unwrap(); + let function_response = function(location, unit); + Ok(function_response) +} + +fn get_current_weather(location: &str, unit: &str) -> serde_json::Value { + let mut rng = thread_rng(); + + let temperature: i32 = rng.gen_range(20..=55); + + let forecasts = [ + "sunny", "cloudy", "overcast", "rainy", "windy", "foggy", "snowy", + ]; + + let forecast = forecasts.choose(&mut rng).unwrap_or(&"sunny"); + + let weather_info = json!({ + "location": location, + "temperature": temperature.to_string(), + "unit": unit, + "forecast": forecast + }); + + weather_info +} diff --git a/examples/tool-call/Cargo.toml b/examples/tool-call/Cargo.toml new file mode 100644 index 00000000..e6a2dc63 --- /dev/null +++ b/examples/tool-call/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "tool-call" +version = "0.1.0" +edition = "2021" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-openai = {path = "../../async-openai"} +rand = "0.8.5" +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } +futures = "0.3.31" diff --git a/examples/tool-call/src/main.rs b/examples/tool-call/src/main.rs new file mode 100644 index 00000000..c88fa2fa --- /dev/null +++ b/examples/tool-call/src/main.rs @@ -0,0 +1,176 @@ +use std::collections::HashMap; +use std::io::{stdout, Write}; + +use async_openai::types::{ + ChatCompletionMessageToolCall, ChatCompletionRequestAssistantMessageArgs, + ChatCompletionRequestMessage, ChatCompletionRequestToolMessageArgs, + ChatCompletionRequestUserMessageArgs, ChatCompletionToolArgs, ChatCompletionToolType, + FunctionObjectArgs, +}; +use async_openai::{types::CreateChatCompletionRequestArgs, Client}; +use futures::StreamExt; +use rand::seq::SliceRandom; +use rand::{thread_rng, Rng}; +use serde_json::{json, Value}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + let user_prompt = "What's the weather like in Boston and Atlanta?"; + + let request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-4-1106-preview") + .messages([ChatCompletionRequestUserMessageArgs::default() + .content(user_prompt) + .build()? + .into()]) + .tools(vec![ChatCompletionToolArgs::default() + .r#type(ChatCompletionToolType::Function) + .function( + FunctionObjectArgs::default() + .name("get_current_weather") + .description("Get the current weather in a given location") + .parameters(json!({ + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }, + }, + "required": ["location"], + })) + .build()?, + ) + .build()?]) + .build()?; + + let response_message = client + .chat() + .create(request) + .await? + .choices + .first() + .unwrap() + .message + .clone(); + + if let Some(tool_calls) = response_message.tool_calls { + let mut handles = Vec::new(); + for tool_call in tool_calls { + let name = tool_call.function.name.clone(); + let args = tool_call.function.arguments.clone(); + let tool_call_clone = tool_call.clone(); + + let handle = + tokio::spawn(async move { call_fn(&name, &args).await.unwrap_or_default() }); + handles.push((handle, tool_call_clone)); + } + + let mut function_responses = Vec::new(); + + for (handle, tool_call_clone) in handles { + if let Ok(response_content) = handle.await { + function_responses.push((tool_call_clone, response_content)); + } + } + + let mut messages: Vec = + vec![ChatCompletionRequestUserMessageArgs::default() + .content(user_prompt) + .build()? + .into()]; + + let tool_calls: Vec = function_responses + .iter() + .map(|(tool_call, _response_content)| tool_call.clone()) + .collect(); + + let assistant_messages: ChatCompletionRequestMessage = + ChatCompletionRequestAssistantMessageArgs::default() + .tool_calls(tool_calls) + .build()? + .into(); + + let tool_messages: Vec = function_responses + .iter() + .map(|(tool_call, response_content)| { + ChatCompletionRequestToolMessageArgs::default() + .content(response_content.to_string()) + .tool_call_id(tool_call.id.clone()) + .build() + .unwrap() + .into() + }) + .collect(); + + messages.push(assistant_messages); + messages.extend(tool_messages); + + let subsequent_request = CreateChatCompletionRequestArgs::default() + .max_tokens(512u32) + .model("gpt-4-1106-preview") + .messages(messages) + .build() + .map_err(|e| Box::new(e) as Box)?; + + let mut stream = client.chat().create_stream(subsequent_request).await?; + + let mut response_content = String::new(); + let mut lock = stdout().lock(); + while let Some(result) = stream.next().await { + match result { + Ok(response) => { + for chat_choice in response.choices.iter() { + if let Some(ref content) = chat_choice.delta.content { + write!(lock, "{}", content).unwrap(); + response_content.push_str(content); + } + } + } + Err(err) => { + return Err(Box::new(err) as Box); + } + } + } + } + + Ok(()) +} + +async fn call_fn(name: &str, args: &str) -> Result> { + let mut available_functions: HashMap<&str, fn(&str, &str) -> serde_json::Value> = + HashMap::new(); + available_functions.insert("get_current_weather", get_current_weather); + + let function_args: serde_json::Value = args.parse().unwrap(); + + let location = function_args["location"].as_str().unwrap(); + let unit = function_args["unit"].as_str().unwrap_or("fahrenheit"); + let function = available_functions.get(name).unwrap(); + let function_response = function(location, unit); + Ok(function_response) +} + +fn get_current_weather(location: &str, unit: &str) -> serde_json::Value { + let mut rng = thread_rng(); + + let temperature: i32 = rng.gen_range(20..=55); + + let forecasts = [ + "sunny", "cloudy", "overcast", "rainy", "windy", "foggy", "snowy", + ]; + + let forecast = forecasts.choose(&mut rng).unwrap_or(&"sunny"); + + let weather_info = json!({ + "location": location, + "temperature": temperature.to_string(), + "unit": unit, + "forecast": forecast + }); + + weather_info +} diff --git a/examples/vector-store-retrieval/Cargo.toml b/examples/vector-store-retrieval/Cargo.toml new file mode 100644 index 00000000..a4b8bb22 --- /dev/null +++ b/examples/vector-store-retrieval/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "vector-store-retrieval" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +async-openai = { path = "../../async-openai" } +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/vector-store-retrieval/README.md b/examples/vector-store-retrieval/README.md new file mode 100644 index 00000000..2fcd2b8d --- /dev/null +++ b/examples/vector-store-retrieval/README.md @@ -0,0 +1,33 @@ +## Intro + +This example is based on https://platform.openai.com/docs/guides/retrieval + + +## Data + +Uber Annual Report obtained from https://investor.uber.com/financials/ + +Lyft Annual Report obtained from https://investor.lyft.com/financials-and-reports/annual-reports/default.aspx + + +## Output + +``` +Waiting for vector store to be[] ready... +Search results: VectorStoreSearchResultsPage { + object: "vector_store.search_results.page", + search_query: [ + "uber profit", + ], + data: [ + VectorStoreSearchResultItem { + file_id: "file-1XFoSYUzJudwJLkAazLdjd", + filename: "uber-10k.pdf", + score: 0.5618923, + attributes: {}, + content: [ + VectorStoreSearchResultContentObject { + type: "text", + text: "(In millions) Q1 2022 Q2 2022 Q3 2022 Q4 2022 Q1 2023 Q2 2023 Q3 2023 Q4 2023\n\nMobility $ 10,723 $ 13,364 $ 13,684 $ 14,894 $ 14,981 $ 16,728 $ 17,903 $ 19,285 \nDelivery 13,903 13,876 13,684 14,315 15,026 15,595 16,094 17,011 \nFreight 1,823 1,838 1,751 1,540 1,401 1,278 1,284 1,279 \n\nAdjusted EBITDA. +... +``` diff --git a/examples/vector-store-retrieval/input/lyft-10k.pdf b/examples/vector-store-retrieval/input/lyft-10k.pdf new file mode 100644 index 00000000..7e28d3c4 Binary files /dev/null and b/examples/vector-store-retrieval/input/lyft-10k.pdf differ diff --git a/examples/vector-store-retrieval/input/uber-10k.pdf b/examples/vector-store-retrieval/input/uber-10k.pdf new file mode 100644 index 00000000..8b2298b4 Binary files /dev/null and b/examples/vector-store-retrieval/input/uber-10k.pdf differ diff --git a/examples/vector-store-retrieval/src/main.rs b/examples/vector-store-retrieval/src/main.rs new file mode 100644 index 00000000..9867144f --- /dev/null +++ b/examples/vector-store-retrieval/src/main.rs @@ -0,0 +1,78 @@ +use std::error::Error; + +use async_openai::{ + types::{ + CreateFileRequest, CreateVectorStoreRequest, FilePurpose, VectorStoreSearchRequest, + VectorStoreStatus, + }, + Client, +}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + // + // Step 1: Upload files and add them to a Vector Store + // + + // upload files to add to vector store + let uber_file = client + .files() + .create(CreateFileRequest { + file: "./input/uber-10k.pdf".into(), + purpose: FilePurpose::Assistants, + }) + .await?; + + let lyft_file = client + .files() + .create(CreateFileRequest { + file: "./input/lyft-10k.pdf".into(), + purpose: FilePurpose::Assistants, + }) + .await?; + + // Create a vector store called "Financial Statements" + // add uploaded file to vector store + let mut vector_store = client + .vector_stores() + .create(CreateVectorStoreRequest { + name: Some("Financial Statements".into()), + file_ids: Some(vec![uber_file.id.clone(), lyft_file.id.clone()]), + ..Default::default() + }) + .await?; + + // + // Step 4: Wait for the vector store to be ready + // + while vector_store.status != VectorStoreStatus::Completed { + println!("Waiting for vector store to be ready..."); + tokio::time::sleep(std::time::Duration::from_secs(5)).await; + vector_store = client.vector_stores().retrieve(&vector_store.id).await?; + } + + // + // Step 5: Search the vector store + // + let results = client + .vector_stores() + .search( + &vector_store.id, + VectorStoreSearchRequest { + query: "uber profit".into(), + ..Default::default() + }, + ) + .await?; + + // Print the search results + println!("Search results: {:#?}", results); + // Cleanup to avoid costs + let _ = client.vector_stores().delete(&vector_store.id).await?; + + let _ = client.files().delete(&uber_file.id).await?; + + let _ = client.files().delete(&lyft_file.id).await?; + Ok(()) +} diff --git a/examples/vision-chat/Cargo.toml b/examples/vision-chat/Cargo.toml new file mode 100644 index 00000000..11a7cde5 --- /dev/null +++ b/examples/vision-chat/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "vision-chat" +version = "0.1.0" +edition = "2021" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-openai = { path = "../../async-openai" } +serde_json = "1.0.135" +tokio = { version = "1.43.0", features = ["full"] } diff --git a/examples/vision-chat/README.md b/examples/vision-chat/README.md new file mode 100644 index 00000000..fd211978 --- /dev/null +++ b/examples/vision-chat/README.md @@ -0,0 +1,5 @@ +### Output + +> Response: +> +> 0: Role: assistant Content: "This is an image of a wooden boardwalk trail extending through a lush green meadow or wetland area. The sky is partly cloudy with a rich blue color, and it seems to be a bright sunny day. This type of boardwalk is often constructed in natural areas to allow people to enjoy the scenery without disturbing the local flora and fauna. It provides a clear path through potentially marshy or sensitive ecosystems and can be found in nature reserves, parks, or conservation areas." diff --git a/examples/vision-chat/src/main.rs b/examples/vision-chat/src/main.rs new file mode 100644 index 00000000..a28538a4 --- /dev/null +++ b/examples/vision-chat/src/main.rs @@ -0,0 +1,57 @@ +use std::error::Error; + +use async_openai::{ + types::{ + ChatCompletionRequestMessageContentPartImageArgs, + ChatCompletionRequestMessageContentPartTextArgs, ChatCompletionRequestUserMessageArgs, + CreateChatCompletionRequestArgs, ImageDetail, ImageUrlArgs, + }, + Client, +}; + +/// https://platform.openai.com/docs/guides/vision - quickstart +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + let image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"; + + let request = CreateChatCompletionRequestArgs::default() + .model("gpt-4o-mini") + .max_tokens(300_u32) + .messages([ChatCompletionRequestUserMessageArgs::default() + .content(vec![ + ChatCompletionRequestMessageContentPartTextArgs::default() + .text("What is this image?") + .build()? + .into(), + ChatCompletionRequestMessageContentPartImageArgs::default() + .image_url( + ImageUrlArgs::default() + .url(image_url) + .detail(ImageDetail::High) + .build()?, + ) + .build()? + .into(), + ]) + .build()? + .into()]) + .build()?; + + println!("{}", serde_json::to_string(&request).unwrap()); + + let response = client.chat().create(request).await?; + + println!("\nResponse:\n"); + for choice in response.choices { + println!( + "{}: Role: {} Content: {:?}", + choice.index, + choice.message.role, + choice.message.content.unwrap_or_default() + ); + } + + Ok(()) +} diff --git a/openapi.yaml b/openapi.yaml index 2d37cf40..d5af4799 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -1,3200 +1,17525 @@ openapi: 3.0.0 info: - title: OpenAI API - description: APIs for sampling from and fine-tuning language models - version: '1.1.0' + title: OpenAI API + description: The OpenAI REST API. Please see https://platform.openai.com/docs/api-reference for more details. + version: "2.3.0" + termsOfService: https://openai.com/policies/terms-of-use + contact: + name: OpenAI Support + url: https://help.openai.com/ + license: + name: MIT + url: https://github.com/openai/openai-openapi/blob/master/LICENSE servers: - - url: https://api.openai.com/v1 + - url: https://api.openai.com/v1 tags: -- name: OpenAI - description: The OpenAI REST API + - name: Assistants + description: Build Assistants that can call models and use tools. + - name: Audio + description: Turn audio into text or text into audio. + - name: Chat + description: Given a list of messages comprising a conversation, the model will return a response. + - name: Completions + description: Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. + - name: Embeddings + description: Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. + - name: Fine-tuning + description: Manage fine-tuning jobs to tailor a model to your specific training data. + - name: Batch + description: Create large batches of API requests to run asynchronously. + - name: Files + description: Files are used to upload documents that can be used with features like Assistants and Fine-tuning. + - name: Uploads + description: Use Uploads to upload large files in multiple parts. + - name: Images + description: Given a prompt and/or an input image, the model will generate a new image. + - name: Models + description: List and describe the various models available in the API. + - name: Moderations + description: Given text and/or image inputs, classifies if those inputs are potentially harmful. + - name: Audit Logs + description: List user actions and configuration changes within this organization. paths: - /engines: - get: - operationId: listEngines - deprecated: true - tags: - - OpenAI - summary: Lists the currently available (non-finetuned) models, and provides basic information about each one such as the owner and availability. - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/ListEnginesResponse' - x-oaiMeta: - name: List engines - group: engines - path: list - examples: - curl: | - curl https://api.openai.com/v1/engines \ - -H 'Authorization: Bearer YOUR_API_KEY' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Engine.list() - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.listEngines(); - response: | - { - "data": [ - { - "id": "engine-id-0", - "object": "engine", - "owner": "organization-owner", - "ready": true - }, - { - "id": "engine-id-2", - "object": "engine", - "owner": "organization-owner", - "ready": true - }, - { - "id": "engine-id-3", - "object": "engine", - "owner": "openai", - "ready": false - }, - ], - "object": "list" - } - - /engines/{engine_id}: - get: - operationId: retrieveEngine - deprecated: true - tags: - - OpenAI - summary: Retrieves a model instance, providing basic information about it such as the owner and availability. - parameters: - - in: path - name: engine_id - required: true - schema: - type: string - # ideally this will be an actual ID, so this will always work from browser - example: - davinci - description: &engine_id_description > - The ID of the engine to use for this request - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/Engine' - x-oaiMeta: - name: Retrieve engine - group: engines - path: retrieve - examples: - curl: | - curl -u :YOUR_API_KEY https://api.openai.com/v1/engines/VAR_model_id - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Engine.retrieve("VAR_model_id") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.retrieveEngine("VAR_model_id"); - response: | - { - "id": "VAR_model_id", - "object": "engine", - "owner": "openai", - "ready": true - } - - /completions: - post: - operationId: createCompletion - tags: - - OpenAI - summary: Creates a completion for the provided prompt and parameters - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateCompletionRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/CreateCompletionResponse' - x-oaiMeta: - name: Create completion - group: completions - path: create - examples: - curl: | - curl https://api.openai.com/v1/completions \ - -H 'Content-Type: application/json' \ - -H 'Authorization: Bearer YOUR_API_KEY' \ - -d '{ - "model": "VAR_model_id", - "prompt": "Say this is a test", - "max_tokens": 6, - "temperature": 0 - }' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Completion.create( - model="VAR_model_id", - prompt="Say this is a test", - max_tokens=6, - temperature=0 - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createCompletion({ - model: "VAR_model_id", - prompt: "Say this is a test", - max_tokens: 6, - temperature: 0, - }); - parameters: | - { - "model": "VAR_model_id", - "prompt": "Say this is a test", - "max_tokens": 6, - "temperature": 0, - "top_p": 1, - "n": 1, - "stream": false, - "logprobs": null, - "stop": "\n" - } - response: | - { - "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7", - "object": "text_completion", - "created": 1589478378, - "model": "VAR_model_id", - "choices": [ - { - "text": "\n\nThis is a test", - "index": 0, - "logprobs": null, - "finish_reason": "length" - } - ], - "usage": { - "prompt_tokens": 5, - "completion_tokens": 6, - "total_tokens": 11 - } - } - - /edits: - post: - operationId: createEdit - tags: - - OpenAI - summary: Creates a new edit for the provided input, instruction, and parameters - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateEditRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/CreateEditResponse' - x-oaiMeta: - name: Create edit - group: edits - path: create - examples: - curl: | - curl https://api.openai.com/v1/edits \ - -H 'Content-Type: application/json' \ - -H 'Authorization: Bearer YOUR_API_KEY' \ - -d '{ - "model": "VAR_model_id", - "input": "What day of the wek is it?", - "instruction": "Fix the spelling mistakes" - }' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Edit.create( - model="VAR_model_id", - input="What day of the wek is it?", - instruction="Fix the spelling mistakes" - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createEdit({ - model: "VAR_model_id", - input: "What day of the wek is it?", - instruction: "Fix the spelling mistakes", - }); - parameters: | - { - "model": "VAR_model_id", - "input": "What day of the wek is it?", - "instruction": "Fix the spelling mistakes", - } - response: | - { - "object": "edit", - "created": 1589478378, - "choices": [ - { - "text": "What day of the week is it?", - "index": 0, - } - ], - "usage": { - "prompt_tokens": 25, - "completion_tokens": 32, - "total_tokens": 57 - } - } - - /images/generations: - post: - operationId: createImage - tags: - - OpenAI - summary: Creates an image given a prompt. - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateImageRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/ImagesResponse' - x-oaiMeta: - name: Create image - group: images - path: create - examples: - curl: | - curl https://api.openai.com/v1/images/generations \ - -H 'Content-Type: application/json' \ - -H 'Authorization: Bearer YOUR_API_KEY' \ - -d '{ - "prompt": "A cute baby sea otter", - "n": 2, - "size": "1024x1024" - }' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Image.create( - prompt="A cute baby sea otter", - n=2, - size="1024x1024" - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createImage({ - prompt: "A cute baby sea otter", - n: 2, - size: "1024x1024", - }); - parameters: | - { - "prompt": "A cute baby sea otter", - "n": 2, - "size": "1024x1024" - } - response: | - { - "created": 1589478378, - "data": [ - { - "url": "https://..." - }, - { - "url": "https://..." - } - ] - } - - /images/edits: - post: - operationId: createImageEdit - tags: - - OpenAI - summary: Creates an edited or extended image given an original image and a prompt. - requestBody: - required: true - content: - multipart/form-data: - schema: - $ref: '#/components/schemas/CreateImageEditRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/ImagesResponse' - x-oaiMeta: - name: Create image edit - group: images - path: create-edit - examples: - curl: | - curl https://api.openai.com/v1/images/edits \ - -H 'Authorization: Bearer YOUR_API_KEY' \ - -F image='@otter.png' \ - -F mask='@mask.png' \ - -F prompt="A cute baby sea otter wearing a beret" \ - -F n=2 \ - -F size="1024x1024" - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Image.create_edit( - image=open("otter.png"), - mask=open("mask.png"), - prompt="A cute baby sea otter wearing a beret", - n=2, - size="1024x1024" - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createImageEdit( - fs.createReadStream("otter.png"), - fs.createReadStream("mask.png"), - "A cute baby sea otter wearing a beret", - 2, - "1024x1024", - ); - response: | - { - "created": 1589478378, - "data": [ - { - "url": "https://..." - }, - { - "url": "https://..." - } - ] - } - - /images/variations: - post: - operationId: createImageVariation - tags: - - OpenAI - summary: Creates a variation of a given image. - requestBody: - required: true - content: - multipart/form-data: - schema: - $ref: '#/components/schemas/CreateImageVariationRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/ImagesResponse' - x-oaiMeta: - name: Create image variation - group: images - path: create-variation - examples: - curl: | - curl https://api.openai.com/v1/images/variations \ - -H 'Authorization: Bearer YOUR_API_KEY' \ - -F image='@otter.png' \ - -F n=2 \ - -F size="1024x1024" - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Image.create_variation( - image=open("otter.png"), - n=2, - size="1024x1024" - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createImageVariation( - fs.createReadStream("otter.png"), - 2, - "1024x1024", - ); - response: | - { - "created": 1589478378, - "data": [ - { - "url": "https://..." - }, - { - "url": "https://..." - } - ] - } - - /embeddings: - post: - operationId: createEmbedding - tags: - - OpenAI - summary: Creates an embedding vector representing the input text. - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateEmbeddingRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/CreateEmbeddingResponse' - x-oaiMeta: - name: Create embeddings - group: embeddings - path: create - examples: - curl: | - curl https://api.openai.com/v1/embeddings \ - -X POST \ - -H "Authorization: Bearer YOUR_API_KEY" \ - -H "Content-Type: application/json" \ - -d '{"input": "The food was delicious and the waiter...", - "model": "text-similarity-babbage-001"}' - - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Embedding.create( - model="text-similarity-babbage-001", - input="The food was delicious and the waiter..." - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createEmbedding({ - model: "text-similarity-babbage-001", - input: "The food was delicious and the waiter...", - }); - parameters: | - { - "model": "text-similarity-babbage-001", - "input": "The food was delicious and the waiter..." - } - response: | - { - "object": "list", - "data": [ - { - "object": "embedding", - "embedding": [ - 0.018990106880664825, - -0.0073809814639389515, - .... (1024 floats total for ada) - 0.021276434883475304, - ], - "index": 0 - } - ], - "usage": { - "prompt_tokens": 8, - "total_tokens": 8 - } - } - - /engines/{engine_id}/search: - post: - operationId: createSearch - deprecated: true - tags: - - OpenAI - summary: | - The search endpoint computes similarity scores between provided query and documents. Documents can be passed directly to the API if there are no more than 200 of them. - - To go beyond the 200 document limit, documents can be processed offline and then used for efficient retrieval at query time. When `file` is set, the search endpoint searches over all the documents in the given file and returns up to the `max_rerank` number of documents. These documents will be returned along with their search scores. - - The similarity score is a positive score that usually ranges from 0 to 300 (but can sometimes go higher), where a score above 200 usually means the document is semantically similar to the query. - parameters: - - in: path - name: engine_id - required: true - schema: - type: string - example: davinci - description: The ID of the engine to use for this request. You can select one of `ada`, `babbage`, `curie`, or `davinci`. - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateSearchRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/CreateSearchResponse' - x-oaiMeta: - name: Create search - group: searches - path: create - examples: - curl: | - curl https://api.openai.com/v1/engines/davinci/search \ - -H "Content-Type: application/json" \ - -H 'Authorization: Bearer YOUR_API_KEY' \ - -d '{ - "documents": ["White House", "hospital", "school"], - "query": "the president" - }' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Engine("davinci").search( - documents=["White House", "hospital", "school"], - query="the president" - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createSearch("davinci", { - documents: ["White House", "hospital", "school"], - query: "the president", - }); - parameters: | - { - "documents": [ - "White House", - "hospital", - "school" - ], - "query": "the president" - } - response: | - { - "data": [ - { - "document": 0, - "object": "search_result", - "score": 215.412 - }, - { - "document": 1, - "object": "search_result", - "score": 40.316 - }, - { - "document": 2, - "object": "search_result", - "score": 55.226 - } - ], - "object": "list" - } - - /files: - get: - operationId: listFiles - tags: - - OpenAI - summary: Returns a list of files that belong to the user's organization. - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/ListFilesResponse' - x-oaiMeta: - name: List files - group: files - path: list - examples: - curl: | - curl https://api.openai.com/v1/files \ - -H 'Authorization: Bearer YOUR_API_KEY' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.File.list() - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.listFiles(); - response: | - { - "data": [ - { - "id": "file-ccdDZrC3iZVNiQVeEA6Z66wf", - "object": "file", - "bytes": 175, - "created_at": 1613677385, - "filename": "train.jsonl", - "purpose": "search" - }, - { - "id": "file-XjGxS3KTG0uNmNOK362iJua3", - "object": "file", - "bytes": 140, - "created_at": 1613779121, - "filename": "puppy.jsonl", - "purpose": "search" - } - ], - "object": "list" - } - post: - operationId: createFile - tags: - - OpenAI - summary: | - Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit. - - requestBody: - required: true - content: - multipart/form-data: - schema: - $ref: '#/components/schemas/CreateFileRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/OpenAIFile' - x-oaiMeta: - name: Upload file - group: files - path: upload - examples: - curl: | - curl https://api.openai.com/v1/files \ - -H "Authorization: Bearer YOUR_API_KEY" \ - -F purpose="fine-tune" \ - -F file='@mydata.jsonl' - - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.File.create( - file=open("mydata.jsonl"), - purpose='fine-tune' - ) - node.js: | - const fs = require("fs"); - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createFile( - fs.createReadStream("mydata.jsonl"), - "fine-tune" - ); - response: | - { - "id": "file-XjGxS3KTG0uNmNOK362iJua3", - "object": "file", - "bytes": 140, - "created_at": 1613779121, - "filename": "mydata.jsonl", - "purpose": "fine-tune" - } - - /files/{file_id}: - delete: - operationId: deleteFile - tags: - - OpenAI - summary: Delete a file. - parameters: - - in: path - name: file_id - required: true - schema: - type: string - description: The ID of the file to use for this request - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/DeleteFileResponse' - x-oaiMeta: - name: Delete file - group: files - path: delete - examples: - curl: | - curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3 \ - -X DELETE \ - -H 'Authorization: Bearer YOUR_API_KEY' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.File.delete("file-XjGxS3KTG0uNmNOK362iJua3") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.deleteFile("file-XjGxS3KTG0uNmNOK362iJua3"); - response: | - { - "id": "file-XjGxS3KTG0uNmNOK362iJua3", - "object": "file", - "deleted": true - } - get: - operationId: retrieveFile - tags: - - OpenAI - summary: Returns information about a specific file. - parameters: - - in: path - name: file_id - required: true - schema: - type: string - description: The ID of the file to use for this request - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/OpenAIFile' - x-oaiMeta: - name: Retrieve file - group: files - path: retrieve - examples: - curl: | - curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3 \ - -H 'Authorization: Bearer YOUR_API_KEY' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.File.retrieve("file-XjGxS3KTG0uNmNOK362iJua3") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.retrieveFile("file-XjGxS3KTG0uNmNOK362iJua3"); - response: | - { - "id": "file-XjGxS3KTG0uNmNOK362iJua3", - "object": "file", - "bytes": 140, - "created_at": 1613779657, - "filename": "mydata.jsonl", - "purpose": "fine-tune" - } - - /files/{file_id}/content: - get: - operationId: downloadFile - tags: - - OpenAI - summary: Returns the contents of the specified file - parameters: - - in: path - name: file_id - required: true - schema: - type: string - description: The ID of the file to use for this request - responses: - "200": - description: OK - content: - application/json: - schema: - type: string - x-oaiMeta: - name: Retrieve file content - group: files - path: retrieve-content - examples: - curl: | - curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3/content \ - -H 'Authorization: Bearer YOUR_API_KEY' > file.jsonl - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - content = openai.File.download("file-XjGxS3KTG0uNmNOK362iJua3") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.downloadFile("file-XjGxS3KTG0uNmNOK362iJua3"); - - /answers: - post: - operationId: createAnswer - deprecated: true - tags: - - OpenAI - summary: | - Answers the specified question using the provided documents and examples. - - The endpoint first [searches](/docs/api-reference/searches) over provided documents or files to find relevant context. The relevant context is combined with the provided examples and question to create the prompt for [completion](/docs/api-reference/completions). - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateAnswerRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/CreateAnswerResponse' - x-oaiMeta: - name: Create answer - group: answers - path: create - examples: - curl: | - curl https://api.openai.com/v1/answers \ - -X POST \ - -H "Authorization: Bearer YOUR_API_KEY" \ - -H 'Content-Type: application/json' \ - -d '{ - "documents": ["Puppy A is happy.", "Puppy B is sad."], - "question": "which puppy is happy?", - "search_model": "ada", - "model": "curie", - "examples_context": "In 2017, U.S. life expectancy was 78.6 years.", - "examples": [["What is human life expectancy in the United States?","78 years."]], - "max_tokens": 5, - "stop": ["\n", "<|endoftext|>"] - }' - - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Answer.create( - search_model="ada", - model="curie", - question="which puppy is happy?", - documents=["Puppy A is happy.", "Puppy B is sad."], - examples_context="In 2017, U.S. life expectancy was 78.6 years.", - examples=[["What is human life expectancy in the United States?","78 years."]], - max_tokens=5, - stop=["\n", "<|endoftext|>"], - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createAnswer({ - search_model: "ada", - model: "curie", - question: "which puppy is happy?", - documents: ["Puppy A is happy.", "Puppy B is sad."], - examples_context: "In 2017, U.S. life expectancy was 78.6 years.", - examples: [["What is human life expectancy in the United States?","78 years."]], - max_tokens: 5, - stop: ["\n", "<|endoftext|>"], - }); - parameters: | - { - "documents": ["Puppy A is happy.", "Puppy B is sad."], - "question": "which puppy is happy?", - "search_model": "ada", - "model": "curie", - "examples_context": "In 2017, U.S. life expectancy was 78.6 years.", - "examples": [["What is human life expectancy in the United States?","78 years."]], - "max_tokens": 5, - "stop": ["\n", "<|endoftext|>"] - } - response: | - { - "answers": [ - "puppy A." - ], - "completion": "cmpl-2euVa1kmKUuLpSX600M41125Mo9NI", - "model": "curie:2020-05-03", - "object": "answer", - "search_model": "ada", - "selected_documents": [ - { - "document": 0, - "text": "Puppy A is happy. " - }, - { - "document": 1, - "text": "Puppy B is sad. " - } - ] - } - - /classifications: - post: - operationId: createClassification - deprecated: true - tags: - - OpenAI - summary: | - Classifies the specified `query` using provided examples. - - The endpoint first [searches](/docs/api-reference/searches) over the labeled examples - to select the ones most relevant for the particular query. Then, the relevant examples - are combined with the query to construct a prompt to produce the final label via the - [completions](/docs/api-reference/completions) endpoint. - - Labeled examples can be provided via an uploaded `file`, or explicitly listed in the - request using the `examples` parameter for quick tests and small scale use cases. - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateClassificationRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/CreateClassificationResponse' - x-oaiMeta: - name: Create classification - group: classifications - path: create - examples: - curl: | - curl https://api.openai.com/v1/classifications \ - -X POST \ - -H "Authorization: Bearer YOUR_API_KEY" \ - -H 'Content-Type: application/json' \ - -d '{ - "examples": [ - ["A happy moment", "Positive"], - ["I am sad.", "Negative"], - ["I am feeling awesome", "Positive"]], - "query": "It is a raining day :(", - "search_model": "ada", - "model": "curie", - "labels":["Positive", "Negative", "Neutral"] - }' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Classification.create( - search_model="ada", - model="curie", - examples=[ - ["A happy moment", "Positive"], - ["I am sad.", "Negative"], - ["I am feeling awesome", "Positive"] - ], - query="It is a raining day :(", - labels=["Positive", "Negative", "Neutral"], - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createClassification({ - search_model: "ada", - model: "curie", - examples: [ - ["A happy moment", "Positive"], - ["I am sad.", "Negative"], - ["I am feeling awesome", "Positive"] - ], - query:"It is a raining day :(", - labels: ["Positive", "Negative", "Neutral"], - }); - parameters: | - { - "examples": [ - ["A happy moment", "Positive"], - ["I am sad.", "Negative"], - ["I am feeling awesome", "Positive"] - ], - "labels": ["Positive", "Negative", "Neutral"], - "query": "It is a raining day :(", - "search_model": "ada", - "model": "curie" - } - response: | - { - "completion": "cmpl-2euN7lUVZ0d4RKbQqRV79IiiE6M1f", - "label": "Negative", - "model": "curie:2020-05-03", - "object": "classification", - "search_model": "ada", - "selected_examples": [ - { - "document": 1, - "label": "Negative", - "text": "I am sad." - }, - { - "document": 0, - "label": "Positive", - "text": "A happy moment" - }, - { - "document": 2, - "label": "Positive", - "text": "I am feeling awesome" - } - ] - } - - /fine-tunes: - post: - operationId: createFineTune - tags: - - OpenAI - summary: | - Creates a job that fine-tunes a specified model from a given dataset. - - Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete. - - [Learn more about Fine-tuning](/docs/guides/fine-tuning) - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateFineTuneRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/FineTune' - x-oaiMeta: - name: Create fine-tune - group: fine-tunes - path: create - beta: true - examples: - curl: | - curl https://api.openai.com/v1/fine-tunes \ - -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer YOUR_API_KEY" \ - -d '{ - "training_file": "file-XGinujblHPwGLSztz8cPS8XY" - }' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.FineTune.create(training_file="file-XGinujblHPwGLSztz8cPS8XY") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createFineTune({ - training_file: "file-XGinujblHPwGLSztz8cPS8XY", - }); - response: | - { - "id": "ft-AF1WoRqd3aJAHsqc9NY7iL8F", - "object": "fine-tune", - "model": "curie", - "created_at": 1614807352, - "events": [ - { - "object": "fine-tune-event", - "created_at": 1614807352, - "level": "info", - "message": "Job enqueued. Waiting for jobs ahead to complete. Queue number: 0." - } - ], - "fine_tuned_model": null, - "hyperparams": { - "batch_size": 4, - "learning_rate_multiplier": 0.1, - "n_epochs": 4, - "prompt_loss_weight": 0.1, - }, - "organization_id": "org-...", - "result_files": [], - "status": "pending", - "validation_files": [], - "training_files": [ - { - "id": "file-XGinujblHPwGLSztz8cPS8XY", - "object": "file", - "bytes": 1547276, - "created_at": 1610062281, - "filename": "my-data-train.jsonl", - "purpose": "fine-tune-train" - } - ], - "updated_at": 1614807352, - } - get: - operationId: listFineTunes - tags: - - OpenAI - summary: | - List your organization's fine-tuning jobs - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/ListFineTunesResponse' - x-oaiMeta: - name: List fine-tunes - group: fine-tunes - path: list - beta: true - examples: - curl: | - curl https://api.openai.com/v1/fine-tunes \ - -H 'Authorization: Bearer YOUR_API_KEY' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.FineTune.list() - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.listFineTunes(); - response: | - { - "object": "list", - "data": [ - { - "id": "ft-AF1WoRqd3aJAHsqc9NY7iL8F", - "object": "fine-tune", - "model": "curie", - "created_at": 1614807352, - "fine_tuned_model": null, - "hyperparams": { ... }, - "organization_id": "org-...", - "result_files": [], - "status": "pending", - "validation_files": [], - "training_files": [ { ... } ], - "updated_at": 1614807352, - }, - { ... }, - { ... } - ] - } - - /fine-tunes/{fine_tune_id}: - get: - operationId: retrieveFineTune - tags: - - OpenAI - summary: | - Gets info about the fine-tune job. - - [Learn more about Fine-tuning](/docs/guides/fine-tuning) - parameters: - - in: path - name: fine_tune_id - required: true - schema: - type: string - example: - ft-AF1WoRqd3aJAHsqc9NY7iL8F - description: | - The ID of the fine-tune job - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/FineTune' - x-oaiMeta: - name: Retrieve fine-tune - group: fine-tunes - path: retrieve - beta: true - examples: - curl: | - curl https://api.openai.com/v1/fine-tunes/ft-AF1WoRqd3aJAHsqc9NY7iL8F \ - -H "Authorization: Bearer YOUR_API_KEY" - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.FineTune.retrieve(id="ft-AF1WoRqd3aJAHsqc9NY7iL8F") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.retrieveFineTune("ft-AF1WoRqd3aJAHsqc9NY7iL8F"); - response: | - { - "id": "ft-AF1WoRqd3aJAHsqc9NY7iL8F", - "object": "fine-tune", - "model": "curie", - "created_at": 1614807352, - "events": [ - { - "object": "fine-tune-event", - "created_at": 1614807352, - "level": "info", - "message": "Job enqueued. Waiting for jobs ahead to complete. Queue number: 0." - }, - { - "object": "fine-tune-event", - "created_at": 1614807356, - "level": "info", - "message": "Job started." - }, - { - "object": "fine-tune-event", - "created_at": 1614807861, - "level": "info", - "message": "Uploaded snapshot: curie:ft-acmeco-2021-03-03-21-44-20." - }, - { - "object": "fine-tune-event", - "created_at": 1614807864, - "level": "info", - "message": "Uploaded result files: file-QQm6ZpqdNwAaVC3aSz5sWwLT." - }, - { - "object": "fine-tune-event", - "created_at": 1614807864, - "level": "info", - "message": "Job succeeded." - } - ], - "fine_tuned_model": "curie:ft-acmeco-2021-03-03-21-44-20", - "hyperparams": { - "batch_size": 4, - "learning_rate_multiplier": 0.1, - "n_epochs": 4, - "prompt_loss_weight": 0.1, - }, - "organization_id": "org-...", - "result_files": [ - { - "id": "file-QQm6ZpqdNwAaVC3aSz5sWwLT", - "object": "file", - "bytes": 81509, - "created_at": 1614807863, - "filename": "compiled_results.csv", - "purpose": "fine-tune-results" - } - ], - "status": "succeeded", - "validation_files": [], - "training_files": [ - { - "id": "file-XGinujblHPwGLSztz8cPS8XY", - "object": "file", - "bytes": 1547276, - "created_at": 1610062281, - "filename": "my-data-train.jsonl", - "purpose": "fine-tune-train" - } - ], - "updated_at": 1614807865, - } - - /fine-tunes/{fine_tune_id}/cancel: - post: - operationId: cancelFineTune - tags: - - OpenAI - summary: | - Immediately cancel a fine-tune job. - parameters: - - in: path - name: fine_tune_id - required: true - schema: - type: string - example: - ft-AF1WoRqd3aJAHsqc9NY7iL8F - description: | - The ID of the fine-tune job to cancel - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/FineTune' - x-oaiMeta: - name: Cancel fine-tune - group: fine-tunes - path: cancel - beta: true - examples: - curl: | - curl https://api.openai.com/v1/fine-tunes/ft-AF1WoRqd3aJAHsqc9NY7iL8F/cancel \ - -X POST \ - -H "Authorization: Bearer YOUR_API_KEY" - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.FineTune.cancel(id="ft-AF1WoRqd3aJAHsqc9NY7iL8F") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.cancelFineTune("ft-AF1WoRqd3aJAHsqc9NY7iL8F"); - response: | - { - "id": "ft-xhrpBbvVUzYGo8oUO1FY4nI7", - "object": "fine-tune", - "model": "curie", - "created_at": 1614807770, - "events": [ { ... } ], - "fine_tuned_model": null, - "hyperparams": { ... }, - "organization_id": "org-...", - "result_files": [], - "status": "cancelled", - "validation_files": [], - "training_files": [ - { - "id": "file-XGinujblHPwGLSztz8cPS8XY", - "object": "file", - "bytes": 1547276, - "created_at": 1610062281, - "filename": "my-data-train.jsonl", - "purpose": "fine-tune-train" - } - ], - "updated_at": 1614807789, - } - - /fine-tunes/{fine_tune_id}/events: - get: - operationId: listFineTuneEvents - tags: - - OpenAI - summary: | - Get fine-grained status updates for a fine-tune job. - parameters: - - in: path - name: fine_tune_id - required: true - schema: - type: string - example: - ft-AF1WoRqd3aJAHsqc9NY7iL8F - description: | - The ID of the fine-tune job to get events for. - - in: query - name: stream - required: false - schema: + # Note: When adding an endpoint, make sure you also add it in the `groups` section, in the end of this file, + # under the appropriate group + /chat/completions: + post: + operationId: createChatCompletion + tags: + - Chat + summary: Creates a model response for the given chat conversation. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateChatCompletionRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/CreateChatCompletionResponse" + + x-oaiMeta: + name: Create chat completion + group: chat + returns: | + Returns a [chat completion](/docs/api-reference/chat/object) object, or a streamed sequence of [chat completion chunk](/docs/api-reference/chat/streaming) objects if the request is streamed. + path: create + examples: + - title: Default + request: + curl: | + curl https://api.openai.com/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "VAR_model_id", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant." + }, + { + "role": "user", + "content": "Hello!" + } + ] + }' + python: | + from openai import OpenAI + client = OpenAI() + + completion = client.chat.completions.create( + model="VAR_model_id", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ] + ) + + print(completion.choices[0].message) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: "system", content: "You are a helpful assistant." }], + model: "VAR_model_id", + }); + + console.log(completion.choices[0]); + } + + main(); + response: &chat_completion_example | + { + "id": "chatcmpl-123", + "object": "chat.completion", + "created": 1677652288, + "model": "gpt-4o-mini", + "system_fingerprint": "fp_44709d6fcb", + "choices": [{ + "index": 0, + "message": { + "role": "assistant", + "content": "\n\nHello there, how may I assist you today?", + }, + "logprobs": null, + "finish_reason": "stop" + }], + "usage": { + "prompt_tokens": 9, + "completion_tokens": 12, + "total_tokens": 21, + "completion_tokens_details": { + "reasoning_tokens": 0 + } + } + } + - title: Image input + request: + curl: | + curl https://api.openai.com/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What'\''s in this image?" + }, + { + "type": "image_url", + "image_url": { + "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + } + ] + } + ], + "max_tokens": 300 + }' + python: | + from openai import OpenAI + + client = OpenAI() + + response = client.chat.completions.create( + model="gpt-4o", + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": { + "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + } + }, + ], + } + ], + max_tokens=300, + ) + + print(response.choices[0]) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const response = await openai.chat.completions.create({ + model: "gpt-4o", + messages: [ + { + role: "user", + content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", + image_url: { + "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", + }, + } + ], + }, + ], + }); + console.log(response.choices[0]); + } + main(); + response: &chat_completion_image_example | + { + "id": "chatcmpl-123", + "object": "chat.completion", + "created": 1677652288, + "model": "gpt-4o-mini", + "system_fingerprint": "fp_44709d6fcb", + "choices": [{ + "index": 0, + "message": { + "role": "assistant", + "content": "\n\nThis image shows a wooden boardwalk extending through a lush green marshland.", + }, + "logprobs": null, + "finish_reason": "stop" + }], + "usage": { + "prompt_tokens": 9, + "completion_tokens": 12, + "total_tokens": 21, + "completion_tokens_details": { + "reasoning_tokens": 0 + } + } + } + - title: Streaming + request: + curl: | + curl https://api.openai.com/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "VAR_model_id", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant." + }, + { + "role": "user", + "content": "Hello!" + } + ], + "stream": true + }' + python: | + from openai import OpenAI + client = OpenAI() + + completion = client.chat.completions.create( + model="VAR_model_id", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ], + stream=True + ) + + for chunk in completion: + print(chunk.choices[0].delta) + + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const completion = await openai.chat.completions.create({ + model: "VAR_model_id", + messages: [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"} + ], + stream: true, + }); + + for await (const chunk of completion) { + console.log(chunk.choices[0].delta.content); + } + } + + main(); + response: &chat_completion_chunk_example | + {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]} + + {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":null,"finish_reason":null}]} + + .... + + {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]} + - title: Functions + request: + curl: | + curl https://api.openai.com/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "gpt-4o", + "messages": [ + { + "role": "user", + "content": "What'\''s the weather like in Boston today?" + } + ], + "tools": [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"] + } + }, + "required": ["location"] + } + } + } + ], + "tool_choice": "auto" + }' + python: | + from openai import OpenAI + client = OpenAI() + + tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ] + messages = [{"role": "user", "content": "What's the weather like in Boston today?"}] + completion = client.chat.completions.create( + model="VAR_model_id", + messages=messages, + tools=tools, + tool_choice="auto" + ) + + print(completion) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]; + const tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ]; + + const response = await openai.chat.completions.create({ + model: "gpt-4o", + messages: messages, + tools: tools, + tool_choice: "auto", + }); + + console.log(response); + } + + main(); + response: &chat_completion_function_example | + { + "id": "chatcmpl-abc123", + "object": "chat.completion", + "created": 1699896916, + "model": "gpt-4o-mini", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": null, + "tool_calls": [ + { + "id": "call_abc123", + "type": "function", + "function": { + "name": "get_current_weather", + "arguments": "{\n\"location\": \"Boston, MA\"\n}" + } + } + ] + }, + "logprobs": null, + "finish_reason": "tool_calls" + } + ], + "usage": { + "prompt_tokens": 82, + "completion_tokens": 17, + "total_tokens": 99, + "completion_tokens_details": { + "reasoning_tokens": 0 + } + } + } + - title: Logprobs + request: + curl: | + curl https://api.openai.com/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "VAR_model_id", + "messages": [ + { + "role": "user", + "content": "Hello!" + } + ], + "logprobs": true, + "top_logprobs": 2 + }' + python: | + from openai import OpenAI + client = OpenAI() + + completion = client.chat.completions.create( + model="VAR_model_id", + messages=[ + {"role": "user", "content": "Hello!"} + ], + logprobs=True, + top_logprobs=2 + ) + + print(completion.choices[0].message) + print(completion.choices[0].logprobs) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const completion = await openai.chat.completions.create({ + messages: [{ role: "user", content: "Hello!" }], + model: "VAR_model_id", + logprobs: true, + top_logprobs: 2, + }); + + console.log(completion.choices[0]); + } + + main(); + response: | + { + "id": "chatcmpl-123", + "object": "chat.completion", + "created": 1702685778, + "model": "gpt-4o-mini", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "Hello! How can I assist you today?" + }, + "logprobs": { + "content": [ + { + "token": "Hello", + "logprob": -0.31725305, + "bytes": [72, 101, 108, 108, 111], + "top_logprobs": [ + { + "token": "Hello", + "logprob": -0.31725305, + "bytes": [72, 101, 108, 108, 111] + }, + { + "token": "Hi", + "logprob": -1.3190403, + "bytes": [72, 105] + } + ] + }, + { + "token": "!", + "logprob": -0.02380986, + "bytes": [ + 33 + ], + "top_logprobs": [ + { + "token": "!", + "logprob": -0.02380986, + "bytes": [33] + }, + { + "token": " there", + "logprob": -3.787621, + "bytes": [32, 116, 104, 101, 114, 101] + } + ] + }, + { + "token": " How", + "logprob": -0.000054669687, + "bytes": [32, 72, 111, 119], + "top_logprobs": [ + { + "token": " How", + "logprob": -0.000054669687, + "bytes": [32, 72, 111, 119] + }, + { + "token": "<|end|>", + "logprob": -10.953937, + "bytes": null + } + ] + }, + { + "token": " can", + "logprob": -0.015801601, + "bytes": [32, 99, 97, 110], + "top_logprobs": [ + { + "token": " can", + "logprob": -0.015801601, + "bytes": [32, 99, 97, 110] + }, + { + "token": " may", + "logprob": -4.161023, + "bytes": [32, 109, 97, 121] + } + ] + }, + { + "token": " I", + "logprob": -3.7697225e-6, + "bytes": [ + 32, + 73 + ], + "top_logprobs": [ + { + "token": " I", + "logprob": -3.7697225e-6, + "bytes": [32, 73] + }, + { + "token": " assist", + "logprob": -13.596657, + "bytes": [32, 97, 115, 115, 105, 115, 116] + } + ] + }, + { + "token": " assist", + "logprob": -0.04571125, + "bytes": [32, 97, 115, 115, 105, 115, 116], + "top_logprobs": [ + { + "token": " assist", + "logprob": -0.04571125, + "bytes": [32, 97, 115, 115, 105, 115, 116] + }, + { + "token": " help", + "logprob": -3.1089056, + "bytes": [32, 104, 101, 108, 112] + } + ] + }, + { + "token": " you", + "logprob": -5.4385737e-6, + "bytes": [32, 121, 111, 117], + "top_logprobs": [ + { + "token": " you", + "logprob": -5.4385737e-6, + "bytes": [32, 121, 111, 117] + }, + { + "token": " today", + "logprob": -12.807695, + "bytes": [32, 116, 111, 100, 97, 121] + } + ] + }, + { + "token": " today", + "logprob": -0.0040071653, + "bytes": [32, 116, 111, 100, 97, 121], + "top_logprobs": [ + { + "token": " today", + "logprob": -0.0040071653, + "bytes": [32, 116, 111, 100, 97, 121] + }, + { + "token": "?", + "logprob": -5.5247097, + "bytes": [63] + } + ] + }, + { + "token": "?", + "logprob": -0.0008108172, + "bytes": [63], + "top_logprobs": [ + { + "token": "?", + "logprob": -0.0008108172, + "bytes": [63] + }, + { + "token": "?\n", + "logprob": -7.184561, + "bytes": [63, 10] + } + ] + } + ] + }, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 9, + "completion_tokens": 9, + "total_tokens": 18, + "completion_tokens_details": { + "reasoning_tokens": 0 + } + }, + "system_fingerprint": null + } + + /completions: + post: + operationId: createCompletion + tags: + - Completions + summary: Creates a completion for the provided prompt and parameters. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateCompletionRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/CreateCompletionResponse" + x-oaiMeta: + name: Create completion + group: completions + returns: | + Returns a [completion](/docs/api-reference/completions/object) object, or a sequence of completion objects if the request is streamed. + legacy: true + examples: + - title: No streaming + request: + curl: | + curl https://api.openai.com/v1/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "VAR_model_id", + "prompt": "Say this is a test", + "max_tokens": 7, + "temperature": 0 + }' + python: | + from openai import OpenAI + client = OpenAI() + + client.completions.create( + model="VAR_model_id", + prompt="Say this is a test", + max_tokens=7, + temperature=0 + ) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const completion = await openai.completions.create({ + model: "VAR_model_id", + prompt: "Say this is a test.", + max_tokens: 7, + temperature: 0, + }); + + console.log(completion); + } + main(); + response: | + { + "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7", + "object": "text_completion", + "created": 1589478378, + "model": "VAR_model_id", + "system_fingerprint": "fp_44709d6fcb", + "choices": [ + { + "text": "\n\nThis is indeed a test", + "index": 0, + "logprobs": null, + "finish_reason": "length" + } + ], + "usage": { + "prompt_tokens": 5, + "completion_tokens": 7, + "total_tokens": 12 + } + } + - title: Streaming + request: + curl: | + curl https://api.openai.com/v1/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "VAR_model_id", + "prompt": "Say this is a test", + "max_tokens": 7, + "temperature": 0, + "stream": true + }' + python: | + from openai import OpenAI + client = OpenAI() + + for chunk in client.completions.create( + model="VAR_model_id", + prompt="Say this is a test", + max_tokens=7, + temperature=0, + stream=True + ): + print(chunk.choices[0].text) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const stream = await openai.completions.create({ + model: "VAR_model_id", + prompt: "Say this is a test.", + stream: true, + }); + + for await (const chunk of stream) { + console.log(chunk.choices[0].text) + } + } + main(); + response: | + { + "id": "cmpl-7iA7iJjj8V2zOkCGvWF2hAkDWBQZe", + "object": "text_completion", + "created": 1690759702, + "choices": [ + { + "text": "This", + "index": 0, + "logprobs": null, + "finish_reason": null + } + ], + "model": "gpt-3.5-turbo-instruct" + "system_fingerprint": "fp_44709d6fcb", + } + + /images/generations: + post: + operationId: createImage + tags: + - Images + summary: Creates an image given a prompt. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateImageRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ImagesResponse" + x-oaiMeta: + name: Create image + group: images + returns: Returns a list of [image](/docs/api-reference/images/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/images/generations \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "dall-e-3", + "prompt": "A cute baby sea otter", + "n": 1, + "size": "1024x1024" + }' + python: | + from openai import OpenAI + client = OpenAI() + + client.images.generate( + model="dall-e-3", + prompt="A cute baby sea otter", + n=1, + size="1024x1024" + ) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const image = await openai.images.generate({ model: "dall-e-3", prompt: "A cute baby sea otter" }); + + console.log(image.data); + } + main(); + response: | + { + "created": 1589478378, + "data": [ + { + "url": "https://..." + }, + { + "url": "https://..." + } + ] + } + /images/edits: + post: + operationId: createImageEdit + tags: + - Images + summary: Creates an edited or extended image given an original image and a prompt. + requestBody: + required: true + content: + multipart/form-data: + schema: + $ref: "#/components/schemas/CreateImageEditRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ImagesResponse" + x-oaiMeta: + name: Create image edit + group: images + returns: Returns a list of [image](/docs/api-reference/images/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/images/edits \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -F image="@otter.png" \ + -F mask="@mask.png" \ + -F prompt="A cute baby sea otter wearing a beret" \ + -F n=2 \ + -F size="1024x1024" + python: | + from openai import OpenAI + client = OpenAI() + + client.images.edit( + image=open("otter.png", "rb"), + mask=open("mask.png", "rb"), + prompt="A cute baby sea otter wearing a beret", + n=2, + size="1024x1024" + ) + node.js: |- + import fs from "fs"; + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const image = await openai.images.edit({ + image: fs.createReadStream("otter.png"), + mask: fs.createReadStream("mask.png"), + prompt: "A cute baby sea otter wearing a beret", + }); + + console.log(image.data); + } + main(); + response: | + { + "created": 1589478378, + "data": [ + { + "url": "https://..." + }, + { + "url": "https://..." + } + ] + } + /images/variations: + post: + operationId: createImageVariation + tags: + - Images + summary: Creates a variation of a given image. + requestBody: + required: true + content: + multipart/form-data: + schema: + $ref: "#/components/schemas/CreateImageVariationRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ImagesResponse" + x-oaiMeta: + name: Create image variation + group: images + returns: Returns a list of [image](/docs/api-reference/images/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/images/variations \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -F image="@otter.png" \ + -F n=2 \ + -F size="1024x1024" + python: | + from openai import OpenAI + client = OpenAI() + + response = client.images.create_variation( + image=open("image_edit_original.png", "rb"), + n=2, + size="1024x1024" + ) + node.js: |- + import fs from "fs"; + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const image = await openai.images.createVariation({ + image: fs.createReadStream("otter.png"), + }); + + console.log(image.data); + } + main(); + response: | + { + "created": 1589478378, + "data": [ + { + "url": "https://..." + }, + { + "url": "https://..." + } + ] + } + + /embeddings: + post: + operationId: createEmbedding + tags: + - Embeddings + summary: Creates an embedding vector representing the input text. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateEmbeddingRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/CreateEmbeddingResponse" + x-oaiMeta: + name: Create embeddings + group: embeddings + returns: A list of [embedding](/docs/api-reference/embeddings/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/embeddings \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "input": "The food was delicious and the waiter...", + "model": "text-embedding-ada-002", + "encoding_format": "float" + }' + python: | + from openai import OpenAI + client = OpenAI() + + client.embeddings.create( + model="text-embedding-ada-002", + input="The food was delicious and the waiter...", + encoding_format="float" + ) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const embedding = await openai.embeddings.create({ + model: "text-embedding-ada-002", + input: "The quick brown fox jumped over the lazy dog", + encoding_format: "float", + }); + + console.log(embedding); + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "object": "embedding", + "embedding": [ + 0.0023064255, + -0.009327292, + .... (1536 floats total for ada-002) + -0.0028842222, + ], + "index": 0 + } + ], + "model": "text-embedding-ada-002", + "usage": { + "prompt_tokens": 8, + "total_tokens": 8 + } + } + + /audio/speech: + post: + operationId: createSpeech + tags: + - Audio + summary: Generates audio from the input text. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateSpeechRequest" + responses: + "200": + description: OK + headers: + Transfer-Encoding: + schema: + type: string + description: chunked + content: + application/octet-stream: + schema: + type: string + format: binary + x-oaiMeta: + name: Create speech + group: audio + returns: The audio file content. + examples: + request: + curl: | + curl https://api.openai.com/v1/audio/speech \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "tts-1", + "input": "The quick brown fox jumped over the lazy dog.", + "voice": "alloy" + }' \ + --output speech.mp3 + python: | + from pathlib import Path + import openai + + speech_file_path = Path(__file__).parent / "speech.mp3" + response = openai.audio.speech.create( + model="tts-1", + voice="alloy", + input="The quick brown fox jumped over the lazy dog." + ) + response.stream_to_file(speech_file_path) + node: | + import fs from "fs"; + import path from "path"; + import OpenAI from "openai"; + + const openai = new OpenAI(); + + const speechFile = path.resolve("./speech.mp3"); + + async function main() { + const mp3 = await openai.audio.speech.create({ + model: "tts-1", + voice: "alloy", + input: "Today is a wonderful day to build something people love!", + }); + console.log(speechFile); + const buffer = Buffer.from(await mp3.arrayBuffer()); + await fs.promises.writeFile(speechFile, buffer); + } + main(); + /audio/transcriptions: + post: + operationId: createTranscription + tags: + - Audio + summary: Transcribes audio into the input language. + requestBody: + required: true + content: + multipart/form-data: + schema: + $ref: "#/components/schemas/CreateTranscriptionRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + oneOf: + - $ref: "#/components/schemas/CreateTranscriptionResponseJson" + - $ref: "#/components/schemas/CreateTranscriptionResponseVerboseJson" + x-oaiMeta: + name: Create transcription + group: audio + returns: The [transcription object](/docs/api-reference/audio/json-object) or a [verbose transcription object](/docs/api-reference/audio/verbose-json-object). + examples: + - title: Default + request: + curl: | + curl https://api.openai.com/v1/audio/transcriptions \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F file="@/path/to/file/audio.mp3" \ + -F model="whisper-1" + python: | + from openai import OpenAI + client = OpenAI() + + audio_file = open("speech.mp3", "rb") + transcript = client.audio.transcriptions.create( + model="whisper-1", + file=audio_file + ) + node: | + import fs from "fs"; + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const transcription = await openai.audio.transcriptions.create({ + file: fs.createReadStream("audio.mp3"), + model: "whisper-1", + }); + + console.log(transcription.text); + } + main(); + response: &basic_transcription_response_example | + { + "text": "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger. This is a place where you can get to do that." + } + - title: Word timestamps + request: + curl: | + curl https://api.openai.com/v1/audio/transcriptions \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F file="@/path/to/file/audio.mp3" \ + -F "timestamp_granularities[]=word" \ + -F model="whisper-1" \ + -F response_format="verbose_json" + python: | + from openai import OpenAI + client = OpenAI() + + audio_file = open("speech.mp3", "rb") + transcript = client.audio.transcriptions.create( + file=audio_file, + model="whisper-1", + response_format="verbose_json", + timestamp_granularities=["word"] + ) + + print(transcript.words) + node: | + import fs from "fs"; + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const transcription = await openai.audio.transcriptions.create({ + file: fs.createReadStream("audio.mp3"), + model: "whisper-1", + response_format: "verbose_json", + timestamp_granularities: ["word"] + }); + + console.log(transcription.text); + } + main(); + response: | + { + "task": "transcribe", + "language": "english", + "duration": 8.470000267028809, + "text": "The beach was a popular spot on a hot summer day. People were swimming in the ocean, building sandcastles, and playing beach volleyball.", + "words": [ + { + "word": "The", + "start": 0.0, + "end": 0.23999999463558197 + }, + ... + { + "word": "volleyball", + "start": 7.400000095367432, + "end": 7.900000095367432 + } + ] + } + - title: Segment timestamps + request: + curl: | + curl https://api.openai.com/v1/audio/transcriptions \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F file="@/path/to/file/audio.mp3" \ + -F "timestamp_granularities[]=segment" \ + -F model="whisper-1" \ + -F response_format="verbose_json" + python: | + from openai import OpenAI + client = OpenAI() + + audio_file = open("speech.mp3", "rb") + transcript = client.audio.transcriptions.create( + file=audio_file, + model="whisper-1", + response_format="verbose_json", + timestamp_granularities=["segment"] + ) + + print(transcript.words) + node: | + import fs from "fs"; + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const transcription = await openai.audio.transcriptions.create({ + file: fs.createReadStream("audio.mp3"), + model: "whisper-1", + response_format: "verbose_json", + timestamp_granularities: ["segment"] + }); + + console.log(transcription.text); + } + main(); + response: &verbose_transcription_response_example | + { + "task": "transcribe", + "language": "english", + "duration": 8.470000267028809, + "text": "The beach was a popular spot on a hot summer day. People were swimming in the ocean, building sandcastles, and playing beach volleyball.", + "segments": [ + { + "id": 0, + "seek": 0, + "start": 0.0, + "end": 3.319999933242798, + "text": " The beach was a popular spot on a hot summer day.", + "tokens": [ + 50364, 440, 7534, 390, 257, 3743, 4008, 322, 257, 2368, 4266, 786, 13, 50530 + ], + "temperature": 0.0, + "avg_logprob": -0.2860786020755768, + "compression_ratio": 1.2363636493682861, + "no_speech_prob": 0.00985979475080967 + }, + ... + ] + } + /audio/translations: + post: + operationId: createTranslation + tags: + - Audio + summary: Translates audio into English. + requestBody: + required: true + content: + multipart/form-data: + schema: + $ref: "#/components/schemas/CreateTranslationRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + oneOf: + - $ref: "#/components/schemas/CreateTranslationResponseJson" + - $ref: "#/components/schemas/CreateTranslationResponseVerboseJson" + x-oaiMeta: + name: Create translation + group: audio + returns: The translated text. + examples: + request: + curl: | + curl https://api.openai.com/v1/audio/translations \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: multipart/form-data" \ + -F file="@/path/to/file/german.m4a" \ + -F model="whisper-1" + python: | + from openai import OpenAI + client = OpenAI() + + audio_file = open("speech.mp3", "rb") + transcript = client.audio.translations.create( + model="whisper-1", + file=audio_file + ) + node: | + import fs from "fs"; + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const translation = await openai.audio.translations.create({ + file: fs.createReadStream("speech.mp3"), + model: "whisper-1", + }); + + console.log(translation.text); + } + main(); + response: | + { + "text": "Hello, my name is Wolfgang and I come from Germany. Where are you heading today?" + } + + /files: + get: + operationId: listFiles + tags: + - Files + summary: Returns a list of files that belong to the user's organization. + parameters: + - in: query + name: purpose + required: false + schema: + type: string + description: Only return files with the given purpose. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListFilesResponse" + x-oaiMeta: + name: List files + group: files + returns: A list of [File](/docs/api-reference/files/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/files \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.files.list() + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const list = await openai.files.list(); + + for await (const file of list) { + console.log(file); + } + } + + main(); + response: | + { + "data": [ + { + "id": "file-abc123", + "object": "file", + "bytes": 175, + "created_at": 1613677385, + "filename": "salesOverview.pdf", + "purpose": "assistants", + }, + { + "id": "file-abc123", + "object": "file", + "bytes": 140, + "created_at": 1613779121, + "filename": "puppy.jsonl", + "purpose": "fine-tune", + } + ], + "object": "list" + } + post: + operationId: createFile + tags: + - Files + summary: | + Upload a file that can be used across various endpoints. Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB. + + The Assistants API supports files up to 2 million tokens and of specific file types. See the [Assistants Tools guide](/docs/assistants/tools) for details. + + The Fine-tuning API only supports `.jsonl` files. The input also has certain required formats for fine-tuning [chat](/docs/api-reference/fine-tuning/chat-input) or [completions](/docs/api-reference/fine-tuning/completions-input) models. + + The Batch API only supports `.jsonl` files up to 100 MB in size. The input also has a specific required [format](/docs/api-reference/batch/request-input). + + Please [contact us](https://help.openai.com/) if you need to increase these storage limits. + requestBody: + required: true + content: + multipart/form-data: + schema: + $ref: "#/components/schemas/CreateFileRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/OpenAIFile" + x-oaiMeta: + name: Upload file + group: files + returns: The uploaded [File](/docs/api-reference/files/object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/files \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -F purpose="fine-tune" \ + -F file="@mydata.jsonl" + python: | + from openai import OpenAI + client = OpenAI() + + client.files.create( + file=open("mydata.jsonl", "rb"), + purpose="fine-tune" + ) + node.js: |- + import fs from "fs"; + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const file = await openai.files.create({ + file: fs.createReadStream("mydata.jsonl"), + purpose: "fine-tune", + }); + + console.log(file); + } + + main(); + response: | + { + "id": "file-abc123", + "object": "file", + "bytes": 120000, + "created_at": 1677610602, + "filename": "mydata.jsonl", + "purpose": "fine-tune", + } + /files/{file_id}: + delete: + operationId: deleteFile + tags: + - Files + summary: Delete a file. + parameters: + - in: path + name: file_id + required: true + schema: + type: string + description: The ID of the file to use for this request. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/DeleteFileResponse" + x-oaiMeta: + name: Delete file + group: files + returns: Deletion status. + examples: + request: + curl: | + curl https://api.openai.com/v1/files/file-abc123 \ + -X DELETE \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.files.delete("file-abc123") + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const file = await openai.files.del("file-abc123"); + + console.log(file); + } + + main(); + response: | + { + "id": "file-abc123", + "object": "file", + "deleted": true + } + get: + operationId: retrieveFile + tags: + - Files + summary: Returns information about a specific file. + parameters: + - in: path + name: file_id + required: true + schema: + type: string + description: The ID of the file to use for this request. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/OpenAIFile" + x-oaiMeta: + name: Retrieve file + group: files + returns: The [File](/docs/api-reference/files/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/files/file-abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.files.retrieve("file-abc123") + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const file = await openai.files.retrieve("file-abc123"); + + console.log(file); + } + + main(); + response: | + { + "id": "file-abc123", + "object": "file", + "bytes": 120000, + "created_at": 1677610602, + "filename": "mydata.jsonl", + "purpose": "fine-tune", + } + /files/{file_id}/content: + get: + operationId: downloadFile + tags: + - Files + summary: Returns the contents of the specified file. + parameters: + - in: path + name: file_id + required: true + schema: + type: string + description: The ID of the file to use for this request. + responses: + "200": + description: OK + content: + application/json: + schema: + type: string + x-oaiMeta: + name: Retrieve file content + group: files + returns: The file content. + examples: + request: + curl: | + curl https://api.openai.com/v1/files/file-abc123/content \ + -H "Authorization: Bearer $OPENAI_API_KEY" > file.jsonl + python: | + from openai import OpenAI + client = OpenAI() + + content = client.files.content("file-abc123") + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const file = await openai.files.content("file-abc123"); + + console.log(file); + } + + main(); + /uploads: + post: + operationId: createUpload + tags: + - Uploads + summary: | + Creates an intermediate [Upload](/docs/api-reference/uploads/object) object that you can add [Parts](/docs/api-reference/uploads/part-object) to. Currently, an Upload can accept at most 8 GB in total and expires after an hour after you create it. + + Once you complete the Upload, we will create a [File](/docs/api-reference/files/object) object that contains all the parts you uploaded. This File is usable in the rest of our platform as a regular File object. + + For certain `purpose`s, the correct `mime_type` must be specified. Please refer to documentation for the supported MIME types for your use case: + - [Assistants](/docs/assistants/tools/file-search/supported-files) + + For guidance on the proper filename extensions for each purpose, please follow the documentation on [creating a File](/docs/api-reference/files/create). + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateUploadRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/Upload" + x-oaiMeta: + name: Create upload + group: uploads + returns: The [Upload](/docs/api-reference/uploads/object) object with status `pending`. + examples: + request: + curl: | + curl https://api.openai.com/v1/uploads \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "purpose": "fine-tune", + "filename": "training_examples.jsonl", + "bytes": 2147483648, + "mime_type": "text/jsonl" + }' + response: | + { + "id": "upload_abc123", + "object": "upload", + "bytes": 2147483648, + "created_at": 1719184911, + "filename": "training_examples.jsonl", + "purpose": "fine-tune", + "status": "pending", + "expires_at": 1719127296 + } + + /uploads/{upload_id}/parts: + post: + operationId: addUploadPart + tags: + - Uploads + summary: | + Adds a [Part](/docs/api-reference/uploads/part-object) to an [Upload](/docs/api-reference/uploads/object) object. A Part represents a chunk of bytes from the file you are trying to upload. + + Each Part can be at most 64 MB, and you can add Parts until you hit the Upload maximum of 8 GB. + + It is possible to add multiple Parts in parallel. You can decide the intended order of the Parts when you [complete the Upload](/docs/api-reference/uploads/complete). + parameters: + - in: path + name: upload_id + required: true + schema: + type: string + example: upload_abc123 + description: | + The ID of the Upload. + requestBody: + required: true + content: + multipart/form-data: + schema: + $ref: "#/components/schemas/AddUploadPartRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/UploadPart" + x-oaiMeta: + name: Add upload part + group: uploads + returns: The upload [Part](/docs/api-reference/uploads/part-object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/uploads/upload_abc123/parts + -F data="aHR0cHM6Ly9hcGkub3BlbmFpLmNvbS92MS91cGxvYWRz..." + response: | + { + "id": "part_def456", + "object": "upload.part", + "created_at": 1719185911, + "upload_id": "upload_abc123" + } + + /uploads/{upload_id}/complete: + post: + operationId: completeUpload + tags: + - Uploads + summary: | + Completes the [Upload](/docs/api-reference/uploads/object). + + Within the returned Upload object, there is a nested [File](/docs/api-reference/files/object) object that is ready to use in the rest of the platform. + + You can specify the order of the Parts by passing in an ordered list of the Part IDs. + + The number of bytes uploaded upon completion must match the number of bytes initially specified when creating the Upload object. No Parts may be added after an Upload is completed. + parameters: + - in: path + name: upload_id + required: true + schema: + type: string + example: upload_abc123 + description: | + The ID of the Upload. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CompleteUploadRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/Upload" + x-oaiMeta: + name: Complete upload + group: uploads + returns: The [Upload](/docs/api-reference/uploads/object) object with status `completed` with an additional `file` property containing the created usable File object. + examples: + request: + curl: | + curl https://api.openai.com/v1/uploads/upload_abc123/complete + -d '{ + "part_ids": ["part_def456", "part_ghi789"] + }' + response: | + { + "id": "upload_abc123", + "object": "upload", + "bytes": 2147483648, + "created_at": 1719184911, + "filename": "training_examples.jsonl", + "purpose": "fine-tune", + "status": "completed", + "expires_at": 1719127296, + "file": { + "id": "file-xyz321", + "object": "file", + "bytes": 2147483648, + "created_at": 1719186911, + "filename": "training_examples.jsonl", + "purpose": "fine-tune", + } + } + + /uploads/{upload_id}/cancel: + post: + operationId: cancelUpload + tags: + - Uploads + summary: | + Cancels the Upload. No Parts may be added after an Upload is cancelled. + parameters: + - in: path + name: upload_id + required: true + schema: + type: string + example: upload_abc123 + description: | + The ID of the Upload. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/Upload" + x-oaiMeta: + name: Cancel upload + group: uploads + returns: The [Upload](/docs/api-reference/uploads/object) object with status `cancelled`. + examples: + request: + curl: | + curl https://api.openai.com/v1/uploads/upload_abc123/cancel + response: | + { + "id": "upload_abc123", + "object": "upload", + "bytes": 2147483648, + "created_at": 1719184911, + "filename": "training_examples.jsonl", + "purpose": "fine-tune", + "status": "cancelled", + "expires_at": 1719127296 + } + + /fine_tuning/jobs: + post: + operationId: createFineTuningJob + tags: + - Fine-tuning + summary: | + Creates a fine-tuning job which begins the process of creating a new model from a given dataset. + + Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete. + + [Learn more about fine-tuning](/docs/guides/fine-tuning) + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateFineTuningJobRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/FineTuningJob" + x-oaiMeta: + name: Create fine-tuning job + group: fine-tuning + returns: A [fine-tuning.job](/docs/api-reference/fine-tuning/object) object. + examples: + - title: Default + request: + curl: | + curl https://api.openai.com/v1/fine_tuning/jobs \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "training_file": "file-BK7bzQj3FfZFXr7DbL6xJwfo", + "model": "gpt-4o-mini" + }' + python: | + from openai import OpenAI + client = OpenAI() + + client.fine_tuning.jobs.create( + training_file="file-abc123", + model="gpt-4o-mini" + ) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const fineTune = await openai.fineTuning.jobs.create({ + training_file: "file-abc123" + }); + + console.log(fineTune); + } + + main(); + response: | + { + "object": "fine_tuning.job", + "id": "ftjob-abc123", + "model": "gpt-4o-mini-2024-07-18", + "created_at": 1721764800, + "fine_tuned_model": null, + "organization_id": "org-123", + "result_files": [], + "status": "queued", + "validation_file": null, + "training_file": "file-abc123", + } + - title: Epochs + request: + curl: | + curl https://api.openai.com/v1/fine_tuning/jobs \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "training_file": "file-abc123", + "model": "gpt-4o-mini", + "hyperparameters": { + "n_epochs": 2 + } + }' + python: | + from openai import OpenAI + client = OpenAI() + + client.fine_tuning.jobs.create( + training_file="file-abc123", + model="gpt-4o-mini", + hyperparameters={ + "n_epochs":2 + } + ) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const fineTune = await openai.fineTuning.jobs.create({ + training_file: "file-abc123", + model: "gpt-4o-mini", + hyperparameters: { n_epochs: 2 } + }); + + console.log(fineTune); + } + + main(); + response: | + { + "object": "fine_tuning.job", + "id": "ftjob-abc123", + "model": "gpt-4o-mini-2024-07-18", + "created_at": 1721764800, + "fine_tuned_model": null, + "organization_id": "org-123", + "result_files": [], + "status": "queued", + "validation_file": null, + "training_file": "file-abc123", + "hyperparameters": {"n_epochs": 2}, + } + - title: Validation file + request: + curl: | + curl https://api.openai.com/v1/fine_tuning/jobs \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "training_file": "file-abc123", + "validation_file": "file-abc123", + "model": "gpt-4o-mini" + }' + python: | + from openai import OpenAI + client = OpenAI() + + client.fine_tuning.jobs.create( + training_file="file-abc123", + validation_file="file-def456", + model="gpt-4o-mini" + ) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const fineTune = await openai.fineTuning.jobs.create({ + training_file: "file-abc123", + validation_file: "file-abc123" + }); + + console.log(fineTune); + } + + main(); + response: | + { + "object": "fine_tuning.job", + "id": "ftjob-abc123", + "model": "gpt-4o-mini-2024-07-18", + "created_at": 1721764800, + "fine_tuned_model": null, + "organization_id": "org-123", + "result_files": [], + "status": "queued", + "validation_file": "file-abc123", + "training_file": "file-abc123", + } + - title: W&B Integration + request: + curl: | + curl https://api.openai.com/v1/fine_tuning/jobs \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "training_file": "file-abc123", + "validation_file": "file-abc123", + "model": "gpt-4o-mini", + "integrations": [ + { + "type": "wandb", + "wandb": { + "project": "my-wandb-project", + "name": "ft-run-display-name" + "tags": [ + "first-experiment", "v2" + ] + } + } + ] + }' + response: | + { + "object": "fine_tuning.job", + "id": "ftjob-abc123", + "model": "gpt-4o-mini-2024-07-18", + "created_at": 1721764800, + "fine_tuned_model": null, + "organization_id": "org-123", + "result_files": [], + "status": "queued", + "validation_file": "file-abc123", + "training_file": "file-abc123", + "integrations": [ + { + "type": "wandb", + "wandb": { + "project": "my-wandb-project", + "entity": None, + "run_id": "ftjob-abc123" + } + } + ] + } + get: + operationId: listPaginatedFineTuningJobs + tags: + - Fine-tuning + summary: | + List your organization's fine-tuning jobs + parameters: + - name: after + in: query + description: Identifier for the last job from the previous pagination request. + required: false + schema: + type: string + - name: limit + in: query + description: Number of fine-tuning jobs to retrieve. + required: false + schema: + type: integer + default: 20 + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListPaginatedFineTuningJobsResponse" + x-oaiMeta: + name: List fine-tuning jobs + group: fine-tuning + returns: A list of paginated [fine-tuning job](/docs/api-reference/fine-tuning/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/fine_tuning/jobs?limit=2 \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.fine_tuning.jobs.list() + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const list = await openai.fineTuning.jobs.list(); + + for await (const fineTune of list) { + console.log(fineTune); + } + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "object": "fine_tuning.job.event", + "id": "ft-event-TjX0lMfOniCZX64t9PUQT5hn", + "created_at": 1689813489, + "level": "warn", + "message": "Fine tuning process stopping due to job cancellation", + "data": null, + "type": "message" + }, + { ... }, + { ... } + ], "has_more": true + } + /fine_tuning/jobs/{fine_tuning_job_id}: + get: + operationId: retrieveFineTuningJob + tags: + - Fine-tuning + summary: | + Get info about a fine-tuning job. + + [Learn more about fine-tuning](/docs/guides/fine-tuning) + parameters: + - in: path + name: fine_tuning_job_id + required: true + schema: + type: string + example: ft-AF1WoRqd3aJAHsqc9NY7iL8F + description: | + The ID of the fine-tuning job. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/FineTuningJob" + x-oaiMeta: + name: Retrieve fine-tuning job + group: fine-tuning + returns: The [fine-tuning](/docs/api-reference/fine-tuning/object) object with the given ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/fine_tuning/jobs/ft-AF1WoRqd3aJAHsqc9NY7iL8F \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.fine_tuning.jobs.retrieve("ftjob-abc123") + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const fineTune = await openai.fineTuning.jobs.retrieve("ftjob-abc123"); + + console.log(fineTune); + } + + main(); + response: &fine_tuning_example | + { + "object": "fine_tuning.job", + "id": "ftjob-abc123", + "model": "davinci-002", + "created_at": 1692661014, + "finished_at": 1692661190, + "fine_tuned_model": "ft:davinci-002:my-org:custom_suffix:7q8mpxmy", + "organization_id": "org-123", + "result_files": [ + "file-abc123" + ], + "status": "succeeded", + "validation_file": null, + "training_file": "file-abc123", + "hyperparameters": { + "n_epochs": 4, + "batch_size": 1, + "learning_rate_multiplier": 1.0 + }, + "trained_tokens": 5768, + "integrations": [], + "seed": 0, + "estimated_finish": 0 + } + /fine_tuning/jobs/{fine_tuning_job_id}/events: + get: + operationId: listFineTuningEvents + tags: + - Fine-tuning + summary: | + Get status updates for a fine-tuning job. + parameters: + - in: path + name: fine_tuning_job_id + required: true + schema: + type: string + example: ft-AF1WoRqd3aJAHsqc9NY7iL8F + description: | + The ID of the fine-tuning job to get events for. + - name: after + in: query + description: Identifier for the last event from the previous pagination request. + required: false + schema: + type: string + - name: limit + in: query + description: Number of events to retrieve. + required: false + schema: + type: integer + default: 20 + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListFineTuningJobEventsResponse" + x-oaiMeta: + name: List fine-tuning events + group: fine-tuning + returns: A list of fine-tuning event objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/events \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.fine_tuning.jobs.list_events( + fine_tuning_job_id="ftjob-abc123", + limit=2 + ) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const list = await openai.fineTuning.list_events(id="ftjob-abc123", limit=2); + + for await (const fineTune of list) { + console.log(fineTune); + } + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "object": "fine_tuning.job.event", + "id": "ft-event-ddTJfwuMVpfLXseO0Am0Gqjm", + "created_at": 1721764800, + "level": "info", + "message": "Fine tuning job successfully completed", + "data": null, + "type": "message" + }, + { + "object": "fine_tuning.job.event", + "id": "ft-event-tyiGuB72evQncpH87xe505Sv", + "created_at": 1721764800, + "level": "info", + "message": "New fine-tuned model created: ft:gpt-4o-mini:openai::7p4lURel", + "data": null, + "type": "message" + } + ], + "has_more": true + } + /fine_tuning/jobs/{fine_tuning_job_id}/cancel: + post: + operationId: cancelFineTuningJob + tags: + - Fine-tuning + summary: | + Immediately cancel a fine-tune job. + parameters: + - in: path + name: fine_tuning_job_id + required: true + schema: + type: string + example: ft-AF1WoRqd3aJAHsqc9NY7iL8F + description: | + The ID of the fine-tuning job to cancel. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/FineTuningJob" + x-oaiMeta: + name: Cancel fine-tuning + group: fine-tuning + returns: The cancelled [fine-tuning](/docs/api-reference/fine-tuning/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/cancel \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.fine_tuning.jobs.cancel("ftjob-abc123") + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const fineTune = await openai.fineTuning.jobs.cancel("ftjob-abc123"); + + console.log(fineTune); + } + main(); + response: | + { + "object": "fine_tuning.job", + "id": "ftjob-abc123", + "model": "gpt-4o-mini-2024-07-18", + "created_at": 1721764800, + "fine_tuned_model": null, + "organization_id": "org-123", + "result_files": [], + "hyperparameters": { + "n_epochs": "auto" + }, + "status": "cancelled", + "validation_file": "file-abc123", + "training_file": "file-abc123" + } + /fine_tuning/jobs/{fine_tuning_job_id}/checkpoints: + get: + operationId: listFineTuningJobCheckpoints + tags: + - Fine-tuning + summary: | + List checkpoints for a fine-tuning job. + parameters: + - in: path + name: fine_tuning_job_id + required: true + schema: + type: string + example: ft-AF1WoRqd3aJAHsqc9NY7iL8F + description: | + The ID of the fine-tuning job to get checkpoints for. + - name: after + in: query + description: Identifier for the last checkpoint ID from the previous pagination request. + required: false + schema: + type: string + - name: limit + in: query + description: Number of checkpoints to retrieve. + required: false + schema: + type: integer + default: 10 + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListFineTuningJobCheckpointsResponse" + x-oaiMeta: + name: List fine-tuning checkpoints + group: fine-tuning + returns: A list of fine-tuning [checkpoint objects](/docs/api-reference/fine-tuning/checkpoint-object) for a fine-tuning job. + examples: + request: + curl: | + curl https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/checkpoints \ + -H "Authorization: Bearer $OPENAI_API_KEY" + response: | + { + "object": "list" + "data": [ + { + "object": "fine_tuning.job.checkpoint", + "id": "ftckpt_zc4Q7MP6XxulcVzj4MZdwsAB", + "created_at": 1721764867, + "fine_tuned_model_checkpoint": "ft:gpt-4o-mini-2024-07-18:my-org:custom-suffix:96olL566:ckpt-step-2000", + "metrics": { + "full_valid_loss": 0.134, + "full_valid_mean_token_accuracy": 0.874 + }, + "fine_tuning_job_id": "ftjob-abc123", + "step_number": 2000, + }, + { + "object": "fine_tuning.job.checkpoint", + "id": "ftckpt_enQCFmOTGj3syEpYVhBRLTSy", + "created_at": 1721764800, + "fine_tuned_model_checkpoint": "ft:gpt-4o-mini-2024-07-18:my-org:custom-suffix:7q8mpxmy:ckpt-step-1000", + "metrics": { + "full_valid_loss": 0.167, + "full_valid_mean_token_accuracy": 0.781 + }, + "fine_tuning_job_id": "ftjob-abc123", + "step_number": 1000, + }, + ], + "first_id": "ftckpt_zc4Q7MP6XxulcVzj4MZdwsAB", + "last_id": "ftckpt_enQCFmOTGj3syEpYVhBRLTSy", + "has_more": true + } + + /models: + get: + operationId: listModels + tags: + - Models + summary: Lists the currently available models, and provides basic information about each one such as the owner and availability. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListModelsResponse" + x-oaiMeta: + name: List models + group: models + returns: A list of [model](/docs/api-reference/models/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/models \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.models.list() + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const list = await openai.models.list(); + + for await (const model of list) { + console.log(model); + } + } + main(); + response: | + { + "object": "list", + "data": [ + { + "id": "model-id-0", + "object": "model", + "created": 1686935002, + "owned_by": "organization-owner" + }, + { + "id": "model-id-1", + "object": "model", + "created": 1686935002, + "owned_by": "organization-owner", + }, + { + "id": "model-id-2", + "object": "model", + "created": 1686935002, + "owned_by": "openai" + }, + ], + "object": "list" + } + /models/{model}: + get: + operationId: retrieveModel + tags: + - Models + summary: Retrieves a model instance, providing basic information about the model such as the owner and permissioning. + parameters: + - in: path + name: model + required: true + schema: + type: string + # ideally this will be an actual ID, so this will always work from browser + example: gpt-4o-mini + description: The ID of the model to use for this request + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/Model" + x-oaiMeta: + name: Retrieve model + group: models + returns: The [model](/docs/api-reference/models/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/models/VAR_model_id \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.models.retrieve("VAR_model_id") + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const model = await openai.models.retrieve("VAR_model_id"); + + console.log(model); + } + + main(); + response: &retrieve_model_response | + { + "id": "VAR_model_id", + "object": "model", + "created": 1686935002, + "owned_by": "openai" + } + delete: + operationId: deleteModel + tags: + - Models + summary: Delete a fine-tuned model. You must have the Owner role in your organization to delete a model. + parameters: + - in: path + name: model + required: true + schema: + type: string + example: ft:gpt-4o-mini:acemeco:suffix:abc123 + description: The model to delete + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/DeleteModelResponse" + x-oaiMeta: + name: Delete a fine-tuned model + group: models + returns: Deletion status. + examples: + request: + curl: | + curl https://api.openai.com/v1/models/ft:gpt-4o-mini:acemeco:suffix:abc123 \ + -X DELETE \ + -H "Authorization: Bearer $OPENAI_API_KEY" + python: | + from openai import OpenAI + client = OpenAI() + + client.models.delete("ft:gpt-4o-mini:acemeco:suffix:abc123") + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const model = await openai.models.del("ft:gpt-4o-mini:acemeco:suffix:abc123"); + + console.log(model); + } + main(); + response: | + { + "id": "ft:gpt-4o-mini:acemeco:suffix:abc123", + "object": "model", + "deleted": true + } + + /moderations: + post: + operationId: createModeration + tags: + - Moderations + summary: | + Classifies if text and/or image inputs are potentially harmful. Learn + more in the [moderation guide](/docs/guides/moderation). + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateModerationRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/CreateModerationResponse" + x-oaiMeta: + name: Create moderation + group: moderations + returns: A [moderation](/docs/api-reference/moderations/object) object. + examples: + - title: Single string + request: + curl: | + curl https://api.openai.com/v1/moderations \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "input": "I want to kill them." + }' + python: | + from openai import OpenAI + client = OpenAI() + + moderation = client.moderations.create(input="I want to kill them.") + print(moderation) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const moderation = await openai.moderations.create({ input: "I want to kill them." }); + + console.log(moderation); + } + main(); + response: &moderation_example | + { + "id": "modr-AB8CjOTu2jiq12hp1AQPfeqFWaORR", + "model": "text-moderation-007", + "results": [ + { + "flagged": true, + "categories": { + "sexual": false, + "hate": false, + "harassment": true, + "self-harm": false, + "sexual/minors": false, + "hate/threatening": false, + "violence/graphic": false, + "self-harm/intent": false, + "self-harm/instructions": false, + "harassment/threatening": true, + "violence": true + }, + "category_scores": { + "sexual": 0.000011726012417057063, + "hate": 0.22706663608551025, + "harassment": 0.5215635299682617, + "self-harm": 2.227119921371923e-6, + "sexual/minors": 7.107352217872176e-8, + "hate/threatening": 0.023547329008579254, + "violence/graphic": 0.00003391829886822961, + "self-harm/intent": 1.646940972932498e-6, + "self-harm/instructions": 1.1198755256458526e-9, + "harassment/threatening": 0.5694745779037476, + "violence": 0.9971134662628174 + } + } + ] + } + - title: Image and text + request: + curl: | + curl https://api.openai.com/v1/moderations \ + -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -d '{ + "model": "omni-moderation-latest", + "input": [ + { "type": "text", "text": "...text to classify goes here..." }, + { + "type": "image_url", + "image_url": { + "url": "https://example.com/image.png" + } + } + ] + }' + python: | + from openai import OpenAI + client = OpenAI() + + response = client.moderations.create( + model="omni-moderation-latest", + input=[ + {"type": "text", "text": "...text to classify goes here..."}, + { + "type": "image_url", + "image_url": { + "url": "https://example.com/image.png", + # can also use base64 encoded image URLs + # "url": "data:image/jpeg;base64,abcdefg..." + } + }, + ], + ) + + print(response) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + const moderation = await openai.moderations.create({ + model: "omni-moderation-latest", + input: [ + { type: "text", text: "...text to classify goes here..." }, + { + type: "image_url", + image_url: { + url: "https://example.com/image.png" + // can also use base64 encoded image URLs + // url: "data:image/jpeg;base64,abcdefg..." + } + } + ], + }); + + console.log(moderation); + response: &moderation_example | + { + "id": "modr-0d9740456c391e43c445bf0f010940c7", + "model": "omni-moderation-latest", + "results": [ + { + "flagged": true, + "categories": { + "harassment": true, + "harassment/threatening": true, + "sexual": false, + "hate": false, + "hate/threatening": false, + "illicit": false, + "illicit/violent": false, + "self-harm/intent": false, + "self-harm/instructions": false, + "self-harm": false, + "sexual/minors": false, + "violence": true, + "violence/graphic": true + }, + "category_scores": { + "harassment": 0.8189693396524255, + "harassment/threatening": 0.804985420696006, + "sexual": 1.573112165348997e-6, + "hate": 0.007562942636942845, + "hate/threatening": 0.004208854591835476, + "illicit": 0.030535955153511665, + "illicit/violent": 0.008925306722380033, + "self-harm/intent": 0.00023023930975076432, + "self-harm/instructions": 0.0002293869201073356, + "self-harm": 0.012598046106750154, + "sexual/minors": 2.212566909570261e-8, + "violence": 0.9999992735124786, + "violence/graphic": 0.843064871157054 + }, + "category_applied_input_types": { + "harassment": [ + "text" + ], + "harassment/threatening": [ + "text" + ], + "sexual": [ + "text", + "image" + ], + "hate": [ + "text" + ], + "hate/threatening": [ + "text" + ], + "illicit": [ + "text" + ], + "illicit/violent": [ + "text" + ], + "self-harm/intent": [ + "text", + "image" + ], + "self-harm/instructions": [ + "text", + "image" + ], + "self-harm": [ + "text", + "image" + ], + "sexual/minors": [ + "text" + ], + "violence": [ + "text", + "image" + ], + "violence/graphic": [ + "text", + "image" + ] + } + } + ] + } + + /assistants: + get: + operationId: listAssistants + tags: + - Assistants + summary: Returns a list of assistants. + parameters: + - name: limit + in: query + description: &pagination_limit_param_description | + A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20. + required: false + schema: + type: integer + default: 20 + - name: order + in: query + description: &pagination_order_param_description | + Sort order by the `created_at` timestamp of the objects. `asc` for ascending order and `desc` for descending order. + schema: + type: string + default: desc + enum: ["asc", "desc"] + - name: after + in: query + description: &pagination_after_param_description | + A cursor for use in pagination. `after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list. + schema: + type: string + - name: before + in: query + description: &pagination_before_param_description | + A cursor for use in pagination. `before` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list. + schema: + type: string + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListAssistantsResponse" + x-oaiMeta: + name: List assistants + group: assistants + beta: true + returns: A list of [assistant](/docs/api-reference/assistants/object) objects. + examples: + request: + curl: | + curl "https://api.openai.com/v1/assistants?order=desc&limit=20" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + my_assistants = client.beta.assistants.list( + order="desc", + limit="20", + ) + print(my_assistants.data) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const myAssistants = await openai.beta.assistants.list({ + order: "desc", + limit: "20", + }); + + console.log(myAssistants.data); + } + + main(); + response: &list_assistants_example | + { + "object": "list", + "data": [ + { + "id": "asst_abc123", + "object": "assistant", + "created_at": 1698982736, + "name": "Coding Tutor", + "description": null, + "model": "gpt-4o", + "instructions": "You are a helpful assistant designed to make me better at coding!", + "tools": [], + "tool_resources": {}, + "metadata": {}, + "top_p": 1.0, + "temperature": 1.0, + "response_format": "auto" + }, + { + "id": "asst_abc456", + "object": "assistant", + "created_at": 1698982718, + "name": "My Assistant", + "description": null, + "model": "gpt-4o", + "instructions": "You are a helpful assistant designed to make me better at coding!", + "tools": [], + "tool_resources": {}, + "metadata": {}, + "top_p": 1.0, + "temperature": 1.0, + "response_format": "auto" + }, + { + "id": "asst_abc789", + "object": "assistant", + "created_at": 1698982643, + "name": null, + "description": null, + "model": "gpt-4o", + "instructions": null, + "tools": [], + "tool_resources": {}, + "metadata": {}, + "top_p": 1.0, + "temperature": 1.0, + "response_format": "auto" + } + ], + "first_id": "asst_abc123", + "last_id": "asst_abc789", + "has_more": false + } + post: + operationId: createAssistant + tags: + - Assistants + summary: Create an assistant with a model and instructions. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateAssistantRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/AssistantObject" + x-oaiMeta: + name: Create assistant + group: assistants + beta: true + returns: An [assistant](/docs/api-reference/assistants/object) object. + examples: + - title: Code Interpreter + request: + curl: | + curl "https://api.openai.com/v1/assistants" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", + "name": "Math Tutor", + "tools": [{"type": "code_interpreter"}], + "model": "gpt-4o" + }' + + python: | + from openai import OpenAI + client = OpenAI() + + my_assistant = client.beta.assistants.create( + instructions="You are a personal math tutor. When asked a question, write and run Python code to answer the question.", + name="Math Tutor", + tools=[{"type": "code_interpreter"}], + model="gpt-4o", + ) + print(my_assistant) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const myAssistant = await openai.beta.assistants.create({ + instructions: + "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", + name: "Math Tutor", + tools: [{ type: "code_interpreter" }], + model: "gpt-4o", + }); + + console.log(myAssistant); + } + + main(); + response: &create_assistants_example | + { + "id": "asst_abc123", + "object": "assistant", + "created_at": 1698984975, + "name": "Math Tutor", + "description": null, + "model": "gpt-4o", + "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", + "tools": [ + { + "type": "code_interpreter" + } + ], + "metadata": {}, + "top_p": 1.0, + "temperature": 1.0, + "response_format": "auto" + } + - title: Files + request: + curl: | + curl https://api.openai.com/v1/assistants \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies.", + "tools": [{"type": "file_search"}], + "tool_resources": {"file_search": {"vector_store_ids": ["vs_123"]}}, + "model": "gpt-4o" + }' + python: | + from openai import OpenAI + client = OpenAI() + + my_assistant = client.beta.assistants.create( + instructions="You are an HR bot, and you have access to files to answer employee questions about company policies.", + name="HR Helper", + tools=[{"type": "file_search"}], + tool_resources={"file_search": {"vector_store_ids": ["vs_123"]}}, + model="gpt-4o" + ) + print(my_assistant) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const myAssistant = await openai.beta.assistants.create({ + instructions: + "You are an HR bot, and you have access to files to answer employee questions about company policies.", + name: "HR Helper", + tools: [{ type: "file_search" }], + tool_resources: { + file_search: { + vector_store_ids: ["vs_123"] + } + }, + model: "gpt-4o" + }); + + console.log(myAssistant); + } + + main(); + response: | + { + "id": "asst_abc123", + "object": "assistant", + "created_at": 1699009403, + "name": "HR Helper", + "description": null, + "model": "gpt-4o", + "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies.", + "tools": [ + { + "type": "file_search" + } + ], + "tool_resources": { + "file_search": { + "vector_store_ids": ["vs_123"] + } + }, + "metadata": {}, + "top_p": 1.0, + "temperature": 1.0, + "response_format": "auto" + } + + /assistants/{assistant_id}: + get: + operationId: getAssistant + tags: + - Assistants + summary: Retrieves an assistant. + parameters: + - in: path + name: assistant_id + required: true + schema: + type: string + description: The ID of the assistant to retrieve. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/AssistantObject" + x-oaiMeta: + name: Retrieve assistant + group: assistants + beta: true + returns: The [assistant](/docs/api-reference/assistants/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/assistants/asst_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + my_assistant = client.beta.assistants.retrieve("asst_abc123") + print(my_assistant) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const myAssistant = await openai.beta.assistants.retrieve( + "asst_abc123" + ); + + console.log(myAssistant); + } + + main(); + response: | + { + "id": "asst_abc123", + "object": "assistant", + "created_at": 1699009709, + "name": "HR Helper", + "description": null, + "model": "gpt-4o", + "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies.", + "tools": [ + { + "type": "file_search" + } + ], + "metadata": {}, + "top_p": 1.0, + "temperature": 1.0, + "response_format": "auto" + } + post: + operationId: modifyAssistant + tags: + - Assistants + summary: Modifies an assistant. + parameters: + - in: path + name: assistant_id + required: true + schema: + type: string + description: The ID of the assistant to modify. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/ModifyAssistantRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/AssistantObject" + x-oaiMeta: + name: Modify assistant + group: assistants + beta: true + returns: The modified [assistant](/docs/api-reference/assistants/object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/assistants/asst_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.", + "tools": [{"type": "file_search"}], + "model": "gpt-4o" + }' + python: | + from openai import OpenAI + client = OpenAI() + + my_updated_assistant = client.beta.assistants.update( + "asst_abc123", + instructions="You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.", + name="HR Helper", + tools=[{"type": "file_search"}], + model="gpt-4o" + ) + + print(my_updated_assistant) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const myUpdatedAssistant = await openai.beta.assistants.update( + "asst_abc123", + { + instructions: + "You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.", + name: "HR Helper", + tools: [{ type: "file_search" }], + model: "gpt-4o" + } + ); + + console.log(myUpdatedAssistant); + } + + main(); + response: | + { + "id": "asst_123", + "object": "assistant", + "created_at": 1699009709, + "name": "HR Helper", + "description": null, + "model": "gpt-4o", + "instructions": "You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.", + "tools": [ + { + "type": "file_search" + } + ], + "tool_resources": { + "file_search": { + "vector_store_ids": [] + } + }, + "metadata": {}, + "top_p": 1.0, + "temperature": 1.0, + "response_format": "auto" + } + delete: + operationId: deleteAssistant + tags: + - Assistants + summary: Delete an assistant. + parameters: + - in: path + name: assistant_id + required: true + schema: + type: string + description: The ID of the assistant to delete. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/DeleteAssistantResponse" + x-oaiMeta: + name: Delete assistant + group: assistants + beta: true + returns: Deletion status + examples: + request: + curl: | + curl https://api.openai.com/v1/assistants/asst_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -X DELETE + python: | + from openai import OpenAI + client = OpenAI() + + response = client.beta.assistants.delete("asst_abc123") + print(response) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const response = await openai.beta.assistants.del("asst_abc123"); + + console.log(response); + } + main(); + response: | + { + "id": "asst_abc123", + "object": "assistant.deleted", + "deleted": true + } + + /threads: + post: + operationId: createThread + tags: + - Assistants + summary: Create a thread. + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/CreateThreadRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ThreadObject" + x-oaiMeta: + name: Create thread + group: threads + beta: true + returns: A [thread](/docs/api-reference/threads) object. + examples: + - title: Empty + request: + curl: | + curl https://api.openai.com/v1/threads \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '' + python: | + from openai import OpenAI + client = OpenAI() + + empty_thread = client.beta.threads.create() + print(empty_thread) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const emptyThread = await openai.beta.threads.create(); + + console.log(emptyThread); + } + + main(); + response: | + { + "id": "thread_abc123", + "object": "thread", + "created_at": 1699012949, + "metadata": {}, + "tool_resources": {} + } + - title: Messages + request: + curl: | + curl https://api.openai.com/v1/threads \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "messages": [{ + "role": "user", + "content": "Hello, what is AI?" + }, { + "role": "user", + "content": "How does AI work? Explain it in simple terms." + }] + }' + python: | + from openai import OpenAI + client = OpenAI() + + message_thread = client.beta.threads.create( + messages=[ + { + "role": "user", + "content": "Hello, what is AI?" + }, + { + "role": "user", + "content": "How does AI work? Explain it in simple terms." + }, + ] + ) + + print(message_thread) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const messageThread = await openai.beta.threads.create({ + messages: [ + { + role: "user", + content: "Hello, what is AI?" + }, + { + role: "user", + content: "How does AI work? Explain it in simple terms.", + }, + ], + }); + + console.log(messageThread); + } + + main(); + response: | + { + "id": "thread_abc123", + "object": "thread", + "created_at": 1699014083, + "metadata": {}, + "tool_resources": {} + } + + /threads/{thread_id}: + get: + operationId: getThread + tags: + - Assistants + summary: Retrieves a thread. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the thread to retrieve. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ThreadObject" + x-oaiMeta: + name: Retrieve thread + group: threads + beta: true + returns: The [thread](/docs/api-reference/threads/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + my_thread = client.beta.threads.retrieve("thread_abc123") + print(my_thread) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const myThread = await openai.beta.threads.retrieve( + "thread_abc123" + ); + + console.log(myThread); + } + + main(); + response: | + { + "id": "thread_abc123", + "object": "thread", + "created_at": 1699014083, + "metadata": {}, + "tool_resources": { + "code_interpreter": { + "file_ids": [] + } + } + } + post: + operationId: modifyThread + tags: + - Assistants + summary: Modifies a thread. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the thread to modify. Only the `metadata` can be modified. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/ModifyThreadRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ThreadObject" + x-oaiMeta: + name: Modify thread + group: threads + beta: true + returns: The modified [thread](/docs/api-reference/threads/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "metadata": { + "modified": "true", + "user": "abc123" + } + }' + python: | + from openai import OpenAI + client = OpenAI() + + my_updated_thread = client.beta.threads.update( + "thread_abc123", + metadata={ + "modified": "true", + "user": "abc123" + } + ) + print(my_updated_thread) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const updatedThread = await openai.beta.threads.update( + "thread_abc123", + { + metadata: { modified: "true", user: "abc123" }, + } + ); + + console.log(updatedThread); + } + + main(); + response: | + { + "id": "thread_abc123", + "object": "thread", + "created_at": 1699014083, + "metadata": { + "modified": "true", + "user": "abc123" + }, + "tool_resources": {} + } + delete: + operationId: deleteThread + tags: + - Assistants + summary: Delete a thread. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the thread to delete. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/DeleteThreadResponse" + x-oaiMeta: + name: Delete thread + group: threads + beta: true + returns: Deletion status + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -X DELETE + python: | + from openai import OpenAI + client = OpenAI() + + response = client.beta.threads.delete("thread_abc123") + print(response) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const response = await openai.beta.threads.del("thread_abc123"); + + console.log(response); + } + main(); + response: | + { + "id": "thread_abc123", + "object": "thread.deleted", + "deleted": true + } + + /threads/{thread_id}/messages: + get: + operationId: listMessages + tags: + - Assistants + summary: Returns a list of messages for a given thread. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the [thread](/docs/api-reference/threads) the messages belong to. + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: order + in: query + description: *pagination_order_param_description + schema: + type: string + default: desc + enum: ["asc", "desc"] + - name: after + in: query + description: *pagination_after_param_description + schema: + type: string + - name: before + in: query + description: *pagination_before_param_description + schema: + type: string + - name: run_id + in: query + description: | + Filter messages by the run ID that generated them. + schema: + type: string + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListMessagesResponse" + x-oaiMeta: + name: List messages + group: threads + beta: true + returns: A list of [message](/docs/api-reference/messages) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/messages \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + thread_messages = client.beta.threads.messages.list("thread_abc123") + print(thread_messages.data) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const threadMessages = await openai.beta.threads.messages.list( + "thread_abc123" + ); + + console.log(threadMessages.data); + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "id": "msg_abc123", + "object": "thread.message", + "created_at": 1699016383, + "assistant_id": null, + "thread_id": "thread_abc123", + "run_id": null, + "role": "user", + "content": [ + { + "type": "text", + "text": { + "value": "How does AI work? Explain it in simple terms.", + "annotations": [] + } + } + ], + "attachments": [], + "metadata": {} + }, + { + "id": "msg_abc456", + "object": "thread.message", + "created_at": 1699016383, + "assistant_id": null, + "thread_id": "thread_abc123", + "run_id": null, + "role": "user", + "content": [ + { + "type": "text", + "text": { + "value": "Hello, what is AI?", + "annotations": [] + } + } + ], + "attachments": [], + "metadata": {} + } + ], + "first_id": "msg_abc123", + "last_id": "msg_abc456", + "has_more": false + } + post: + operationId: createMessage + tags: + - Assistants + summary: Create a message. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the [thread](/docs/api-reference/threads) to create a message for. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateMessageRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/MessageObject" + x-oaiMeta: + name: Create message + group: threads + beta: true + returns: A [message](/docs/api-reference/messages/object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/messages \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "role": "user", + "content": "How does AI work? Explain it in simple terms." + }' + python: | + from openai import OpenAI + client = OpenAI() + + thread_message = client.beta.threads.messages.create( + "thread_abc123", + role="user", + content="How does AI work? Explain it in simple terms.", + ) + print(thread_message) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const threadMessages = await openai.beta.threads.messages.create( + "thread_abc123", + { role: "user", content: "How does AI work? Explain it in simple terms." } + ); + + console.log(threadMessages); + } + + main(); + response: | + { + "id": "msg_abc123", + "object": "thread.message", + "created_at": 1713226573, + "assistant_id": null, + "thread_id": "thread_abc123", + "run_id": null, + "role": "user", + "content": [ + { + "type": "text", + "text": { + "value": "How does AI work? Explain it in simple terms.", + "annotations": [] + } + } + ], + "attachments": [], + "metadata": {} + } + + /threads/{thread_id}/messages/{message_id}: + get: + operationId: getMessage + tags: + - Assistants + summary: Retrieve a message. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the [thread](/docs/api-reference/threads) to which this message belongs. + - in: path + name: message_id + required: true + schema: + type: string + description: The ID of the message to retrieve. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/MessageObject" + x-oaiMeta: + name: Retrieve message + group: threads + beta: true + returns: The [message](/docs/api-reference/messages/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + message = client.beta.threads.messages.retrieve( + message_id="msg_abc123", + thread_id="thread_abc123", + ) + print(message) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const message = await openai.beta.threads.messages.retrieve( + "thread_abc123", + "msg_abc123" + ); + + console.log(message); + } + + main(); + response: | + { + "id": "msg_abc123", + "object": "thread.message", + "created_at": 1699017614, + "assistant_id": null, + "thread_id": "thread_abc123", + "run_id": null, + "role": "user", + "content": [ + { + "type": "text", + "text": { + "value": "How does AI work? Explain it in simple terms.", + "annotations": [] + } + } + ], + "attachments": [], + "metadata": {} + } + post: + operationId: modifyMessage + tags: + - Assistants + summary: Modifies a message. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the thread to which this message belongs. + - in: path + name: message_id + required: true + schema: + type: string + description: The ID of the message to modify. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/ModifyMessageRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/MessageObject" + x-oaiMeta: + name: Modify message + group: threads + beta: true + returns: The modified [message](/docs/api-reference/messages/object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "metadata": { + "modified": "true", + "user": "abc123" + } + }' + python: | + from openai import OpenAI + client = OpenAI() + + message = client.beta.threads.messages.update( + message_id="msg_abc12", + thread_id="thread_abc123", + metadata={ + "modified": "true", + "user": "abc123", + }, + ) + print(message) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const message = await openai.beta.threads.messages.update( + "thread_abc123", + "msg_abc123", + { + metadata: { + modified: "true", + user: "abc123", + }, + } + }' + response: | + { + "id": "msg_abc123", + "object": "thread.message", + "created_at": 1699017614, + "assistant_id": null, + "thread_id": "thread_abc123", + "run_id": null, + "role": "user", + "content": [ + { + "type": "text", + "text": { + "value": "How does AI work? Explain it in simple terms.", + "annotations": [] + } + } + ], + "file_ids": [], + "metadata": { + "modified": "true", + "user": "abc123" + } + } + delete: + operationId: deleteMessage + tags: + - Assistants + summary: Deletes a message. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the thread to which this message belongs. + - in: path + name: message_id + required: true + schema: + type: string + description: The ID of the message to delete. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/DeleteMessageResponse" + x-oaiMeta: + name: Delete message + group: threads + beta: true + returns: Deletion status + examples: + request: + curl: | + curl -X DELETE https://api.openai.com/v1/threads/thread_abc123/messages/msg_abc123 \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + deleted_message = client.beta.threads.messages.delete( + message_id="msg_abc12", + thread_id="thread_abc123", + ) + print(deleted_message) + node.js: |- + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const deletedMessage = await openai.beta.threads.messages.del( + "thread_abc123", + "msg_abc123" + ); + + console.log(deletedMessage); + } + response: | + { + "id": "msg_abc123", + "object": "thread.message.deleted", + "deleted": true + } + + /threads/runs: + post: + operationId: createThreadAndRun + tags: + - Assistants + summary: Create a thread and run it in one request. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateThreadAndRunRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/RunObject" + x-oaiMeta: + name: Create thread and run + group: threads + beta: true + returns: A [run](/docs/api-reference/runs/object) object. + examples: + - title: Default + request: + curl: | + curl https://api.openai.com/v1/threads/runs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "assistant_id": "asst_abc123", + "thread": { + "messages": [ + {"role": "user", "content": "Explain deep learning to a 5 year old."} + ] + } + }' + python: | + from openai import OpenAI + client = OpenAI() + + run = client.beta.threads.create_and_run( + assistant_id="asst_abc123", + thread={ + "messages": [ + {"role": "user", "content": "Explain deep learning to a 5 year old."} + ] + } + ) + + print(run) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const run = await openai.beta.threads.createAndRun({ + assistant_id: "asst_abc123", + thread: { + messages: [ + { role: "user", content: "Explain deep learning to a 5 year old." }, + ], + }, + }); + + console.log(run); + } + + main(); + response: | + { + "id": "run_abc123", + "object": "thread.run", + "created_at": 1699076792, + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "status": "queued", + "started_at": null, + "expires_at": 1699077392, + "cancelled_at": null, + "failed_at": null, + "completed_at": null, + "required_action": null, + "last_error": null, + "model": "gpt-4o", + "instructions": "You are a helpful assistant.", + "tools": [], + "tool_resources": {}, + "metadata": {}, + "temperature": 1.0, + "top_p": 1.0, + "max_completion_tokens": null, + "max_prompt_tokens": null, + "truncation_strategy": { + "type": "auto", + "last_messages": null + }, + "incomplete_details": null, + "usage": null, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + } + + - title: Streaming + request: + curl: | + curl https://api.openai.com/v1/threads/runs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "assistant_id": "asst_123", + "thread": { + "messages": [ + {"role": "user", "content": "Hello"} + ] + }, + "stream": true + }' + python: | + from openai import OpenAI + client = OpenAI() + + stream = client.beta.threads.create_and_run( + assistant_id="asst_123", + thread={ + "messages": [ + {"role": "user", "content": "Hello"} + ] + }, + stream=True + ) + + for event in stream: + print(event) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const stream = await openai.beta.threads.createAndRun({ + assistant_id: "asst_123", + thread: { + messages: [ + { role: "user", content: "Hello" }, + ], + }, + stream: true + }); + + for await (const event of stream) { + console.log(event); + } + } + + main(); + response: | + event: thread.created + data: {"id":"thread_123","object":"thread","created_at":1710348075,"metadata":{}} + + event: thread.run.created + data: {"id":"run_123","object":"thread.run","created_at":1710348075,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710348675,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"tool_resources":{},"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true} + + event: thread.run.queued + data: {"id":"run_123","object":"thread.run","created_at":1710348075,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710348675,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"tool_resources":{},"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true} + + event: thread.run.in_progress + data: {"id":"run_123","object":"thread.run","created_at":1710348075,"assistant_id":"asst_123","thread_id":"thread_123","status":"in_progress","started_at":null,"expires_at":1710348675,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"tool_resources":{},"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true} + + event: thread.run.step.created + data: {"id":"step_001","object":"thread.run.step","created_at":1710348076,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710348675,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":null} + + event: thread.run.step.in_progress + data: {"id":"step_001","object":"thread.run.step","created_at":1710348076,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710348675,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":null} + + event: thread.message.created + data: {"id":"msg_001","object":"thread.message","created_at":1710348076,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"in_progress","incomplete_details":null,"incomplete_at":null,"completed_at":null,"role":"assistant","content":[], "metadata":{}} + + event: thread.message.in_progress + data: {"id":"msg_001","object":"thread.message","created_at":1710348076,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"in_progress","incomplete_details":null,"incomplete_at":null,"completed_at":null,"role":"assistant","content":[], "metadata":{}} + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"Hello","annotations":[]}}]}} + + ... + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":" today"}}]}} + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"?"}}]}} + + event: thread.message.completed + data: {"id":"msg_001","object":"thread.message","created_at":1710348076,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"completed","incomplete_details":null,"incomplete_at":null,"completed_at":1710348077,"role":"assistant","content":[{"type":"text","text":{"value":"Hello! How can I assist you today?","annotations":[]}}], "metadata":{}} + + event: thread.run.step.completed + data: {"id":"step_001","object":"thread.run.step","created_at":1710348076,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"completed","cancelled_at":null,"completed_at":1710348077,"expires_at":1710348675,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":{"prompt_tokens":20,"completion_tokens":11,"total_tokens":31}} + + event: thread.run.completed + {"id":"run_123","object":"thread.run","created_at":1710348076,"assistant_id":"asst_123","thread_id":"thread_123","status":"completed","started_at":1713226836,"expires_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1713226837,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":{"prompt_tokens":345,"completion_tokens":11,"total_tokens":356},"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true} + + event: done + data: [DONE] + + - title: Streaming with Functions + request: + curl: | + curl https://api.openai.com/v1/threads/runs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "assistant_id": "asst_abc123", + "thread": { + "messages": [ + {"role": "user", "content": "What is the weather like in San Francisco?"} + ] + }, + "tools": [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"] + } + }, + "required": ["location"] + } + } + } + ], + "stream": true + }' + python: | + from openai import OpenAI + client = OpenAI() + + tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ] + + stream = client.beta.threads.create_and_run( + thread={ + "messages": [ + {"role": "user", "content": "What is the weather like in San Francisco?"} + ] + }, + assistant_id="asst_abc123", + tools=tools, + stream=True + ) + + for event in stream: + print(event) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + const tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ]; + + async function main() { + const stream = await openai.beta.threads.createAndRun({ + assistant_id: "asst_123", + thread: { + messages: [ + { role: "user", content: "What is the weather like in San Francisco?" }, + ], + }, + tools: tools, + stream: true + }); + + for await (const event of stream) { + console.log(event); + } + } + + main(); + response: | + event: thread.created + data: {"id":"thread_123","object":"thread","created_at":1710351818,"metadata":{}} + + event: thread.run.created + data: {"id":"run_123","object":"thread.run","created_at":1710351818,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710352418,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.queued + data: {"id":"run_123","object":"thread.run","created_at":1710351818,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710352418,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.in_progress + data: {"id":"run_123","object":"thread.run","created_at":1710351818,"assistant_id":"asst_123","thread_id":"thread_123","status":"in_progress","started_at":1710351818,"expires_at":1710352418,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.step.created + data: {"id":"step_001","object":"thread.run.step","created_at":1710351819,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"tool_calls","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710352418,"failed_at":null,"last_error":null,"step_details":{"type":"tool_calls","tool_calls":[]},"usage":null} + + event: thread.run.step.in_progress + data: {"id":"step_001","object":"thread.run.step","created_at":1710351819,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"tool_calls","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710352418,"failed_at":null,"last_error":null,"step_details":{"type":"tool_calls","tool_calls":[]},"usage":null} + + event: thread.run.step.delta + data: {"id":"step_001","object":"thread.run.step.delta","delta":{"step_details":{"type":"tool_calls","tool_calls":[{"index":0,"id":"call_XXNp8YGaFrjrSjgqxtC8JJ1B","type":"function","function":{"name":"get_current_weather","arguments":"","output":null}}]}}} + + event: thread.run.step.delta + data: {"id":"step_001","object":"thread.run.step.delta","delta":{"step_details":{"type":"tool_calls","tool_calls":[{"index":0,"type":"function","function":{"arguments":"{\""}}]}}} + + event: thread.run.step.delta + data: {"id":"step_001","object":"thread.run.step.delta","delta":{"step_details":{"type":"tool_calls","tool_calls":[{"index":0,"type":"function","function":{"arguments":"location"}}]}}} + + ... + + event: thread.run.step.delta + data: {"id":"step_001","object":"thread.run.step.delta","delta":{"step_details":{"type":"tool_calls","tool_calls":[{"index":0,"type":"function","function":{"arguments":"ahrenheit"}}]}}} + + event: thread.run.step.delta + data: {"id":"step_001","object":"thread.run.step.delta","delta":{"step_details":{"type":"tool_calls","tool_calls":[{"index":0,"type":"function","function":{"arguments":"\"}"}}]}}} + + event: thread.run.requires_action + data: {"id":"run_123","object":"thread.run","created_at":1710351818,"assistant_id":"asst_123","thread_id":"thread_123","status":"requires_action","started_at":1710351818,"expires_at":1710352418,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":{"type":"submit_tool_outputs","submit_tool_outputs":{"tool_calls":[{"id":"call_XXNp8YGaFrjrSjgqxtC8JJ1B","type":"function","function":{"name":"get_current_weather","arguments":"{\"location\":\"San Francisco, CA\",\"unit\":\"fahrenheit\"}"}}]}},"last_error":null,"model":"gpt-4o","instructions":null,"tools":[{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":{"prompt_tokens":345,"completion_tokens":11,"total_tokens":356},"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: done + data: [DONE] + + /threads/{thread_id}/runs: + get: + operationId: listRuns + tags: + - Assistants + summary: Returns a list of runs belonging to a thread. + parameters: + - name: thread_id + in: path + required: true + schema: + type: string + description: The ID of the thread the run belongs to. + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: order + in: query + description: *pagination_order_param_description + schema: + type: string + default: desc + enum: ["asc", "desc"] + - name: after + in: query + description: *pagination_after_param_description + schema: + type: string + - name: before + in: query + description: *pagination_before_param_description + schema: + type: string + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListRunsResponse" + x-oaiMeta: + name: List runs + group: threads + beta: true + returns: A list of [run](/docs/api-reference/runs/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/runs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + runs = client.beta.threads.runs.list( + "thread_abc123" + ) + + print(runs) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const runs = await openai.beta.threads.runs.list( + "thread_abc123" + ); + + console.log(runs); + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "id": "run_abc123", + "object": "thread.run", + "created_at": 1699075072, + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "status": "completed", + "started_at": 1699075072, + "expires_at": null, + "cancelled_at": null, + "failed_at": null, + "completed_at": 1699075073, + "last_error": null, + "model": "gpt-4o", + "instructions": null, + "incomplete_details": null, + "tools": [ + { + "type": "code_interpreter" + } + ], + "tool_resources": { + "code_interpreter": { + "file_ids": [ + "file-abc123", + "file-abc456" + ] + } + }, + "metadata": {}, + "usage": { + "prompt_tokens": 123, + "completion_tokens": 456, + "total_tokens": 579 + }, + "temperature": 1.0, + "top_p": 1.0, + "max_prompt_tokens": 1000, + "max_completion_tokens": 1000, + "truncation_strategy": { + "type": "auto", + "last_messages": null + }, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + }, + { + "id": "run_abc456", + "object": "thread.run", + "created_at": 1699063290, + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "status": "completed", + "started_at": 1699063290, + "expires_at": null, + "cancelled_at": null, + "failed_at": null, + "completed_at": 1699063291, + "last_error": null, + "model": "gpt-4o", + "instructions": null, + "incomplete_details": null, + "tools": [ + { + "type": "code_interpreter" + } + ], + "tool_resources": { + "code_interpreter": { + "file_ids": [ + "file-abc123", + "file-abc456" + ] + } + }, + "metadata": {}, + "usage": { + "prompt_tokens": 123, + "completion_tokens": 456, + "total_tokens": 579 + }, + "temperature": 1.0, + "top_p": 1.0, + "max_prompt_tokens": 1000, + "max_completion_tokens": 1000, + "truncation_strategy": { + "type": "auto", + "last_messages": null + }, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + } + ], + "first_id": "run_abc123", + "last_id": "run_abc456", + "has_more": false + } + post: + operationId: createRun + tags: + - Assistants + summary: Create a run. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the thread to run. + - name: include[] + in: query + description: &include_param_description | + A list of additional fields to include in the response. Currently the only supported value is `step_details.tool_calls[*].file_search.results[*].content` to fetch the file search result content. + + See the [file search tool documentation](/docs/assistants/tools/file-search/customizing-file-search-settings) for more information. + schema: + type: array + items: + type: string + enum: ["step_details.tool_calls[*].file_search.results[*].content"] + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateRunRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/RunObject" + x-oaiMeta: + name: Create run + group: threads + beta: true + returns: A [run](/docs/api-reference/runs/object) object. + examples: + - title: Default + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/runs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "assistant_id": "asst_abc123" + }' + python: | + from openai import OpenAI + client = OpenAI() + + run = client.beta.threads.runs.create( + thread_id="thread_abc123", + assistant_id="asst_abc123" + ) + + print(run) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const run = await openai.beta.threads.runs.create( + "thread_abc123", + { assistant_id: "asst_abc123" } + ); + + console.log(run); + } + + main(); + response: &run_object_example | + { + "id": "run_abc123", + "object": "thread.run", + "created_at": 1699063290, + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "status": "queued", + "started_at": 1699063290, + "expires_at": null, + "cancelled_at": null, + "failed_at": null, + "completed_at": 1699063291, + "last_error": null, + "model": "gpt-4o", + "instructions": null, + "incomplete_details": null, + "tools": [ + { + "type": "code_interpreter" + } + ], + "metadata": {}, + "usage": null, + "temperature": 1.0, + "top_p": 1.0, + "max_prompt_tokens": 1000, + "max_completion_tokens": 1000, + "truncation_strategy": { + "type": "auto", + "last_messages": null + }, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + } + - title: Streaming + request: + curl: | + curl https://api.openai.com/v1/threads/thread_123/runs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "assistant_id": "asst_123", + "stream": true + }' + python: | + from openai import OpenAI + client = OpenAI() + + stream = client.beta.threads.runs.create( + thread_id="thread_123", + assistant_id="asst_123", + stream=True + ) + + for event in stream: + print(event) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const stream = await openai.beta.threads.runs.create( + "thread_123", + { assistant_id: "asst_123", stream: true } + ); + + for await (const event of stream) { + console.log(event); + } + } + + main(); + response: | + event: thread.run.created + data: {"id":"run_123","object":"thread.run","created_at":1710330640,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710331240,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.queued + data: {"id":"run_123","object":"thread.run","created_at":1710330640,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710331240,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.in_progress + data: {"id":"run_123","object":"thread.run","created_at":1710330640,"assistant_id":"asst_123","thread_id":"thread_123","status":"in_progress","started_at":1710330641,"expires_at":1710331240,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.step.created + data: {"id":"step_001","object":"thread.run.step","created_at":1710330641,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710331240,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":null} + + event: thread.run.step.in_progress + data: {"id":"step_001","object":"thread.run.step","created_at":1710330641,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710331240,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":null} + + event: thread.message.created + data: {"id":"msg_001","object":"thread.message","created_at":1710330641,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"in_progress","incomplete_details":null,"incomplete_at":null,"completed_at":null,"role":"assistant","content":[],"metadata":{}} + + event: thread.message.in_progress + data: {"id":"msg_001","object":"thread.message","created_at":1710330641,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"in_progress","incomplete_details":null,"incomplete_at":null,"completed_at":null,"role":"assistant","content":[],"metadata":{}} + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"Hello","annotations":[]}}]}} + + ... + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":" today"}}]}} + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"?"}}]}} + + event: thread.message.completed + data: {"id":"msg_001","object":"thread.message","created_at":1710330641,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"completed","incomplete_details":null,"incomplete_at":null,"completed_at":1710330642,"role":"assistant","content":[{"type":"text","text":{"value":"Hello! How can I assist you today?","annotations":[]}}],"metadata":{}} + + event: thread.run.step.completed + data: {"id":"step_001","object":"thread.run.step","created_at":1710330641,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"completed","cancelled_at":null,"completed_at":1710330642,"expires_at":1710331240,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":{"prompt_tokens":20,"completion_tokens":11,"total_tokens":31}} + + event: thread.run.completed + data: {"id":"run_123","object":"thread.run","created_at":1710330640,"assistant_id":"asst_123","thread_id":"thread_123","status":"completed","started_at":1710330641,"expires_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1710330642,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":{"prompt_tokens":20,"completion_tokens":11,"total_tokens":31},"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: done + data: [DONE] + + - title: Streaming with Functions + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/runs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "assistant_id": "asst_abc123", + "tools": [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"] + } + }, + "required": ["location"] + } + } + } + ], + "stream": true + }' + python: | + from openai import OpenAI + client = OpenAI() + + tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ] + + stream = client.beta.threads.runs.create( + thread_id="thread_abc123", + assistant_id="asst_abc123", + tools=tools, + stream=True + ) + + for event in stream: + print(event) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + const tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } + ]; + + async function main() { + const stream = await openai.beta.threads.runs.create( + "thread_abc123", + { + assistant_id: "asst_abc123", + tools: tools, + stream: true + } + ); + + for await (const event of stream) { + console.log(event); + } + } + + main(); + response: | + event: thread.run.created + data: {"id":"run_123","object":"thread.run","created_at":1710348075,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710348675,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.queued + data: {"id":"run_123","object":"thread.run","created_at":1710348075,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710348675,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.in_progress + data: {"id":"run_123","object":"thread.run","created_at":1710348075,"assistant_id":"asst_123","thread_id":"thread_123","status":"in_progress","started_at":1710348075,"expires_at":1710348675,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.step.created + data: {"id":"step_001","object":"thread.run.step","created_at":1710348076,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710348675,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":null} + + event: thread.run.step.in_progress + data: {"id":"step_001","object":"thread.run.step","created_at":1710348076,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710348675,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":null} + + event: thread.message.created + data: {"id":"msg_001","object":"thread.message","created_at":1710348076,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"in_progress","incomplete_details":null,"incomplete_at":null,"completed_at":null,"role":"assistant","content":[],"metadata":{}} + + event: thread.message.in_progress + data: {"id":"msg_001","object":"thread.message","created_at":1710348076,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"in_progress","incomplete_details":null,"incomplete_at":null,"completed_at":null,"role":"assistant","content":[],"metadata":{}} + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"Hello","annotations":[]}}]}} + + ... + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":" today"}}]}} + + event: thread.message.delta + data: {"id":"msg_001","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"?"}}]}} + + event: thread.message.completed + data: {"id":"msg_001","object":"thread.message","created_at":1710348076,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"completed","incomplete_details":null,"incomplete_at":null,"completed_at":1710348077,"role":"assistant","content":[{"type":"text","text":{"value":"Hello! How can I assist you today?","annotations":[]}}],"metadata":{}} + + event: thread.run.step.completed + data: {"id":"step_001","object":"thread.run.step","created_at":1710348076,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"completed","cancelled_at":null,"completed_at":1710348077,"expires_at":1710348675,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_001"}},"usage":{"prompt_tokens":20,"completion_tokens":11,"total_tokens":31}} + + event: thread.run.completed + data: {"id":"run_123","object":"thread.run","created_at":1710348075,"assistant_id":"asst_123","thread_id":"thread_123","status":"completed","started_at":1710348075,"expires_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1710348077,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":{"prompt_tokens":20,"completion_tokens":11,"total_tokens":31},"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: done + data: [DONE] + + /threads/{thread_id}/runs/{run_id}: + get: + operationId: getRun + tags: + - Assistants + summary: Retrieves a run. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the [thread](/docs/api-reference/threads) that was run. + - in: path + name: run_id + required: true + schema: + type: string + description: The ID of the run to retrieve. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/RunObject" + x-oaiMeta: + name: Retrieve run + group: threads + beta: true + returns: The [run](/docs/api-reference/runs/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + run = client.beta.threads.runs.retrieve( + thread_id="thread_abc123", + run_id="run_abc123" + ) + + print(run) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const run = await openai.beta.threads.runs.retrieve( + "thread_abc123", + "run_abc123" + ); + + console.log(run); + } + + main(); + response: | + { + "id": "run_abc123", + "object": "thread.run", + "created_at": 1699075072, + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "status": "completed", + "started_at": 1699075072, + "expires_at": null, + "cancelled_at": null, + "failed_at": null, + "completed_at": 1699075073, + "last_error": null, + "model": "gpt-4o", + "instructions": null, + "incomplete_details": null, + "tools": [ + { + "type": "code_interpreter" + } + ], + "metadata": {}, + "usage": { + "prompt_tokens": 123, + "completion_tokens": 456, + "total_tokens": 579 + }, + "temperature": 1.0, + "top_p": 1.0, + "max_prompt_tokens": 1000, + "max_completion_tokens": 1000, + "truncation_strategy": { + "type": "auto", + "last_messages": null + }, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + } + post: + operationId: modifyRun + tags: + - Assistants + summary: Modifies a run. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the [thread](/docs/api-reference/threads) that was run. + - in: path + name: run_id + required: true + schema: + type: string + description: The ID of the run to modify. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/ModifyRunRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/RunObject" + x-oaiMeta: + name: Modify run + group: threads + beta: true + returns: The modified [run](/docs/api-reference/runs/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "metadata": { + "user_id": "user_abc123" + } + }' + python: | + from openai import OpenAI + client = OpenAI() + + run = client.beta.threads.runs.update( + thread_id="thread_abc123", + run_id="run_abc123", + metadata={"user_id": "user_abc123"}, + ) + + print(run) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const run = await openai.beta.threads.runs.update( + "thread_abc123", + "run_abc123", + { + metadata: { + user_id: "user_abc123", + }, + } + ); + + console.log(run); + } + + main(); + response: | + { + "id": "run_abc123", + "object": "thread.run", + "created_at": 1699075072, + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "status": "completed", + "started_at": 1699075072, + "expires_at": null, + "cancelled_at": null, + "failed_at": null, + "completed_at": 1699075073, + "last_error": null, + "model": "gpt-4o", + "instructions": null, + "incomplete_details": null, + "tools": [ + { + "type": "code_interpreter" + } + ], + "tool_resources": { + "code_interpreter": { + "file_ids": [ + "file-abc123", + "file-abc456" + ] + } + }, + "metadata": { + "user_id": "user_abc123" + }, + "usage": { + "prompt_tokens": 123, + "completion_tokens": 456, + "total_tokens": 579 + }, + "temperature": 1.0, + "top_p": 1.0, + "max_prompt_tokens": 1000, + "max_completion_tokens": 1000, + "truncation_strategy": { + "type": "auto", + "last_messages": null + }, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + } + + /threads/{thread_id}/runs/{run_id}/submit_tool_outputs: + post: + operationId: submitToolOuputsToRun + tags: + - Assistants + summary: | + When a run has the `status: "requires_action"` and `required_action.type` is `submit_tool_outputs`, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the [thread](/docs/api-reference/threads) to which this run belongs. + - in: path + name: run_id + required: true + schema: + type: string + description: The ID of the run that requires the tool output submission. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/SubmitToolOutputsRunRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/RunObject" + x-oaiMeta: + name: Submit tool outputs to run + group: threads + beta: true + returns: The modified [run](/docs/api-reference/runs/object) object matching the specified ID. + examples: + - title: Default + request: + curl: | + curl https://api.openai.com/v1/threads/thread_123/runs/run_123/submit_tool_outputs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "tool_outputs": [ + { + "tool_call_id": "call_001", + "output": "70 degrees and sunny." + } + ] + }' + python: | + from openai import OpenAI + client = OpenAI() + + run = client.beta.threads.runs.submit_tool_outputs( + thread_id="thread_123", + run_id="run_123", + tool_outputs=[ + { + "tool_call_id": "call_001", + "output": "70 degrees and sunny." + } + ] + ) + + print(run) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const run = await openai.beta.threads.runs.submitToolOutputs( + "thread_123", + "run_123", + { + tool_outputs: [ + { + tool_call_id: "call_001", + output: "70 degrees and sunny.", + }, + ], + } + ); + + console.log(run); + } + + main(); + response: | + { + "id": "run_123", + "object": "thread.run", + "created_at": 1699075592, + "assistant_id": "asst_123", + "thread_id": "thread_123", + "status": "queued", + "started_at": 1699075592, + "expires_at": 1699076192, + "cancelled_at": null, + "failed_at": null, + "completed_at": null, + "last_error": null, + "model": "gpt-4o", + "instructions": null, + "tools": [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"] + } + }, + "required": ["location"] + } + } + } + ], + "metadata": {}, + "usage": null, + "temperature": 1.0, + "top_p": 1.0, + "max_prompt_tokens": 1000, + "max_completion_tokens": 1000, + "truncation_strategy": { + "type": "auto", + "last_messages": null + }, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + } + + - title: Streaming + request: + curl: | + curl https://api.openai.com/v1/threads/thread_123/runs/run_123/submit_tool_outputs \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "tool_outputs": [ + { + "tool_call_id": "call_001", + "output": "70 degrees and sunny." + } + ], + "stream": true + }' + python: | + from openai import OpenAI + client = OpenAI() + + stream = client.beta.threads.runs.submit_tool_outputs( + thread_id="thread_123", + run_id="run_123", + tool_outputs=[ + { + "tool_call_id": "call_001", + "output": "70 degrees and sunny." + } + ], + stream=True + ) + + for event in stream: + print(event) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const stream = await openai.beta.threads.runs.submitToolOutputs( + "thread_123", + "run_123", + { + tool_outputs: [ + { + tool_call_id: "call_001", + output: "70 degrees and sunny.", + }, + ], + } + ); + + for await (const event of stream) { + console.log(event); + } + } + + main(); + response: | + event: thread.run.step.completed + data: {"id":"step_001","object":"thread.run.step","created_at":1710352449,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"tool_calls","status":"completed","cancelled_at":null,"completed_at":1710352475,"expires_at":1710353047,"failed_at":null,"last_error":null,"step_details":{"type":"tool_calls","tool_calls":[{"id":"call_iWr0kQ2EaYMaxNdl0v3KYkx7","type":"function","function":{"name":"get_current_weather","arguments":"{\"location\":\"San Francisco, CA\",\"unit\":\"fahrenheit\"}","output":"70 degrees and sunny."}}]},"usage":{"prompt_tokens":291,"completion_tokens":24,"total_tokens":315}} + + event: thread.run.queued + data: {"id":"run_123","object":"thread.run","created_at":1710352447,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":1710352448,"expires_at":1710353047,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.in_progress + data: {"id":"run_123","object":"thread.run","created_at":1710352447,"assistant_id":"asst_123","thread_id":"thread_123","status":"in_progress","started_at":1710352475,"expires_at":1710353047,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: thread.run.step.created + data: {"id":"step_002","object":"thread.run.step","created_at":1710352476,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710353047,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_002"}},"usage":null} + + event: thread.run.step.in_progress + data: {"id":"step_002","object":"thread.run.step","created_at":1710352476,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"in_progress","cancelled_at":null,"completed_at":null,"expires_at":1710353047,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_002"}},"usage":null} + + event: thread.message.created + data: {"id":"msg_002","object":"thread.message","created_at":1710352476,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"in_progress","incomplete_details":null,"incomplete_at":null,"completed_at":null,"role":"assistant","content":[],"metadata":{}} + + event: thread.message.in_progress + data: {"id":"msg_002","object":"thread.message","created_at":1710352476,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"in_progress","incomplete_details":null,"incomplete_at":null,"completed_at":null,"role":"assistant","content":[],"metadata":{}} + + event: thread.message.delta + data: {"id":"msg_002","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"The","annotations":[]}}]}} + + event: thread.message.delta + data: {"id":"msg_002","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":" current"}}]}} + + event: thread.message.delta + data: {"id":"msg_002","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":" weather"}}]}} + + ... + + event: thread.message.delta + data: {"id":"msg_002","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":" sunny"}}]}} + + event: thread.message.delta + data: {"id":"msg_002","object":"thread.message.delta","delta":{"content":[{"index":0,"type":"text","text":{"value":"."}}]}} + + event: thread.message.completed + data: {"id":"msg_002","object":"thread.message","created_at":1710352476,"assistant_id":"asst_123","thread_id":"thread_123","run_id":"run_123","status":"completed","incomplete_details":null,"incomplete_at":null,"completed_at":1710352477,"role":"assistant","content":[{"type":"text","text":{"value":"The current weather in San Francisco, CA is 70 degrees Fahrenheit and sunny.","annotations":[]}}],"metadata":{}} + + event: thread.run.step.completed + data: {"id":"step_002","object":"thread.run.step","created_at":1710352476,"run_id":"run_123","assistant_id":"asst_123","thread_id":"thread_123","type":"message_creation","status":"completed","cancelled_at":null,"completed_at":1710352477,"expires_at":1710353047,"failed_at":null,"last_error":null,"step_details":{"type":"message_creation","message_creation":{"message_id":"msg_002"}},"usage":{"prompt_tokens":329,"completion_tokens":18,"total_tokens":347}} + + event: thread.run.completed + data: {"id":"run_123","object":"thread.run","created_at":1710352447,"assistant_id":"asst_123","thread_id":"thread_123","status":"completed","started_at":1710352475,"expires_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1710352477,"required_action":null,"last_error":null,"model":"gpt-4o","instructions":null,"tools":[{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":{"prompt_tokens":20,"completion_tokens":11,"total_tokens":31},"response_format":"auto","tool_choice":"auto","parallel_tool_calls":true}} + + event: done + data: [DONE] + + /threads/{thread_id}/runs/{run_id}/cancel: + post: + operationId: cancelRun + tags: + - Assistants + summary: Cancels a run that is `in_progress`. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the thread to which this run belongs. + - in: path + name: run_id + required: true + schema: + type: string + description: The ID of the run to cancel. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/RunObject" + x-oaiMeta: + name: Cancel a run + group: threads + beta: true + returns: The modified [run](/docs/api-reference/runs/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/cancel \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "OpenAI-Beta: assistants=v2" \ + -X POST + python: | + from openai import OpenAI + client = OpenAI() + + run = client.beta.threads.runs.cancel( + thread_id="thread_abc123", + run_id="run_abc123" + ) + + print(run) + node.js: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const run = await openai.beta.threads.runs.cancel( + "thread_abc123", + "run_abc123" + ); + + console.log(run); + } + + main(); + response: | + { + "id": "run_abc123", + "object": "thread.run", + "created_at": 1699076126, + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "status": "cancelling", + "started_at": 1699076126, + "expires_at": 1699076726, + "cancelled_at": null, + "failed_at": null, + "completed_at": null, + "last_error": null, + "model": "gpt-4o", + "instructions": "You summarize books.", + "tools": [ + { + "type": "file_search" + } + ], + "tool_resources": { + "file_search": { + "vector_store_ids": ["vs_123"] + } + }, + "metadata": {}, + "usage": null, + "temperature": 1.0, + "top_p": 1.0, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + } + + /threads/{thread_id}/runs/{run_id}/steps: + get: + operationId: listRunSteps + tags: + - Assistants + summary: Returns a list of run steps belonging to a run. + parameters: + - name: thread_id + in: path + required: true + schema: + type: string + description: The ID of the thread the run and run steps belong to. + - name: run_id + in: path + required: true + schema: + type: string + description: The ID of the run the run steps belong to. + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: order + in: query + description: *pagination_order_param_description + schema: + type: string + default: desc + enum: ["asc", "desc"] + - name: after + in: query + description: *pagination_after_param_description + schema: + type: string + - name: before + in: query + description: *pagination_before_param_description + schema: + type: string + - name: include[] + in: query + description: *include_param_description + schema: + type: array + items: + type: string + enum: ["step_details.tool_calls[*].file_search.results[*].content"] + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListRunStepsResponse" + x-oaiMeta: + name: List run steps + group: threads + beta: true + returns: A list of [run step](/docs/api-reference/run-steps/step-object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/steps \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + run_steps = client.beta.threads.runs.steps.list( + thread_id="thread_abc123", + run_id="run_abc123" + ) + + print(run_steps) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const runStep = await openai.beta.threads.runs.steps.list( + "thread_abc123", + "run_abc123" + ); + console.log(runStep); + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "id": "step_abc123", + "object": "thread.run.step", + "created_at": 1699063291, + "run_id": "run_abc123", + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "type": "message_creation", + "status": "completed", + "cancelled_at": null, + "completed_at": 1699063291, + "expired_at": null, + "failed_at": null, + "last_error": null, + "step_details": { + "type": "message_creation", + "message_creation": { + "message_id": "msg_abc123" + } + }, + "usage": { + "prompt_tokens": 123, + "completion_tokens": 456, + "total_tokens": 579 + } + } + ], + "first_id": "step_abc123", + "last_id": "step_abc456", + "has_more": false + } + + /threads/{thread_id}/runs/{run_id}/steps/{step_id}: + get: + operationId: getRunStep + tags: + - Assistants + summary: Retrieves a run step. + parameters: + - in: path + name: thread_id + required: true + schema: + type: string + description: The ID of the thread to which the run and run step belongs. + - in: path + name: run_id + required: true + schema: + type: string + description: The ID of the run to which the run step belongs. + - in: path + name: step_id + required: true + schema: + type: string + description: The ID of the run step to retrieve. + - name: include[] + in: query + description: *include_param_description + schema: + type: array + items: + type: string + enum: ["step_details.tool_calls[*].file_search.results[*].content"] + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/RunStepObject" + x-oaiMeta: + name: Retrieve run step + group: threads + beta: true + returns: The [run step](/docs/api-reference/run-steps/step-object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/threads/thread_abc123/runs/run_abc123/steps/step_abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + run_step = client.beta.threads.runs.steps.retrieve( + thread_id="thread_abc123", + run_id="run_abc123", + step_id="step_abc123" + ) + + print(run_step) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const runStep = await openai.beta.threads.runs.steps.retrieve( + "thread_abc123", + "run_abc123", + "step_abc123" + ); + console.log(runStep); + } + + main(); + response: &run_step_object_example | + { + "id": "step_abc123", + "object": "thread.run.step", + "created_at": 1699063291, + "run_id": "run_abc123", + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "type": "message_creation", + "status": "completed", + "cancelled_at": null, + "completed_at": 1699063291, + "expired_at": null, + "failed_at": null, + "last_error": null, + "step_details": { + "type": "message_creation", + "message_creation": { + "message_id": "msg_abc123" + } + }, + "usage": { + "prompt_tokens": 123, + "completion_tokens": 456, + "total_tokens": 579 + } + } + + /vector_stores: + get: + operationId: listVectorStores + tags: + - Vector Stores + summary: Returns a list of vector stores. + parameters: + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: order + in: query + description: *pagination_order_param_description + schema: + type: string + default: desc + enum: ["asc", "desc"] + - name: after + in: query + description: *pagination_after_param_description + schema: + type: string + - name: before + in: query + description: *pagination_before_param_description + schema: + type: string + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListVectorStoresResponse" + x-oaiMeta: + name: List vector stores + group: vector_stores + beta: true + returns: A list of [vector store](/docs/api-reference/vector-stores/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + vector_stores = client.beta.vector_stores.list() + print(vector_stores) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const vectorStores = await openai.beta.vectorStores.list(); + console.log(vectorStores); + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "id": "vs_abc123", + "object": "vector_store", + "created_at": 1699061776, + "name": "Support FAQ", + "bytes": 139920, + "file_counts": { + "in_progress": 0, + "completed": 3, + "failed": 0, + "cancelled": 0, + "total": 3 + } + }, + { + "id": "vs_abc456", + "object": "vector_store", + "created_at": 1699061776, + "name": "Support FAQ v2", + "bytes": 139920, + "file_counts": { + "in_progress": 0, + "completed": 3, + "failed": 0, + "cancelled": 0, + "total": 3 + } + } + ], + "first_id": "vs_abc123", + "last_id": "vs_abc456", + "has_more": false + } + post: + operationId: createVectorStore + tags: + - Vector Stores + summary: Create a vector store. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateVectorStoreRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/VectorStoreObject" + x-oaiMeta: + name: Create vector store + group: vector_stores + beta: true + returns: A [vector store](/docs/api-reference/vector-stores/object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + -d '{ + "name": "Support FAQ" + }' + python: | + from openai import OpenAI + client = OpenAI() + + vector_store = client.beta.vector_stores.create( + name="Support FAQ" + ) + print(vector_store) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const vectorStore = await openai.beta.vectorStores.create({ + name: "Support FAQ" + }); + console.log(vectorStore); + } + + main(); + response: | + { + "id": "vs_abc123", + "object": "vector_store", + "created_at": 1699061776, + "name": "Support FAQ", + "bytes": 139920, + "file_counts": { + "in_progress": 0, + "completed": 3, + "failed": 0, + "cancelled": 0, + "total": 3 + } + } + + /vector_stores/{vector_store_id}: + get: + operationId: getVectorStore + tags: + - Vector Stores + summary: Retrieves a vector store. + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + description: The ID of the vector store to retrieve. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/VectorStoreObject" + x-oaiMeta: + name: Retrieve vector store + group: vector_stores + beta: true + returns: The [vector store](/docs/api-reference/vector-stores/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + vector_store = client.beta.vector_stores.retrieve( + vector_store_id="vs_abc123" + ) + print(vector_store) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const vectorStore = await openai.beta.vectorStores.retrieve( + "vs_abc123" + ); + console.log(vectorStore); + } + + main(); + response: | + { + "id": "vs_abc123", + "object": "vector_store", + "created_at": 1699061776 + } + post: + operationId: modifyVectorStore + tags: + - Vector Stores + summary: Modifies a vector store. + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + description: The ID of the vector store to modify. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/UpdateVectorStoreRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/VectorStoreObject" + x-oaiMeta: + name: Modify vector store + group: vector_stores + beta: true + returns: The modified [vector store](/docs/api-reference/vector-stores/object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + -d '{ + "name": "Support FAQ" + }' + python: | + from openai import OpenAI + client = OpenAI() + + vector_store = client.beta.vector_stores.update( + vector_store_id="vs_abc123", + name="Support FAQ" + ) + print(vector_store) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const vectorStore = await openai.beta.vectorStores.update( + "vs_abc123", + { + name: "Support FAQ" + } + ); + console.log(vectorStore); + } + + main(); + response: | + { + "id": "vs_abc123", + "object": "vector_store", + "created_at": 1699061776, + "name": "Support FAQ", + "bytes": 139920, + "file_counts": { + "in_progress": 0, + "completed": 3, + "failed": 0, + "cancelled": 0, + "total": 3 + } + } + + delete: + operationId: deleteVectorStore + tags: + - Vector Stores + summary: Delete a vector store. + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + description: The ID of the vector store to delete. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/DeleteVectorStoreResponse" + x-oaiMeta: + name: Delete vector store + group: vector_stores + beta: true + returns: Deletion status + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -X DELETE + python: | + from openai import OpenAI + client = OpenAI() + + deleted_vector_store = client.beta.vector_stores.delete( + vector_store_id="vs_abc123" + ) + print(deleted_vector_store) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const deletedVectorStore = await openai.beta.vectorStores.del( + "vs_abc123" + ); + console.log(deletedVectorStore); + } + + main(); + response: | + { + id: "vs_abc123", + object: "vector_store.deleted", + deleted: true + } + + /vector_stores/{vector_store_id}/files: + get: + operationId: listVectorStoreFiles + tags: + - Vector Stores + summary: Returns a list of vector store files. + parameters: + - name: vector_store_id + in: path + description: The ID of the vector store that the files belong to. + required: true + schema: + type: string + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: order + in: query + description: *pagination_order_param_description + schema: + type: string + default: desc + enum: ["asc", "desc"] + - name: after + in: query + description: *pagination_after_param_description + schema: + type: string + - name: before + in: query + description: *pagination_before_param_description + schema: + type: string + - name: filter + in: query + description: "Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`." + schema: + type: string + enum: ["in_progress", "completed", "failed", "cancelled"] + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListVectorStoreFilesResponse" + x-oaiMeta: + name: List vector store files + group: vector_stores + beta: true + returns: A list of [vector store file](/docs/api-reference/vector-stores-files/file-object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123/files \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + vector_store_files = client.beta.vector_stores.files.list( + vector_store_id="vs_abc123" + ) + print(vector_store_files) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const vectorStoreFiles = await openai.beta.vectorStores.files.list( + "vs_abc123" + ); + console.log(vectorStoreFiles); + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "id": "file-abc123", + "object": "vector_store.file", + "created_at": 1699061776, + "vector_store_id": "vs_abc123" + }, + { + "id": "file-abc456", + "object": "vector_store.file", + "created_at": 1699061776, + "vector_store_id": "vs_abc123" + } + ], + "first_id": "file-abc123", + "last_id": "file-abc456", + "has_more": false + } + post: + operationId: createVectorStoreFile + tags: + - Vector Stores + summary: Create a vector store file by attaching a [File](/docs/api-reference/files) to a [vector store](/docs/api-reference/vector-stores/object). + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + example: vs_abc123 + description: | + The ID of the vector store for which to create a File. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateVectorStoreFileRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/VectorStoreFileObject" + x-oaiMeta: + name: Create vector store file + group: vector_stores + beta: true + returns: A [vector store file](/docs/api-reference/vector-stores-files/file-object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123/files \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "file_id": "file-abc123" + }' + python: | + from openai import OpenAI + client = OpenAI() + + vector_store_file = client.beta.vector_stores.files.create( + vector_store_id="vs_abc123", + file_id="file-abc123" + ) + print(vector_store_file) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const myVectorStoreFile = await openai.beta.vectorStores.files.create( + "vs_abc123", + { + file_id: "file-abc123" + } + ); + console.log(myVectorStoreFile); + } + + main(); + response: | + { + "id": "file-abc123", + "object": "vector_store.file", + "created_at": 1699061776, + "usage_bytes": 1234, + "vector_store_id": "vs_abcd", + "status": "completed", + "last_error": null + } + + /vector_stores/{vector_store_id}/files/{file_id}: + get: + operationId: getVectorStoreFile + tags: + - Vector Stores + summary: Retrieves a vector store file. + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + example: vs_abc123 + description: The ID of the vector store that the file belongs to. + - in: path + name: file_id + required: true + schema: + type: string + example: file-abc123 + description: The ID of the file being retrieved. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/VectorStoreFileObject" + x-oaiMeta: + name: Retrieve vector store file + group: vector_stores + beta: true + returns: The [vector store file](/docs/api-reference/vector-stores-files/file-object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123/files/file-abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + vector_store_file = client.beta.vector_stores.files.retrieve( + vector_store_id="vs_abc123", + file_id="file-abc123" + ) + print(vector_store_file) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const vectorStoreFile = await openai.beta.vectorStores.files.retrieve( + "vs_abc123", + "file-abc123" + ); + console.log(vectorStoreFile); + } + + main(); + response: | + { + "id": "file-abc123", + "object": "vector_store.file", + "created_at": 1699061776, + "vector_store_id": "vs_abcd", + "status": "completed", + "last_error": null + } + delete: + operationId: deleteVectorStoreFile + tags: + - Vector Stores + summary: Delete a vector store file. This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the [delete file](/docs/api-reference/files/delete) endpoint. + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + description: The ID of the vector store that the file belongs to. + - in: path + name: file_id + required: true + schema: + type: string + description: The ID of the file to delete. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/DeleteVectorStoreFileResponse" + x-oaiMeta: + name: Delete vector store file + group: vector_stores + beta: true + returns: Deletion status + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123/files/file-abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -X DELETE + python: | + from openai import OpenAI + client = OpenAI() + + deleted_vector_store_file = client.beta.vector_stores.files.delete( + vector_store_id="vs_abc123", + file_id="file-abc123" + ) + print(deleted_vector_store_file) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const deletedVectorStoreFile = await openai.beta.vectorStores.files.del( + "vs_abc123", + "file-abc123" + ); + console.log(deletedVectorStoreFile); + } + + main(); + response: | + { + id: "file-abc123", + object: "vector_store.file.deleted", + deleted: true + } + + /vector_stores/{vector_store_id}/file_batches: + post: + operationId: createVectorStoreFileBatch + tags: + - Vector Stores + summary: Create a vector store file batch. + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + example: vs_abc123 + description: | + The ID of the vector store for which to create a File Batch. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/CreateVectorStoreFileBatchRequest" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/VectorStoreFileBatchObject" + x-oaiMeta: + name: Create vector store file batch + group: vector_stores + beta: true + returns: A [vector store file batch](/docs/api-reference/vector-stores-file-batches/batch-object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123/file_batches \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json \ + -H "OpenAI-Beta: assistants=v2" \ + -d '{ + "file_ids": ["file-abc123", "file-abc456"] + }' + python: | + from openai import OpenAI + client = OpenAI() + + vector_store_file_batch = client.beta.vector_stores.file_batches.create( + vector_store_id="vs_abc123", + file_ids=["file-abc123", "file-abc456"] + ) + print(vector_store_file_batch) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const myVectorStoreFileBatch = await openai.beta.vectorStores.fileBatches.create( + "vs_abc123", + { + file_ids: ["file-abc123", "file-abc456"] + } + ); + console.log(myVectorStoreFileBatch); + } + + main(); + response: | + { + "id": "vsfb_abc123", + "object": "vector_store.file_batch", + "created_at": 1699061776, + "vector_store_id": "vs_abc123", + "status": "in_progress", + "file_counts": { + "in_progress": 1, + "completed": 1, + "failed": 0, + "cancelled": 0, + "total": 0, + } + } + + /vector_stores/{vector_store_id}/file_batches/{batch_id}: + get: + operationId: getVectorStoreFileBatch + tags: + - Vector Stores + summary: Retrieves a vector store file batch. + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + example: vs_abc123 + description: The ID of the vector store that the file batch belongs to. + - in: path + name: batch_id + required: true + schema: + type: string + example: vsfb_abc123 + description: The ID of the file batch being retrieved. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/VectorStoreFileBatchObject" + x-oaiMeta: + name: Retrieve vector store file batch + group: vector_stores + beta: true + returns: The [vector store file batch](/docs/api-reference/vector-stores-file-batches/batch-object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123/files_batches/vsfb_abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + vector_store_file_batch = client.beta.vector_stores.file_batches.retrieve( + vector_store_id="vs_abc123", + batch_id="vsfb_abc123" + ) + print(vector_store_file_batch) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const vectorStoreFileBatch = await openai.beta.vectorStores.fileBatches.retrieve( + "vs_abc123", + "vsfb_abc123" + ); + console.log(vectorStoreFileBatch); + } + + main(); + response: | + { + "id": "vsfb_abc123", + "object": "vector_store.file_batch", + "created_at": 1699061776, + "vector_store_id": "vs_abc123", + "status": "in_progress", + "file_counts": { + "in_progress": 1, + "completed": 1, + "failed": 0, + "cancelled": 0, + "total": 0, + } + } + + /vector_stores/{vector_store_id}/file_batches/{batch_id}/cancel: + post: + operationId: cancelVectorStoreFileBatch + tags: + - Vector Stores + summary: Cancel a vector store file batch. This attempts to cancel the processing of files in this batch as soon as possible. + parameters: + - in: path + name: vector_store_id + required: true + schema: + type: string + description: The ID of the vector store that the file batch belongs to. + - in: path + name: batch_id + required: true + schema: + type: string + description: The ID of the file batch to cancel. + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/VectorStoreFileBatchObject" + x-oaiMeta: + name: Cancel vector store file batch + group: vector_stores + beta: true + returns: The modified vector store file batch object. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123/files_batches/vsfb_abc123/cancel \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" \ + -X POST + python: | + from openai import OpenAI + client = OpenAI() + + deleted_vector_store_file_batch = client.beta.vector_stores.file_batches.cancel( + vector_store_id="vs_abc123", + file_batch_id="vsfb_abc123" + ) + print(deleted_vector_store_file_batch) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const deletedVectorStoreFileBatch = await openai.vector_stores.fileBatches.cancel( + "vs_abc123", + "vsfb_abc123" + ); + console.log(deletedVectorStoreFileBatch); + } + + main(); + response: | + { + "id": "vsfb_abc123", + "object": "vector_store.file_batch", + "created_at": 1699061776, + "vector_store_id": "vs_abc123", + "status": "cancelling", + "file_counts": { + "in_progress": 12, + "completed": 3, + "failed": 0, + "cancelled": 0, + "total": 15, + } + } + + /vector_stores/{vector_store_id}/file_batches/{batch_id}/files: + get: + operationId: listFilesInVectorStoreBatch + tags: + - Vector Stores + summary: Returns a list of vector store files in a batch. + parameters: + - name: vector_store_id + in: path + description: The ID of the vector store that the files belong to. + required: true + schema: + type: string + - name: batch_id + in: path + description: The ID of the file batch that the files belong to. + required: true + schema: + type: string + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: order + in: query + description: *pagination_order_param_description + schema: + type: string + default: desc + enum: ["asc", "desc"] + - name: after + in: query + description: *pagination_after_param_description + schema: + type: string + - name: before + in: query + description: *pagination_before_param_description + schema: + type: string + - name: filter + in: query + description: "Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`." + schema: + type: string + enum: ["in_progress", "completed", "failed", "cancelled"] + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/ListVectorStoreFilesResponse" + x-oaiMeta: + name: List vector store files in a batch + group: vector_stores + beta: true + returns: A list of [vector store file](/docs/api-reference/vector-stores-files/file-object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/vector_stores/vs_abc123/files_batches/vsfb_abc123/files \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -H "OpenAI-Beta: assistants=v2" + python: | + from openai import OpenAI + client = OpenAI() + + vector_store_files = client.beta.vector_stores.file_batches.list_files( + vector_store_id="vs_abc123", + batch_id="vsfb_abc123" + ) + print(vector_store_files) + node.js: | + import OpenAI from "openai"; + const openai = new OpenAI(); + + async function main() { + const vectorStoreFiles = await openai.beta.vectorStores.fileBatches.listFiles( + "vs_abc123", + "vsfb_abc123" + ); + console.log(vectorStoreFiles); + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "id": "file-abc123", + "object": "vector_store.file", + "created_at": 1699061776, + "vector_store_id": "vs_abc123" + }, + { + "id": "file-abc456", + "object": "vector_store.file", + "created_at": 1699061776, + "vector_store_id": "vs_abc123" + } + ], + "first_id": "file-abc123", + "last_id": "file-abc456", + "has_more": false + } + + /batches: + post: + summary: Creates and executes a batch from an uploaded file of requests + operationId: createBatch + tags: + - Batch + requestBody: + required: true + content: + application/json: + schema: + type: object + required: + - input_file_id + - endpoint + - completion_window + properties: + input_file_id: + type: string + description: | + The ID of an uploaded file that contains requests for the new batch. + + See [upload file](/docs/api-reference/files/create) for how to upload a file. + + Your input file must be formatted as a [JSONL file](/docs/api-reference/batch/request-input), and must be uploaded with the purpose `batch`. The file can contain up to 50,000 requests, and can be up to 100 MB in size. + endpoint: + type: string + enum: + [ + "/v1/chat/completions", + "/v1/embeddings", + "/v1/completions", + ] + description: The endpoint to be used for all requests in the batch. Currently `/v1/chat/completions`, `/v1/embeddings`, and `/v1/completions` are supported. Note that `/v1/embeddings` batches are also restricted to a maximum of 50,000 embedding inputs across all requests in the batch. + completion_window: + type: string + enum: ["24h"] + description: The time frame within which the batch should be processed. Currently only `24h` is supported. + metadata: + type: object + additionalProperties: + type: string + description: Optional custom metadata for the batch. + nullable: true + responses: + "200": + description: Batch created successfully. + content: + application/json: + schema: + $ref: "#/components/schemas/Batch" + x-oaiMeta: + name: Create batch + group: batch + returns: The created [Batch](/docs/api-reference/batch/object) object. + examples: + request: + curl: | + curl https://api.openai.com/v1/batches \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "input_file_id": "file-abc123", + "endpoint": "/v1/chat/completions", + "completion_window": "24h" + }' + python: | + from openai import OpenAI + client = OpenAI() + + client.batches.create( + input_file_id="file-abc123", + endpoint="/v1/chat/completions", + completion_window="24h" + ) + node: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const batch = await openai.batches.create({ + input_file_id: "file-abc123", + endpoint: "/v1/chat/completions", + completion_window: "24h" + }); + + console.log(batch); + } + + main(); + response: | + { + "id": "batch_abc123", + "object": "batch", + "endpoint": "/v1/chat/completions", + "errors": null, + "input_file_id": "file-abc123", + "completion_window": "24h", + "status": "validating", + "output_file_id": null, + "error_file_id": null, + "created_at": 1711471533, + "in_progress_at": null, + "expires_at": null, + "finalizing_at": null, + "completed_at": null, + "failed_at": null, + "expired_at": null, + "cancelling_at": null, + "cancelled_at": null, + "request_counts": { + "total": 0, + "completed": 0, + "failed": 0 + }, + "metadata": { + "customer_id": "user_123456789", + "batch_description": "Nightly eval job", + } + } + get: + operationId: listBatches + tags: + - Batch + summary: List your organization's batches. + parameters: + - in: query + name: after + required: false + schema: + type: string + description: *pagination_after_param_description + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + responses: + "200": + description: Batch listed successfully. + content: + application/json: + schema: + $ref: "#/components/schemas/ListBatchesResponse" + x-oaiMeta: + name: List batch + group: batch + returns: A list of paginated [Batch](/docs/api-reference/batch/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/batches?limit=2 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" + python: | + from openai import OpenAI + client = OpenAI() + + client.batches.list() + node: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const list = await openai.batches.list(); + + for await (const batch of list) { + console.log(batch); + } + } + + main(); + response: | + { + "object": "list", + "data": [ + { + "id": "batch_abc123", + "object": "batch", + "endpoint": "/v1/chat/completions", + "errors": null, + "input_file_id": "file-abc123", + "completion_window": "24h", + "status": "completed", + "output_file_id": "file-cvaTdG", + "error_file_id": "file-HOWS94", + "created_at": 1711471533, + "in_progress_at": 1711471538, + "expires_at": 1711557933, + "finalizing_at": 1711493133, + "completed_at": 1711493163, + "failed_at": null, + "expired_at": null, + "cancelling_at": null, + "cancelled_at": null, + "request_counts": { + "total": 100, + "completed": 95, + "failed": 5 + }, + "metadata": { + "customer_id": "user_123456789", + "batch_description": "Nightly job", + } + }, + { ... }, + ], + "first_id": "batch_abc123", + "last_id": "batch_abc456", + "has_more": true + } + + /batches/{batch_id}: + get: + operationId: retrieveBatch + tags: + - Batch + summary: Retrieves a batch. + parameters: + - in: path + name: batch_id + required: true + schema: + type: string + description: The ID of the batch to retrieve. + responses: + "200": + description: Batch retrieved successfully. + content: + application/json: + schema: + $ref: "#/components/schemas/Batch" + x-oaiMeta: + name: Retrieve batch + group: batch + returns: The [Batch](/docs/api-reference/batch/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/batches/batch_abc123 \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + python: | + from openai import OpenAI + client = OpenAI() + + client.batches.retrieve("batch_abc123") + node: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const batch = await openai.batches.retrieve("batch_abc123"); + + console.log(batch); + } + + main(); + response: &batch_object | + { + "id": "batch_abc123", + "object": "batch", + "endpoint": "/v1/completions", + "errors": null, + "input_file_id": "file-abc123", + "completion_window": "24h", + "status": "completed", + "output_file_id": "file-cvaTdG", + "error_file_id": "file-HOWS94", + "created_at": 1711471533, + "in_progress_at": 1711471538, + "expires_at": 1711557933, + "finalizing_at": 1711493133, + "completed_at": 1711493163, + "failed_at": null, + "expired_at": null, + "cancelling_at": null, + "cancelled_at": null, + "request_counts": { + "total": 100, + "completed": 95, + "failed": 5 + }, + "metadata": { + "customer_id": "user_123456789", + "batch_description": "Nightly eval job", + } + } + + /batches/{batch_id}/cancel: + post: + operationId: cancelBatch + tags: + - Batch + summary: Cancels an in-progress batch. The batch will be in status `cancelling` for up to 10 minutes, before changing to `cancelled`, where it will have partial results (if any) available in the output file. + parameters: + - in: path + name: batch_id + required: true + schema: + type: string + description: The ID of the batch to cancel. + responses: + "200": + description: Batch is cancelling. Returns the cancelling batch's details. + content: + application/json: + schema: + $ref: "#/components/schemas/Batch" + x-oaiMeta: + name: Cancel batch + group: batch + returns: The [Batch](/docs/api-reference/batch/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/batches/batch_abc123/cancel \ + -H "Authorization: Bearer $OPENAI_API_KEY" \ + -H "Content-Type: application/json" \ + -X POST + python: | + from openai import OpenAI + client = OpenAI() + + client.batches.cancel("batch_abc123") + node: | + import OpenAI from "openai"; + + const openai = new OpenAI(); + + async function main() { + const batch = await openai.batches.cancel("batch_abc123"); + + console.log(batch); + } + + main(); + response: | + { + "id": "batch_abc123", + "object": "batch", + "endpoint": "/v1/chat/completions", + "errors": null, + "input_file_id": "file-abc123", + "completion_window": "24h", + "status": "cancelling", + "output_file_id": null, + "error_file_id": null, + "created_at": 1711471533, + "in_progress_at": 1711471538, + "expires_at": 1711557933, + "finalizing_at": null, + "completed_at": null, + "failed_at": null, + "expired_at": null, + "cancelling_at": 1711475133, + "cancelled_at": null, + "request_counts": { + "total": 100, + "completed": 23, + "failed": 1 + }, + "metadata": { + "customer_id": "user_123456789", + "batch_description": "Nightly eval job", + } + } + + # Organization + # Audit Logs List + /organization/audit_logs: + get: + summary: List user actions and configuration changes within this organization. + operationId: list-audit-logs + tags: + - Audit Logs + parameters: + - name: effective_at + in: query + description: Return only events whose `effective_at` (Unix seconds) is in this range. + required: false + schema: + type: object + properties: + gt: + type: integer + description: Return only events whose `effective_at` (Unix seconds) is greater than this value. + gte: + type: integer + description: Return only events whose `effective_at` (Unix seconds) is greater than or equal to this value. + lt: + type: integer + description: Return only events whose `effective_at` (Unix seconds) is less than this value. + lte: + type: integer + description: Return only events whose `effective_at` (Unix seconds) is less than or equal to this value. + - name: project_ids[] + in: query + description: Return only events for these projects. + required: false + schema: + type: array + items: + type: string + - name: event_types[] + in: query + description: Return only events with a `type` in one of these values. For example, `project.created`. For all options, see the documentation for the [audit log object](/docs/api-reference/audit-logs/object). + required: false + schema: + type: array + items: + $ref: "#/components/schemas/AuditLogEventType" + - name: actor_ids[] + in: query + description: Return only events performed by these actors. Can be a user ID, a service account ID, or an api key tracking ID. + required: false + schema: + type: array + items: + type: string + - name: actor_emails[] + in: query + description: Return only events performed by users with these emails. + required: false + schema: + type: array + items: + type: string + - name: resource_ids[] + in: query + description: Return only events performed on these targets. For example, a project ID updated. + required: false + schema: + type: array + items: + type: string + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: after + in: query + description: *pagination_after_param_description + schema: + type: string + - name: before + in: query + description: *pagination_before_param_description + schema: + type: string + responses: + "200": + description: Audit logs listed successfully. + content: + application/json: + schema: + $ref: "#/components/schemas/ListAuditLogsResponse" + x-oaiMeta: + name: List audit logs + group: audit-logs + returns: A list of paginated [Audit Log](/docs/api-reference/audit-logs/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/audit_logs \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" \ + response: | + { + "object": "list", + "data": [ + { + "id": "audit_log-xxx_yyyymmdd", + "type": "project.archived", + "effective_at": 1722461446, + "actor": { + "type": "api_key", + "api_key": { + "type": "user", + "user": { + "id": "user-xxx", + "email": "user@example.com" + } + } + }, + "project.archived": { + "id": "proj_abc" + }, + }, + { + "id": "audit_log-yyy__20240101", + "type": "api_key.updated", + "effective_at": 1720804190, + "actor": { + "type": "session", + "session": { + "user": { + "id": "user-xxx", + "email": "user@example.com" + }, + "ip_address": "127.0.0.1", + "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" + } + }, + "api_key.updated": { + "id": "key_xxxx", + "data": { + "scopes": ["resource_2.operation_2"] + } + }, + } + ], + "first_id": "audit_log-xxx__20240101", + "last_id": "audit_log_yyy__20240101", + "has_more": true + } + /organization/invites: + get: + summary: Returns a list of invites in the organization. + operationId: list-invites + tags: + - Invites + parameters: + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: after + in: query + description: *pagination_after_param_description + required: false + schema: + type: string + responses: + "200": + description: Invites listed successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/InviteListResponse' + x-oaiMeta: + name: List invites + group: administration + returns: A list of [Invite](/docs/api-reference/invite/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/invites?after=invite-abc&limit=20 \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "list", + "data": [ + { + "object": "organization.invite", + "id": "invite-abc", + "email": "user@example.com", + "role": "owner", + "status": "accepted", + "invited_at": 1711471533, + "expires_at": 1711471533, + "accepted_at": 1711471533 + } + ], + "first_id": "invite-abc", + "last_id": "invite-abc", + "has_more": false + } + + post: + summary: Create an invite for a user to the organization. The invite must be accepted by the user before they have access to the organization. + operationId: inviteUser + tags: + - Invites + requestBody: + description: The invite request payload. + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/InviteRequest' + responses: + "200": + description: User invited successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/Invite' + x-oaiMeta: + name: Create invite + group: administration + returns: The created [Invite](/docs/api-reference/invite/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/organization/invites \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "email": "user@example.com", + "role": "owner" + }' + response: + content: | + { + "object": "organization.invite", + "id": "invite-abc", + "email": "user@example.com", + "role": "owner", + "invited_at": 1711471533, + "expires_at": 1711471533, + "accepted_at": null + } + + /organization/invites/{invite_id}: + get: + summary: Retrieves an invite. + operationId: retrieve-invite + tags: + - Invites + parameters: + - in: path + name: invite_id + required: true + schema: + type: string + description: The ID of the invite to retrieve. + responses: + "200": + description: Invite retrieved successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/Invite' + x-oaiMeta: + name: Retrieve invite + group: administration + returns: The [Invite](/docs/api-reference/invite/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/invites/invite-abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.invite", + "id": "invite-abc", + "email": "user@example.com", + "role": "owner", + "status": "accepted", + "invited_at": 1711471533, + "expires_at": 1711471533, + "accepted_at": 1711471533 + } + delete: + summary: Delete an invite. If the invite has already been accepted, it cannot be deleted. + operationId: delete-invite + tags: + - Invites + parameters: + - in: path + name: invite_id + required: true + schema: + type: string + description: The ID of the invite to delete. + responses: + "200": + description: Invite deleted successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/InviteDeleteResponse' + x-oaiMeta: + name: Delete invite + group: administration + returns: Confirmation that the invite has been deleted + examples: + request: + curl: | + curl -X DELETE https://api.openai.com/v1/organization/invites/invite-abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.invite.deleted", + "id": "invite-abc", + "deleted": true + } + + /organization/users: + get: + summary: Lists all of the users in the organization. + operationId: list-users + tags: + - Users + parameters: + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: after + in: query + description: *pagination_after_param_description + required: false + schema: + type: string + responses: + "200": + description: Users listed successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/UserListResponse' + x-oaiMeta: + name: List users + group: administration + returns: A list of [User](/docs/api-reference/users/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/users?after=user_abc&limit=20 \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "list", + "data": [ + { + "object": "organization.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + ], + "first_id": "user-abc", + "last_id": "user-xyz", + "has_more": false + } + + /organization/users/{user_id}: + get: + summary: Retrieves a user by their identifier. + operationId: retrieve-user + tags: + - Users + parameters: + - name: user_id + in: path + description: The ID of the user. + required: true + schema: + type: string + responses: + "200": + description: User retrieved successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/User' + x-oaiMeta: + name: Retrieve user + group: administration + returns: The [User](/docs/api-reference/users/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/users/user_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + + post: + summary: Modifies a user's role in the organization. + operationId: modify-user + tags: + - Users + requestBody: + description: The new user role to modify. This must be one of `owner` or `member`. + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/UserRoleUpdateRequest' + responses: + "200": + description: User role updated successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/User' + x-oaiMeta: + name: Modify user + group: administration + returns: The updated [User](/docs/api-reference/users/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/organization/users/user_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "role": "owner" + }' + response: + content: | + { + "object": "organization.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + + delete: + summary: Deletes a user from the organization. + operationId: delete-user + tags: + - Users + parameters: + - name: user_id + in: path + description: The ID of the user. + required: true + schema: + type: string + responses: + "200": + description: User deleted successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/UserDeleteResponse' + x-oaiMeta: + name: Delete user + group: administration + returns: Confirmation of the deleted user + examples: + request: + curl: | + curl -X DELETE https://api.openai.com/v1/organization/users/user_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.user.deleted", + "id": "user_abc", + "deleted": true + } + /organization/projects: + get: + summary: Returns a list of projects. + operationId: list-projects + tags: + - Projects + parameters: + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: after + in: query + description: *pagination_after_param_description + required: false + schema: + type: string + - name: include_archived + in: query + schema: + type: boolean + default: false + description: If `true` returns all projects including those that have been `archived`. Archived projects are not included by default. + responses: + "200": + description: Projects listed successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectListResponse' + x-oaiMeta: + name: List projects + group: administration + returns: A list of [Project](/docs/api-reference/projects/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/projects?after=proj_abc&limit=20&include_archived=false \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "list", + "data": [ + { + "id": "proj_abc", + "object": "organization.project", + "name": "Project example", + "created_at": 1711471533, + "archived_at": null, + "status": "active" + } + ], + "first_id": "proj-abc", + "last_id": "proj-xyz", + "has_more": false + } + + post: + summary: Create a new project in the organization. Projects can be created and archived, but cannot be deleted. + operationId: create-project + tags: + - Projects + requestBody: + description: The project create request payload. + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectCreateRequest' + responses: + "200": + description: Project created successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/Project' + x-oaiMeta: + name: Create project + group: administration + returns: The created [Project](/docs/api-reference/projects/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/organization/projects \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Project ABC" + }' + response: + content: | + { + "id": "proj_abc", + "object": "organization.project", + "name": "Project ABC", + "created_at": 1711471533, + "archived_at": null, + "status": "active" + } + + /organization/projects/{project_id}: + get: + summary: Retrieves a project. + operationId: retrieve-project + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + responses: + "200": + description: Project retrieved successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/Project' + x-oaiMeta: + name: Retrieve project + group: administration + description: Retrieve a project. + returns: The [Project](/docs/api-reference/projects/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/projects/proj_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "id": "proj_abc", + "object": "organization.project", + "name": "Project example", + "created_at": 1711471533, + "archived_at": null, + "status": "active" + } + + post: + summary: Modifies a project in the organization. + operationId: modify-project + tags: + - Projects + requestBody: + description: The project update request payload. + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectUpdateRequest' + responses: + "200": + description: Project updated successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/Project' + "400": + description: Error response when updating the default project. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + x-oaiMeta: + name: Modify project + group: administration + returns: The updated [Project](/docs/api-reference/projects/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/organization/projects/proj_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Project DEF" + }' + + /organization/projects/{project_id}/archive: + post: + summary: Archives a project in the organization. Archived projects cannot be used or updated. + operationId: archive-project + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + responses: + "200": + description: Project archived successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/Project' + x-oaiMeta: + name: Archive project + group: administration + returns: The archived [Project](/docs/api-reference/projects/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/organization/projects/proj_abc/archive \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "id": "proj_abc", + "object": "organization.project", + "name": "Project DEF", + "created_at": 1711471533, + "archived_at": 1711471533, + "status": "archived" + } + + + /organization/projects/{project_id}/users: + get: + summary: Returns a list of users in the project. + operationId: list-project-users + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: after + in: query + description: *pagination_after_param_description + required: false + schema: + type: string + responses: + "200": + description: Project users listed successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectUserListResponse' + "400": + description: Error response when project is archived. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + x-oaiMeta: + name: List project users + group: administration + returns: A list of [ProjectUser](/docs/api-reference/project-users/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/projects/proj_abc/users?after=user_abc&limit=20 \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "list", + "data": [ + { + "object": "organization.project.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + ], + "first_id": "user-abc", + "last_id": "user-xyz", + "has_more": false + } + error_response: + content: | + { + "code": 400, + "message": "Project {name} is archived" + } + + post: + summary: Adds a user to the project. Users must already be members of the organization to be added to a project. + operationId: create-project-user + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + tags: + - Projects + requestBody: + description: The project user create request payload. + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectUserCreateRequest' + responses: + "200": + description: User added to project successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectUser' + "400": + description: Error response for various conditions. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + x-oaiMeta: + name: Create project user + group: administration + returns: The created [ProjectUser](/docs/api-reference/project-users/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/organization/projects/proj_abc/users \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "user_id": "user_abc", + "role": "member" + }' + response: + content: | + { + "object": "organization.project.user", + "id": "user_abc", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + error_response: + content: | + { + "code": 400, + "message": "Project {name} is archived" + } + + /organization/projects/{project_id}/users/{user_id}: + get: + summary: Retrieves a user in the project. + operationId: retrieve-project-user + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: user_id + in: path + description: The ID of the user. + required: true + schema: + type: string + responses: + "200": + description: Project user retrieved successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectUser' + x-oaiMeta: + name: Retrieve project user + group: administration + returns: The [ProjectUser](/docs/api-reference/project-users/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/projects/proj_abc/users/user_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.project.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + + post: + summary: Modifies a user's role in the project. + operationId: modify-project-user + tags: + - Projects + requestBody: + description: The project user update request payload. + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectUserUpdateRequest' + responses: + "200": + description: Project user's role updated successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectUser' + "400": + description: Error response for various conditions. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + x-oaiMeta: + name: Modify project user + group: administration + returns: The updated [ProjectUser](/docs/api-reference/project-users/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/organization/projects/proj_abc/users/user_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "role": "owner" + }' + response: + content: | + { + "object": "organization.project.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + + delete: + summary: Deletes a user from the project. + operationId: delete-project-user + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: user_id + in: path + description: The ID of the user. + required: true + schema: + type: string + responses: + "200": + description: Project user deleted successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectUserDeleteResponse' + "400": + description: Error response for various conditions. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + x-oaiMeta: + name: Delete project user + group: administration + returns: Confirmation that project has been deleted or an error in case of an archived project, which has no users + examples: + request: + curl: | + curl -X DELETE https://api.openai.com/v1/organization/projects/proj_abc/users/user_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.project.user.deleted", + "id": "user_abc", + "deleted": true + } + + /organization/projects/{project_id}/service_accounts: + get: + summary: Returns a list of service accounts in the project. + operationId: list-project-service-accounts + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: after + in: query + description: *pagination_after_param_description + required: false + schema: + type: string + responses: + "200": + description: Project service accounts listed successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectServiceAccountListResponse' + "400": + description: Error response when project is archived. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + x-oaiMeta: + name: List project service accounts + group: administration + returns: A list of [ProjectServiceAccount](/docs/api-reference/project-service-accounts/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/projects/proj_abc/service_accounts?after=custom_id&limit=20 \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "list", + "data": [ + { + "object": "organization.project.service_account", + "id": "svc_acct_abc", + "name": "Service Account", + "role": "owner", + "created_at": 1711471533 + } + ], + "first_id": "svc_acct_abc", + "last_id": "svc_acct_xyz", + "has_more": false + } + + post: + summary: Creates a new service account in the project. This also returns an unredacted API key for the service account. + operationId: create-project-service-account + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + requestBody: + description: The project service account create request payload. + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectServiceAccountCreateRequest' + responses: + "200": + description: Project service account created successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectServiceAccountCreateResponse' + "400": + description: Error response when project is archived. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + x-oaiMeta: + name: Create project service account + group: administration + returns: The created [ProjectServiceAccount](/docs/api-reference/project-service-accounts/object) object. + examples: + request: + curl: | + curl -X POST https://api.openai.com/v1/organization/projects/proj_abc/service_accounts \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Production App" + }' + response: + content: | + { + "object": "organization.project.service_account", + "id": "svc_acct_abc", + "name": "Production App", + "role": "member", + "created_at": 1711471533, + "api_key": { + "object": "organization.project.service_account.api_key", + "value": "sk-abcdefghijklmnop123", + "name": "Secret Key", + "created_at": 1711471533, + "id": "key_abc" + } + } + + /organization/projects/{project_id}/service_accounts/{service_account_id}: + get: + summary: Retrieves a service account in the project. + operationId: retrieve-project-service-account + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: service_account_id + in: path + description: The ID of the service account. + required: true + schema: + type: string + responses: + "200": + description: Project service account retrieved successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectServiceAccount' + x-oaiMeta: + name: Retrieve project service account + group: administration + returns: The [ProjectServiceAccount](/docs/api-reference/project-service-accounts/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/projects/proj_abc/service_accounts/svc_acct_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.project.service_account", + "id": "svc_acct_abc", + "name": "Service Account", + "role": "owner", + "created_at": 1711471533 + } + + delete: + summary: Deletes a service account from the project. + operationId: delete-project-service-account + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: service_account_id + in: path + description: The ID of the service account. + required: true + schema: + type: string + responses: + "200": + description: Project service account deleted successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectServiceAccountDeleteResponse' + x-oaiMeta: + name: Delete project service account + group: administration + returns: Confirmation of service account being deleted, or an error in case of an archived project, which has no service accounts + examples: + request: + curl: | + curl -X DELETE https://api.openai.com/v1/organization/projects/proj_abc/service_accounts/svc_acct_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.project.service_account.deleted", + "id": "svc_acct_abc", + "deleted": true + } + + /organization/projects/{project_id}/api_keys: + get: + summary: Returns a list of API keys in the project. + operationId: list-project-api-keys + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: limit + in: query + description: *pagination_limit_param_description + required: false + schema: + type: integer + default: 20 + - name: after + in: query + description: *pagination_after_param_description + required: false + schema: + type: string + responses: + "200": + description: Project API keys listed successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectApiKeyListResponse' + + x-oaiMeta: + name: List project API keys + group: administration + returns: A list of [ProjectApiKey](/docs/api-reference/project-api-keys/object) objects. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/projects/proj_abc/api_keys?after=key_abc&limit=20 \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "list", + "data": [ + { + "object": "organization.project.api_key", + "redacted_value": "sk-abc...def", + "name": "My API Key", + "created_at": 1711471533, + "id": "key_abc", + "owner": { + "type": "user", + "user": { + "object": "organization.project.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + } + } + ], + "first_id": "key_abc", + "last_id": "key_xyz", + "has_more": false + } + error_response: + content: | + { + "code": 400, + "message": "Project {name} is archived" + } + + /organization/projects/{project_id}/api_keys/{key_id}: + get: + summary: Retrieves an API key in the project. + operationId: retrieve-project-api-key + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: key_id + in: path + description: The ID of the API key. + required: true + schema: + type: string + responses: + "200": + description: Project API key retrieved successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectApiKey' + x-oaiMeta: + name: Retrieve project API key + group: administration + returns: The [ProjectApiKey](/docs/api-reference/project-api-keys/object) object matching the specified ID. + examples: + request: + curl: | + curl https://api.openai.com/v1/organization/projects/proj_abc/api_keys/key_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.project.api_key", + "redacted_value": "sk-abc...def", + "name": "My API Key", + "created_at": 1711471533, + "id": "key_abc", + "owner": { + "type": "user", + "user": { + "object": "organization.project.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + } + } + + delete: + summary: Deletes an API key from the project. + operationId: delete-project-api-key + tags: + - Projects + parameters: + - name: project_id + in: path + description: The ID of the project. + required: true + schema: + type: string + - name: key_id + in: path + description: The ID of the API key. + required: true + schema: + type: string + responses: + "200": + description: Project API key deleted successfully. + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectApiKeyDeleteResponse' + "400": + description: Error response for various conditions. + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + x-oaiMeta: + name: Delete project API key + group: administration + returns: Confirmation of the key's deletion or an error if the key belonged to a service account + examples: + request: + curl: | + curl -X DELETE https://api.openai.com/v1/organization/projects/proj_abc/api_keys/key_abc \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: + content: | + { + "object": "organization.project.api_key.deleted", + "id": "key_abc", + "deleted": true + } + error_response: + content: | + { + "code": 400, + "message": "API keys cannot be deleted for service accounts, please delete the service account" + } + +components: + securitySchemes: + ApiKeyAuth: + type: http + scheme: "bearer" + + schemas: + Error: + type: object + properties: + code: + type: string + nullable: true + message: + type: string + nullable: false + param: + type: string + nullable: true + type: + type: string + nullable: false + required: + - type + - message + - param + - code + ErrorResponse: + type: object + properties: + error: + $ref: "#/components/schemas/Error" + required: + - error + + ListModelsResponse: + type: object + properties: + object: + type: string + enum: [list] + data: + type: array + items: + $ref: "#/components/schemas/Model" + required: + - object + - data + DeleteModelResponse: + type: object + properties: + id: + type: string + deleted: + type: boolean + object: + type: string + required: + - id + - object + - deleted + + CreateCompletionRequest: + type: object + properties: + model: + description: &model_description | + ID of the model to use. You can use the [List models](/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](/docs/models/overview) for descriptions of them. + anyOf: + - type: string + - type: string + enum: ["gpt-3.5-turbo-instruct", "davinci-002", "babbage-002"] + x-oaiTypeLabel: string + prompt: + description: &completions_prompt_description | + The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays. + + Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document. + default: "<|endoftext|>" + nullable: true + oneOf: + - type: string + default: "" + example: "This is a test." + - type: array + items: + type: string + default: "" + example: "This is a test." + - type: array + minItems: 1 + items: + type: integer + example: "[1212, 318, 257, 1332, 13]" + - type: array + minItems: 1 + items: + type: array + minItems: 1 + items: + type: integer + example: "[[1212, 318, 257, 1332, 13]]" + best_of: + type: integer + default: 1 + minimum: 0 + maximum: 20 + nullable: true + description: &completions_best_of_description | + Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed. + + When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`. + + **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. + echo: + type: boolean + default: false + nullable: true + description: &completions_echo_description > + Echo back the prompt in addition to the completion + frequency_penalty: + type: number + default: 0 + minimum: -2 + maximum: 2 + nullable: true + description: &completions_frequency_penalty_description | + Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. + + [See more information about frequency and presence penalties.](/docs/guides/text-generation/parameter-details) + logit_bias: &completions_logit_bias + type: object + x-oaiTypeLabel: map + default: null + nullable: true + additionalProperties: + type: integer + description: &completions_logit_bias_description | + Modify the likelihood of specified tokens appearing in the completion. + + Accepts a JSON object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](/tokenizer?view=bpe) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. + + As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated. + logprobs: &completions_logprobs_configuration + type: integer + minimum: 0 + maximum: 5 + default: null + nullable: true + description: &completions_logprobs_description | + Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response. + + The maximum value for `logprobs` is 5. + max_tokens: + type: integer + minimum: 0 + default: 16 + example: 16 + nullable: true + description: &completions_max_tokens_description | + The maximum number of [tokens](/tokenizer) that can be generated in the completion. + + The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. + n: + type: integer + minimum: 1 + maximum: 128 + default: 1 + example: 1 + nullable: true + description: &completions_completions_description | + How many completions to generate for each prompt. + + **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. + presence_penalty: + type: number + default: 0 + minimum: -2 + maximum: 2 + nullable: true + description: &completions_presence_penalty_description | + Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. + + [See more information about frequency and presence penalties.](/docs/guides/text-generation/parameter-details) + seed: &completions_seed_param + type: integer + minimum: -9223372036854775808 + maximum: 9223372036854775807 + nullable: true + description: | + If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result. + + Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. + stop: + description: &completions_stop_description > + Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. + default: null + nullable: true + oneOf: + - type: string + default: <|endoftext|> + example: "\n" + nullable: true + - type: array + minItems: 1 + maxItems: 4 + items: + type: string + example: '["\n"]' + stream: + description: > + Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) + as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions). + type: boolean + nullable: true + default: false + stream_options: + $ref: "#/components/schemas/ChatCompletionStreamOptions" + suffix: + description: | + The suffix that comes after a completion of inserted text. + + This parameter is only supported for `gpt-3.5-turbo-instruct`. + default: null + nullable: true + type: string + example: "test." + temperature: + type: number + minimum: 0 + maximum: 2 + default: 1 + example: 1 + nullable: true + description: &completions_temperature_description | + What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + + We generally recommend altering this or `top_p` but not both. + top_p: + type: number + minimum: 0 + maximum: 1 + default: 1 + example: 1 + nullable: true + description: &completions_top_p_description | + An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + + We generally recommend altering this or `temperature` but not both. + user: &end_user_param_configuration + type: string + example: user-1234 + description: | + A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](/docs/guides/safety-best-practices/end-user-ids). + required: + - model + - prompt + + CreateCompletionResponse: + type: object + description: | + Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint). + properties: + id: + type: string + description: A unique identifier for the completion. + choices: + type: array + description: The list of completion choices the model generated for the input prompt. + items: + type: object + required: + - finish_reason + - index + - logprobs + - text + properties: + finish_reason: + type: string + description: &completion_finish_reason_description | + The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence, + `length` if the maximum number of tokens specified in the request was reached, + or `content_filter` if content was omitted due to a flag from our content filters. + enum: ["stop", "length", "content_filter"] + index: + type: integer + logprobs: + type: object + nullable: true + properties: + text_offset: + type: array + items: + type: integer + token_logprobs: + type: array + items: + type: number + tokens: + type: array + items: + type: string + top_logprobs: + type: array + items: + type: object + additionalProperties: + type: number + text: + type: string + created: + type: integer + description: The Unix timestamp (in seconds) of when the completion was created. + model: + type: string + description: The model used for completion. + system_fingerprint: + type: string + description: | + This fingerprint represents the backend configuration that the model runs with. + + Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism. + object: + type: string + description: The object type, which is always "text_completion" + enum: [text_completion] + usage: + $ref: "#/components/schemas/CompletionUsage" + required: + - id + - object + - created + - model + - choices + x-oaiMeta: + name: The completion object + legacy: true + example: | + { + "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7", + "object": "text_completion", + "created": 1589478378, + "model": "gpt-4-turbo", + "choices": [ + { + "text": "\n\nThis is indeed a test", + "index": 0, + "logprobs": null, + "finish_reason": "length" + } + ], + "usage": { + "prompt_tokens": 5, + "completion_tokens": 7, + "total_tokens": 12 + } + } + + ChatCompletionRequestMessageContentPartText: + type: object + title: Text content part + properties: + type: + type: string + enum: ["text"] + description: The type of the content part. + text: + type: string + description: The text content. + required: + - type + - text + + ChatCompletionRequestMessageContentPartImage: + type: object + title: Image content part + properties: + type: + type: string + enum: ["image_url"] + description: The type of the content part. + image_url: + type: object + properties: + url: + type: string + description: Either a URL of the image or the base64 encoded image data. + format: uri + detail: + type: string + description: Specifies the detail level of the image. Learn more in the [Vision guide](/docs/guides/vision/low-or-high-fidelity-image-understanding). + enum: ["auto", "low", "high"] + default: "auto" + required: + - url + required: + - type + - image_url + + ChatCompletionRequestMessageContentPartRefusal: + type: object + title: Refusal content part + properties: + type: + type: string + enum: ["refusal"] + description: The type of the content part. + refusal: + type: string + description: The refusal message generated by the model. + required: + - type + - refusal + + ChatCompletionRequestMessage: + oneOf: + - $ref: "#/components/schemas/ChatCompletionRequestSystemMessage" + - $ref: "#/components/schemas/ChatCompletionRequestUserMessage" + - $ref: "#/components/schemas/ChatCompletionRequestAssistantMessage" + - $ref: "#/components/schemas/ChatCompletionRequestToolMessage" + - $ref: "#/components/schemas/ChatCompletionRequestFunctionMessage" + x-oaiExpandable: true + + ChatCompletionRequestSystemMessageContentPart: + oneOf: + - $ref: "#/components/schemas/ChatCompletionRequestMessageContentPartText" + x-oaiExpandable: true + + ChatCompletionRequestUserMessageContentPart: + oneOf: + - $ref: "#/components/schemas/ChatCompletionRequestMessageContentPartText" + - $ref: "#/components/schemas/ChatCompletionRequestMessageContentPartImage" + x-oaiExpandable: true + + ChatCompletionRequestAssistantMessageContentPart: + oneOf: + - $ref: "#/components/schemas/ChatCompletionRequestMessageContentPartText" + - $ref: "#/components/schemas/ChatCompletionRequestMessageContentPartRefusal" + x-oaiExpandable: true + + ChatCompletionRequestToolMessageContentPart: + oneOf: + - $ref: "#/components/schemas/ChatCompletionRequestMessageContentPartText" + x-oaiExpandable: true + + ChatCompletionRequestSystemMessage: + type: object + title: System message + properties: + content: + description: The contents of the system message. + oneOf: + - type: string + description: The contents of the system message. + title: Text content + - type: array + description: An array of content parts with a defined type. For system messages, only type `text` is supported. + title: Array of content parts + items: + $ref: "#/components/schemas/ChatCompletionRequestSystemMessageContentPart" + minItems: 1 + role: + type: string + enum: ["system"] + description: The role of the messages author, in this case `system`. + name: + type: string + description: An optional name for the participant. Provides the model information to differentiate between participants of the same role. + required: + - content + - role + + ChatCompletionRequestUserMessage: + type: object + title: User message + properties: + content: + description: | + The contents of the user message. + oneOf: + - type: string + description: The text contents of the message. + title: Text content + - type: array + description: An array of content parts with a defined type, each can be of type `text` or `image_url` when passing in images. You can pass multiple images by adding multiple `image_url` content parts. Image input is only supported when using the `gpt-4o` model. + title: Array of content parts + items: + $ref: "#/components/schemas/ChatCompletionRequestUserMessageContentPart" + minItems: 1 + x-oaiExpandable: true + role: + type: string + enum: ["user"] + description: The role of the messages author, in this case `user`. + name: + type: string + description: An optional name for the participant. Provides the model information to differentiate between participants of the same role. + required: + - content + - role + + ChatCompletionRequestAssistantMessage: + type: object + title: Assistant message + properties: + content: + nullable: true + oneOf: + - type: string + description: The contents of the assistant message. + title: Text content + - type: array + description: An array of content parts with a defined type. Can be one or more of type `text`, or exactly one of type `refusal`. + title: Array of content parts + items: + $ref: "#/components/schemas/ChatCompletionRequestAssistantMessageContentPart" + minItems: 1 + description: | + The contents of the assistant message. Required unless `tool_calls` or `function_call` is specified. + refusal: + nullable: true + type: string + description: The refusal message by the assistant. + role: + type: string + enum: ["assistant"] + description: The role of the messages author, in this case `assistant`. + name: + type: string + description: An optional name for the participant. Provides the model information to differentiate between participants of the same role. + tool_calls: + $ref: "#/components/schemas/ChatCompletionMessageToolCalls" + function_call: + type: object + deprecated: true + description: "Deprecated and replaced by `tool_calls`. The name and arguments of a function that should be called, as generated by the model." + nullable: true + properties: + arguments: + type: string + description: The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function. + name: + type: string + description: The name of the function to call. + required: + - arguments + - name + required: + - role + + FineTuneChatCompletionRequestAssistantMessage: + allOf: + - type: object + title: Assistant message + deprecated: false + properties: + weight: + type: integer + enum: [0, 1] + description: "Controls whether the assistant message is trained against (0 or 1)" + - $ref: "#/components/schemas/ChatCompletionRequestAssistantMessage" + required: + - role + + ChatCompletionRequestToolMessage: + type: object + title: Tool message + properties: + role: + type: string + enum: ["tool"] + description: The role of the messages author, in this case `tool`. + content: + oneOf: + - type: string + description: The contents of the tool message. + title: Text content + - type: array + description: An array of content parts with a defined type. For tool messages, only type `text` is supported. + title: Array of content parts + items: + $ref: "#/components/schemas/ChatCompletionRequestToolMessageContentPart" + minItems: 1 + description: The contents of the tool message. + tool_call_id: + type: string + description: Tool call that this message is responding to. + required: + - role + - content + - tool_call_id + + ChatCompletionRequestFunctionMessage: + type: object + title: Function message + deprecated: true + properties: + role: + type: string + enum: ["function"] + description: The role of the messages author, in this case `function`. + content: + nullable: true + type: string + description: The contents of the function message. + name: + type: string + description: The name of the function to call. + required: + - role + - content + - name + + FunctionParameters: + type: object + description: "The parameters the functions accepts, described as a JSON Schema object. See the [guide](/docs/guides/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format. \n\nOmitting `parameters` defines a function with an empty parameter list." + additionalProperties: true + + ChatCompletionFunctions: + type: object + deprecated: true + properties: + description: + type: string + description: A description of what the function does, used by the model to choose when and how to call the function. + name: + type: string + description: The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + parameters: + $ref: "#/components/schemas/FunctionParameters" + required: + - name + + ChatCompletionFunctionCallOption: + type: object + description: > + Specifying a particular function via `{"name": "my_function"}` forces the model to call that function. + properties: + name: + type: string + description: The name of the function to call. + required: + - name + + ChatCompletionTool: + type: object + properties: + type: + type: string + enum: ["function"] + description: The type of the tool. Currently, only `function` is supported. + function: + $ref: "#/components/schemas/FunctionObject" + required: + - type + - function + + FunctionObject: + type: object + properties: + description: + type: string + description: A description of what the function does, used by the model to choose when and how to call the function. + name: + type: string + description: The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + parameters: + $ref: "#/components/schemas/FunctionParameters" + strict: + type: boolean + nullable: true + default: false + description: Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the `parameters` field. Only a subset of JSON Schema is supported when `strict` is `true`. Learn more about Structured Outputs in the [function calling guide](docs/guides/function-calling). + required: + - name + + ResponseFormatText: + type: object + properties: + type: + type: string + description: "The type of response format being defined: `text`" + enum: ["text"] + required: + - type + + ResponseFormatJsonObject: + type: object + properties: + type: + type: string + description: "The type of response format being defined: `json_object`" + enum: ["json_object"] + required: + - type + + ResponseFormatJsonSchemaSchema: + type: object + description: "The schema for the response format, described as a JSON Schema object." + additionalProperties: true + + ResponseFormatJsonSchema: + type: object + properties: + type: + type: string + description: 'The type of response format being defined: `json_schema`' + enum: ['json_schema'] + json_schema: + type: object + properties: + description: + type: string + description: A description of what the response format is for, used by the model to determine how to respond in the format. + name: + type: string + description: The name of the response format. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + schema: + $ref: '#/components/schemas/ResponseFormatJsonSchemaSchema' + strict: + type: boolean + nullable: true + default: false + description: Whether to enable strict schema adherence when generating the output. If set to true, the model will always follow the exact schema defined in the `schema` field. Only a subset of JSON Schema is supported when `strict` is `true`. To learn more, read the [Structured Outputs guide](/docs/guides/structured-outputs). + required: + - type + - name + required: + - type + - json_schema + + ChatCompletionToolChoiceOption: + description: | + Controls which (if any) tool is called by the model. + `none` means the model will not call any tool and instead generates a message. + `auto` means the model can pick between generating a message or calling one or more tools. + `required` means the model must call one or more tools. + Specifying a particular tool via `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool. + + `none` is the default when no tools are present. `auto` is the default if tools are present. + oneOf: + - type: string + description: > + `none` means the model will not call any tool and instead generates a message. + `auto` means the model can pick between generating a message or calling one or more tools. + `required` means the model must call one or more tools. + enum: [none, auto, required] + - $ref: "#/components/schemas/ChatCompletionNamedToolChoice" + x-oaiExpandable: true + + ChatCompletionNamedToolChoice: + type: object + description: Specifies a tool the model should use. Use to force the model to call a specific function. + properties: + type: + type: string + enum: ["function"] + description: The type of the tool. Currently, only `function` is supported. + function: + type: object + properties: + name: + type: string + description: The name of the function to call. + required: + - name + required: + - type + - function + + ParallelToolCalls: + description: Whether to enable [parallel function calling](/docs/guides/function-calling/parallel-function-calling) during tool use. type: boolean - default: false - description: | - Whether to stream events for the fine-tune job. If set to true, - events will be sent as data-only - [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) - as they become available. The stream will terminate with a - `data: [DONE]` message when the job is finished (succeeded, cancelled, - or failed). - - If set to false, only events generated so far will be returned. - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/ListFineTuneEventsResponse' - x-oaiMeta: - name: List fine-tune events - group: fine-tunes - path: events - beta: true - examples: - curl: | - curl https://api.openai.com/v1/fine-tunes/ft-AF1WoRqd3aJAHsqc9NY7iL8F/events \ - -H "Authorization: Bearer YOUR_API_KEY" - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.FineTune.list_events(id="ft-AF1WoRqd3aJAHsqc9NY7iL8F") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.listFineTuneEvents("ft-AF1WoRqd3aJAHsqc9NY7iL8F"); - response: | - { - "object": "list", - "data": [ - { - "object": "fine-tune-event", - "created_at": 1614807352, - "level": "info", - "message": "Job enqueued. Waiting for jobs ahead to complete. Queue number: 0." - }, - { - "object": "fine-tune-event", - "created_at": 1614807356, - "level": "info", - "message": "Job started." - }, - { - "object": "fine-tune-event", - "created_at": 1614807861, - "level": "info", - "message": "Uploaded snapshot: curie:ft-acmeco-2021-03-03-21-44-20." - }, - { - "object": "fine-tune-event", - "created_at": 1614807864, - "level": "info", - "message": "Uploaded result files: file-QQm6ZpqdNwAaVC3aSz5sWwLT." - }, - { - "object": "fine-tune-event", - "created_at": 1614807864, - "level": "info", - "message": "Job succeeded." - } - ] - } - - /models: - get: - operationId: listModels - tags: - - OpenAI - summary: Lists the currently available models, and provides basic information about each one such as the owner and availability. - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/ListModelsResponse' - x-oaiMeta: - name: List models - group: models - path: list - examples: - curl: | - curl https://api.openai.com/v1/models \ - -H 'Authorization: Bearer YOUR_API_KEY' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Model.list() - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.listModels(); - response: | - { - "data": [ - { - "id": "model-id-0", - "object": "model", - "owned_by": "organization-owner", - "permission": [...] - }, - { - "id": "model-id-1", - "object": "model", - "owned_by": "organization-owner", - "permission": [...] - }, - { - "id": "model-id-2", - "object": "model", - "owned_by": "openai", - "permission": [...] - }, - ], - "object": "list" - } - - /models/{model}: - get: - operationId: retrieveModel - tags: - - OpenAI - summary: Retrieves a model instance, providing basic information about the model such as the owner and permissioning. - parameters: - - in: path - name: model - required: true - schema: + default: true + + ChatCompletionMessageToolCalls: + type: array + description: The tool calls generated by the model, such as function calls. + items: + $ref: "#/components/schemas/ChatCompletionMessageToolCall" + + ChatCompletionMessageToolCall: + type: object + properties: + # TODO: index included when streaming + id: + type: string + description: The ID of the tool call. + type: + type: string + enum: ["function"] + description: The type of the tool. Currently, only `function` is supported. + function: + type: object + description: The function that the model called. + properties: + name: + type: string + description: The name of the function to call. + arguments: + type: string + description: The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function. + required: + - name + - arguments + required: + - id + - type + - function + + ChatCompletionMessageToolCallChunk: + type: object + properties: + index: + type: integer + id: + type: string + description: The ID of the tool call. + type: + type: string + enum: ["function"] + description: The type of the tool. Currently, only `function` is supported. + function: + type: object + properties: + name: + type: string + description: The name of the function to call. + arguments: + type: string + description: The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function. + required: + - index + + # Note, this isn't referenced anywhere, but is kept as a convenience to record all possible roles in one place. + ChatCompletionRole: type: string - # ideally this will be an actual ID, so this will always work from browser - example: - text-davinci-001 - description: - The ID of the model to use for this request - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/Model' - x-oaiMeta: - name: Retrieve model - group: models - path: retrieve - examples: - curl: | - curl -u :YOUR_API_KEY https://api.openai.com/v1/models/VAR_model_id - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Model.retrieve("VAR_model_id") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.retrieveModel("VAR_model_id"); - response: | - { - "id": "VAR_model_id", - "object": "model", - "owned_by": "openai", - "permission": [...] - } - delete: - operationId: deleteModel - tags: - - OpenAI - summary: Delete a fine-tuned model. You must have the Owner role in your organization. - parameters: - - in: path - name: model - required: true - schema: + description: The role of the author of a message + enum: + - system + - user + - assistant + - tool + - function + + ChatCompletionStreamOptions: + description: | + Options for streaming response. Only set this when you set `stream: true`. + type: object + nullable: true + default: null + properties: + include_usage: + type: boolean + description: | + If set, an additional chunk will be streamed before the `data: [DONE]` message. The `usage` field on this chunk shows the token usage statistics for the entire request, and the `choices` field will always be an empty array. All other chunks will also include a `usage` field, but with a null value. + + ChatCompletionResponseMessage: + type: object + description: A chat completion message generated by the model. + properties: + content: + type: string + description: The contents of the message. + nullable: true + refusal: + type: string + description: The refusal message generated by the model. + nullable: true + tool_calls: + $ref: "#/components/schemas/ChatCompletionMessageToolCalls" + role: + type: string + enum: ["assistant"] + description: The role of the author of this message. + function_call: + type: object + deprecated: true + description: "Deprecated and replaced by `tool_calls`. The name and arguments of a function that should be called, as generated by the model." + properties: + arguments: + type: string + description: The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function. + name: + type: string + description: The name of the function to call. + required: + - name + - arguments + required: + - role + - content + - refusal + + ChatCompletionStreamResponseDelta: + type: object + description: A chat completion delta generated by streamed model responses. + properties: + content: + type: string + description: The contents of the chunk message. + nullable: true + function_call: + deprecated: true + type: object + description: "Deprecated and replaced by `tool_calls`. The name and arguments of a function that should be called, as generated by the model." + properties: + arguments: + type: string + description: The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function. + name: + type: string + description: The name of the function to call. + tool_calls: + type: array + items: + $ref: "#/components/schemas/ChatCompletionMessageToolCallChunk" + role: + type: string + enum: ["system", "user", "assistant", "tool"] + description: The role of the author of this message. + refusal: + type: string + description: The refusal message generated by the model. + nullable: true + + CreateChatCompletionRequest: + type: object + properties: + messages: + description: A list of messages comprising the conversation so far. [Example Python code](https://cookbook.openai.com/examples/how_to_format_inputs_to_chatgpt_models). + type: array + minItems: 1 + items: + $ref: "#/components/schemas/ChatCompletionRequestMessage" + model: + description: ID of the model to use. See the [model endpoint compatibility](/docs/models/model-endpoint-compatibility) table for details on which models work with the Chat API. + example: "gpt-4o" + anyOf: + - type: string + - type: string + enum: + [ + "o1-preview", + "o1-preview-2024-09-12", + "o1-mini", + "o1-mini-2024-09-12", + "gpt-4o", + "gpt-4o-2024-08-06", + "gpt-4o-2024-05-13", + "gpt-4o-2024-08-06", + "chatgpt-4o-latest", + "gpt-4o-mini", + "gpt-4o-mini-2024-07-18", + "gpt-4-turbo", + "gpt-4-turbo-2024-04-09", + "gpt-4-0125-preview", + "gpt-4-turbo-preview", + "gpt-4-1106-preview", + "gpt-4-vision-preview", + "gpt-4", + "gpt-4-0314", + "gpt-4-0613", + "gpt-4-32k", + "gpt-4-32k-0314", + "gpt-4-32k-0613", + "gpt-3.5-turbo", + "gpt-3.5-turbo-16k", + "gpt-3.5-turbo-0301", + "gpt-3.5-turbo-0613", + "gpt-3.5-turbo-1106", + "gpt-3.5-turbo-0125", + "gpt-3.5-turbo-16k-0613", + ] + x-oaiTypeLabel: string + frequency_penalty: + type: number + default: 0 + minimum: -2 + maximum: 2 + nullable: true + description: *completions_frequency_penalty_description + logit_bias: + type: object + x-oaiTypeLabel: map + default: null + nullable: true + additionalProperties: + type: integer + description: | + Modify the likelihood of specified tokens appearing in the completion. + + Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. + logprobs: + description: Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the `content` of `message`. + type: boolean + default: false + nullable: true + top_logprobs: + description: An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. `logprobs` must be set to `true` if this parameter is used. + type: integer + minimum: 0 + maximum: 20 + nullable: true + max_tokens: + description: | + The maximum number of [tokens](/tokenizer) that can be generated in the chat completion. This value can be used to control [costs](https://openai.com/api/pricing/) for text generated via API. + + This value is now deprecated in favor of `max_completion_tokens`, and is not compatible with [o1 series models](/docs/guides/reasoning). + type: integer + nullable: true + deprecated: true + max_completion_tokens: + description: | + An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and [reasoning tokens](/docs/guides/reasoning). + type: integer + nullable: true + + n: + type: integer + minimum: 1 + maximum: 128 + default: 1 + example: 1 + nullable: true + description: How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep `n` as `1` to minimize costs. + presence_penalty: + type: number + default: 0 + minimum: -2 + maximum: 2 + nullable: true + description: *completions_presence_penalty_description + response_format: + description: | + An object specifying the format that the model must output. Compatible with [GPT-4o](/docs/models/gpt-4o), [GPT-4o mini](/docs/models/gpt-4o-mini), [GPT-4 Turbo](/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. + + Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures the model will match your supplied JSON schema. Learn more in the [Structured Outputs guide](/docs/guides/structured-outputs). + + Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the message the model generates is valid JSON. + + **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. + oneOf: + - $ref: "#/components/schemas/ResponseFormatText" + - $ref: "#/components/schemas/ResponseFormatJsonObject" + - $ref: "#/components/schemas/ResponseFormatJsonSchema" + x-oaiExpandable: true + seed: + type: integer + minimum: -9223372036854775808 + maximum: 9223372036854775807 + nullable: true + description: | + This feature is in Beta. + If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result. + Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. + x-oaiMeta: + beta: true + service_tier: + description: | + Specifies the latency tier to use for processing the request. This parameter is relevant for customers subscribed to the scale tier service: + - If set to 'auto', and the Project is Scale tier enabled, the system will utilize scale tier credits until they are exhausted. + - If set to 'auto', and the Project is not Scale tier enabled, the request will be processed using the default service tier with a lower uptime SLA and no latency guarentee. + - If set to 'default', the request will be processed using the default service tier with a lower uptime SLA and no latency guarentee. + - When not set, the default behavior is 'auto'. + + When this parameter is set, the response body will include the `service_tier` utilized. + type: string + enum: ["auto", "default"] + nullable: true + default: null + stop: + description: | + Up to 4 sequences where the API will stop generating further tokens. + default: null + oneOf: + - type: string + nullable: true + - type: array + minItems: 1 + maxItems: 4 + items: + type: string + stream: + description: > + If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) + as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions). + type: boolean + nullable: true + default: false + stream_options: + $ref: "#/components/schemas/ChatCompletionStreamOptions" + temperature: + type: number + minimum: 0 + maximum: 2 + default: 1 + example: 1 + nullable: true + description: *completions_temperature_description + top_p: + type: number + minimum: 0 + maximum: 1 + default: 1 + example: 1 + nullable: true + description: *completions_top_p_description + tools: + type: array + description: > + A list of tools the model may call. Currently, only functions are supported as a tool. + Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported. + items: + $ref: "#/components/schemas/ChatCompletionTool" + tool_choice: + $ref: "#/components/schemas/ChatCompletionToolChoiceOption" + parallel_tool_calls: + $ref: "#/components/schemas/ParallelToolCalls" + user: *end_user_param_configuration + function_call: + deprecated: true + description: | + Deprecated in favor of `tool_choice`. + + Controls which (if any) function is called by the model. + `none` means the model will not call a function and instead generates a message. + `auto` means the model can pick between generating a message or calling a function. + Specifying a particular function via `{"name": "my_function"}` forces the model to call that function. + + `none` is the default when no functions are present. `auto` is the default if functions are present. + oneOf: + - type: string + description: > + `none` means the model will not call a function and instead generates a message. + `auto` means the model can pick between generating a message or calling a function. + enum: [none, auto] + - $ref: "#/components/schemas/ChatCompletionFunctionCallOption" + x-oaiExpandable: true + functions: + deprecated: true + description: | + Deprecated in favor of `tools`. + + A list of functions the model may generate JSON inputs for. + type: array + minItems: 1 + maxItems: 128 + items: + $ref: "#/components/schemas/ChatCompletionFunctions" + + required: + - model + - messages + + CreateChatCompletionResponse: + type: object + description: Represents a chat completion response returned by model, based on the provided input. + properties: + id: + type: string + description: A unique identifier for the chat completion. + choices: + type: array + description: A list of chat completion choices. Can be more than one if `n` is greater than 1. + items: + type: object + required: + - finish_reason + - index + - message + - logprobs + properties: + finish_reason: + type: string + description: &chat_completion_finish_reason_description | + The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence, + `length` if the maximum number of tokens specified in the request was reached, + `content_filter` if content was omitted due to a flag from our content filters, + `tool_calls` if the model called a tool, or `function_call` (deprecated) if the model called a function. + enum: + [ + "stop", + "length", + "tool_calls", + "content_filter", + "function_call", + ] + index: + type: integer + description: The index of the choice in the list of choices. + message: + $ref: "#/components/schemas/ChatCompletionResponseMessage" + logprobs: &chat_completion_response_logprobs + description: Log probability information for the choice. + type: object + nullable: true + properties: + content: + description: A list of message content tokens with log probability information. + type: array + items: + $ref: "#/components/schemas/ChatCompletionTokenLogprob" + nullable: true + refusal: + description: A list of message refusal tokens with log probability information. + type: array + items: + $ref: "#/components/schemas/ChatCompletionTokenLogprob" + nullable: true + required: + - content + - refusal + + created: + type: integer + description: The Unix timestamp (in seconds) of when the chat completion was created. + model: + type: string + description: The model used for the chat completion. + service_tier: + description: The service tier used for processing the request. This field is only included if the `service_tier` parameter is specified in the request. + type: string + enum: ["scale", "default"] + example: "scale" + nullable: true + system_fingerprint: + type: string + description: | + This fingerprint represents the backend configuration that the model runs with. + + Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism. + object: + type: string + description: The object type, which is always `chat.completion`. + enum: [chat.completion] + usage: + $ref: "#/components/schemas/CompletionUsage" + required: + - choices + - created + - id + - model + - object + x-oaiMeta: + name: The chat completion object + group: chat + example: *chat_completion_example + + CreateChatCompletionFunctionResponse: + type: object + description: Represents a chat completion response returned by model, based on the provided input. + properties: + id: + type: string + description: A unique identifier for the chat completion. + choices: + type: array + description: A list of chat completion choices. Can be more than one if `n` is greater than 1. + items: + type: object + required: + - finish_reason + - index + - message + - logprobs + properties: + finish_reason: + type: string + description: + &chat_completion_function_finish_reason_description | + The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence, `length` if the maximum number of tokens specified in the request was reached, `content_filter` if content was omitted due to a flag from our content filters, or `function_call` if the model called a function. + enum: + ["stop", "length", "function_call", "content_filter"] + index: + type: integer + description: The index of the choice in the list of choices. + message: + $ref: "#/components/schemas/ChatCompletionResponseMessage" + created: + type: integer + description: The Unix timestamp (in seconds) of when the chat completion was created. + model: + type: string + description: The model used for the chat completion. + system_fingerprint: + type: string + description: | + This fingerprint represents the backend configuration that the model runs with. + + Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism. + object: + type: string + description: The object type, which is always `chat.completion`. + enum: [chat.completion] + usage: + $ref: "#/components/schemas/CompletionUsage" + required: + - choices + - created + - id + - model + - object + x-oaiMeta: + name: The chat completion object + group: chat + example: *chat_completion_function_example + + ChatCompletionTokenLogprob: + type: object + properties: + token: &chat_completion_response_logprobs_token + description: The token. + type: string + logprob: &chat_completion_response_logprobs_token_logprob + description: The log probability of this token, if it is within the top 20 most likely tokens. Otherwise, the value `-9999.0` is used to signify that the token is very unlikely. + type: number + bytes: &chat_completion_response_logprobs_bytes + description: A list of integers representing the UTF-8 bytes representation of the token. Useful in instances where characters are represented by multiple tokens and their byte representations must be combined to generate the correct text representation. Can be `null` if there is no bytes representation for the token. + type: array + items: + type: integer + nullable: true + top_logprobs: + description: List of the most likely tokens and their log probability, at this token position. In rare cases, there may be fewer than the number of requested `top_logprobs` returned. + type: array + items: + type: object + properties: + token: *chat_completion_response_logprobs_token + logprob: *chat_completion_response_logprobs_token_logprob + bytes: *chat_completion_response_logprobs_bytes + required: + - token + - logprob + - bytes + required: + - token + - logprob + - bytes + - top_logprobs + + ListPaginatedFineTuningJobsResponse: + type: object + properties: + data: + type: array + items: + $ref: "#/components/schemas/FineTuningJob" + has_more: + type: boolean + object: + type: string + enum: [list] + required: + - object + - data + - has_more + + CreateChatCompletionStreamResponse: + type: object + description: Represents a streamed chunk of a chat completion response returned by model, based on the provided input. + properties: + id: + type: string + description: A unique identifier for the chat completion. Each chunk has the same ID. + choices: + type: array + description: | + A list of chat completion choices. Can contain more than one elements if `n` is greater than 1. Can also be empty for the + last chunk if you set `stream_options: {"include_usage": true}`. + items: + type: object + required: + - delta + - finish_reason + - index + properties: + delta: + $ref: "#/components/schemas/ChatCompletionStreamResponseDelta" + logprobs: *chat_completion_response_logprobs + finish_reason: + type: string + description: *chat_completion_finish_reason_description + enum: + [ + "stop", + "length", + "tool_calls", + "content_filter", + "function_call", + ] + nullable: true + index: + type: integer + description: The index of the choice in the list of choices. + created: + type: integer + description: The Unix timestamp (in seconds) of when the chat completion was created. Each chunk has the same timestamp. + model: + type: string + description: The model to generate the completion. + service_tier: + description: The service tier used for processing the request. This field is only included if the `service_tier` parameter is specified in the request. + type: string + enum: ["scale", "default"] + example: "scale" + nullable: true + system_fingerprint: + type: string + description: | + This fingerprint represents the backend configuration that the model runs with. + Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism. + object: + type: string + description: The object type, which is always `chat.completion.chunk`. + enum: [chat.completion.chunk] + usage: + type: object + description: | + An optional field that will only be present when you set `stream_options: {"include_usage": true}` in your request. + When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request. + properties: + completion_tokens: + type: integer + description: Number of tokens in the generated completion. + prompt_tokens: + type: integer + description: Number of tokens in the prompt. + total_tokens: + type: integer + description: Total number of tokens used in the request (prompt + completion). + required: + - prompt_tokens + - completion_tokens + - total_tokens + required: + - choices + - created + - id + - model + - object + x-oaiMeta: + name: The chat completion chunk object + group: chat + example: *chat_completion_chunk_example + + CreateChatCompletionImageResponse: + type: object + description: Represents a streamed chunk of a chat completion response returned by model, based on the provided input. + x-oaiMeta: + name: The chat completion chunk object + group: chat + example: *chat_completion_image_example + + CreateImageRequest: + type: object + properties: + prompt: + description: A text description of the desired image(s). The maximum length is 1000 characters for `dall-e-2` and 4000 characters for `dall-e-3`. + type: string + example: "A cute baby sea otter" + model: + anyOf: + - type: string + - type: string + enum: ["dall-e-2", "dall-e-3"] + x-oaiTypeLabel: string + default: "dall-e-2" + example: "dall-e-3" + nullable: true + description: The model to use for image generation. + n: &images_n + type: integer + minimum: 1 + maximum: 10 + default: 1 + example: 1 + nullable: true + description: The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported. + quality: + type: string + enum: ["standard", "hd"] + default: "standard" + example: "standard" + description: The quality of the image that will be generated. `hd` creates images with finer details and greater consistency across the image. This param is only supported for `dall-e-3`. + response_format: &images_response_format + type: string + enum: ["url", "b64_json"] + default: "url" + example: "url" + nullable: true + description: The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated. + size: &images_size + type: string + enum: ["256x256", "512x512", "1024x1024", "1792x1024", "1024x1792"] + default: "1024x1024" + example: "1024x1024" + nullable: true + description: The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`. Must be one of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3` models. + style: + type: string + enum: ["vivid", "natural"] + default: "vivid" + example: "vivid" + nullable: true + description: The style of the generated images. Must be one of `vivid` or `natural`. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This param is only supported for `dall-e-3`. + user: *end_user_param_configuration + required: + - prompt + + ImagesResponse: + properties: + created: + type: integer + data: + type: array + items: + $ref: "#/components/schemas/Image" + required: + - created + - data + + Image: + type: object + description: Represents the url or the content of an image generated by the OpenAI API. + properties: + b64_json: + type: string + description: The base64-encoded JSON of the generated image, if `response_format` is `b64_json`. + url: + type: string + description: The URL of the generated image, if `response_format` is `url` (default). + revised_prompt: + type: string + description: The prompt that was used to generate the image, if there was any revision to the prompt. + x-oaiMeta: + name: The image object + example: | + { + "url": "...", + "revised_prompt": "..." + } + + CreateImageEditRequest: + type: object + properties: + image: + description: The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask. + type: string + format: binary + prompt: + description: A text description of the desired image(s). The maximum length is 1000 characters. + type: string + example: "A cute baby sea otter wearing a beret" + mask: + description: An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`. + type: string + format: binary + model: + anyOf: + - type: string + - type: string + enum: ["dall-e-2"] + x-oaiTypeLabel: string + default: "dall-e-2" + example: "dall-e-2" + nullable: true + description: The model to use for image generation. Only `dall-e-2` is supported at this time. + n: + type: integer + minimum: 1 + maximum: 10 + default: 1 + example: 1 + nullable: true + description: The number of images to generate. Must be between 1 and 10. + size: &dalle2_images_size + type: string + enum: ["256x256", "512x512", "1024x1024"] + default: "1024x1024" + example: "1024x1024" + nullable: true + description: The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`. + response_format: *images_response_format + user: *end_user_param_configuration + required: + - prompt + - image + + CreateImageVariationRequest: + type: object + properties: + image: + description: The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square. + type: string + format: binary + model: + anyOf: + - type: string + - type: string + enum: ["dall-e-2"] + x-oaiTypeLabel: string + default: "dall-e-2" + example: "dall-e-2" + nullable: true + description: The model to use for image generation. Only `dall-e-2` is supported at this time. + n: *images_n + response_format: *images_response_format + size: *dalle2_images_size + user: *end_user_param_configuration + required: + - image + + CreateModerationRequest: + type: object + properties: + input: + description: | + Input (or inputs) to classify. Can be a single string, an array of strings, or + an array of multi-modal input objects similar to other models. + oneOf: + - type: string + description: A string of text to classify for moderation. + default: "" + example: "I want to kill them." + - type: array + description: An array of strings to classify for moderation. + items: + type: string + default: "" + example: "I want to kill them." + - type: array + description: An array of multi-modal inputs to the moderation model. + items: + x-oaiExpandable: true + oneOf: + - type: object + description: An object describing an image to classify. + properties: + type: + description: Always `image_url`. + type: string + enum: [ "image_url" ] + image_url: + type: object + description: Contains either an image URL or a data URL for a base64 encoded image. + properties: + url: + type: string + description: Either a URL of the image or the base64 encoded image data. + format: uri + example: "https://example.com/image.jpg" + required: + - url + required: + - type + - image_url + - type: object + description: An object describing text to classify. + properties: + type: + description: Always `text`. + type: string + enum: [ "text" ] + text: + description: A string of text to classify. + type: string + example: "I want to kill them" + required: + - type + - text + x-oaiExpandable: true + model: + description: | + The content moderation model you would like to use. Learn more in + [the moderation guide](/docs/guides/moderation), and learn about + available models [here](/docs/models/moderation). + nullable: false + default: "omni-moderation-latest" + example: "omni-moderation-2024-09-26" + anyOf: + - type: string + - type: string + enum: ["omni-moderation-latest", "omni-moderation-2024-09-26", "text-moderation-latest", "text-moderation-stable"] + x-oaiTypeLabel: string + required: + - input + + CreateModerationResponse: + type: object + description: Represents if a given text input is potentially harmful. + properties: + id: + type: string + description: The unique identifier for the moderation request. + model: + type: string + description: The model used to generate the moderation results. + results: + type: array + description: A list of moderation objects. + items: + type: object + properties: + flagged: + type: boolean + description: Whether any of the below categories are flagged. + categories: + type: object + description: A list of the categories, and whether they are flagged or not. + properties: + hate: + type: boolean + description: Content that expresses, incites, or promotes hate based on race, gender, ethnicity, religion, nationality, sexual orientation, disability status, or caste. Hateful content aimed at non-protected groups (e.g., chess players) is harassment. + hate/threatening: + type: boolean + description: Hateful content that also includes violence or serious harm towards the targeted group based on race, gender, ethnicity, religion, nationality, sexual orientation, disability status, or caste. + harassment: + type: boolean + description: Content that expresses, incites, or promotes harassing language towards any target. + harassment/threatening: + type: boolean + description: Harassment content that also includes violence or serious harm towards any target. + illicit: + type: boolean + description: Content that includes instructions or advice that facilitate the planning or execution of wrongdoing, or that gives advice or instruction on how to commit illicit acts. For example, "how to shoplift" would fit this category. + illicit/violent: + type: boolean + description: Content that includes instructions or advice that facilitate the planning or execution of wrongdoing that also includes violence, or that gives advice or instruction on the procurement of any weapon. + self-harm: + type: boolean + description: Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting, and eating disorders. + self-harm/intent: + type: boolean + description: Content where the speaker expresses that they are engaging or intend to engage in acts of self-harm, such as suicide, cutting, and eating disorders. + self-harm/instructions: + type: boolean + description: Content that encourages performing acts of self-harm, such as suicide, cutting, and eating disorders, or that gives instructions or advice on how to commit such acts. + sexual: + type: boolean + description: Content meant to arouse sexual excitement, such as the description of sexual activity, or that promotes sexual services (excluding sex education and wellness). + sexual/minors: + type: boolean + description: Sexual content that includes an individual who is under 18 years old. + violence: + type: boolean + description: Content that depicts death, violence, or physical injury. + violence/graphic: + type: boolean + description: Content that depicts death, violence, or physical injury in graphic detail. + required: + - hate + - hate/threatening + - harassment + - harassment/threatening + - illicit + - illicit/violent + - self-harm + - self-harm/intent + - self-harm/instructions + - sexual + - sexual/minors + - violence + - violence/graphic + category_scores: + type: object + description: A list of the categories along with their scores as predicted by model. + properties: + hate: + type: number + description: The score for the category 'hate'. + hate/threatening: + type: number + description: The score for the category 'hate/threatening'. + harassment: + type: number + description: The score for the category 'harassment'. + harassment/threatening: + type: number + description: The score for the category 'harassment/threatening'. + illicit: + type: number + description: The score for the category 'illicit'. + illicit/violent: + type: number + description: The score for the category 'illicit/violent'. + self-harm: + type: number + description: The score for the category 'self-harm'. + self-harm/intent: + type: number + description: The score for the category 'self-harm/intent'. + self-harm/instructions: + type: number + description: The score for the category 'self-harm/instructions'. + sexual: + type: number + description: The score for the category 'sexual'. + sexual/minors: + type: number + description: The score for the category 'sexual/minors'. + violence: + type: number + description: The score for the category 'violence'. + violence/graphic: + type: number + description: The score for the category 'violence/graphic'. + required: + - hate + - hate/threatening + - harassment + - harassment/threatening + - illicit + - illicit/violent + - self-harm + - self-harm/intent + - self-harm/instructions + - sexual + - sexual/minors + - violence + - violence/graphic + category_applied_input_types: + type: object + description: A list of the categories along with the input type(s) that the score applies to. + properties: + hate: + type: array + description: The applied input type(s) for the category 'hate'. + items: + type: string + enum: [ "text" ] + hate/threatening: + type: array + description: The applied input type(s) for the category 'hate/threatening'. + items: + type: string + enum: [ "text" ] + harassment: + type: array + description: The applied input type(s) for the category 'harassment'. + items: + type: string + enum: [ "text" ] + harassment/threatening: + type: array + description: The applied input type(s) for the category 'harassment/threatening'. + items: + type: string + enum: [ "text" ] + illicit: + type: array + description: The applied input type(s) for the category 'illicit'. + items: + type: string + enum: [ "text" ] + illicit/violent: + type: array + description: The applied input type(s) for the category 'illicit/violent'. + items: + type: string + enum: [ "text" ] + self-harm: + type: array + description: The applied input type(s) for the category 'self-harm'. + items: + type: string + enum: [ "text", "image" ] + self-harm/intent: + type: array + description: The applied input type(s) for the category 'self-harm/intent'. + items: + type: string + enum: [ "text", "image" ] + self-harm/instructions: + type: array + description: The applied input type(s) for the category 'self-harm/instructions'. + items: + type: string + enum: [ "text", "image" ] + sexual: + type: array + description: The applied input type(s) for the category 'sexual'. + items: + type: string + enum: [ "text", "image" ] + sexual/minors: + type: array + description: The applied input type(s) for the category 'sexual/minors'. + items: + type: string + enum: [ "text" ] + violence: + type: array + description: The applied input type(s) for the category 'violence'. + items: + type: string + enum: [ "text", "image" ] + violence/graphic: + type: array + description: The applied input type(s) for the category 'violence/graphic'. + items: + type: string + enum: [ "text", "image" ] + required: + - hate + - hate/threatening + - harassment + - harassment/threatening + - illicit + - illicit/violent + - self-harm + - self-harm/intent + - self-harm/instructions + - sexual + - sexual/minors + - violence + - violence/graphic + required: + - flagged + - categories + - category_scores + - category_applied_input_types + required: + - id + - model + - results + x-oaiMeta: + name: The moderation object + example: *moderation_example + + ListFilesResponse: + type: object + properties: + data: + type: array + items: + $ref: "#/components/schemas/OpenAIFile" + object: + type: string + enum: [list] + required: + - object + - data + + CreateFileRequest: + type: object + additionalProperties: false + properties: + file: + description: | + The File object (not file name) to be uploaded. + type: string + format: binary + purpose: + description: | + The intended purpose of the uploaded file. + + Use "assistants" for [Assistants](/docs/api-reference/assistants) and [Message](/docs/api-reference/messages) files, "vision" for Assistants image file inputs, "batch" for [Batch API](/docs/guides/batch), and "fine-tune" for [Fine-tuning](/docs/api-reference/fine-tuning). + type: string + enum: ["assistants", "batch", "fine-tune", "vision"] + required: + - file + - purpose + + DeleteFileResponse: + type: object + properties: + id: + type: string + object: + type: string + enum: [file] + deleted: + type: boolean + required: + - id + - object + - deleted + + CreateUploadRequest: + type: object + additionalProperties: false + properties: + filename: + description: | + The name of the file to upload. + type: string + purpose: + description: | + The intended purpose of the uploaded file. + + See the [documentation on File purposes](/docs/api-reference/files/create#files-create-purpose). + type: string + enum: ["assistants", "batch", "fine-tune", "vision"] + bytes: + description: | + The number of bytes in the file you are uploading. + type: integer + mime_type: + description: | + The MIME type of the file. + + This must fall within the supported MIME types for your file purpose. See the supported MIME types for assistants and vision. + type: string + required: + - filename + - purpose + - bytes + - mime_type + + AddUploadPartRequest: + type: object + additionalProperties: false + properties: + data: + description: | + The chunk of bytes for this Part. + type: string + format: binary + required: + - data + + CompleteUploadRequest: + type: object + additionalProperties: false + properties: + part_ids: + type: array + description: | + The ordered list of Part IDs. + items: + type: string + md5: + description: | + The optional md5 checksum for the file contents to verify if the bytes uploaded matches what you expect. + type: string + required: + - part_ids + + CancelUploadRequest: + type: object + additionalProperties: false + + CreateFineTuningJobRequest: + type: object + properties: + model: + description: | + The name of the model to fine-tune. You can select one of the + [supported models](/docs/guides/fine-tuning/which-models-can-be-fine-tuned). + example: "gpt-4o-mini" + anyOf: + - type: string + - type: string + enum: ["babbage-002", "davinci-002", "gpt-3.5-turbo", "gpt-4o-mini"] + x-oaiTypeLabel: string + training_file: + description: | + The ID of an uploaded file that contains training data. + + See [upload file](/docs/api-reference/files/create) for how to upload a file. + + Your dataset must be formatted as a JSONL file. Additionally, you must upload your file with the purpose `fine-tune`. + + The contents of the file should differ depending on if the model uses the [chat](/docs/api-reference/fine-tuning/chat-input) or [completions](/docs/api-reference/fine-tuning/completions-input) format. + + See the [fine-tuning guide](/docs/guides/fine-tuning) for more details. + type: string + example: "file-abc123" + hyperparameters: + type: object + description: The hyperparameters used for the fine-tuning job. + properties: + batch_size: + description: | + Number of examples in each batch. A larger batch size means that model parameters + are updated less frequently, but with lower variance. + oneOf: + - type: string + enum: [auto] + - type: integer + minimum: 1 + maximum: 256 + default: auto + learning_rate_multiplier: + description: | + Scaling factor for the learning rate. A smaller learning rate may be useful to avoid + overfitting. + oneOf: + - type: string + enum: [auto] + - type: number + minimum: 0 + exclusiveMinimum: true + default: auto + n_epochs: + description: | + The number of epochs to train the model for. An epoch refers to one full cycle + through the training dataset. + oneOf: + - type: string + enum: [auto] + - type: integer + minimum: 1 + maximum: 50 + default: auto + suffix: + description: | + A string of up to 64 characters that will be added to your fine-tuned model name. + + For example, a `suffix` of "custom-model-name" would produce a model name like `ft:gpt-4o-mini:openai:custom-model-name:7p4lURel`. + type: string + minLength: 1 + maxLength: 64 + default: null + nullable: true + validation_file: + description: | + The ID of an uploaded file that contains validation data. + + If you provide this file, the data is used to generate validation + metrics periodically during fine-tuning. These metrics can be viewed in + the fine-tuning results file. + The same data should not be present in both train and validation files. + + Your dataset must be formatted as a JSONL file. You must upload your file with the purpose `fine-tune`. + + See the [fine-tuning guide](/docs/guides/fine-tuning) for more details. + type: string + nullable: true + example: "file-abc123" + integrations: + type: array + description: A list of integrations to enable for your fine-tuning job. + nullable: true + items: + type: object + required: + - type + - wandb + properties: + type: + description: | + The type of integration to enable. Currently, only "wandb" (Weights and Biases) is supported. + oneOf: + - type: string + enum: [wandb] + wandb: + type: object + description: | + The settings for your integration with Weights and Biases. This payload specifies the project that + metrics will be sent to. Optionally, you can set an explicit display name for your run, add tags + to your run, and set a default entity (team, username, etc) to be associated with your run. + required: + - project + properties: + project: + description: | + The name of the project that the new run will be created under. + type: string + example: "my-wandb-project" + name: + description: | + A display name to set for the run. If not set, we will use the Job ID as the name. + nullable: true + type: string + entity: + description: | + The entity to use for the run. This allows you to set the team or username of the WandB user that you would + like associated with the run. If not set, the default entity for the registered WandB API key is used. + nullable: true + type: string + tags: + description: | + A list of tags to be attached to the newly created run. These tags are passed through directly to WandB. Some + default tags are generated by OpenAI: "openai/finetune", "openai/{base-model}", "openai/{ftjob-abcdef}". + type: array + items: + type: string + example: "custom-tag" + + seed: + description: | + The seed controls the reproducibility of the job. Passing in the same seed and job parameters should produce the same results, but may differ in rare cases. + If a seed is not specified, one will be generated for you. + type: integer + nullable: true + minimum: 0 + maximum: 2147483647 + example: 42 + required: + - model + - training_file + + ListFineTuningJobEventsResponse: + type: object + properties: + data: + type: array + items: + $ref: "#/components/schemas/FineTuningJobEvent" + object: + type: string + enum: [list] + required: + - object + - data + + ListFineTuningJobCheckpointsResponse: + type: object + properties: + data: + type: array + items: + $ref: "#/components/schemas/FineTuningJobCheckpoint" + object: + type: string + enum: [list] + first_id: + type: string + nullable: true + last_id: + type: string + nullable: true + has_more: + type: boolean + required: + - object + - data + - has_more + + CreateEmbeddingRequest: + type: object + additionalProperties: false + properties: + input: + description: | + Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. The input must not exceed the max input tokens for the model (8192 tokens for `text-embedding-ada-002`), cannot be an empty string, and any array must be 2048 dimensions or less. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. + example: "The quick brown fox jumped over the lazy dog" + oneOf: + - type: string + title: string + description: The string that will be turned into an embedding. + default: "" + example: "This is a test." + - type: array + title: array + description: The array of strings that will be turned into an embedding. + minItems: 1 + maxItems: 2048 + items: + type: string + default: "" + example: "['This is a test.']" + - type: array + title: array + description: The array of integers that will be turned into an embedding. + minItems: 1 + maxItems: 2048 + items: + type: integer + example: "[1212, 318, 257, 1332, 13]" + - type: array + title: array + description: The array of arrays containing integers that will be turned into an embedding. + minItems: 1 + maxItems: 2048 + items: + type: array + minItems: 1 + items: + type: integer + example: "[[1212, 318, 257, 1332, 13]]" + x-oaiExpandable: true + model: + description: *model_description + example: "text-embedding-3-small" + anyOf: + - type: string + - type: string + enum: + [ + "text-embedding-ada-002", + "text-embedding-3-small", + "text-embedding-3-large", + ] + x-oaiTypeLabel: string + encoding_format: + description: "The format to return the embeddings in. Can be either `float` or [`base64`](https://pypi.org/project/pybase64/)." + example: "float" + default: "float" + type: string + enum: ["float", "base64"] + dimensions: + description: | + The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models. + type: integer + minimum: 1 + user: *end_user_param_configuration + required: + - model + - input + + CreateEmbeddingResponse: + type: object + properties: + data: + type: array + description: The list of embeddings generated by the model. + items: + $ref: "#/components/schemas/Embedding" + model: + type: string + description: The name of the model used to generate the embedding. + object: + type: string + description: The object type, which is always "list". + enum: [list] + usage: + type: object + description: The usage information for the request. + properties: + prompt_tokens: + type: integer + description: The number of tokens used by the prompt. + total_tokens: + type: integer + description: The total number of tokens used by the request. + required: + - prompt_tokens + - total_tokens + required: + - object + - model + - data + - usage + + CreateTranscriptionRequest: + type: object + additionalProperties: false + properties: + file: + description: | + The audio file object (not file name) to transcribe, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + type: string + x-oaiTypeLabel: file + format: binary + model: + description: | + ID of the model to use. Only `whisper-1` (which is powered by our open source Whisper V2 model) is currently available. + example: whisper-1 + anyOf: + - type: string + - type: string + enum: ["whisper-1"] + x-oaiTypeLabel: string + language: + description: | + The language of the input audio. Supplying the input language in [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) format will improve accuracy and latency. + type: string + prompt: + description: | + An optional text to guide the model's style or continue a previous audio segment. The [prompt](/docs/guides/speech-to-text/prompting) should match the audio language. + type: string + response_format: + $ref: "#/components/schemas/AudioResponseFormat" + temperature: + description: | + The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use [log probability](https://en.wikipedia.org/wiki/Log_probability) to automatically increase the temperature until certain thresholds are hit. + type: number + default: 0 + timestamp_granularities[]: + description: | + The timestamp granularities to populate for this transcription. `response_format` must be set `verbose_json` to use timestamp granularities. Either or both of these options are supported: `word`, or `segment`. Note: There is no additional latency for segment timestamps, but generating word timestamps incurs additional latency. + type: array + items: + type: string + enum: + - word + - segment + default: [segment] + required: + - file + - model + + # Note: This does not currently support the non-default response format types. + CreateTranscriptionResponseJson: + type: object + description: Represents a transcription response returned by model, based on the provided input. + properties: + text: + type: string + description: The transcribed text. + required: + - text + x-oaiMeta: + name: The transcription object (JSON) + group: audio + example: *basic_transcription_response_example + + TranscriptionSegment: + type: object + properties: + id: + type: integer + description: Unique identifier of the segment. + seek: + type: integer + description: Seek offset of the segment. + start: + type: number + format: float + description: Start time of the segment in seconds. + end: + type: number + format: float + description: End time of the segment in seconds. + text: + type: string + description: Text content of the segment. + tokens: + type: array + items: + type: integer + description: Array of token IDs for the text content. + temperature: + type: number + format: float + description: Temperature parameter used for generating the segment. + avg_logprob: + type: number + format: float + description: Average logprob of the segment. If the value is lower than -1, consider the logprobs failed. + compression_ratio: + type: number + format: float + description: Compression ratio of the segment. If the value is greater than 2.4, consider the compression failed. + no_speech_prob: + type: number + format: float + description: Probability of no speech in the segment. If the value is higher than 1.0 and the `avg_logprob` is below -1, consider this segment silent. + required: + - id + - seek + - start + - end + - text + - tokens + - temperature + - avg_logprob + - compression_ratio + - no_speech_prob + + TranscriptionWord: + type: object + properties: + word: + type: string + description: The text content of the word. + start: + type: number + format: float + description: Start time of the word in seconds. + end: + type: number + format: float + description: End time of the word in seconds. + required: [word, start, end] + + CreateTranscriptionResponseVerboseJson: + type: object + description: Represents a verbose json transcription response returned by model, based on the provided input. + properties: + language: + type: string + description: The language of the input audio. + duration: + type: number + description: The duration of the input audio. + text: + type: string + description: The transcribed text. + words: + type: array + description: Extracted words and their corresponding timestamps. + items: + $ref: "#/components/schemas/TranscriptionWord" + segments: + type: array + description: Segments of the transcribed text and their corresponding details. + items: + $ref: "#/components/schemas/TranscriptionSegment" + required: [language, duration, text] + x-oaiMeta: + name: The transcription object (Verbose JSON) + group: audio + example: *verbose_transcription_response_example + + AudioResponseFormat: + description: | + The format of the output, in one of these options: `json`, `text`, `srt`, `verbose_json`, or `vtt`. type: string - example: curie:ft-acmeco-2021-03-03-21-44-20 - description: The model to delete - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/DeleteModelResponse' - x-oaiMeta: - name: Delete fine-tune model - group: fine-tunes - path: delete-model - beta: true - examples: - curl: | - curl https://api.openai.com/v1/models/curie:ft-acmeco-2021-03-03-21-44-20 \ - -X DELETE \ - -H "Authorization: Bearer YOUR_API_KEY" - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Model.delete("curie:ft-acmeco-2021-03-03-21-44-20") - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.deleteModel('curie:ft-acmeco-2021-03-03-21-44-20'); - response: | - { - "id": "curie:ft-acmeco-2021-03-03-21-44-20", - "object": "model", - "deleted": true - } - - /moderations: - post: - operationId: createModeration - tags: - - OpenAI - summary: Classifies if text violates OpenAI's Content Policy - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/CreateModerationRequest' - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/CreateModerationResponse' - x-oaiMeta: - name: Create moderation - group: moderations - path: create - examples: - curl: | - curl https://api.openai.com/v1/moderations \ - -H 'Content-Type: application/json' \ - -H 'Authorization: Bearer YOUR_API_KEY' \ - -d '{ - "input": "I want to kill them." - }' - python: | - import os - import openai - openai.api_key = os.getenv("OPENAI_API_KEY") - openai.Moderation.create( - input="I want to kill them.", - ) - node.js: | - const { Configuration, OpenAIApi } = require("openai"); - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - const openai = new OpenAIApi(configuration); - const response = await openai.createModeration({ - input: "I want to kill them.", - }); - parameters: | - { - "input": "I want to kill them." - } - response: | - { - "id": "modr-5MWoLO", - "model": "text-moderation-001", - "results": [ - { - "categories": { - "hate": false, - "hate/threatening": true, - "self-harm": false, - "sexual": false, - "sexual/minors": false, - "violence": true, - "violence/graphic": false - }, - "category_scores": { - "hate": 0.22714105248451233, - "hate/threatening": 0.4132447838783264, - "self-harm": 0.005232391878962517, - "sexual": 0.01407341007143259, - "sexual/minors": 0.0038522258400917053, - "violence": 0.9223177433013916, - "violence/graphic": 0.036865197122097015 - }, - "flagged": true - } - ] - } + enum: + - json + - text + - srt + - verbose_json + - vtt + default: json + + CreateTranslationRequest: + type: object + additionalProperties: false + properties: + file: + description: | + The audio file object (not file name) translate, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + type: string + x-oaiTypeLabel: file + format: binary + model: + description: | + ID of the model to use. Only `whisper-1` (which is powered by our open source Whisper V2 model) is currently available. + example: whisper-1 + anyOf: + - type: string + - type: string + enum: ["whisper-1"] + x-oaiTypeLabel: string + prompt: + description: | + An optional text to guide the model's style or continue a previous audio segment. The [prompt](/docs/guides/speech-to-text/prompting) should be in English. + type: string + response_format: + $ref: "#/components/schemas/AudioResponseFormat" + temperature: + description: | + The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use [log probability](https://en.wikipedia.org/wiki/Log_probability) to automatically increase the temperature until certain thresholds are hit. + type: number + default: 0 + required: + - file + - model + + # Note: This does not currently support the non-default response format types. + CreateTranslationResponseJson: + type: object + properties: + text: + type: string + required: + - text + + CreateTranslationResponseVerboseJson: + type: object + properties: + language: + type: string + description: The language of the output translation (always `english`). + duration: + type: number + description: The duration of the input audio. + text: + type: string + description: The translated text. + segments: + type: array + description: Segments of the translated text and their corresponding details. + items: + $ref: "#/components/schemas/TranscriptionSegment" + required: [language, duration, text] + + CreateSpeechRequest: + type: object + additionalProperties: false + properties: + model: + description: | + One of the available [TTS models](/docs/models/tts): `tts-1` or `tts-1-hd` + anyOf: + - type: string + - type: string + enum: ["tts-1", "tts-1-hd"] + x-oaiTypeLabel: string + input: + type: string + description: The text to generate audio for. The maximum length is 4096 characters. + maxLength: 4096 + voice: + description: The voice to use when generating the audio. Supported voices are `alloy`, `echo`, `fable`, `onyx`, `nova`, and `shimmer`. Previews of the voices are available in the [Text to speech guide](/docs/guides/text-to-speech/voice-options). + type: string + enum: ["alloy", "echo", "fable", "onyx", "nova", "shimmer"] + response_format: + description: "The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`, `wav`, and `pcm`." + default: "mp3" + type: string + enum: ["mp3", "opus", "aac", "flac", "wav", "pcm"] + speed: + description: "The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is the default." + type: number + default: 1.0 + minimum: 0.25 + maximum: 4.0 + required: + - model + - input + - voice + + Model: + title: Model + description: Describes an OpenAI model offering that can be used with the API. + properties: + id: + type: string + description: The model identifier, which can be referenced in the API endpoints. + created: + type: integer + description: The Unix timestamp (in seconds) when the model was created. + object: + type: string + description: The object type, which is always "model". + enum: [model] + owned_by: + type: string + description: The organization that owns the model. + required: + - id + - object + - created + - owned_by + x-oaiMeta: + name: The model object + example: *retrieve_model_response + + OpenAIFile: + title: OpenAIFile + description: The `File` object represents a document that has been uploaded to OpenAI. + properties: + id: + type: string + description: The file identifier, which can be referenced in the API endpoints. + bytes: + type: integer + description: The size of the file, in bytes. + created_at: + type: integer + description: The Unix timestamp (in seconds) for when the file was created. + filename: + type: string + description: The name of the file. + object: + type: string + description: The object type, which is always `file`. + enum: ["file"] + purpose: + type: string + description: The intended purpose of the file. Supported values are `assistants`, `assistants_output`, `batch`, `batch_output`, `fine-tune`, `fine-tune-results` and `vision`. + enum: + [ + "assistants", + "assistants_output", + "batch", + "batch_output", + "fine-tune", + "fine-tune-results", + "vision", + ] + status: + type: string + deprecated: true + description: Deprecated. The current status of the file, which can be either `uploaded`, `processed`, or `error`. + enum: ["uploaded", "processed", "error"] + status_details: + type: string + deprecated: true + description: Deprecated. For details on why a fine-tuning training file failed validation, see the `error` field on `fine_tuning.job`. + required: + - id + - object + - bytes + - created_at + - filename + - purpose + - status + x-oaiMeta: + name: The file object + example: | + { + "id": "file-abc123", + "object": "file", + "bytes": 120000, + "created_at": 1677610602, + "filename": "salesOverview.pdf", + "purpose": "assistants", + } + Upload: + type: object + title: Upload + description: | + The Upload object can accept byte chunks in the form of Parts. + properties: + id: + type: string + description: The Upload unique identifier, which can be referenced in API endpoints. + created_at: + type: integer + description: The Unix timestamp (in seconds) for when the Upload was created. + filename: + type: string + description: The name of the file to be uploaded. + bytes: + type: integer + description: The intended number of bytes to be uploaded. + purpose: + type: string + description: The intended purpose of the file. [Please refer here](/docs/api-reference/files/object#files/object-purpose) for acceptable values. + status: + type: string + description: The status of the Upload. + enum: ["pending", "completed", "cancelled", "expired"] + expires_at: + type: integer + description: The Unix timestamp (in seconds) for when the Upload was created. + object: + type: string + description: The object type, which is always "upload". + enum: [upload] + file: + $ref: "#/components/schemas/OpenAIFile" + nullable: true + description: The ready File object after the Upload is completed. + required: + - bytes + - created_at + - expires_at + - filename + - id + - purpose + - status + - step_number + x-oaiMeta: + name: The upload object + example: | + { + "id": "upload_abc123", + "object": "upload", + "bytes": 2147483648, + "created_at": 1719184911, + "filename": "training_examples.jsonl", + "purpose": "fine-tune", + "status": "completed", + "expires_at": 1719127296, + "file": { + "id": "file-xyz321", + "object": "file", + "bytes": 2147483648, + "created_at": 1719186911, + "filename": "training_examples.jsonl", + "purpose": "fine-tune", + } + } + UploadPart: + type: object + title: UploadPart + description: | + The upload Part represents a chunk of bytes we can add to an Upload object. + properties: + id: + type: string + description: The upload Part unique identifier, which can be referenced in API endpoints. + created_at: + type: integer + description: The Unix timestamp (in seconds) for when the Part was created. + upload_id: + type: string + description: The ID of the Upload object that this Part was added to. + object: + type: string + description: The object type, which is always `upload.part`. + enum: ['upload.part'] + required: + - created_at + - id + - object + - upload_id + x-oaiMeta: + name: The upload part object + example: | + { + "id": "part_def456", + "object": "upload.part", + "created_at": 1719186911, + "upload_id": "upload_abc123" + } + Embedding: + type: object + description: | + Represents an embedding vector returned by embedding endpoint. + properties: + index: + type: integer + description: The index of the embedding in the list of embeddings. + embedding: + type: array + description: | + The embedding vector, which is a list of floats. The length of vector depends on the model as listed in the [embedding guide](/docs/guides/embeddings). + items: + type: number + object: + type: string + description: The object type, which is always "embedding". + enum: [embedding] + required: + - index + - object + - embedding + x-oaiMeta: + name: The embedding object + example: | + { + "object": "embedding", + "embedding": [ + 0.0023064255, + -0.009327292, + .... (1536 floats total for ada-002) + -0.0028842222, + ], + "index": 0 + } + + FineTuningJob: + type: object + title: FineTuningJob + description: | + The `fine_tuning.job` object represents a fine-tuning job that has been created through the API. + properties: + id: + type: string + description: The object identifier, which can be referenced in the API endpoints. + created_at: + type: integer + description: The Unix timestamp (in seconds) for when the fine-tuning job was created. + error: + type: object + nullable: true + description: For fine-tuning jobs that have `failed`, this will contain more information on the cause of the failure. + properties: + code: + type: string + description: A machine-readable error code. + message: + type: string + description: A human-readable error message. + param: + type: string + description: The parameter that was invalid, usually `training_file` or `validation_file`. This field will be null if the failure was not parameter-specific. + nullable: true + required: + - code + - message + - param + fine_tuned_model: + type: string + nullable: true + description: The name of the fine-tuned model that is being created. The value will be null if the fine-tuning job is still running. + finished_at: + type: integer + nullable: true + description: The Unix timestamp (in seconds) for when the fine-tuning job was finished. The value will be null if the fine-tuning job is still running. + hyperparameters: + type: object + description: The hyperparameters used for the fine-tuning job. See the [fine-tuning guide](/docs/guides/fine-tuning) for more details. + properties: + n_epochs: + oneOf: + - type: string + enum: [auto] + - type: integer + minimum: 1 + maximum: 50 + default: auto + description: + The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset. + + "auto" decides the optimal number of epochs based on the size of the dataset. If setting the number manually, we support any number between 1 and 50 epochs. + required: + - n_epochs + model: + type: string + description: The base model that is being fine-tuned. + object: + type: string + description: The object type, which is always "fine_tuning.job". + enum: [fine_tuning.job] + organization_id: + type: string + description: The organization that owns the fine-tuning job. + result_files: + type: array + description: The compiled results file ID(s) for the fine-tuning job. You can retrieve the results with the [Files API](/docs/api-reference/files/retrieve-contents). + items: + type: string + example: file-abc123 + status: + type: string + description: The current status of the fine-tuning job, which can be either `validating_files`, `queued`, `running`, `succeeded`, `failed`, or `cancelled`. + enum: + [ + "validating_files", + "queued", + "running", + "succeeded", + "failed", + "cancelled", + ] + trained_tokens: + type: integer + nullable: true + description: The total number of billable tokens processed by this fine-tuning job. The value will be null if the fine-tuning job is still running. + training_file: + type: string + description: The file ID used for training. You can retrieve the training data with the [Files API](/docs/api-reference/files/retrieve-contents). + validation_file: + type: string + nullable: true + description: The file ID used for validation. You can retrieve the validation results with the [Files API](/docs/api-reference/files/retrieve-contents). + integrations: + type: array + nullable: true + description: A list of integrations to enable for this fine-tuning job. + maxItems: 5 + items: + oneOf: + - $ref: "#/components/schemas/FineTuningIntegration" + x-oaiExpandable: true + seed: + type: integer + description: The seed used for the fine-tuning job. + estimated_finish: + type: integer + nullable: true + description: The Unix timestamp (in seconds) for when the fine-tuning job is estimated to finish. The value will be null if the fine-tuning job is not running. + required: + - created_at + - error + - finished_at + - fine_tuned_model + - hyperparameters + - id + - model + - object + - organization_id + - result_files + - status + - trained_tokens + - training_file + - validation_file + - seed + x-oaiMeta: + name: The fine-tuning job object + example: *fine_tuning_example + + FineTuningIntegration: + type: object + title: Fine-Tuning Job Integration + required: + - type + - wandb + properties: + type: + type: string + description: "The type of the integration being enabled for the fine-tuning job" + enum: ["wandb"] + wandb: + type: object + description: | + The settings for your integration with Weights and Biases. This payload specifies the project that + metrics will be sent to. Optionally, you can set an explicit display name for your run, add tags + to your run, and set a default entity (team, username, etc) to be associated with your run. + required: + - project + properties: + project: + description: | + The name of the project that the new run will be created under. + type: string + example: "my-wandb-project" + name: + description: | + A display name to set for the run. If not set, we will use the Job ID as the name. + nullable: true + type: string + entity: + description: | + The entity to use for the run. This allows you to set the team or username of the WandB user that you would + like associated with the run. If not set, the default entity for the registered WandB API key is used. + nullable: true + type: string + tags: + description: | + A list of tags to be attached to the newly created run. These tags are passed through directly to WandB. Some + default tags are generated by OpenAI: "openai/finetune", "openai/{base-model}", "openai/{ftjob-abcdef}". + type: array + items: + type: string + example: "custom-tag" + + FineTuningJobEvent: + type: object + description: Fine-tuning job event object + properties: + id: + type: string + created_at: + type: integer + level: + type: string + enum: ["info", "warn", "error"] + message: + type: string + object: + type: string + enum: [fine_tuning.job.event] + required: + - id + - object + - created_at + - level + - message + x-oaiMeta: + name: The fine-tuning job event object + example: | + { + "object": "fine_tuning.job.event", + "id": "ftevent-abc123" + "created_at": 1677610602, + "level": "info", + "message": "Created fine-tuning job" + } + + FineTuningJobCheckpoint: + type: object + title: FineTuningJobCheckpoint + description: | + The `fine_tuning.job.checkpoint` object represents a model checkpoint for a fine-tuning job that is ready to use. + properties: + id: + type: string + description: The checkpoint identifier, which can be referenced in the API endpoints. + created_at: + type: integer + description: The Unix timestamp (in seconds) for when the checkpoint was created. + fine_tuned_model_checkpoint: + type: string + description: The name of the fine-tuned checkpoint model that is created. + step_number: + type: integer + description: The step number that the checkpoint was created at. + metrics: + type: object + description: Metrics at the step number during the fine-tuning job. + properties: + step: + type: number + train_loss: + type: number + train_mean_token_accuracy: + type: number + valid_loss: + type: number + valid_mean_token_accuracy: + type: number + full_valid_loss: + type: number + full_valid_mean_token_accuracy: + type: number + fine_tuning_job_id: + type: string + description: The name of the fine-tuning job that this checkpoint was created from. + object: + type: string + description: The object type, which is always "fine_tuning.job.checkpoint". + enum: [fine_tuning.job.checkpoint] + required: + - created_at + - fine_tuning_job_id + - fine_tuned_model_checkpoint + - id + - metrics + - object + - step_number + x-oaiMeta: + name: The fine-tuning job checkpoint object + example: | + { + "object": "fine_tuning.job.checkpoint", + "id": "ftckpt_qtZ5Gyk4BLq1SfLFWp3RtO3P", + "created_at": 1712211699, + "fine_tuned_model_checkpoint": "ft:gpt-4o-mini-2024-07-18:my-org:custom_suffix:9ABel2dg:ckpt-step-88", + "fine_tuning_job_id": "ftjob-fpbNQ3H1GrMehXRf8cO97xTN", + "metrics": { + "step": 88, + "train_loss": 0.478, + "train_mean_token_accuracy": 0.924, + "valid_loss": 10.112, + "valid_mean_token_accuracy": 0.145, + "full_valid_loss": 0.567, + "full_valid_mean_token_accuracy": 0.944 + }, + "step_number": 88 + } + + FinetuneChatRequestInput: + type: object + description: The per-line training example of a fine-tuning input file for chat models + properties: + messages: + type: array + minItems: 1 + items: + oneOf: + - $ref: "#/components/schemas/ChatCompletionRequestSystemMessage" + - $ref: "#/components/schemas/ChatCompletionRequestUserMessage" + - $ref: "#/components/schemas/FineTuneChatCompletionRequestAssistantMessage" + - $ref: "#/components/schemas/ChatCompletionRequestToolMessage" + - $ref: "#/components/schemas/ChatCompletionRequestFunctionMessage" + x-oaiExpandable: true + tools: + type: array + description: A list of tools the model may generate JSON inputs for. + items: + $ref: "#/components/schemas/ChatCompletionTool" + parallel_tool_calls: + $ref: "#/components/schemas/ParallelToolCalls" + functions: + deprecated: true + description: + A list of functions the model may generate JSON inputs for. + type: array + minItems: 1 + maxItems: 128 + items: + $ref: "#/components/schemas/ChatCompletionFunctions" + x-oaiMeta: + name: Training format for chat models + example: | + { + "messages": [ + { "role": "user", "content": "What is the weather in San Francisco?" }, + { + "role": "assistant", + "tool_calls": [ + { + "id": "call_id", + "type": "function", + "function": { + "name": "get_current_weather", + "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}" + } + } + ] + } + ], + "parallel_tool_calls": false, + "tools": [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and country, eg. San Francisco, USA" + }, + "format": { "type": "string", "enum": ["celsius", "fahrenheit"] } + }, + "required": ["location", "format"] + } + } + } + ] + } + + FinetuneCompletionRequestInput: + type: object + description: The per-line training example of a fine-tuning input file for completions models + properties: + prompt: + type: string + description: The input prompt for this training example. + completion: + type: string + description: The desired completion for this training example. + x-oaiMeta: + name: Training format for completions models + example: | + { + "prompt": "What is the answer to 2+2", + "completion": "4" + } + + CompletionUsage: + type: object + description: Usage statistics for the completion request. + properties: + completion_tokens: + type: integer + description: Number of tokens in the generated completion. + prompt_tokens: + type: integer + description: Number of tokens in the prompt. + total_tokens: + type: integer + description: Total number of tokens used in the request (prompt + completion). + completion_tokens_details: + type: object + description: Breakdown of tokens used in a completion. + properties: + reasoning_tokens: + type: integer + description: Tokens generated by the model for reasoning. + required: + - prompt_tokens + - completion_tokens + - total_tokens + + RunCompletionUsage: + type: object + description: Usage statistics related to the run. This value will be `null` if the run is not in a terminal state (i.e. `in_progress`, `queued`, etc.). + properties: + completion_tokens: + type: integer + description: Number of completion tokens used over the course of the run. + prompt_tokens: + type: integer + description: Number of prompt tokens used over the course of the run. + total_tokens: + type: integer + description: Total number of tokens used (prompt + completion). + required: + - prompt_tokens + - completion_tokens + - total_tokens + nullable: true + + RunStepCompletionUsage: + type: object + description: Usage statistics related to the run step. This value will be `null` while the run step's status is `in_progress`. + properties: + completion_tokens: + type: integer + description: Number of completion tokens used over the course of the run step. + prompt_tokens: + type: integer + description: Number of prompt tokens used over the course of the run step. + total_tokens: + type: integer + description: Total number of tokens used (prompt + completion). + required: + - prompt_tokens + - completion_tokens + - total_tokens + nullable: true + + AssistantsApiResponseFormatOption: + description: | + Specifies the format that the model must output. Compatible with [GPT-4o](/docs/models/gpt-4o), [GPT-4 Turbo](/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`. + + Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures the model will match your supplied JSON schema. Learn more in the [Structured Outputs guide](/docs/guides/structured-outputs). + + Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the message the model generates is valid JSON. + + **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. + oneOf: + - type: string + description: > + `auto` is the default value + enum: [auto] + - $ref: '#/components/schemas/ResponseFormatText' + - $ref: '#/components/schemas/ResponseFormatJsonObject' + - $ref: '#/components/schemas/ResponseFormatJsonSchema' + x-oaiExpandable: true + + AssistantObject: + type: object + title: Assistant + description: Represents an `assistant` that can call the model and use tools. + properties: + id: + description: The identifier, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `assistant`. + type: string + enum: [assistant] + created_at: + description: The Unix timestamp (in seconds) for when the assistant was created. + type: integer + name: + description: &assistant_name_param_description | + The name of the assistant. The maximum length is 256 characters. + type: string + maxLength: 256 + nullable: true + description: + description: &assistant_description_param_description | + The description of the assistant. The maximum length is 512 characters. + type: string + maxLength: 512 + nullable: true + model: + description: *model_description + type: string + instructions: + description: &assistant_instructions_param_description | + The system instructions that the assistant uses. The maximum length is 256,000 characters. + type: string + maxLength: 256000 + nullable: true + tools: + description: &assistant_tools_param_description | + A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types `code_interpreter`, `file_search`, or `function`. + default: [] + type: array + maxItems: 128 + items: + oneOf: + - $ref: "#/components/schemas/AssistantToolsCode" + - $ref: "#/components/schemas/AssistantToolsFileSearch" + - $ref: "#/components/schemas/AssistantToolsFunction" + x-oaiExpandable: true + tool_resources: + type: object + description: | + A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + properties: + code_interpreter: + type: object + properties: + file_ids: + type: array + description: | + A list of [file](/docs/api-reference/files) IDs made available to the `code_interpreter`` tool. There can be a maximum of 20 files associated with the tool. + default: [] + maxItems: 20 + items: + type: string + file_search: + type: object + properties: + vector_store_ids: + type: array + description: | + The ID of the [vector store](/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant. + maxItems: 1 + items: + type: string + nullable: true + metadata: + description: &metadata_description | + Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long. + type: object + x-oaiTypeLabel: map + nullable: true + temperature: + description: &run_temperature_description | + What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + type: number + minimum: 0 + maximum: 2 + default: 1 + example: 1 + nullable: true + top_p: + type: number + minimum: 0 + maximum: 1 + default: 1 + example: 1 + nullable: true + description: &run_top_p_description | + An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + + We generally recommend altering this or temperature but not both. + response_format: + $ref: "#/components/schemas/AssistantsApiResponseFormatOption" + nullable: true + required: + - id + - object + - created_at + - name + - description + - model + - instructions + - tools + - metadata + x-oaiMeta: + name: The assistant object + beta: true + example: *create_assistants_example + + CreateAssistantRequest: + type: object + additionalProperties: false + properties: + model: + description: *model_description + example: "gpt-4o" + anyOf: + - type: string + - type: string + enum: + [ + "gpt-4o", + "gpt-4o-2024-08-06", + "gpt-4o-2024-05-13", + "gpt-4o-2024-08-06", + "gpt-4o-mini", + "gpt-4o-mini-2024-07-18", + "gpt-4-turbo", + "gpt-4-turbo-2024-04-09", + "gpt-4-0125-preview", + "gpt-4-turbo-preview", + "gpt-4-1106-preview", + "gpt-4-vision-preview", + "gpt-4", + "gpt-4-0314", + "gpt-4-0613", + "gpt-4-32k", + "gpt-4-32k-0314", + "gpt-4-32k-0613", + "gpt-3.5-turbo", + "gpt-3.5-turbo-16k", + "gpt-3.5-turbo-0613", + "gpt-3.5-turbo-1106", + "gpt-3.5-turbo-0125", + "gpt-3.5-turbo-16k-0613", + ] + x-oaiTypeLabel: string + name: + description: *assistant_name_param_description + type: string + nullable: true + maxLength: 256 + description: + description: *assistant_description_param_description + type: string + nullable: true + maxLength: 512 + instructions: + description: *assistant_instructions_param_description + type: string + nullable: true + maxLength: 256000 + tools: + description: *assistant_tools_param_description + default: [] + type: array + maxItems: 128 + items: + oneOf: + - $ref: "#/components/schemas/AssistantToolsCode" + - $ref: "#/components/schemas/AssistantToolsFileSearch" + - $ref: "#/components/schemas/AssistantToolsFunction" + x-oaiExpandable: true + tool_resources: + type: object + description: | + A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + properties: + code_interpreter: + type: object + properties: + file_ids: + type: array + description: | + A list of [file](/docs/api-reference/files) IDs made available to the `code_interpreter` tool. There can be a maximum of 20 files associated with the tool. + default: [] + maxItems: 20 + items: + type: string + file_search: + type: object + properties: + vector_store_ids: + type: array + description: | + The [vector store](/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant. + maxItems: 1 + items: + type: string + vector_stores: + type: array + description: | + A helper to create a [vector store](/docs/api-reference/vector-stores/object) with file_ids and attach it to this assistant. There can be a maximum of 1 vector store attached to the assistant. + maxItems: 1 + items: + type: object + properties: + file_ids: + type: array + description: | + A list of [file](/docs/api-reference/files) IDs to add to the vector store. There can be a maximum of 10000 files in a vector store. + maxItems: 10000 + items: + type: string + chunking_strategy: + # Ideally we'd reuse the chunking strategy schema here, but it doesn't expand properly + type: object + description: The chunking strategy used to chunk the file(s). If not set, will use the `auto` strategy. + oneOf: + - type: object + title: Auto Chunking Strategy + description: The default strategy. This strategy currently uses a `max_chunk_size_tokens` of `800` and `chunk_overlap_tokens` of `400`. + additionalProperties: false + properties: + type: + type: string + description: Always `auto`. + enum: ["auto"] + required: + - type + - type: object + title: Static Chunking Strategy + additionalProperties: false + properties: + type: + type: string + description: Always `static`. + enum: ["static"] + static: + type: object + additionalProperties: false + properties: + max_chunk_size_tokens: + type: integer + minimum: 100 + maximum: 4096 + description: The maximum number of tokens in each chunk. The default value is `800`. The minimum value is `100` and the maximum value is `4096`. + chunk_overlap_tokens: + type: integer + description: | + The number of tokens that overlap between chunks. The default value is `400`. + + Note that the overlap must not exceed half of `max_chunk_size_tokens`. + required: + - max_chunk_size_tokens + - chunk_overlap_tokens + required: + - type + - static + x-oaiExpandable: true + metadata: + type: object + description: | + Set of 16 key-value pairs that can be attached to a vector store. This can be useful for storing additional information about the vector store in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long. + x-oaiTypeLabel: map + oneOf: + - required: [vector_store_ids] + - required: [vector_stores] + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + temperature: + description: *run_temperature_description + type: number + minimum: 0 + maximum: 2 + default: 1 + example: 1 + nullable: true + top_p: + type: number + minimum: 0 + maximum: 1 + default: 1 + example: 1 + nullable: true + description: *run_top_p_description + response_format: + $ref: "#/components/schemas/AssistantsApiResponseFormatOption" + nullable: true + required: + - model + + ModifyAssistantRequest: + type: object + additionalProperties: false + properties: + model: + description: *model_description + anyOf: + - type: string + name: + description: *assistant_name_param_description + type: string + nullable: true + maxLength: 256 + description: + description: *assistant_description_param_description + type: string + nullable: true + maxLength: 512 + instructions: + description: *assistant_instructions_param_description + type: string + nullable: true + maxLength: 256000 + tools: + description: *assistant_tools_param_description + default: [] + type: array + maxItems: 128 + items: + oneOf: + - $ref: "#/components/schemas/AssistantToolsCode" + - $ref: "#/components/schemas/AssistantToolsFileSearch" + - $ref: "#/components/schemas/AssistantToolsFunction" + x-oaiExpandable: true + tool_resources: + type: object + description: | + A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + properties: + code_interpreter: + type: object + properties: + file_ids: + type: array + description: | + Overrides the list of [file](/docs/api-reference/files) IDs made available to the `code_interpreter` tool. There can be a maximum of 20 files associated with the tool. + default: [] + maxItems: 20 + items: + type: string + file_search: + type: object + properties: + vector_store_ids: + type: array + description: | + Overrides the [vector store](/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant. + maxItems: 1 + items: + type: string + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + temperature: + description: *run_temperature_description + type: number + minimum: 0 + maximum: 2 + default: 1 + example: 1 + nullable: true + top_p: + type: number + minimum: 0 + maximum: 1 + default: 1 + example: 1 + nullable: true + description: *run_top_p_description + response_format: + $ref: "#/components/schemas/AssistantsApiResponseFormatOption" + nullable: true + + DeleteAssistantResponse: + type: object + properties: + id: + type: string + deleted: + type: boolean + object: + type: string + enum: [assistant.deleted] + required: + - id + - object + - deleted + + ListAssistantsResponse: + type: object + properties: + object: + type: string + example: "list" + data: + type: array + items: + $ref: "#/components/schemas/AssistantObject" + first_id: + type: string + example: "asst_abc123" + last_id: + type: string + example: "asst_abc456" + has_more: + type: boolean + example: false + required: + - object + - data + - first_id + - last_id + - has_more + x-oaiMeta: + name: List assistants response object + group: chat + example: *list_assistants_example + + AssistantToolsCode: + type: object + title: Code interpreter tool + properties: + type: + type: string + description: "The type of tool being defined: `code_interpreter`" + enum: ["code_interpreter"] + required: + - type + + AssistantToolsFileSearch: + type: object + title: FileSearch tool + properties: + type: + type: string + description: "The type of tool being defined: `file_search`" + enum: ["file_search"] + file_search: + type: object + description: Overrides for the file search tool. + properties: + max_num_results: + type: integer + minimum: 1 + maximum: 50 + description: | + The maximum number of results the file search tool should output. The default is 20 for `gpt-4*` models and 5 for `gpt-3.5-turbo`. This number should be between 1 and 50 inclusive. + + Note that the file search tool may output fewer than `max_num_results` results. See the [file search tool documentation](/docs/assistants/tools/file-search/customizing-file-search-settings) for more information. + ranking_options: + $ref: "#/components/schemas/FileSearchRankingOptions" + required: + - type + + FileSearchRankingOptions: + title: File search tool call ranking options + type: object + description: | + The ranking options for the file search. If not specified, the file search tool will use the `auto` ranker and a score_threshold of 0. + + See the [file search tool documentation](/docs/assistants/tools/file-search/customizing-file-search-settings) for more information. + properties: + ranker: + type: string + description: The ranker to use for the file search. If not specified will use the `auto` ranker. + enum: ["auto", "default_2024_08_21"] + score_threshold: + type: number + description: The score threshold for the file search. All values must be a floating point number between 0 and 1. + minimum: 0 + maximum: 1 + required: + - score_threshold + + AssistantToolsFileSearchTypeOnly: + type: object + title: FileSearch tool + properties: + type: + type: string + description: "The type of tool being defined: `file_search`" + enum: ["file_search"] + required: + - type + + AssistantToolsFunction: + type: object + title: Function tool + properties: + type: + type: string + description: "The type of tool being defined: `function`" + enum: ["function"] + function: + $ref: "#/components/schemas/FunctionObject" + required: + - type + - function + + TruncationObject: + type: object + title: Thread Truncation Controls + description: Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run. + properties: + type: + type: string + description: The truncation strategy to use for the thread. The default is `auto`. If set to `last_messages`, the thread will be truncated to the n most recent messages in the thread. When set to `auto`, messages in the middle of the thread will be dropped to fit the context length of the model, `max_prompt_tokens`. + enum: ["auto", "last_messages"] + last_messages: + type: integer + description: The number of most recent messages from the thread when constructing the context for the run. + minimum: 1 + nullable: true + required: + - type + + AssistantsApiToolChoiceOption: + description: | + Controls which (if any) tool is called by the model. + `none` means the model will not call any tools and instead generates a message. + `auto` is the default value and means the model can pick between generating a message or calling one or more tools. + `required` means the model must call one or more tools before responding to the user. + Specifying a particular tool like `{"type": "file_search"}` or `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool. + + oneOf: + - type: string + description: > + `none` means the model will not call any tools and instead generates a message. + `auto` means the model can pick between generating a message or calling one or more tools. + `required` means the model must call one or more tools before responding to the user. + enum: [none, auto, required] + - $ref: "#/components/schemas/AssistantsNamedToolChoice" + x-oaiExpandable: true + + AssistantsNamedToolChoice: + type: object + description: Specifies a tool the model should use. Use to force the model to call a specific tool. + properties: + type: + type: string + enum: ["function", "code_interpreter", "file_search"] + description: The type of the tool. If type is `function`, the function name must be set + function: + type: object + properties: + name: + type: string + description: The name of the function to call. + required: + - name + required: + - type + + RunObject: + type: object + title: A run on a thread + description: Represents an execution run on a [thread](/docs/api-reference/threads). + properties: + id: + description: The identifier, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `thread.run`. + type: string + enum: ["thread.run"] + created_at: + description: The Unix timestamp (in seconds) for when the run was created. + type: integer + thread_id: + description: The ID of the [thread](/docs/api-reference/threads) that was executed on as a part of this run. + type: string + assistant_id: + description: The ID of the [assistant](/docs/api-reference/assistants) used for execution of this run. + type: string + status: + description: The status of the run, which can be either `queued`, `in_progress`, `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`, `incomplete`, or `expired`. + type: string + enum: + [ + "queued", + "in_progress", + "requires_action", + "cancelling", + "cancelled", + "failed", + "completed", + "incomplete", + "expired", + ] + required_action: + type: object + description: Details on the action required to continue the run. Will be `null` if no action is required. + nullable: true + properties: + type: + description: For now, this is always `submit_tool_outputs`. + type: string + enum: ["submit_tool_outputs"] + submit_tool_outputs: + type: object + description: Details on the tool outputs needed for this run to continue. + properties: + tool_calls: + type: array + description: A list of the relevant tool calls. + items: + $ref: "#/components/schemas/RunToolCallObject" + required: + - tool_calls + required: + - type + - submit_tool_outputs + last_error: + type: object + description: The last error associated with this run. Will be `null` if there are no errors. + nullable: true + properties: + code: + type: string + description: One of `server_error`, `rate_limit_exceeded`, or `invalid_prompt`. + enum: + ["server_error", "rate_limit_exceeded", "invalid_prompt"] + message: + type: string + description: A human-readable description of the error. + required: + - code + - message + expires_at: + description: The Unix timestamp (in seconds) for when the run will expire. + type: integer + nullable: true + started_at: + description: The Unix timestamp (in seconds) for when the run was started. + type: integer + nullable: true + cancelled_at: + description: The Unix timestamp (in seconds) for when the run was cancelled. + type: integer + nullable: true + failed_at: + description: The Unix timestamp (in seconds) for when the run failed. + type: integer + nullable: true + completed_at: + description: The Unix timestamp (in seconds) for when the run was completed. + type: integer + nullable: true + incomplete_details: + description: Details on why the run is incomplete. Will be `null` if the run is not incomplete. + type: object + nullable: true + properties: + reason: + description: The reason why the run is incomplete. This will point to which specific token limit was reached over the course of the run. + type: string + enum: ["max_completion_tokens", "max_prompt_tokens"] + model: + description: The model that the [assistant](/docs/api-reference/assistants) used for this run. + type: string + instructions: + description: The instructions that the [assistant](/docs/api-reference/assistants) used for this run. + type: string + tools: + description: The list of tools that the [assistant](/docs/api-reference/assistants) used for this run. + default: [] + type: array + maxItems: 20 + items: + oneOf: + - $ref: "#/components/schemas/AssistantToolsCode" + - $ref: "#/components/schemas/AssistantToolsFileSearch" + - $ref: "#/components/schemas/AssistantToolsFunction" + x-oaiExpandable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + usage: + $ref: "#/components/schemas/RunCompletionUsage" + temperature: + description: The sampling temperature used for this run. If not set, defaults to 1. + type: number + nullable: true + top_p: + description: The nucleus sampling value used for this run. If not set, defaults to 1. + type: number + nullable: true + max_prompt_tokens: + type: integer + nullable: true + description: | + The maximum number of prompt tokens specified to have been used over the course of the run. + minimum: 256 + max_completion_tokens: + type: integer + nullable: true + description: | + The maximum number of completion tokens specified to have been used over the course of the run. + minimum: 256 + truncation_strategy: + $ref: "#/components/schemas/TruncationObject" + nullable: true + tool_choice: + $ref: "#/components/schemas/AssistantsApiToolChoiceOption" + nullable: true + parallel_tool_calls: + $ref: "#/components/schemas/ParallelToolCalls" + response_format: + $ref: "#/components/schemas/AssistantsApiResponseFormatOption" + nullable: true + required: + - id + - object + - created_at + - thread_id + - assistant_id + - status + - required_action + - last_error + - expires_at + - started_at + - cancelled_at + - failed_at + - completed_at + - model + - instructions + - tools + - metadata + - usage + - incomplete_details + - max_prompt_tokens + - max_completion_tokens + - truncation_strategy + - tool_choice + - parallel_tool_calls + - response_format + x-oaiMeta: + name: The run object + beta: true + example: | + { + "id": "run_abc123", + "object": "thread.run", + "created_at": 1698107661, + "assistant_id": "asst_abc123", + "thread_id": "thread_abc123", + "status": "completed", + "started_at": 1699073476, + "expires_at": null, + "cancelled_at": null, + "failed_at": null, + "completed_at": 1699073498, + "last_error": null, + "model": "gpt-4o", + "instructions": null, + "tools": [{"type": "file_search"}, {"type": "code_interpreter"}], + "metadata": {}, + "incomplete_details": null, + "usage": { + "prompt_tokens": 123, + "completion_tokens": 456, + "total_tokens": 579 + }, + "temperature": 1.0, + "top_p": 1.0, + "max_prompt_tokens": 1000, + "max_completion_tokens": 1000, + "truncation_strategy": { + "type": "auto", + "last_messages": null + }, + "response_format": "auto", + "tool_choice": "auto", + "parallel_tool_calls": true + } + CreateRunRequest: + type: object + additionalProperties: false + properties: + assistant_id: + description: The ID of the [assistant](/docs/api-reference/assistants) to use to execute this run. + type: string + model: + description: The ID of the [Model](/docs/api-reference/models) to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used. + example: "gpt-4o" + anyOf: + - type: string + - type: string + enum: + [ + "gpt-4o", + "gpt-4o-2024-08-06", + "gpt-4o-2024-05-13", + "gpt-4o-2024-08-06", + "gpt-4o-mini", + "gpt-4o-mini-2024-07-18", + "gpt-4-turbo", + "gpt-4-turbo-2024-04-09", + "gpt-4-0125-preview", + "gpt-4-turbo-preview", + "gpt-4-1106-preview", + "gpt-4-vision-preview", + "gpt-4", + "gpt-4-0314", + "gpt-4-0613", + "gpt-4-32k", + "gpt-4-32k-0314", + "gpt-4-32k-0613", + "gpt-3.5-turbo", + "gpt-3.5-turbo-16k", + "gpt-3.5-turbo-0613", + "gpt-3.5-turbo-1106", + "gpt-3.5-turbo-0125", + "gpt-3.5-turbo-16k-0613", + ] + x-oaiTypeLabel: string + nullable: true + instructions: + description: Overrides the [instructions](/docs/api-reference/assistants/createAssistant) of the assistant. This is useful for modifying the behavior on a per-run basis. + type: string + nullable: true + additional_instructions: + description: Appends additional instructions at the end of the instructions for the run. This is useful for modifying the behavior on a per-run basis without overriding other instructions. + type: string + nullable: true + additional_messages: + description: Adds additional messages to the thread before creating the run. + type: array + items: + $ref: "#/components/schemas/CreateMessageRequest" + nullable: true + tools: + description: Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis. + nullable: true + type: array + maxItems: 20 + items: + oneOf: + - $ref: "#/components/schemas/AssistantToolsCode" + - $ref: "#/components/schemas/AssistantToolsFileSearch" + - $ref: "#/components/schemas/AssistantToolsFunction" + x-oaiExpandable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + temperature: + type: number + minimum: 0 + maximum: 2 + default: 1 + example: 1 + nullable: true + description: *run_temperature_description + top_p: + type: number + minimum: 0 + maximum: 1 + default: 1 + example: 1 + nullable: true + description: *run_top_p_description + stream: + type: boolean + nullable: true + description: | + If `true`, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a `data: [DONE]` message. + max_prompt_tokens: + type: integer + nullable: true + description: | + The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info. + minimum: 256 + max_completion_tokens: + type: integer + nullable: true + description: | + The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info. + minimum: 256 + truncation_strategy: + $ref: "#/components/schemas/TruncationObject" + nullable: true + tool_choice: + $ref: "#/components/schemas/AssistantsApiToolChoiceOption" + nullable: true + parallel_tool_calls: + $ref: "#/components/schemas/ParallelToolCalls" + response_format: + $ref: "#/components/schemas/AssistantsApiResponseFormatOption" + nullable: true + required: + - thread_id + - assistant_id + ListRunsResponse: + type: object + properties: + object: + type: string + example: "list" + data: + type: array + items: + $ref: "#/components/schemas/RunObject" + first_id: + type: string + example: "run_abc123" + last_id: + type: string + example: "run_abc456" + has_more: + type: boolean + example: false + required: + - object + - data + - first_id + - last_id + - has_more + ModifyRunRequest: + type: object + additionalProperties: false + properties: + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + SubmitToolOutputsRunRequest: + type: object + additionalProperties: false + properties: + tool_outputs: + description: A list of tools for which the outputs are being submitted. + type: array + items: + type: object + properties: + tool_call_id: + type: string + description: The ID of the tool call in the `required_action` object within the run object the output is being submitted for. + output: + type: string + description: The output of the tool call to be submitted to continue the run. + stream: + type: boolean + nullable: true + description: | + If `true`, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a `data: [DONE]` message. + required: + - tool_outputs + + RunToolCallObject: + type: object + description: Tool call objects + properties: + id: + type: string + description: The ID of the tool call. This ID must be referenced when you submit the tool outputs in using the [Submit tool outputs to run](/docs/api-reference/runs/submitToolOutputs) endpoint. + type: + type: string + description: The type of tool call the output is required for. For now, this is always `function`. + enum: ["function"] + function: + type: object + description: The function definition. + properties: + name: + type: string + description: The name of the function. + arguments: + type: string + description: The arguments that the model expects you to pass to the function. + required: + - name + - arguments + required: + - id + - type + - function + + CreateThreadAndRunRequest: + type: object + additionalProperties: false + properties: + assistant_id: + description: The ID of the [assistant](/docs/api-reference/assistants) to use to execute this run. + type: string + thread: + $ref: "#/components/schemas/CreateThreadRequest" + description: If no thread is provided, an empty thread will be created. + model: + description: The ID of the [Model](/docs/api-reference/models) to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used. + example: "gpt-4o" + anyOf: + - type: string + - type: string + enum: + [ + "gpt-4o", + "gpt-4o-2024-08-06", + "gpt-4o-2024-05-13", + "gpt-4o-2024-08-06", + "gpt-4o-mini", + "gpt-4o-mini-2024-07-18", + "gpt-4-turbo", + "gpt-4-turbo-2024-04-09", + "gpt-4-0125-preview", + "gpt-4-turbo-preview", + "gpt-4-1106-preview", + "gpt-4-vision-preview", + "gpt-4", + "gpt-4-0314", + "gpt-4-0613", + "gpt-4-32k", + "gpt-4-32k-0314", + "gpt-4-32k-0613", + "gpt-3.5-turbo", + "gpt-3.5-turbo-16k", + "gpt-3.5-turbo-0613", + "gpt-3.5-turbo-1106", + "gpt-3.5-turbo-0125", + "gpt-3.5-turbo-16k-0613", + ] + x-oaiTypeLabel: string + nullable: true + instructions: + description: Override the default system message of the assistant. This is useful for modifying the behavior on a per-run basis. + type: string + nullable: true + tools: + description: Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis. + nullable: true + type: array + maxItems: 20 + items: + oneOf: + - $ref: "#/components/schemas/AssistantToolsCode" + - $ref: "#/components/schemas/AssistantToolsFileSearch" + - $ref: "#/components/schemas/AssistantToolsFunction" + tool_resources: + type: object + description: | + A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + properties: + code_interpreter: + type: object + properties: + file_ids: + type: array + description: | + A list of [file](/docs/api-reference/files) IDs made available to the `code_interpreter` tool. There can be a maximum of 20 files associated with the tool. + default: [] + maxItems: 20 + items: + type: string + file_search: + type: object + properties: + vector_store_ids: + type: array + description: | + The ID of the [vector store](/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant. + maxItems: 1 + items: + type: string + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + temperature: + type: number + minimum: 0 + maximum: 2 + default: 1 + example: 1 + nullable: true + description: *run_temperature_description + top_p: + type: number + minimum: 0 + maximum: 1 + default: 1 + example: 1 + nullable: true + description: *run_top_p_description + stream: + type: boolean + nullable: true + description: | + If `true`, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a `data: [DONE]` message. + max_prompt_tokens: + type: integer + nullable: true + description: | + The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info. + minimum: 256 + max_completion_tokens: + type: integer + nullable: true + description: | + The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info. + minimum: 256 + truncation_strategy: + $ref: "#/components/schemas/TruncationObject" + nullable: true + tool_choice: + $ref: "#/components/schemas/AssistantsApiToolChoiceOption" + nullable: true + parallel_tool_calls: + $ref: "#/components/schemas/ParallelToolCalls" + response_format: + $ref: "#/components/schemas/AssistantsApiResponseFormatOption" + nullable: true + required: + - thread_id + - assistant_id + + ThreadObject: + type: object + title: Thread + description: Represents a thread that contains [messages](/docs/api-reference/messages). + properties: + id: + description: The identifier, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `thread`. + type: string + enum: ["thread"] + created_at: + description: The Unix timestamp (in seconds) for when the thread was created. + type: integer + tool_resources: + type: object + description: | + A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + properties: + code_interpreter: + type: object + properties: + file_ids: + type: array + description: | + A list of [file](/docs/api-reference/files) IDs made available to the `code_interpreter` tool. There can be a maximum of 20 files associated with the tool. + default: [] + maxItems: 20 + items: + type: string + file_search: + type: object + properties: + vector_store_ids: + type: array + description: | + The [vector store](/docs/api-reference/vector-stores/object) attached to this thread. There can be a maximum of 1 vector store attached to the thread. + maxItems: 1 + items: + type: string + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + required: + - id + - object + - created_at + - tool_resources + - metadata + x-oaiMeta: + name: The thread object + beta: true + example: | + { + "id": "thread_abc123", + "object": "thread", + "created_at": 1698107661, + "metadata": {} + } + + CreateThreadRequest: + type: object + additionalProperties: false + properties: + messages: + description: A list of [messages](/docs/api-reference/messages) to start the thread with. + type: array + items: + $ref: "#/components/schemas/CreateMessageRequest" + tool_resources: + type: object + description: | + A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + properties: + code_interpreter: + type: object + properties: + file_ids: + type: array + description: | + A list of [file](/docs/api-reference/files) IDs made available to the `code_interpreter` tool. There can be a maximum of 20 files associated with the tool. + default: [] + maxItems: 20 + items: + type: string + file_search: + type: object + properties: + vector_store_ids: + type: array + description: | + The [vector store](/docs/api-reference/vector-stores/object) attached to this thread. There can be a maximum of 1 vector store attached to the thread. + maxItems: 1 + items: + type: string + vector_stores: + type: array + description: | + A helper to create a [vector store](/docs/api-reference/vector-stores/object) with file_ids and attach it to this thread. There can be a maximum of 1 vector store attached to the thread. + maxItems: 1 + items: + type: object + properties: + file_ids: + type: array + description: | + A list of [file](/docs/api-reference/files) IDs to add to the vector store. There can be a maximum of 10000 files in a vector store. + maxItems: 10000 + items: + type: string + chunking_strategy: + # Ideally we'd reuse the chunking strategy schema here, but it doesn't expand properly + type: object + description: The chunking strategy used to chunk the file(s). If not set, will use the `auto` strategy. + oneOf: + - type: object + title: Auto Chunking Strategy + description: The default strategy. This strategy currently uses a `max_chunk_size_tokens` of `800` and `chunk_overlap_tokens` of `400`. + additionalProperties: false + properties: + type: + type: string + description: Always `auto`. + enum: ["auto"] + required: + - type + - type: object + title: Static Chunking Strategy + additionalProperties: false + properties: + type: + type: string + description: Always `static`. + enum: ["static"] + static: + type: object + additionalProperties: false + properties: + max_chunk_size_tokens: + type: integer + minimum: 100 + maximum: 4096 + description: The maximum number of tokens in each chunk. The default value is `800`. The minimum value is `100` and the maximum value is `4096`. + chunk_overlap_tokens: + type: integer + description: | + The number of tokens that overlap between chunks. The default value is `400`. + + Note that the overlap must not exceed half of `max_chunk_size_tokens`. + required: + - max_chunk_size_tokens + - chunk_overlap_tokens + required: + - type + - static + x-oaiExpandable: true + metadata: + type: object + description: | + Set of 16 key-value pairs that can be attached to a vector store. This can be useful for storing additional information about the vector store in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long. + x-oaiTypeLabel: map + x-oaiExpandable: true + oneOf: + - required: [vector_store_ids] + - required: [vector_stores] + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + + ModifyThreadRequest: + type: object + additionalProperties: false + properties: + tool_resources: + type: object + description: | + A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs. + properties: + code_interpreter: + type: object + properties: + file_ids: + type: array + description: | + A list of [file](/docs/api-reference/files) IDs made available to the `code_interpreter` tool. There can be a maximum of 20 files associated with the tool. + default: [] + maxItems: 20 + items: + type: string + file_search: + type: object + properties: + vector_store_ids: + type: array + description: | + The [vector store](/docs/api-reference/vector-stores/object) attached to this thread. There can be a maximum of 1 vector store attached to the thread. + maxItems: 1 + items: + type: string + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + + DeleteThreadResponse: + type: object + properties: + id: + type: string + deleted: + type: boolean + object: + type: string + enum: [thread.deleted] + required: + - id + - object + - deleted + + ListThreadsResponse: + properties: + object: + type: string + example: "list" + data: + type: array + items: + $ref: "#/components/schemas/ThreadObject" + first_id: + type: string + example: "asst_abc123" + last_id: + type: string + example: "asst_abc456" + has_more: + type: boolean + example: false + required: + - object + - data + - first_id + - last_id + - has_more + + MessageObject: + type: object + title: The message object + description: Represents a message within a [thread](/docs/api-reference/threads). + properties: + id: + description: The identifier, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `thread.message`. + type: string + enum: ["thread.message"] + created_at: + description: The Unix timestamp (in seconds) for when the message was created. + type: integer + thread_id: + description: The [thread](/docs/api-reference/threads) ID that this message belongs to. + type: string + status: + description: The status of the message, which can be either `in_progress`, `incomplete`, or `completed`. + type: string + enum: ["in_progress", "incomplete", "completed"] + incomplete_details: + description: On an incomplete message, details about why the message is incomplete. + type: object + properties: + reason: + type: string + description: The reason the message is incomplete. + enum: + [ + "content_filter", + "max_tokens", + "run_cancelled", + "run_expired", + "run_failed", + ] + nullable: true + required: + - reason + completed_at: + description: The Unix timestamp (in seconds) for when the message was completed. + type: integer + nullable: true + incomplete_at: + description: The Unix timestamp (in seconds) for when the message was marked as incomplete. + type: integer + nullable: true + role: + description: The entity that produced the message. One of `user` or `assistant`. + type: string + enum: ["user", "assistant"] + content: + description: The content of the message in array of text and/or images. + type: array + items: + oneOf: + - $ref: "#/components/schemas/MessageContentImageFileObject" + - $ref: "#/components/schemas/MessageContentImageUrlObject" + - $ref: "#/components/schemas/MessageContentTextObject" + - $ref: "#/components/schemas/MessageContentRefusalObject" + x-oaiExpandable: true + assistant_id: + description: If applicable, the ID of the [assistant](/docs/api-reference/assistants) that authored this message. + type: string + nullable: true + run_id: + description: The ID of the [run](/docs/api-reference/runs) associated with the creation of this message. Value is `null` when messages are created manually using the create message or create thread endpoints. + type: string + nullable: true + attachments: + type: array + items: + type: object + properties: + file_id: + type: string + description: The ID of the file to attach to the message. + tools: + description: The tools to add this file to. + type: array + items: + oneOf: + - $ref: "#/components/schemas/AssistantToolsCode" + - $ref: "#/components/schemas/AssistantToolsFileSearchTypeOnly" + x-oaiExpandable: true + description: A list of files attached to the message, and the tools they were added to. + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + required: + - id + - object + - created_at + - thread_id + - status + - incomplete_details + - completed_at + - incomplete_at + - role + - content + - assistant_id + - run_id + - attachments + - metadata + x-oaiMeta: + name: The message object + beta: true + example: | + { + "id": "msg_abc123", + "object": "thread.message", + "created_at": 1698983503, + "thread_id": "thread_abc123", + "role": "assistant", + "content": [ + { + "type": "text", + "text": { + "value": "Hi! How can I help you today?", + "annotations": [] + } + } + ], + "assistant_id": "asst_abc123", + "run_id": "run_abc123", + "attachments": [], + "metadata": {} + } + + MessageDeltaObject: + type: object + title: Message delta object + description: | + Represents a message delta i.e. any changed fields on a message during streaming. + properties: + id: + description: The identifier of the message, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `thread.message.delta`. + type: string + enum: ["thread.message.delta"] + delta: + description: The delta containing the fields that have changed on the Message. + type: object + properties: + role: + description: The entity that produced the message. One of `user` or `assistant`. + type: string + enum: ["user", "assistant"] + content: + description: The content of the message in array of text and/or images. + type: array + items: + oneOf: + - $ref: "#/components/schemas/MessageDeltaContentImageFileObject" + - $ref: "#/components/schemas/MessageDeltaContentTextObject" + - $ref: "#/components/schemas/MessageDeltaContentRefusalObject" + - $ref: "#/components/schemas/MessageDeltaContentImageUrlObject" + x-oaiExpandable: true + required: + - id + - object + - delta + x-oaiMeta: + name: The message delta object + beta: true + example: | + { + "id": "msg_123", + "object": "thread.message.delta", + "delta": { + "content": [ + { + "index": 0, + "type": "text", + "text": { "value": "Hello", "annotations": [] } + } + ] + } + } + + CreateMessageRequest: + type: object + additionalProperties: false + required: + - role + - content + properties: + role: + type: string + enum: ["user", "assistant"] + description: | + The role of the entity that is creating the message. Allowed values include: + - `user`: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages. + - `assistant`: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation. + content: + oneOf: + - type: string + description: The text contents of the message. + title: Text content + - type: array + description: An array of content parts with a defined type, each can be of type `text` or images can be passed with `image_url` or `image_file`. Image types are only supported on [Vision-compatible models](/docs/models/overview). + title: Array of content parts + items: + oneOf: + - $ref: "#/components/schemas/MessageContentImageFileObject" + - $ref: "#/components/schemas/MessageContentImageUrlObject" + - $ref: "#/components/schemas/MessageRequestContentTextObject" + x-oaiExpandable: true + minItems: 1 + x-oaiExpandable: true + attachments: + type: array + items: + type: object + properties: + file_id: + type: string + description: The ID of the file to attach to the message. + tools: + description: The tools to add this file to. + type: array + items: + oneOf: + - $ref: "#/components/schemas/AssistantToolsCode" + - $ref: "#/components/schemas/AssistantToolsFileSearchTypeOnly" + x-oaiExpandable: true + description: A list of files attached to the message, and the tools they should be added to. + required: + - file_id + - tools + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + + ModifyMessageRequest: + type: object + additionalProperties: false + properties: + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + + DeleteMessageResponse: + type: object + properties: + id: + type: string + deleted: + type: boolean + object: + type: string + enum: [thread.message.deleted] + required: + - id + - object + - deleted + + ListMessagesResponse: + properties: + object: + type: string + example: "list" + data: + type: array + items: + $ref: "#/components/schemas/MessageObject" + first_id: + type: string + example: "msg_abc123" + last_id: + type: string + example: "msg_abc123" + has_more: + type: boolean + example: false + required: + - object + - data + - first_id + - last_id + - has_more + + MessageContentImageFileObject: + title: Image file + type: object + description: References an image [File](/docs/api-reference/files) in the content of a message. + properties: + type: + description: Always `image_file`. + type: string + enum: ["image_file"] + image_file: + type: object + properties: + file_id: + description: The [File](/docs/api-reference/files) ID of the image in the message content. Set `purpose="vision"` when uploading the File if you need to later display the file content. + type: string + detail: + type: string + description: Specifies the detail level of the image if specified by the user. `low` uses fewer tokens, you can opt in to high resolution using `high`. + enum: ["auto", "low", "high"] + default: "auto" + required: + - file_id + required: + - type + - image_file + + MessageDeltaContentImageFileObject: + title: Image file + type: object + description: References an image [File](/docs/api-reference/files) in the content of a message. + properties: + index: + type: integer + description: The index of the content part in the message. + type: + description: Always `image_file`. + type: string + enum: ["image_file"] + image_file: + type: object + properties: + file_id: + description: The [File](/docs/api-reference/files) ID of the image in the message content. Set `purpose="vision"` when uploading the File if you need to later display the file content. + type: string + detail: + type: string + description: Specifies the detail level of the image if specified by the user. `low` uses fewer tokens, you can opt in to high resolution using `high`. + enum: ["auto", "low", "high"] + default: "auto" + required: + - index + - type + + MessageContentImageUrlObject: + title: Image URL + type: object + description: References an image URL in the content of a message. + properties: + type: + type: string + enum: ["image_url"] + description: The type of the content part. + image_url: + type: object + properties: + url: + type: string + description: "The external URL of the image, must be a supported image types: jpeg, jpg, png, gif, webp." + format: uri + detail: + type: string + description: Specifies the detail level of the image. `low` uses fewer tokens, you can opt in to high resolution using `high`. Default value is `auto` + enum: ["auto", "low", "high"] + default: "auto" + required: + - url + required: + - type + - image_url + + MessageDeltaContentImageUrlObject: + title: Image URL + type: object + description: References an image URL in the content of a message. + properties: + index: + type: integer + description: The index of the content part in the message. + type: + description: Always `image_url`. + type: string + enum: ["image_url"] + image_url: + type: object + properties: + url: + description: "The URL of the image, must be a supported image types: jpeg, jpg, png, gif, webp." + type: string + detail: + type: string + description: Specifies the detail level of the image. `low` uses fewer tokens, you can opt in to high resolution using `high`. + enum: ["auto", "low", "high"] + default: "auto" + required: + - index + - type + + MessageContentTextObject: + title: Text + type: object + description: The text content that is part of a message. + properties: + type: + description: Always `text`. + type: string + enum: ["text"] + text: + type: object + properties: + value: + description: The data that makes up the text. + type: string + annotations: + type: array + items: + oneOf: + - $ref: "#/components/schemas/MessageContentTextAnnotationsFileCitationObject" + - $ref: "#/components/schemas/MessageContentTextAnnotationsFilePathObject" + x-oaiExpandable: true + required: + - value + - annotations + required: + - type + - text + + MessageContentRefusalObject: + title: Refusal + type: object + description: The refusal content generated by the assistant. + properties: + type: + description: Always `refusal`. + type: string + enum: ["refusal"] + refusal: + type: string + nullable: false + required: + - type + - refusal + + MessageRequestContentTextObject: + title: Text + type: object + description: The text content that is part of a message. + properties: + type: + description: Always `text`. + type: string + enum: ["text"] + text: + type: string + description: Text content to be sent to the model + required: + - type + - text + + MessageContentTextAnnotationsFileCitationObject: + title: File citation + type: object + description: A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "file_search" tool to search files. + properties: + type: + description: Always `file_citation`. + type: string + enum: ["file_citation"] + text: + description: The text in the message content that needs to be replaced. + type: string + file_citation: + type: object + properties: + file_id: + description: The ID of the specific File the citation is from. + type: string + required: + - file_id + start_index: + type: integer + minimum: 0 + end_index: + type: integer + minimum: 0 + required: + - type + - text + - file_citation + - start_index + - end_index + + MessageContentTextAnnotationsFilePathObject: + title: File path + type: object + description: A URL for the file that's generated when the assistant used the `code_interpreter` tool to generate a file. + properties: + type: + description: Always `file_path`. + type: string + enum: ["file_path"] + text: + description: The text in the message content that needs to be replaced. + type: string + file_path: + type: object + properties: + file_id: + description: The ID of the file that was generated. + type: string + required: + - file_id + start_index: + type: integer + minimum: 0 + end_index: + type: integer + minimum: 0 + required: + - type + - text + - file_path + - start_index + - end_index + + MessageDeltaContentTextObject: + title: Text + type: object + description: The text content that is part of a message. + properties: + index: + type: integer + description: The index of the content part in the message. + type: + description: Always `text`. + type: string + enum: ["text"] + text: + type: object + properties: + value: + description: The data that makes up the text. + type: string + annotations: + type: array + items: + oneOf: + - $ref: "#/components/schemas/MessageDeltaContentTextAnnotationsFileCitationObject" + - $ref: "#/components/schemas/MessageDeltaContentTextAnnotationsFilePathObject" + x-oaiExpandable: true + required: + - index + - type + + MessageDeltaContentRefusalObject: + title: Refusal + type: object + description: The refusal content that is part of a message. + properties: + index: + type: integer + description: The index of the refusal part in the message. + type: + description: Always `refusal`. + type: string + enum: ["refusal"] + refusal: + type: string + required: + - index + - type + + + MessageDeltaContentTextAnnotationsFileCitationObject: + title: File citation + type: object + description: A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "file_search" tool to search files. + properties: + index: + type: integer + description: The index of the annotation in the text content part. + type: + description: Always `file_citation`. + type: string + enum: ["file_citation"] + text: + description: The text in the message content that needs to be replaced. + type: string + file_citation: + type: object + properties: + file_id: + description: The ID of the specific File the citation is from. + type: string + quote: + description: The specific quote in the file. + type: string + start_index: + type: integer + minimum: 0 + end_index: + type: integer + minimum: 0 + required: + - index + - type + + MessageDeltaContentTextAnnotationsFilePathObject: + title: File path + type: object + description: A URL for the file that's generated when the assistant used the `code_interpreter` tool to generate a file. + properties: + index: + type: integer + description: The index of the annotation in the text content part. + type: + description: Always `file_path`. + type: string + enum: ["file_path"] + text: + description: The text in the message content that needs to be replaced. + type: string + file_path: + type: object + properties: + file_id: + description: The ID of the file that was generated. + type: string + start_index: + type: integer + minimum: 0 + end_index: + type: integer + minimum: 0 + required: + - index + - type + + RunStepObject: + type: object + title: Run steps + description: | + Represents a step in execution of a run. + properties: + id: + description: The identifier of the run step, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `thread.run.step`. + type: string + enum: ["thread.run.step"] + created_at: + description: The Unix timestamp (in seconds) for when the run step was created. + type: integer + assistant_id: + description: The ID of the [assistant](/docs/api-reference/assistants) associated with the run step. + type: string + thread_id: + description: The ID of the [thread](/docs/api-reference/threads) that was run. + type: string + run_id: + description: The ID of the [run](/docs/api-reference/runs) that this run step is a part of. + type: string + type: + description: The type of run step, which can be either `message_creation` or `tool_calls`. + type: string + enum: ["message_creation", "tool_calls"] + status: + description: The status of the run step, which can be either `in_progress`, `cancelled`, `failed`, `completed`, or `expired`. + type: string + enum: ["in_progress", "cancelled", "failed", "completed", "expired"] + step_details: + type: object + description: The details of the run step. + oneOf: + - $ref: "#/components/schemas/RunStepDetailsMessageCreationObject" + - $ref: "#/components/schemas/RunStepDetailsToolCallsObject" + x-oaiExpandable: true + last_error: + type: object + description: The last error associated with this run step. Will be `null` if there are no errors. + nullable: true + properties: + code: + type: string + description: One of `server_error` or `rate_limit_exceeded`. + enum: ["server_error", "rate_limit_exceeded"] + message: + type: string + description: A human-readable description of the error. + required: + - code + - message + expired_at: + description: The Unix timestamp (in seconds) for when the run step expired. A step is considered expired if the parent run is expired. + type: integer + nullable: true + cancelled_at: + description: The Unix timestamp (in seconds) for when the run step was cancelled. + type: integer + nullable: true + failed_at: + description: The Unix timestamp (in seconds) for when the run step failed. + type: integer + nullable: true + completed_at: + description: The Unix timestamp (in seconds) for when the run step completed. + type: integer + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + usage: + $ref: "#/components/schemas/RunStepCompletionUsage" + required: + - id + - object + - created_at + - assistant_id + - thread_id + - run_id + - type + - status + - step_details + - last_error + - expired_at + - cancelled_at + - failed_at + - completed_at + - metadata + - usage + x-oaiMeta: + name: The run step object + beta: true + example: *run_step_object_example + + RunStepDeltaObject: + type: object + title: Run step delta object + description: | + Represents a run step delta i.e. any changed fields on a run step during streaming. + properties: + id: + description: The identifier of the run step, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `thread.run.step.delta`. + type: string + enum: ["thread.run.step.delta"] + delta: + description: The delta containing the fields that have changed on the run step. + type: object + properties: + step_details: + type: object + description: The details of the run step. + oneOf: + - $ref: "#/components/schemas/RunStepDeltaStepDetailsMessageCreationObject" + - $ref: "#/components/schemas/RunStepDeltaStepDetailsToolCallsObject" + x-oaiExpandable: true + required: + - id + - object + - delta + x-oaiMeta: + name: The run step delta object + beta: true + example: | + { + "id": "step_123", + "object": "thread.run.step.delta", + "delta": { + "step_details": { + "type": "tool_calls", + "tool_calls": [ + { + "index": 0, + "id": "call_123", + "type": "code_interpreter", + "code_interpreter": { "input": "", "outputs": [] } + } + ] + } + } + } + + ListRunStepsResponse: + properties: + object: + type: string + example: "list" + data: + type: array + items: + $ref: "#/components/schemas/RunStepObject" + first_id: + type: string + example: "step_abc123" + last_id: + type: string + example: "step_abc456" + has_more: + type: boolean + example: false + required: + - object + - data + - first_id + - last_id + - has_more + + RunStepDetailsMessageCreationObject: + title: Message creation + type: object + description: Details of the message creation by the run step. + properties: + type: + description: Always `message_creation`. + type: string + enum: ["message_creation"] + message_creation: + type: object + properties: + message_id: + type: string + description: The ID of the message that was created by this run step. + required: + - message_id + required: + - type + - message_creation + + RunStepDeltaStepDetailsMessageCreationObject: + title: Message creation + type: object + description: Details of the message creation by the run step. + properties: + type: + description: Always `message_creation`. + type: string + enum: ["message_creation"] + message_creation: + type: object + properties: + message_id: + type: string + description: The ID of the message that was created by this run step. + required: + - type + + RunStepDetailsToolCallsObject: + title: Tool calls + type: object + description: Details of the tool call. + properties: + type: + description: Always `tool_calls`. + type: string + enum: ["tool_calls"] + tool_calls: + type: array + description: | + An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `file_search`, or `function`. + items: + oneOf: + - $ref: "#/components/schemas/RunStepDetailsToolCallsCodeObject" + - $ref: "#/components/schemas/RunStepDetailsToolCallsFileSearchObject" + - $ref: "#/components/schemas/RunStepDetailsToolCallsFunctionObject" + x-oaiExpandable: true + required: + - type + - tool_calls + + RunStepDeltaStepDetailsToolCallsObject: + title: Tool calls + type: object + description: Details of the tool call. + properties: + type: + description: Always `tool_calls`. + type: string + enum: ["tool_calls"] + tool_calls: + type: array + description: | + An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `file_search`, or `function`. + items: + oneOf: + - $ref: "#/components/schemas/RunStepDeltaStepDetailsToolCallsCodeObject" + - $ref: "#/components/schemas/RunStepDeltaStepDetailsToolCallsFileSearchObject" + - $ref: "#/components/schemas/RunStepDeltaStepDetailsToolCallsFunctionObject" + x-oaiExpandable: true + required: + - type + + RunStepDetailsToolCallsCodeObject: + title: Code Interpreter tool call + type: object + description: Details of the Code Interpreter tool call the run step was involved in. + properties: + id: + type: string + description: The ID of the tool call. + type: + type: string + description: The type of tool call. This is always going to be `code_interpreter` for this type of tool call. + enum: ["code_interpreter"] + code_interpreter: + type: object + description: The Code Interpreter tool call definition. + required: + - input + - outputs + properties: + input: + type: string + description: The input to the Code Interpreter tool call. + outputs: + type: array + description: The outputs from the Code Interpreter tool call. Code Interpreter can output one or more items, including text (`logs`) or images (`image`). Each of these are represented by a different object type. + items: + type: object + oneOf: + - $ref: "#/components/schemas/RunStepDetailsToolCallsCodeOutputLogsObject" + - $ref: "#/components/schemas/RunStepDetailsToolCallsCodeOutputImageObject" + x-oaiExpandable: true + required: + - id + - type + - code_interpreter + + RunStepDeltaStepDetailsToolCallsCodeObject: + title: Code interpreter tool call + type: object + description: Details of the Code Interpreter tool call the run step was involved in. + properties: + index: + type: integer + description: The index of the tool call in the tool calls array. + id: + type: string + description: The ID of the tool call. + type: + type: string + description: The type of tool call. This is always going to be `code_interpreter` for this type of tool call. + enum: ["code_interpreter"] + code_interpreter: + type: object + description: The Code Interpreter tool call definition. + properties: + input: + type: string + description: The input to the Code Interpreter tool call. + outputs: + type: array + description: The outputs from the Code Interpreter tool call. Code Interpreter can output one or more items, including text (`logs`) or images (`image`). Each of these are represented by a different object type. + items: + type: object + oneOf: + - $ref: "#/components/schemas/RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject" + - $ref: "#/components/schemas/RunStepDeltaStepDetailsToolCallsCodeOutputImageObject" + x-oaiExpandable: true + required: + - index + - type + + RunStepDetailsToolCallsCodeOutputLogsObject: + title: Code Interpreter log output + type: object + description: Text output from the Code Interpreter tool call as part of a run step. + properties: + type: + description: Always `logs`. + type: string + enum: ["logs"] + logs: + type: string + description: The text output from the Code Interpreter tool call. + required: + - type + - logs + + RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject: + title: Code interpreter log output + type: object + description: Text output from the Code Interpreter tool call as part of a run step. + properties: + index: + type: integer + description: The index of the output in the outputs array. + type: + description: Always `logs`. + type: string + enum: ["logs"] + logs: + type: string + description: The text output from the Code Interpreter tool call. + required: + - index + - type + + RunStepDetailsToolCallsCodeOutputImageObject: + title: Code Interpreter image output + type: object + properties: + type: + description: Always `image`. + type: string + enum: ["image"] + image: + type: object + properties: + file_id: + description: The [file](/docs/api-reference/files) ID of the image. + type: string + required: + - file_id + required: + - type + - image + + RunStepDeltaStepDetailsToolCallsCodeOutputImageObject: + title: Code interpreter image output + type: object + properties: + index: + type: integer + description: The index of the output in the outputs array. + type: + description: Always `image`. + type: string + enum: ["image"] + image: + type: object + properties: + file_id: + description: The [file](/docs/api-reference/files) ID of the image. + type: string + required: + - index + - type + + RunStepDetailsToolCallsFileSearchObject: + title: File search tool call + type: object + properties: + id: + type: string + description: The ID of the tool call object. + type: + type: string + description: The type of tool call. This is always going to be `file_search` for this type of tool call. + enum: ["file_search"] + file_search: + type: object + description: For now, this is always going to be an empty object. + x-oaiTypeLabel: map + properties: + ranking_options: + $ref: "#/components/schemas/RunStepDetailsToolCallsFileSearchRankingOptionsObject" + results: + type: array + description: The results of the file search. + items: + $ref: "#/components/schemas/RunStepDetailsToolCallsFileSearchResultObject" + required: + - id + - type + - file_search + + RunStepDetailsToolCallsFileSearchRankingOptionsObject: + title: File search tool call ranking options + type: object + description: The ranking options for the file search. + properties: + ranker: + type: string + description: The ranker used for the file search. + enum: ["default_2024_08_21"] + score_threshold: + type: number + description: The score threshold for the file search. All values must be a floating point number between 0 and 1. + minimum: 0 + maximum: 1 + required: + - ranker + - score_threshold + + RunStepDetailsToolCallsFileSearchResultObject: + title: File search tool call result + type: object + description: A result instance of the file search. + x-oaiTypeLabel: map + properties: + file_id: + type: string + description: The ID of the file that result was found in. + file_name: + type: string + description: The name of the file that result was found in. + score: + type: number + description: The score of the result. All values must be a floating point number between 0 and 1. + minimum: 0 + maximum: 1 + content: + type: array + description: The content of the result that was found. The content is only included if requested via the include query parameter. + items: + type: object + properties: + type: + type: string + description: The type of the content. + enum: ["text"] + text: + type: string + description: The text content of the file. + required: + - file_id + - file_name + - score + + RunStepDeltaStepDetailsToolCallsFileSearchObject: + title: File search tool call + type: object + properties: + index: + type: integer + description: The index of the tool call in the tool calls array. + id: + type: string + description: The ID of the tool call object. + type: + type: string + description: The type of tool call. This is always going to be `file_search` for this type of tool call. + enum: ["file_search"] + file_search: + type: object + description: For now, this is always going to be an empty object. + x-oaiTypeLabel: map + required: + - index + - type + - file_search + + RunStepDetailsToolCallsFunctionObject: + type: object + title: Function tool call + properties: + id: + type: string + description: The ID of the tool call object. + type: + type: string + description: The type of tool call. This is always going to be `function` for this type of tool call. + enum: ["function"] + function: + type: object + description: The definition of the function that was called. + properties: + name: + type: string + description: The name of the function. + arguments: + type: string + description: The arguments passed to the function. + output: + type: string + description: The output of the function. This will be `null` if the outputs have not been [submitted](/docs/api-reference/runs/submitToolOutputs) yet. + nullable: true + required: + - name + - arguments + - output + required: + - id + - type + - function + + RunStepDeltaStepDetailsToolCallsFunctionObject: + type: object + title: Function tool call + properties: + index: + type: integer + description: The index of the tool call in the tool calls array. + id: + type: string + description: The ID of the tool call object. + type: + type: string + description: The type of tool call. This is always going to be `function` for this type of tool call. + enum: ["function"] + function: + type: object + description: The definition of the function that was called. + properties: + name: + type: string + description: The name of the function. + arguments: + type: string + description: The arguments passed to the function. + output: + type: string + description: The output of the function. This will be `null` if the outputs have not been [submitted](/docs/api-reference/runs/submitToolOutputs) yet. + nullable: true + required: + - index + - type + + VectorStoreExpirationAfter: + type: object + title: Vector store expiration policy + description: The expiration policy for a vector store. + properties: + anchor: + description: "Anchor timestamp after which the expiration policy applies. Supported anchors: `last_active_at`." + type: string + enum: ["last_active_at"] + days: + description: The number of days after the anchor time that the vector store will expire. + type: integer + minimum: 1 + maximum: 365 + required: + - anchor + - days + + VectorStoreObject: + type: object + title: Vector store + description: A vector store is a collection of processed files can be used by the `file_search` tool. + properties: + id: + description: The identifier, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `vector_store`. + type: string + enum: ["vector_store"] + created_at: + description: The Unix timestamp (in seconds) for when the vector store was created. + type: integer + name: + description: The name of the vector store. + type: string + usage_bytes: + description: The total number of bytes used by the files in the vector store. + type: integer + file_counts: + type: object + properties: + in_progress: + description: The number of files that are currently being processed. + type: integer + completed: + description: The number of files that have been successfully processed. + type: integer + failed: + description: The number of files that have failed to process. + type: integer + cancelled: + description: The number of files that were cancelled. + type: integer + total: + description: The total number of files. + type: integer + required: + - in_progress + - completed + - failed + - cancelled + - total + status: + description: The status of the vector store, which can be either `expired`, `in_progress`, or `completed`. A status of `completed` indicates that the vector store is ready for use. + type: string + enum: ["expired", "in_progress", "completed"] + expires_after: + $ref: "#/components/schemas/VectorStoreExpirationAfter" + expires_at: + description: The Unix timestamp (in seconds) for when the vector store will expire. + type: integer + nullable: true + last_active_at: + description: The Unix timestamp (in seconds) for when the vector store was last active. + type: integer + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + required: + - id + - object + - usage_bytes + - created_at + - status + - last_active_at + - name + - file_counts + - metadata + x-oaiMeta: + name: The vector store object + beta: true + example: | + { + "id": "vs_123", + "object": "vector_store", + "created_at": 1698107661, + "usage_bytes": 123456, + "last_active_at": 1698107661, + "name": "my_vector_store", + "status": "completed", + "file_counts": { + "in_progress": 0, + "completed": 100, + "cancelled": 0, + "failed": 0, + "total": 100 + }, + "metadata": {}, + "last_used_at": 1698107661 + } + + CreateVectorStoreRequest: + type: object + additionalProperties: false + properties: + file_ids: + description: A list of [File](/docs/api-reference/files) IDs that the vector store should use. Useful for tools like `file_search` that can access files. + type: array + maxItems: 500 + items: + type: string + name: + description: The name of the vector store. + type: string + expires_after: + $ref: "#/components/schemas/VectorStoreExpirationAfter" + chunking_strategy: + type: object + description: The chunking strategy used to chunk the file(s). If not set, will use the `auto` strategy. Only applicable if `file_ids` is non-empty. + oneOf: + - $ref: "#/components/schemas/AutoChunkingStrategyRequestParam" + - $ref: "#/components/schemas/StaticChunkingStrategyRequestParam" + x-oaiExpandable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + + UpdateVectorStoreRequest: + type: object + additionalProperties: false + properties: + name: + description: The name of the vector store. + type: string + nullable: true + expires_after: + $ref: "#/components/schemas/VectorStoreExpirationAfter" + nullable: true + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + + ListVectorStoresResponse: + properties: + object: + type: string + example: "list" + data: + type: array + items: + $ref: "#/components/schemas/VectorStoreObject" + first_id: + type: string + example: "vs_abc123" + last_id: + type: string + example: "vs_abc456" + has_more: + type: boolean + example: false + required: + - object + - data + - first_id + - last_id + - has_more + + DeleteVectorStoreResponse: + type: object + properties: + id: + type: string + deleted: + type: boolean + object: + type: string + enum: [vector_store.deleted] + required: + - id + - object + - deleted + + VectorStoreFileObject: + type: object + title: Vector store files + description: A list of files attached to a vector store. + properties: + id: + description: The identifier, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `vector_store.file`. + type: string + enum: ["vector_store.file"] + usage_bytes: + description: The total vector store usage in bytes. Note that this may be different from the original file size. + type: integer + created_at: + description: The Unix timestamp (in seconds) for when the vector store file was created. + type: integer + vector_store_id: + description: The ID of the [vector store](/docs/api-reference/vector-stores/object) that the [File](/docs/api-reference/files) is attached to. + type: string + status: + description: The status of the vector store file, which can be either `in_progress`, `completed`, `cancelled`, or `failed`. The status `completed` indicates that the vector store file is ready for use. + type: string + enum: ["in_progress", "completed", "cancelled", "failed"] + last_error: + type: object + description: The last error associated with this vector store file. Will be `null` if there are no errors. + nullable: true + properties: + code: + type: string + description: One of `server_error` or `rate_limit_exceeded`. + enum: + [ + "server_error", + "unsupported_file", + "invalid_file", + ] + message: + type: string + description: A human-readable description of the error. + required: + - code + - message + chunking_strategy: + type: object + description: The strategy used to chunk the file. + oneOf: + - $ref: "#/components/schemas/StaticChunkingStrategyResponseParam" + - $ref: "#/components/schemas/OtherChunkingStrategyResponseParam" + x-oaiExpandable: true + required: + - id + - object + - usage_bytes + - created_at + - vector_store_id + - status + - last_error + x-oaiMeta: + name: The vector store file object + beta: true + example: | + { + "id": "file-abc123", + "object": "vector_store.file", + "usage_bytes": 1234, + "created_at": 1698107661, + "vector_store_id": "vs_abc123", + "status": "completed", + "last_error": null, + "chunking_strategy": { + "type": "static", + "static": { + "max_chunk_size_tokens": 800, + "chunk_overlap_tokens": 400 + } + } + } + + OtherChunkingStrategyResponseParam: + type: object + title: Other Chunking Strategy + description: 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. + additionalProperties: false + properties: + type: + type: string + description: Always `other`. + enum: ["other"] + required: + - type + + StaticChunkingStrategyResponseParam: + type: object + title: Static Chunking Strategy + additionalProperties: false + properties: + type: + type: string + description: Always `static`. + enum: ["static"] + static: + $ref: "#/components/schemas/StaticChunkingStrategy" + required: + - type + - static + + StaticChunkingStrategy: + type: object + additionalProperties: false + properties: + max_chunk_size_tokens: + type: integer + minimum: 100 + maximum: 4096 + description: The maximum number of tokens in each chunk. The default value is `800`. The minimum value is `100` and the maximum value is `4096`. + chunk_overlap_tokens: + type: integer + description: | + The number of tokens that overlap between chunks. The default value is `400`. + + Note that the overlap must not exceed half of `max_chunk_size_tokens`. + required: + - max_chunk_size_tokens + - chunk_overlap_tokens + + AutoChunkingStrategyRequestParam: + type: object + title: Auto Chunking Strategy + description: The default strategy. This strategy currently uses a `max_chunk_size_tokens` of `800` and `chunk_overlap_tokens` of `400`. + additionalProperties: false + properties: + type: + type: string + description: Always `auto`. + enum: ["auto"] + required: + - type -components: - schemas: - ListEnginesResponse: - type: object - properties: - object: - type: string - data: - type: array - items: - $ref: '#/components/schemas/Engine' - required: - - object - - data - - ListModelsResponse: - type: object - properties: - object: - type: string - data: - type: array - items: - $ref: '#/components/schemas/Model' - required: - - object - - data - - DeleteModelResponse: - type: object - properties: - id: - type: string - object: - type: string - deleted: - type: boolean - required: - - id - - object - - deleted - - CreateCompletionRequest: - type: object - properties: - model: &model_configuration - description: ID of the model to use. You can use the [List models](/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](/docs/models/overview) for descriptions of them. - type: string - prompt: - description: &completions_prompt_description | - The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays. - - Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document. - default: '<|endoftext|>' - nullable: true - oneOf: - - type: string - default: '' - example: "This is a test." - - type: array - items: - type: string - default: '' - example: "This is a test." - - type: array - minItems: 1 - items: - type: integer - example: "[1212, 318, 257, 1332, 13]" - - type: array - minItems: 1 - items: - type: array - minItems: 1 - items: - type: integer - example: "[[1212, 318, 257, 1332, 13]]" - suffix: - description: - The suffix that comes after a completion of inserted text. - default: null - nullable: true - type: string - example: "test." - max_tokens: - type: integer - minimum: 0 - default: 16 - example: 16 - nullable: true - description: &completions_max_tokens_description | - The maximum number of [tokens](/tokenizer) to generate in the completion. - - The token count of your prompt plus `max_tokens` cannot exceed the model's context length. Most models have a context length of 2048 tokens (except for the newest models, which support 4096). - temperature: - type: number - minimum: 0 - maximum: 2 - default: 1 - example: 1 - nullable: true - description: &completions_temperature_description | - What [sampling temperature](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277) to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. - - We generally recommend altering this or `top_p` but not both. - top_p: - type: number - minimum: 0 - maximum: 1 - default: 1 - example: 1 - nullable: true - description: &completions_top_p_description | - An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. - - We generally recommend altering this or `temperature` but not both. - n: - type: integer - minimum: 1 - maximum: 128 - default: 1 - example: 1 - nullable: true - description: &completions_completions_description | - How many completions to generate for each prompt. - - **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. - stream: - description: > - Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) - as they become available, with the stream terminated by a `data: [DONE]` message. - type: boolean - nullable: true - default: false - logprobs: &completions_logprobs_configuration - type: integer - minimum: 0 - maximum: 5 - default: null - nullable: true - description: &completions_logprobs_description | - Include the log probabilities on the `logprobs` most likely tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response. - - The maximum value for `logprobs` is 5. If you need more than this, please contact us through our [Help center](https://help.openai.com) and describe your use case. - echo: - type: boolean - default: false - nullable: true - description: &completions_echo_description > - Echo back the prompt in addition to the completion - stop: - description: &completions_stop_description > - Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. - default: null - nullable: true - oneOf: - - type: string - default: <|endoftext|> - example: "\n" - nullable: true - - type: array - minItems: 1 - maxItems: 4 - items: - type: string - example: '["\n"]' - presence_penalty: - type: number - default: 0 - minimum: -2 - maximum: 2 - nullable: true - description: &completions_presence_penalty_description | - Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. - - [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details) - frequency_penalty: - type: number - default: 0 - minimum: -2 - maximum: 2 - nullable: true - description: &completions_frequency_penalty_description | - Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. - - [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details) - best_of: - type: integer - default: 1 - minimum: 0 - maximum: 20 - nullable: true - description: &completions_best_of_description | - Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed. - - When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`. - - **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. - logit_bias: &completions_logit_bias - type: object - x-oaiTypeLabel: map - default: null - nullable: true - description: &completions_logit_bias_description | - Modify the likelihood of specified tokens appearing in the completion. - - Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](/tokenizer?view=bpe) (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. - - As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated. - user: &end_user_param_configuration - type: string - example: user-1234 - description: | - A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](/docs/usage-policies/end-user-ids). - required: - - model - - CreateCompletionResponse: - type: object - properties: - id: - type: string - object: - type: string - created: - type: integer - model: - type: string - choices: - type: array - items: - type: object - properties: - text: - type: string - index: - type: integer - logprobs: - type: object - nullable: true - properties: - tokens: + StaticChunkingStrategyRequestParam: + type: object + title: Static Chunking Strategy + additionalProperties: false + properties: + type: + type: string + description: Always `static`. + enum: ["static"] + static: + $ref: "#/components/schemas/StaticChunkingStrategy" + required: + - type + - static + + ChunkingStrategyRequestParam: + type: object + description: The chunking strategy used to chunk the file(s). If not set, will use the `auto` strategy. + oneOf: + - $ref: "#/components/schemas/AutoChunkingStrategyRequestParam" + - $ref: "#/components/schemas/StaticChunkingStrategyRequestParam" + x-oaiExpandable: true + + CreateVectorStoreFileRequest: + type: object + additionalProperties: false + properties: + file_id: + description: A [File](/docs/api-reference/files) ID that the vector store should use. Useful for tools like `file_search` that can access files. + type: string + chunking_strategy: + $ref: "#/components/schemas/ChunkingStrategyRequestParam" + required: + - file_id + + ListVectorStoreFilesResponse: + properties: + object: + type: string + example: "list" + data: type: array items: - type: string - token_logprobs: + $ref: "#/components/schemas/VectorStoreFileObject" + first_id: + type: string + example: "file-abc123" + last_id: + type: string + example: "file-abc456" + has_more: + type: boolean + example: false + required: + - object + - data + - first_id + - last_id + - has_more + + DeleteVectorStoreFileResponse: + type: object + properties: + id: + type: string + deleted: + type: boolean + object: + type: string + enum: [vector_store.file.deleted] + required: + - id + - object + - deleted + + VectorStoreFileBatchObject: + type: object + title: Vector store file batch + description: A batch of files attached to a vector store. + properties: + id: + description: The identifier, which can be referenced in API endpoints. + type: string + object: + description: The object type, which is always `vector_store.file_batch`. + type: string + enum: ["vector_store.files_batch"] + created_at: + description: The Unix timestamp (in seconds) for when the vector store files batch was created. + type: integer + vector_store_id: + description: The ID of the [vector store](/docs/api-reference/vector-stores/object) that the [File](/docs/api-reference/files) is attached to. + type: string + status: + description: The status of the vector store files batch, which can be either `in_progress`, `completed`, `cancelled` or `failed`. + type: string + enum: ["in_progress", "completed", "cancelled", "failed"] + file_counts: + type: object + properties: + in_progress: + description: The number of files that are currently being processed. + type: integer + completed: + description: The number of files that have been processed. + type: integer + failed: + description: The number of files that have failed to process. + type: integer + cancelled: + description: The number of files that where cancelled. + type: integer + total: + description: The total number of files. + type: integer + required: + - in_progress + - completed + - cancelled + - failed + - total + required: + - id + - object + - created_at + - vector_store_id + - status + - file_counts + x-oaiMeta: + name: The vector store files batch object + beta: true + example: | + { + "id": "vsfb_123", + "object": "vector_store.files_batch", + "created_at": 1698107661, + "vector_store_id": "vs_abc123", + "status": "completed", + "file_counts": { + "in_progress": 0, + "completed": 100, + "failed": 0, + "cancelled": 0, + "total": 100 + } + } + + CreateVectorStoreFileBatchRequest: + type: object + additionalProperties: false + properties: + file_ids: + description: A list of [File](/docs/api-reference/files) IDs that the vector store should use. Useful for tools like `file_search` that can access files. type: array + minItems: 1 + maxItems: 500 items: - type: number - top_logprobs: + type: string + chunking_strategy: + $ref: "#/components/schemas/ChunkingStrategyRequestParam" + required: + - file_ids + + AssistantStreamEvent: + description: | + Represents an event emitted when streaming a Run. + + Each event in a server-sent events stream has an `event` and `data` property: + + ``` + event: thread.created + data: {"id": "thread_123", "object": "thread", ...} + ``` + + We emit events whenever a new object is created, transitions to a new state, or is being + streamed in parts (deltas). For example, we emit `thread.run.created` when a new run + is created, `thread.run.completed` when a run completes, and so on. When an Assistant chooses + to create a message during a run, we emit a `thread.message.created event`, a + `thread.message.in_progress` event, many `thread.message.delta` events, and finally a + `thread.message.completed` event. + + We may add additional events over time, so we recommend handling unknown events gracefully + in your code. See the [Assistants API quickstart](/docs/assistants/overview) to learn how to + integrate the Assistants API with streaming. + oneOf: + - $ref: "#/components/schemas/ThreadStreamEvent" + - $ref: "#/components/schemas/RunStreamEvent" + - $ref: "#/components/schemas/RunStepStreamEvent" + - $ref: "#/components/schemas/MessageStreamEvent" + - $ref: "#/components/schemas/ErrorEvent" + - $ref: "#/components/schemas/DoneEvent" + x-oaiMeta: + name: Assistant stream events + beta: true + + ThreadStreamEvent: + oneOf: + - type: object + properties: + event: + type: string + enum: ["thread.created"] + data: + $ref: "#/components/schemas/ThreadObject" + required: + - event + - data + description: Occurs when a new [thread](/docs/api-reference/threads/object) is created. + x-oaiMeta: + dataDescription: "`data` is a [thread](/docs/api-reference/threads/object)" + + RunStreamEvent: + oneOf: + - type: object + properties: + event: + type: string + enum: ["thread.run.created"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a new [run](/docs/api-reference/runs/object) is created. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.queued"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) moves to a `queued` status. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.in_progress"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) moves to an `in_progress` status. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.requires_action"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) moves to a `requires_action` status. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.completed"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) is completed. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: [ "thread.run.incomplete" ] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) ends with status `incomplete`. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.failed"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) fails. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.cancelling"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) moves to a `cancelling` status. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.cancelled"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) is cancelled. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.expired"] + data: + $ref: "#/components/schemas/RunObject" + required: + - event + - data + description: Occurs when a [run](/docs/api-reference/runs/object) expires. + x-oaiMeta: + dataDescription: "`data` is a [run](/docs/api-reference/runs/object)" + + RunStepStreamEvent: + oneOf: + - type: object + properties: + event: + type: string + enum: ["thread.run.step.created"] + data: + $ref: "#/components/schemas/RunStepObject" + required: + - event + - data + description: Occurs when a [run step](/docs/api-reference/run-steps/step-object) is created. + x-oaiMeta: + dataDescription: "`data` is a [run step](/docs/api-reference/run-steps/step-object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.step.in_progress"] + data: + $ref: "#/components/schemas/RunStepObject" + required: + - event + - data + description: Occurs when a [run step](/docs/api-reference/run-steps/step-object) moves to an `in_progress` state. + x-oaiMeta: + dataDescription: "`data` is a [run step](/docs/api-reference/run-steps/step-object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.step.delta"] + data: + $ref: "#/components/schemas/RunStepDeltaObject" + required: + - event + - data + description: Occurs when parts of a [run step](/docs/api-reference/run-steps/step-object) are being streamed. + x-oaiMeta: + dataDescription: "`data` is a [run step delta](/docs/api-reference/assistants-streaming/run-step-delta-object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.step.completed"] + data: + $ref: "#/components/schemas/RunStepObject" + required: + - event + - data + description: Occurs when a [run step](/docs/api-reference/run-steps/step-object) is completed. + x-oaiMeta: + dataDescription: "`data` is a [run step](/docs/api-reference/run-steps/step-object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.step.failed"] + data: + $ref: "#/components/schemas/RunStepObject" + required: + - event + - data + description: Occurs when a [run step](/docs/api-reference/run-steps/step-object) fails. + x-oaiMeta: + dataDescription: "`data` is a [run step](/docs/api-reference/run-steps/step-object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.step.cancelled"] + data: + $ref: "#/components/schemas/RunStepObject" + required: + - event + - data + description: Occurs when a [run step](/docs/api-reference/run-steps/step-object) is cancelled. + x-oaiMeta: + dataDescription: "`data` is a [run step](/docs/api-reference/run-steps/step-object)" + - type: object + properties: + event: + type: string + enum: ["thread.run.step.expired"] + data: + $ref: "#/components/schemas/RunStepObject" + required: + - event + - data + description: Occurs when a [run step](/docs/api-reference/run-steps/step-object) expires. + x-oaiMeta: + dataDescription: "`data` is a [run step](/docs/api-reference/run-steps/step-object)" + + MessageStreamEvent: + oneOf: + - type: object + properties: + event: + type: string + enum: ["thread.message.created"] + data: + $ref: "#/components/schemas/MessageObject" + required: + - event + - data + description: Occurs when a [message](/docs/api-reference/messages/object) is created. + x-oaiMeta: + dataDescription: "`data` is a [message](/docs/api-reference/messages/object)" + - type: object + properties: + event: + type: string + enum: ["thread.message.in_progress"] + data: + $ref: "#/components/schemas/MessageObject" + required: + - event + - data + description: Occurs when a [message](/docs/api-reference/messages/object) moves to an `in_progress` state. + x-oaiMeta: + dataDescription: "`data` is a [message](/docs/api-reference/messages/object)" + - type: object + properties: + event: + type: string + enum: ["thread.message.delta"] + data: + $ref: "#/components/schemas/MessageDeltaObject" + required: + - event + - data + description: Occurs when parts of a [Message](/docs/api-reference/messages/object) are being streamed. + x-oaiMeta: + dataDescription: "`data` is a [message delta](/docs/api-reference/assistants-streaming/message-delta-object)" + - type: object + properties: + event: + type: string + enum: ["thread.message.completed"] + data: + $ref: "#/components/schemas/MessageObject" + required: + - event + - data + description: Occurs when a [message](/docs/api-reference/messages/object) is completed. + x-oaiMeta: + dataDescription: "`data` is a [message](/docs/api-reference/messages/object)" + - type: object + properties: + event: + type: string + enum: ["thread.message.incomplete"] + data: + $ref: "#/components/schemas/MessageObject" + required: + - event + - data + description: Occurs when a [message](/docs/api-reference/messages/object) ends before it is completed. + x-oaiMeta: + dataDescription: "`data` is a [message](/docs/api-reference/messages/object)" + + ErrorEvent: + type: object + properties: + event: + type: string + enum: ["error"] + data: + $ref: "#/components/schemas/Error" + required: + - event + - data + description: Occurs when an [error](/docs/guides/error-codes/api-errors) occurs. This can happen due to an internal server error or a timeout. + x-oaiMeta: + dataDescription: "`data` is an [error](/docs/guides/error-codes/api-errors)" + + DoneEvent: + type: object + properties: + event: + type: string + enum: ["done"] + data: + type: string + enum: ["[DONE]"] + required: + - event + - data + description: Occurs when a stream ends. + x-oaiMeta: + dataDescription: "`data` is `[DONE]`" + + Batch: + type: object + properties: + id: + type: string + object: + type: string + enum: [batch] + description: The object type, which is always `batch`. + endpoint: + type: string + description: The OpenAI API endpoint used by the batch. + + errors: + type: object + properties: + object: + type: string + description: The object type, which is always `list`. + data: + type: array + items: + type: object + properties: + code: + type: string + description: An error code identifying the error type. + message: + type: string + description: A human-readable message providing more details about the error. + param: + type: string + description: The name of the parameter that caused the error, if applicable. + nullable: true + line: + type: integer + description: The line number of the input file where the error occurred, if applicable. + nullable: true + input_file_id: + type: string + description: The ID of the input file for the batch. + completion_window: + type: string + description: The time frame within which the batch should be processed. + status: + type: string + description: The current status of the batch. + enum: + - validating + - failed + - in_progress + - finalizing + - completed + - expired + - cancelling + - cancelled + output_file_id: + type: string + description: The ID of the file containing the outputs of successfully executed requests. + error_file_id: + type: string + description: The ID of the file containing the outputs of requests with errors. + created_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch was created. + in_progress_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch started processing. + expires_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch will expire. + finalizing_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch started finalizing. + completed_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch was completed. + failed_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch failed. + expired_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch expired. + cancelling_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch started cancelling. + cancelled_at: + type: integer + description: The Unix timestamp (in seconds) for when the batch was cancelled. + request_counts: + type: object + properties: + total: + type: integer + description: Total number of requests in the batch. + completed: + type: integer + description: Number of requests that have been completed successfully. + failed: + type: integer + description: Number of requests that have failed. + required: + - total + - completed + - failed + description: The request counts for different statuses within the batch. + metadata: + description: *metadata_description + type: object + x-oaiTypeLabel: map + nullable: true + required: + - id + - object + - endpoint + - input_file_id + - completion_window + - status + - created_at + x-oaiMeta: + name: The batch object + example: *batch_object + + BatchRequestInput: + type: object + description: The per-line object of the batch input file + properties: + custom_id: + type: string + description: A developer-provided per-request id that will be used to match outputs to inputs. Must be unique for each request in a batch. + method: + type: string + enum: ["POST"] + description: The HTTP method to be used for the request. Currently only `POST` is supported. + url: + type: string + description: The OpenAI API relative URL to be used for the request. Currently `/v1/chat/completions`, `/v1/embeddings`, and `/v1/completions` are supported. + x-oaiMeta: + name: The request input object + example: | + {"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-4o-mini", "messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is 2+2?"}]}} + + BatchRequestOutput: + type: object + description: The per-line object of the batch output and error files + properties: + id: + type: string + custom_id: + type: string + description: A developer-provided per-request id that will be used to match outputs to inputs. + response: + type: object + nullable: true + properties: + status_code: + type: integer + description: The HTTP status code of the response + request_id: + type: string + description: An unique identifier for the OpenAI API request. Please include this request ID when contacting support. + body: + type: object + x-oaiTypeLabel: map + description: The JSON body of the response + error: + type: object + nullable: true + description: For requests that failed with a non-HTTP error, this will contain more information on the cause of the failure. + properties: + code: + type: string + description: A machine-readable error code. + message: + type: string + description: A human-readable error message. + x-oaiMeta: + name: The request output object + example: | + {"id": "batch_req_wnaDys", "custom_id": "request-2", "response": {"status_code": 200, "request_id": "req_c187b3", "body": {"id": "chatcmpl-9758Iw", "object": "chat.completion", "created": 1711475054, "model": "gpt-4o-mini", "choices": [{"index": 0, "message": {"role": "assistant", "content": "2 + 2 equals 4."}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 24, "completion_tokens": 15, "total_tokens": 39}, "system_fingerprint": null}}, "error": null} + + ListBatchesResponse: + type: object + properties: + data: type: array items: - type: object - text_offset: + $ref: "#/components/schemas/Batch" + first_id: + type: string + example: "batch_abc123" + last_id: + type: string + example: "batch_abc456" + has_more: + type: boolean + object: + type: string + enum: [list] + required: + - object + - data + - has_more + + AuditLogActorServiceAccount: + type: object + description: The service account that performed the audit logged action. + properties: + id: + type: string + description: The service account id. + + AuditLogActorUser: + type: object + description: The user who performed the audit logged action. + properties: + id: + type: string + description: The user id. + email: + type: string + description: The user email. + + AuditLogActorApiKey: + type: object + description: The API Key used to perform the audit logged action. + properties: + id: + type: string + description: The tracking id of the API key. + type: + type: string + description: The type of API key. Can be either `user` or `service_account`. + enum: ["user", "service_account"] + user: + $ref: "#/components/schemas/AuditLogActorUser" + service_account: + $ref: "#/components/schemas/AuditLogActorServiceAccount" + + AuditLogActorSession: + type: object + description: The session in which the audit logged action was performed. + properties: + user: + $ref: "#/components/schemas/AuditLogActorUser" + ip_address: + type: string + description: The IP address from which the action was performed. + + AuditLogActor: + type: object + description: The actor who performed the audit logged action. + properties: + type: + type: string + description: The type of actor. Is either `session` or `api_key`. + enum: ["session", "api_key"] + session: + type: object + $ref: "#/components/schemas/AuditLogActorSession" + api_key: + type: object + $ref: "#/components/schemas/AuditLogActorApiKey" + + + AuditLogEventType: + type: string + description: The event type. + x-oaiExpandable: true + enum: + - api_key.created + - api_key.updated + - api_key.deleted + - invite.sent + - invite.accepted + - invite.deleted + - login.succeeded + - login.failed + - logout.succeeded + - logout.failed + - organization.updated + - project.created + - project.updated + - project.archived + - service_account.created + - service_account.updated + - service_account.deleted + - user.added + - user.updated + - user.deleted + + AuditLog: + type: object + description: A log of a user action or configuration change within this organization. + properties: + id: + type: string + description: The ID of this log. + type: + $ref: "#/components/schemas/AuditLogEventType" + + effective_at: + type: integer + description: The Unix timestamp (in seconds) of the event. + project: + type: object + description: The project that the action was scoped to. Absent for actions not scoped to projects. + properties: + id: + type: string + description: The project ID. + name: + type: string + description: The project title. + actor: + $ref: "#/components/schemas/AuditLogActor" + api_key.created: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The tracking ID of the API key. + data: + type: object + description: The payload used to create the API key. + properties: + scopes: + type: array + items: + type: string + description: A list of scopes allowed for the API key, e.g. `["api.model.request"]` + api_key.updated: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The tracking ID of the API key. + changes_requested: + type: object + description: The payload used to update the API key. + properties: + scopes: + type: array + items: + type: string + description: A list of scopes allowed for the API key, e.g. `["api.model.request"]` + api_key.deleted: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The tracking ID of the API key. + invite.sent: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The ID of the invite. + data: + type: object + description: The payload used to create the invite. + properties: + email: + type: string + description: The email invited to the organization. + role: + type: string + description: The role the email was invited to be. Is either `owner` or `member`. + invite.accepted: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The ID of the invite. + invite.deleted: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The ID of the invite. + login.failed: + type: object + description: The details for events with this `type`. + properties: + error_code: + type: string + description: The error code of the failure. + error_message: + type: string + description: The error message of the failure. + logout.failed: + type: object + description: The details for events with this `type`. + properties: + error_code: + type: string + description: The error code of the failure. + error_message: + type: string + description: The error message of the failure. + organization.updated: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The organization ID. + changes_requested: + type: object + description: The payload used to update the organization settings. + properties: + title: + type: string + description: The organization title. + description: + type: string + description: The organization description. + name: + type: string + description: The organization name. + settings: + type: object + properties: + threads_ui_visibility: + type: string + description: Visibility of the threads page which shows messages created with the Assistants API and Playground. One of `ANY_ROLE`, `OWNERS`, or `NONE`. + usage_dashboard_visibility: + type: string + description: Visibility of the usage dashboard which shows activity and costs for your organization. One of `ANY_ROLE` or `OWNERS`. + project.created: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The project ID. + data: + type: object + description: The payload used to create the project. + properties: + name: + type: string + description: The project name. + title: + type: string + description: The title of the project as seen on the dashboard. + project.updated: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The project ID. + changes_requested: + type: object + description: The payload used to update the project. + properties: + title: + type: string + description: The title of the project as seen on the dashboard. + project.archived: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The project ID. + service_account.created: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The service account ID. + data: + type: object + description: The payload used to create the service account. + properties: + role: + type: string + description: The role of the service account. Is either `owner` or `member`. + service_account.updated: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The service account ID. + changes_requested: + type: object + description: The payload used to updated the service account. + properties: + role: + type: string + description: The role of the service account. Is either `owner` or `member`. + service_account.deleted: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The service account ID. + user.added: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The user ID. + data: + type: object + description: The payload used to add the user to the project. + properties: + role: + type: string + description: The role of the user. Is either `owner` or `member`. + user.updated: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The project ID. + changes_requested: + type: object + description: The payload used to update the user. + properties: + role: + type: string + description: The role of the user. Is either `owner` or `member`. + user.deleted: + type: object + description: The details for events with this `type`. + properties: + id: + type: string + description: The user ID. + required: + - id + - type + - effective_at + - actor + x-oaiMeta: + name: The audit log object + example: | + { + "id": "req_xxx_20240101", + "type": "api_key.created", + "effective_at": 1720804090, + "actor": { + "type": "session", + "session": { + "user": { + "id": "user-xxx", + "email": "user@example.com" + }, + "ip_address": "127.0.0.1", + "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" + } + }, + "api_key.created": { + "id": "key_xxxx", + "data": { + "scopes": ["resource.operation"] + } + } + } + + ListAuditLogsResponse: + type: object + properties: + object: + type: string + enum: [list] + data: type: array items: - type: integer - finish_reason: - type: string - usage: - type: object - properties: - prompt_tokens: - type: integer - completion_tokens: - type: integer - total_tokens: - type: integer - required: - - prompt_tokens - - completion_tokens - - total_tokens - required: - - id - - object - - created - - model - - choices - - CreateEditRequest: - type: object - properties: - model: *model_configuration - input: - description: - The input text to use as a starting point for the edit. - type: string - default: '' - nullable: true - example: "What day of the wek is it?" - instruction: - description: - The instruction that tells the model how to edit the prompt. - type: string - example: "Fix the spelling mistakes." - n: - type: integer - minimum: 1 - maximum: 20 - default: 1 - example: 1 - nullable: true - description: - How many edits to generate for the input and instruction. - temperature: - type: number - minimum: 0 - maximum: 2 - default: 1 - example: 1 - nullable: true - description: *completions_temperature_description - top_p: - type: number - minimum: 0 - maximum: 1 - default: 1 - example: 1 - nullable: true - description: *completions_top_p_description - required: - - model - - instruction - - CreateEditResponse: - type: object - properties: - id: - type: string - object: - type: string - created: - type: integer - model: - type: string - choices: - type: array - items: - type: object - properties: - text: - type: string - index: - type: integer - logprobs: - type: object - nullable: true - properties: - tokens: + $ref: "#/components/schemas/AuditLog" + first_id: + type: string + example: "audit_log-defb456h8dks" + last_id: + type: string + example: "audit_log-hnbkd8s93s" + has_more: + type: boolean + + required: + - object + - data + - first_id + - last_id + - has_more + + Invite: + type: object + description: Represents an individual `invite` to the organization. + properties: + object: + type: string + enum: [organization.invite] + description: The object type, which is always `organization.invite` + id: + type: string + description: The identifier, which can be referenced in API endpoints + email: + type: string + description: The email address of the individual to whom the invite was sent + role: + type: string + enum: [owner, reader] + description: "`owner` or `reader`" + status: + type: string + enum: [accepted, expired, pending] + description: "`accepted`,`expired`, or `pending`" + invited_at: + type: integer + description: The Unix timestamp (in seconds) of when the invite was sent. + expires_at: + type: integer + description: The Unix timestamp (in seconds) of when the invite expires. + accepted_at: + type: integer + description: The Unix timestamp (in seconds) of when the invite was accepted. + + required: + - object + - id + - email + - role + - status + - invited_at + - expires_at + x-oaiMeta: + name: The invite object + example: | + { + "object": "organization.invite", + "id": "invite-abc", + "email": "user@example.com", + "role": "owner", + "status": "accepted", + "invited_at": 1711471533, + "expires_at": 1711471533, + "accepted_at": 1711471533 + } + + InviteListResponse: + type: object + properties: + object: + type: string + enum: [list] + description: The object type, which is always `list` + data: type: array items: - type: string - token_logprobs: + $ref: '#/components/schemas/Invite' + first_id: + type: string + description: The first `invite_id` in the retrieved `list` + last_id: + type: string + description: The last `invite_id` in the retrieved `list` + has_more: + type: boolean + description: The `has_more` property is used for pagination to indicate there are additional results. + required: + - object + - data + + InviteRequest: + type: object + properties: + email: + type: string + description: "Send an email to this address" + role: + type: string + enum: [reader, owner] + description: "`owner` or `reader`" + required: + - email + - role + + InviteDeleteResponse: + type: object + properties: + object: + type: string + enum: [organization.invite.deleted] + description: The object type, which is always `organization.invite.deleted` + id: + type: string + deleted: + type: boolean + required: + - object + - id + - deleted + + User: + type: object + description: Represents an individual `user` within an organization. + properties: + object: + type: string + enum: [organization.user] + description: The object type, which is always `organization.user` + id: + type: string + description: The identifier, which can be referenced in API endpoints + name: + type: string + description: The name of the user + email: + type: string + description: The email address of the user + role: + type: string + enum: [owner, reader] + description: "`owner` or `reader`" + added_at: + type: integer + description: The Unix timestamp (in seconds) of when the user was added. + required: + - object + - id + - name + - email + - role + - added_at + x-oaiMeta: + name: The user object + example: | + { + "object": "organization.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + + UserListResponse: + type: object + properties: + object: + type: string + enum: [list] + data: type: array items: - type: number - top_logprobs: + $ref: '#/components/schemas/User' + first_id: + type: string + last_id: + type: string + has_more: + type: boolean + required: + - object + - data + - first_id + - last_id + - has_more + + UserRoleUpdateRequest: + type: object + properties: + role: + type: string + enum: [owner,reader] + description: "`owner` or `reader`" + required: + - role + + UserDeleteResponse: + type: object + properties: + object: + type: string + enum: [organization.user.deleted] + id: + type: string + deleted: + type: boolean + required: + - object + - id + - deleted + + Project: + type: object + description: Represents an individual project. + properties: + id: + type: string + description: The identifier, which can be referenced in API endpoints + object: + type: string + enum: [organization.project] + description: The object type, which is always `organization.project` + name: + type: string + description: The name of the project. This appears in reporting. + created_at: + type: integer + description: The Unix timestamp (in seconds) of when the project was created. + archived_at: + type: integer + nullable: true + description: The Unix timestamp (in seconds) of when the project was archived or `null`. + status: + type: string + enum: [active, archived] + description: "`active` or `archived`" + required: + - id + - object + - name + - created_at + - status + x-oaiMeta: + name: The project object + example: | + { + "id": "proj_abc", + "object": "organization.project", + "name": "Project example", + "created_at": 1711471533, + "archived_at": null, + "status": "active" + } + + ProjectListResponse: + type: object + properties: + object: + type: string + enum: [list] + data: type: array items: - type: object - text_offset: + $ref: '#/components/schemas/Project' + first_id: + type: string + last_id: + type: string + has_more: + type: boolean + required: + - object + - data + - first_id + - last_id + - has_more + + ProjectCreateRequest: + type: object + properties: + name: + type: string + description: The friendly name of the project, this name appears in reports. + required: + - name + + ProjectUpdateRequest: + type: object + properties: + name: + type: string + description: The updated name of the project, this name appears in reports. + required: + - name + + DefaultProjectErrorResponse: + type: object + properties: + code: + type: integer + message: + type: string + required: + - code + - message + + ProjectUser: + type: object + description: Represents an individual user in a project. + properties: + object: + type: string + enum: [organization.project.user] + description: The object type, which is always `organization.project.user` + id: + type: string + description: The identifier, which can be referenced in API endpoints + name: + type: string + description: The name of the user + email: + type: string + description: The email address of the user + role: + type: string + enum: [owner, member] + description: "`owner` or `member`" + added_at: + type: integer + description: The Unix timestamp (in seconds) of when the project was added. + + required: + - object + - id + - name + - email + - role + - added_at + x-oaiMeta: + name: The project user object + example: | + { + "object": "organization.project.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "added_at": 1711471533 + } + + ProjectUserListResponse: + type: object + properties: + object: + type: string + data: type: array items: - type: integer - finish_reason: - type: string - usage: - type: object - properties: - prompt_tokens: - type: integer - completion_tokens: - type: integer - total_tokens: - type: integer - required: - - prompt_tokens - - completion_tokens - - total_tokens - required: - - id - - object - - created - - model - - choices - - usage - - CreateImageRequest: - type: object - properties: - prompt: - description: A text description of the desired image(s). The maximum length is 1000 characters. - type: string - example: "A cute baby sea otter" - n: &images_n - type: integer - minimum: 1 - maximum: 10 - default: 1 - example: 1 - nullable: true - description: The number of images to generate. Must be between 1 and 10. - size: &images_size - type: string - enum: ["256x256", "512x512", "1024x1024"] - default: "1024x1024" - example: "1024x1024" - nullable: true - description: The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`. - response_format: &images_response_format - type: string - enum: ["url", "b64_json"] - default: "url" - example: "url" - nullable: true - description: The format in which the generated images are returned. Must be one of `url` or `b64_json`. - user: *end_user_param_configuration - required: - - prompt - - ImagesResponse: - properties: - created: - type: integer - data: - type: array - items: - type: object - properties: - url: - type: string - b64_json: - type: string - required: - - created - - data - - CreateImageEditRequest: - type: object - properties: - image: - description: The image to edit. Must be a valid PNG file, less than 4MB, and square. - type: string - format: binary - mask: - description: An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`. - type: string - format: binary - prompt: - description: A text description of the desired image(s). The maximum length is 1000 characters. - type: string - example: "A cute baby sea otter wearing a beret" - n: *images_n - size: *images_size - response_format: *images_response_format - user: *end_user_param_configuration - required: - - prompt - - image - - mask - - CreateImageVariationRequest: - type: object - properties: - image: - description: The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square. - type: string - format: binary - n: *images_n - size: *images_size - response_format: *images_response_format - user: *end_user_param_configuration - required: - - image - - CreateModerationRequest: - type: object - properties: - input: - description: The input text to classify - oneOf: - - type: string - default: '' - example: "I want to kill them." - - type: array - items: - type: string - default: '' - example: "I want to kill them." - model: - description: | - Two content moderations models are available: `text-moderation-stable` and `text-moderation-latest`. - - The default is `text-moderation-latest` which will be automatically upgraded over time. This ensures you are always using our most accurate model. If you use `text-moderation-stable`, we will provide advanced notice before updating the model. Accuracy of `text-moderation-stable` may be slightly lower than for `text-moderation-latest`. - type: string - nullable: false - default: "text-moderation-latest" - example: "text-moderation-stable" - required: - - input - - CreateModerationResponse: - type: object - properties: - id: - type: string - model: - type: string - results: - type: array - items: - type: object - properties: - flagged: - type: boolean - categories: - type: object - properties: - hate: - type: boolean - hate/threatening: + $ref: '#/components/schemas/ProjectUser' + first_id: + type: string + last_id: + type: string + has_more: type: boolean - self-harm: + required: + - object + - data + - first_id + - last_id + - has_more + + ProjectUserCreateRequest: + type: object + properties: + user_id: + type: string + description: The ID of the user. + role: + type: string + enum: [owner, member] + description: "`owner` or `member`" + required: + - user_id + - role + + ProjectUserUpdateRequest: + type: object + properties: + role: + type: string + enum: [owner, member] + description: "`owner` or `member`" + required: + - role + + ProjectUserDeleteResponse: + type: object + properties: + object: + type: string + enum: [organization.project.user.deleted] + id: + type: string + deleted: type: boolean - sexual: + required: + - object + - id + - deleted + + ProjectServiceAccount: + type: object + description: Represents an individual service account in a project. + properties: + object: + type: string + enum: [organization.project.service_account] + description: The object type, which is always `organization.project.service_account` + id: + type: string + description: The identifier, which can be referenced in API endpoints + name: + type: string + description: The name of the service account + role: + type: string + enum: [owner, member] + description: "`owner` or `member`" + created_at: + type: integer + description: The Unix timestamp (in seconds) of when the service account was created + required: + - object + - id + - name + - role + - created_at + x-oaiMeta: + name: The project service account object + example: | + { + "object": "organization.project.service_account", + "id": "svc_acct_abc", + "name": "Service Account", + "role": "owner", + "created_at": 1711471533 + } + + ProjectServiceAccountListResponse: + type: object + properties: + object: + type: string + enum: [list] + data: + type: array + items: + $ref: '#/components/schemas/ProjectServiceAccount' + first_id: + type: string + last_id: + type: string + has_more: type: boolean - sexual/minors: + required: + - object + - data + - first_id + - last_id + - has_more + + ProjectServiceAccountCreateRequest: + type: object + properties: + name: + type: string + description: The name of the service account being created. + required: + - name + + ProjectServiceAccountCreateResponse: + type: object + properties: + object: + type: string + enum: [organization.project.service_account] + id: + type: string + name: + type: string + role: + type: string + enum: [member] + description: Service accounts can only have one role of type `member` + created_at: + type: integer + api_key: + $ref: '#/components/schemas/ProjectServiceAccountApiKey' + required: + - object + - id + - name + - role + - created_at + - api_key + + ProjectServiceAccountApiKey: + type: object + properties: + object: + type: string + enum: [organization.project.service_account.api_key] + description: The object type, which is always `organization.project.service_account.api_key` + + value: + type: string + name: + type: string + created_at: + type: integer + id: + type: string + required: + - object + - value + - name + - created_at + - id + + ProjectServiceAccountDeleteResponse: + type: object + properties: + object: + type: string + enum: [organization.project.service_account.deleted] + id: + type: string + deleted: type: boolean - violence: + required: + - object + - id + - deleted + + ProjectApiKey: + type: object + description: Represents an individual API key in a project. + properties: + object: + type: string + enum: [organization.project.api_key] + description: The object type, which is always `organization.project.api_key` + redacted_value: + type: string + description: The redacted value of the API key + name: + type: string + description: The name of the API key + created_at: + type: integer + description: The Unix timestamp (in seconds) of when the API key was created + id: + type: string + description: The identifier, which can be referenced in API endpoints + owner: + type: object + properties: + type: + type: string + enum: [user, service_account] + description: "`user` or `service_account`" + user: + $ref: '#/components/schemas/ProjectUser' + service_account: + $ref: '#/components/schemas/ProjectServiceAccount' + required: + - object + - redacted_value + - name + - created_at + - id + - owner + x-oaiMeta: + name: The project API key object + example: | + { + "object": "organization.project.api_key", + "redacted_value": "sk-abc...def", + "name": "My API Key", + "created_at": 1711471533, + "id": "key_abc", + "owner": { + "type": "user", + "user": { + "object": "organization.project.user", + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "created_at": 1711471533 + } + } + } + + ProjectApiKeyListResponse: + type: object + properties: + object: + type: string + enum: [list] + data: + type: array + items: + $ref: '#/components/schemas/ProjectApiKey' + first_id: + type: string + last_id: + type: string + has_more: type: boolean - violence/graphic: + required: + - object + - data + - first_id + - last_id + - has_more + + ProjectApiKeyDeleteResponse: + type: object + properties: + object: + type: string + enum: [organization.project.api_key.deleted] + id: + type: string + deleted: type: boolean - required: - - hate - - hate/threatening - - self-harm - - sexual - - sexual/minors - - violence - - violence/graphic - category_scores: - type: object - properties: - hate: - type: number - hate/threatening: - type: number - self-harm: - type: number - sexual: - type: number - sexual/minors: - type: number - violence: - type: number - violence/graphic: - type: number - required: - - hate - - hate/threatening - - self-harm - - sexual - - sexual/minors - - violence - - violence/graphic - required: - - flagged - - categories - - category_scores - required: - - id - - model - - results - - CreateSearchRequest: - type: object - properties: - query: - description: Query to search against the documents. - type: string - example: "the president" - minLength: 1 - documents: - description: | - Up to 200 documents to search over, provided as a list of strings. + required: + - object + - id + - deleted - The maximum document length (in tokens) is 2034 minus the number of tokens in the query. +security: + - ApiKeyAuth: [] - You should specify either `documents` or a `file`, but not both. - type: array - minItems: 1 - maxItems: 200 - items: - type: string - nullable: true - example: "['White House', 'hospital', 'school']" - file: +x-oaiMeta: + navigationGroups: + - id: endpoints + title: Endpoints + - id: assistants + title: Assistants + - id: administration + title: Administration + - id: legacy + title: Legacy + groups: + # > General Notes + # The `groups` section is used to generate the API reference pages and navigation, in the same + # order listed below. Additionally, each `group` can have a list of `sections`, each of which + # will become a navigation subroute and subsection under the group. Each section has: + # - `type`: Currently, either an `endpoint` or `object`, depending on how the section needs to + # be rendered + # - `key`: The reference key that can be used to lookup the section definition + # - `path`: The path (url) of the section, which is used to generate the navigation link. + # + # > The `object` sections maps to a schema component and the following fields are read for rendering + # - `x-oaiMeta.name`: The name of the object, which will become the section title + # - `x-oaiMeta.example`: The example object, which will be used to generate the example sample (always JSON) + # - `description`: The description of the object, which will be used to generate the section description + # + # > The `endpoint` section maps to an operation path and the following fields are read for rendering: + # - `x-oaiMeta.name`: The name of the endpoint, which will become the section title + # - `x-oaiMeta.examples`: The endpoint examples, which can be an object (meaning a single variation, most + # endpoints, or an array of objects, meaning multiple variations, e.g. the + # chat completion and completion endpoints, with streamed and non-streamed examples. + # - `x-oaiMeta.returns`: text describing what the endpoint returns. + # - `summary`: The summary of the endpoint, which will be used to generate the section description + - id: audio + title: Audio description: | - The ID of an uploaded file that contains documents to search over. + Learn how to turn audio into text or text into audio. - You should specify either `documents` or a `file`, but not both. - type: string - nullable: true - max_rerank: - description: | - The maximum number of documents to be re-ranked and returned by search. - - This flag only takes effect when `file` is set. - type: integer - minimum: 1 - default: 200 - nullable: true - return_metadata: &return_metadata_configuration + Related guide: [Speech to text](/docs/guides/speech-to-text) + navigationGroup: endpoints + sections: + - type: endpoint + key: createSpeech + path: createSpeech + - type: endpoint + key: createTranscription + path: createTranscription + - type: endpoint + key: createTranslation + path: createTranslation + - type: object + key: CreateTranscriptionResponseJson + path: json-object + - type: object + key: CreateTranscriptionResponseVerboseJson + path: verbose-json-object + - id: chat + title: Chat description: | - A special boolean flag for showing metadata. If set to `true`, each document entry in the returned JSON will contain a "metadata" field. - - This flag only takes effect when `file` is set. - type: boolean - default: false - nullable: true - user: *end_user_param_configuration - required: - - query - - CreateSearchResponse: - type: object - properties: - object: - type: string - model: - type: string - data: - type: array - items: - type: object - properties: - object: - type: string - document: - type: integer - score: - type: number - - ListFilesResponse: - type: object - properties: - object: - type: string - data: - type: array - items: - $ref: '#/components/schemas/OpenAIFile' - required: - - object - - data - - CreateFileRequest: - type: object - additionalProperties: false - properties: - file: + Given a list of messages comprising a conversation, the model will return a response. + + Related guide: [Chat Completions](/docs/guides/text-generation) + navigationGroup: endpoints + sections: + - type: endpoint + key: createChatCompletion + path: create + - type: object + key: CreateChatCompletionResponse + path: object + - type: object + key: CreateChatCompletionStreamResponse + path: streaming + - id: embeddings + title: Embeddings description: | - Name of the [JSON Lines](https://jsonlines.readthedocs.io/en/latest/) file to be uploaded. + Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. - If the `purpose` is set to "fine-tune", each line is a JSON record with "prompt" and "completion" fields representing your [training examples](/docs/guides/fine-tuning/prepare-training-data). - type: string - format: binary - purpose: + Related guide: [Embeddings](/docs/guides/embeddings) + navigationGroup: endpoints + sections: + - type: endpoint + key: createEmbedding + path: create + - type: object + key: Embedding + path: object + - id: fine-tuning + title: Fine-tuning description: | - The intended purpose of the uploaded documents. - - Use "fine-tune" for [Fine-tuning](/docs/api-reference/fine-tunes). This allows us to validate the format of the uploaded file. - - type: string - required: - - file - - purpose - - DeleteFileResponse: - type: object - properties: - id: - type: string - object: - type: string - deleted: - type: boolean - required: - - id - - object - - deleted - - CreateAnswerRequest: - type: object - additionalProperties: false - properties: - model: - description: ID of the model to use for completion. You can select one of `ada`, `babbage`, `curie`, or `davinci`. - type: string - question: - description: Question to get answered. - type: string - minLength: 1 - example: "What is the capital of Japan?" - examples: - description: List of (question, answer) pairs that will help steer the model towards the tone and answer format you'd like. We recommend adding 2 to 3 examples. - type: array - minItems: 1 - maxItems: 200 - items: - type: array - minItems: 2 - maxItems: 2 - items: - type: string - minLength: 1 - example: "[['What is the capital of Canada?', 'Ottawa'], ['Which province is Ottawa in?', 'Ontario']]" - examples_context: - description: A text snippet containing the contextual information used to generate the answers for the `examples` you provide. - type: string - example: "Ottawa, Canada's capital, is located in the east of southern Ontario, near the city of Montréal and the U.S. border." - documents: + Manage fine-tuning jobs to tailor a model to your specific training data. + + Related guide: [Fine-tune models](/docs/guides/fine-tuning) + navigationGroup: endpoints + sections: + - type: endpoint + key: createFineTuningJob + path: create + - type: endpoint + key: listPaginatedFineTuningJobs + path: list + - type: endpoint + key: listFineTuningEvents + path: list-events + - type: endpoint + key: listFineTuningJobCheckpoints + path: list-checkpoints + - type: endpoint + key: retrieveFineTuningJob + path: retrieve + - type: endpoint + key: cancelFineTuningJob + path: cancel + - type: object + key: FinetuneChatRequestInput + path: chat-input + - type: object + key: FinetuneCompletionRequestInput + path: completions-input + - type: object + key: FineTuningJob + path: object + - type: object + key: FineTuningJobEvent + path: event-object + - type: object + key: FineTuningJobCheckpoint + path: checkpoint-object + - id: batch + title: Batch description: | - List of documents from which the answer for the input `question` should be derived. If this is an empty list, the question will be answered based on the question-answer examples. + Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount. - You should specify either `documents` or a `file`, but not both. - type: array - maxItems: 200 - items: - type: string - example: "['Japan is an island country in East Asia, located in the northwest Pacific Ocean.', 'Tokyo is the capital and most populous prefecture of Japan.']" - nullable: true - file: + Related guide: [Batch](/docs/guides/batch) + navigationGroup: endpoints + sections: + - type: endpoint + key: createBatch + path: create + - type: endpoint + key: retrieveBatch + path: retrieve + - type: endpoint + key: cancelBatch + path: cancel + - type: endpoint + key: listBatches + path: list + - type: object + key: Batch + path: object + - type: object + key: BatchRequestInput + path: request-input + - type: object + key: BatchRequestOutput + path: request-output + - id: files + title: Files description: | - The ID of an uploaded file that contains documents to search over. See [upload file](/docs/api-reference/files/upload) for how to upload a file of the desired format and purpose. - - You should specify either `documents` or a `file`, but not both. - type: string - nullable: true - search_model: &search_model_configuration - description: ID of the model to use for [Search](/docs/api-reference/searches/create). You can select one of `ada`, `babbage`, `curie`, or `davinci`. - type: string - default: ada - nullable: true - max_rerank: - description: The maximum number of documents to be ranked by [Search](/docs/api-reference/searches/create) when using `file`. Setting it to a higher value leads to improved accuracy but with increased latency and cost. - type: integer - default: 200 - nullable: true - temperature: - description: What [sampling temperature](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277) to use. Higher values mean the model will take more risks and value 0 (argmax sampling) works better for scenarios with a well-defined answer. - type: number - default: 0 - nullable: true - logprobs: &context_completions_logprobs_configuration - type: integer - minimum: 0 - maximum: 5 - default: null - nullable: true + Files are used to upload documents that can be used with features like [Assistants](/docs/api-reference/assistants), [Fine-tuning](/docs/api-reference/fine-tuning), and [Batch API](/docs/guides/batch). + navigationGroup: endpoints + sections: + - type: endpoint + key: createFile + path: create + - type: endpoint + key: listFiles + path: list + - type: endpoint + key: retrieveFile + path: retrieve + - type: endpoint + key: deleteFile + path: delete + - type: endpoint + key: downloadFile + path: retrieve-contents + - type: object + key: OpenAIFile + path: object + - id: uploads + title: Uploads description: | - Include the log probabilities on the `logprobs` most likely tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response. - - The maximum value for `logprobs` is 5. If you need more than this, please contact us through our [Help center](https://help.openai.com) and describe your use case. - - When `logprobs` is set, `completion` will be automatically added into `expand` to get the logprobs. - max_tokens: - description: The maximum number of tokens allowed for the generated answer - type: integer - default: 16 - nullable: true - stop: - description: *completions_stop_description - default: null - oneOf: - - type: string - default: <|endoftext|> - example: "\n" - - type: array - minItems: 1 - maxItems: 4 - items: - type: string - example: '["\n"]' - nullable: true - n: - description: How many answers to generate for each question. - type: integer - minimum: 1 - maximum: 10 - default: 1 - nullable: true - logit_bias: *completions_logit_bias - return_metadata: *return_metadata_configuration - return_prompt: &return_prompt_configuration - description: If set to `true`, the returned JSON will include a "prompt" field containing the final prompt that was used to request a completion. This is mainly useful for debugging purposes. - type: boolean - default: false - nullable: true - expand: &expand_configuration - description: If an object name is in the list, we provide the full information of the object; otherwise, we only provide the object ID. Currently we support `completion` and `file` objects for expansion. - type: array - items: {} - nullable: true - default: [] - user: *end_user_param_configuration - required: - - model - - question - - examples - - examples_context - - CreateAnswerResponse: - type: object - properties: - object: - type: string - model: - type: string - search_model: - type: string - completion: - type: string - answers: - type: array - items: - type: string - selected_documents: - type: array - items: - type: object - properties: - document: - type: integer - text: - type: string - - CreateClassificationRequest: - type: object - additionalProperties: false - properties: - model: *model_configuration - query: - description: Query to be classified. - type: string - minLength: 1 - example: "The plot is not very attractive." - examples: + Allows you to upload large files in multiple parts. + navigationGroup: endpoints + sections: + - type: endpoint + key: createUpload + path: create + - type: endpoint + key: addUploadPart + path: add-part + - type: endpoint + key: completeUpload + path: complete + - type: endpoint + key: cancelUpload + path: cancel + - type: object + key: Upload + path: object + - type: object + key: UploadPart + path: part-object + - id: images + title: Images description: | - A list of examples with labels, in the following format: - - `[["The movie is so interesting.", "Positive"], ["It is quite boring.", "Negative"], ...]` - - All the label strings will be normalized to be capitalized. + Given a prompt and/or an input image, the model will generate a new image. - You should specify either `examples` or `file`, but not both. - type: array - minItems: 2 - maxItems: 200 - items: - type: array - minItems: 2 - maxItems: 2 - items: - type: string - minLength: 1 - example: "[['Do not see this film.', 'Negative'], ['Smart, provocative and blisteringly funny.', 'Positive']]" - nullable: true - file: + Related guide: [Image generation](/docs/guides/images) + navigationGroup: endpoints + sections: + - type: endpoint + key: createImage + path: create + - type: endpoint + key: createImageEdit + path: createEdit + - type: endpoint + key: createImageVariation + path: createVariation + - type: object + key: Image + path: object + - id: models + title: Models description: | - The ID of the uploaded file that contains training examples. See [upload file](/docs/api-reference/files/upload) for how to upload a file of the desired format and purpose. - - You should specify either `examples` or `file`, but not both. - type: string - nullable: true - labels: - description: The set of categories being classified. If not specified, candidate labels will be automatically collected from the examples you provide. All the label strings will be normalized to be capitalized. - type: array - minItems: 2 - maxItems: 200 - default: null - items: - type: string - example: ["Positive", "Negative"] - nullable: true - search_model: *search_model_configuration - temperature: - description: - What sampling `temperature` to use. Higher values mean the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. - type: number - minimum: 0 - maximum: 2 - default: 0 - nullable: true - example: 0 - logprobs: *context_completions_logprobs_configuration - max_examples: - description: The maximum number of examples to be ranked by [Search](/docs/api-reference/searches/create) when using `file`. Setting it to a higher value leads to improved accuracy but with increased latency and cost. - type: integer - default: 200 - nullable: true - logit_bias: *completions_logit_bias - return_prompt: *return_prompt_configuration - return_metadata: *return_metadata_configuration - expand: *expand_configuration - user: *end_user_param_configuration - required: - - model - - query - - CreateClassificationResponse: - type: object - properties: - object: - type: string - model: - type: string - search_model: - type: string - completion: - type: string - label: - type: string - selected_examples: - type: array - items: - type: object - properties: - document: - type: integer - text: - type: string - label: - type: string - - CreateFineTuneRequest: - type: object - properties: - training_file: + List and describe the various models available in the API. You can refer to the [Models](/docs/models) documentation to understand what models are available and the differences between them. + navigationGroup: endpoints + sections: + - type: endpoint + key: listModels + path: list + - type: endpoint + key: retrieveModel + path: retrieve + - type: endpoint + key: deleteModel + path: delete + - type: object + key: Model + path: object + - id: moderations + title: Moderations description: | - The ID of an uploaded file that contains training data. + Given text and/or image inputs, classifies if those inputs are potentially harmful across several categories. + + Related guide: [Moderations](/docs/guides/moderation) + navigationGroup: endpoints + sections: + - type: endpoint + key: createModeration + path: create + - type: object + key: CreateModerationResponse + path: object - See [upload file](/docs/api-reference/files/upload) for how to upload a file. + + - id: assistants + title: Assistants + beta: true + description: | + Build assistants that can call models and use tools to perform tasks. - Your dataset must be formatted as a JSONL file, where each training - example is a JSON object with the keys "prompt" and "completion". - Additionally, you must upload your file with the purpose `fine-tune`. + [Get started with the Assistants API](/docs/assistants) + navigationGroup: assistants + sections: + - type: endpoint + key: createAssistant + path: createAssistant + - type: endpoint + key: listAssistants + path: listAssistants + - type: endpoint + key: getAssistant + path: getAssistant + - type: endpoint + key: modifyAssistant + path: modifyAssistant + - type: endpoint + key: deleteAssistant + path: deleteAssistant + - type: object + key: AssistantObject + path: object + - id: threads + title: Threads + beta: true + description: | + Create threads that assistants can interact with. - See the [fine-tuning guide](/docs/guides/fine-tuning/creating-training-data) for more details. - type: string - example: "file-ajSREls59WBbvgSzJSVWxMCB" - validation_file: + Related guide: [Assistants](/docs/assistants/overview) + navigationGroup: assistants + sections: + - type: endpoint + key: createThread + path: createThread + - type: endpoint + key: getThread + path: getThread + - type: endpoint + key: modifyThread + path: modifyThread + - type: endpoint + key: deleteThread + path: deleteThread + - type: object + key: ThreadObject + path: object + - id: messages + title: Messages + beta: true description: | - The ID of an uploaded file that contains validation data. - - If you provide this file, the data is used to generate validation - metrics periodically during fine-tuning. These metrics can be viewed in - the [fine-tuning results file](/docs/guides/fine-tuning/analyzing-your-fine-tuned-model). - Your train and validation data should be mutually exclusive. - - Your dataset must be formatted as a JSONL file, where each validation - example is a JSON object with the keys "prompt" and "completion". - Additionally, you must upload your file with the purpose `fine-tune`. - - See the [fine-tuning guide](/docs/guides/fine-tuning/creating-training-data) for more details. - type: string - nullable: true - example: "file-XjSREls59WBbvgSzJSVWxMCa" - model: + Create messages within threads + + Related guide: [Assistants](/docs/assistants/overview) + navigationGroup: assistants + sections: + - type: endpoint + key: createMessage + path: createMessage + - type: endpoint + key: listMessages + path: listMessages + - type: endpoint + key: getMessage + path: getMessage + - type: endpoint + key: modifyMessage + path: modifyMessage + - type: endpoint + key: deleteMessage + path: deleteMessage + - type: object + key: MessageObject + path: object + - id: runs + title: Runs + beta: true description: | - The name of the base model to fine-tune. You can select one of "ada", - "babbage", "curie", "davinci", or a fine-tuned model created after 2022-04-21. - To learn more about these models, see the - [Models](https://beta.openai.com/docs/models) documentation. - default: "curie" - type: string - nullable: true - n_epochs: + Represents an execution run on a thread. + + Related guide: [Assistants](/docs/assistants/overview) + navigationGroup: assistants + sections: + - type: endpoint + key: createRun + path: createRun + - type: endpoint + key: createThreadAndRun + path: createThreadAndRun + - type: endpoint + key: listRuns + path: listRuns + - type: endpoint + key: getRun + path: getRun + - type: endpoint + key: modifyRun + path: modifyRun + - type: endpoint + key: submitToolOuputsToRun + path: submitToolOutputs + - type: endpoint + key: cancelRun + path: cancelRun + - type: object + key: RunObject + path: object + - id: run-steps + title: Run Steps + beta: true description: | - The number of epochs to train the model for. An epoch refers to one - full cycle through the training dataset. - default: 4 - type: integer - nullable: true - batch_size: + Represents the steps (model and tool calls) taken during the run. + + Related guide: [Assistants](/docs/assistants/overview) + navigationGroup: assistants + sections: + - type: endpoint + key: listRunSteps + path: listRunSteps + - type: endpoint + key: getRunStep + path: getRunStep + - type: object + key: RunStepObject + path: step-object + - id: vector-stores + title: Vector Stores + beta: true description: | - The batch size to use for training. The batch size is the number of - training examples used to train a single forward and backward pass. - - By default, the batch size will be dynamically configured to be - ~0.2% of the number of examples in the training set, capped at 256 - - in general, we've found that larger batch sizes tend to work better - for larger datasets. - default: null - type: integer - nullable: true - learning_rate_multiplier: + Vector stores are used to store files for use by the `file_search` tool. + + Related guide: [File Search](/docs/assistants/tools/file-search) + navigationGroup: assistants + sections: + - type: endpoint + key: createVectorStore + path: create + - type: endpoint + key: listVectorStores + path: list + - type: endpoint + key: getVectorStore + path: retrieve + - type: endpoint + key: modifyVectorStore + path: modify + - type: endpoint + key: deleteVectorStore + path: delete + - type: object + key: VectorStoreObject + path: object + - id: vector-stores-files + title: Vector Store Files + beta: true description: | - The learning rate multiplier to use for training. - The fine-tuning learning rate is the original learning rate used for - pretraining multiplied by this value. - - By default, the learning rate multiplier is the 0.05, 0.1, or 0.2 - depending on final `batch_size` (larger learning rates tend to - perform better with larger batch sizes). We recommend experimenting - with values in the range 0.02 to 0.2 to see what produces the best - results. - default: null - type: number - nullable: true - prompt_loss_weight: + Vector store files represent files inside a vector store. + + Related guide: [File Search](/docs/assistants/tools/file-search) + navigationGroup: assistants + sections: + - type: endpoint + key: createVectorStoreFile + path: createFile + - type: endpoint + key: listVectorStoreFiles + path: listFiles + - type: endpoint + key: getVectorStoreFile + path: getFile + - type: endpoint + key: deleteVectorStoreFile + path: deleteFile + - type: object + key: VectorStoreFileObject + path: file-object + - id: vector-stores-file-batches + title: Vector Store File Batches + beta: true description: | - The weight to use for loss on the prompt tokens. This controls how - much the model tries to learn to generate the prompt (as compared - to the completion which always has a weight of 1.0), and can add - a stabilizing effect to training when completions are short. - - If prompts are extremely long (relative to completions), it may make - sense to reduce this weight so as to avoid over-prioritizing - learning the prompt. - default: 0.01 - type: number - nullable: true - compute_classification_metrics: + Vector store file batches represent operations to add multiple files to a vector store. + + Related guide: [File Search](/docs/assistants/tools/file-search) + navigationGroup: assistants + sections: + - type: endpoint + key: createVectorStoreFileBatch + path: createBatch + - type: endpoint + key: getVectorStoreFileBatch + path: getBatch + - type: endpoint + key: cancelVectorStoreFileBatch + path: cancelBatch + - type: endpoint + key: listFilesInVectorStoreBatch + path: listBatchFiles + - type: object + key: VectorStoreFileBatchObject + path: batch-object + - id: assistants-streaming + title: Streaming + beta: true description: | - If set, we calculate classification-specific metrics such as accuracy - and F-1 score using the validation set at the end of every epoch. - These metrics can be viewed in the [results file](/docs/guides/fine-tuning/analyzing-your-fine-tuned-model). - - In order to compute classification metrics, you must provide a - `validation_file`. Additionally, you must - specify `classification_n_classes` for multiclass classification or - `classification_positive_class` for binary classification. - type: boolean - default: false - nullable: true - classification_n_classes: + Stream the result of executing a Run or resuming a Run after submitting tool outputs. + + You can stream events from the [Create Thread and Run](/docs/api-reference/runs/createThreadAndRun), + [Create Run](/docs/api-reference/runs/createRun), and [Submit Tool Outputs](/docs/api-reference/runs/submitToolOutputs) + endpoints by passing `"stream": true`. The response will be a [Server-Sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) stream. + + Our Node and Python SDKs provide helpful utilities to make streaming easy. Reference the + [Assistants API quickstart](/docs/assistants/overview) to learn more. + navigationGroup: assistants + sections: + - type: object + key: MessageDeltaObject + path: message-delta-object + - type: object + key: RunStepDeltaObject + path: run-step-delta-object + - type: object + key: AssistantStreamEvent + path: events + + - id: administration + title: Overview description: | - The number of classes in a classification task. + Programmatically manage your organization. + + The Audit Logs endpoint provides a log of all actions taken in the + organization for security and monitoring purposes. - This parameter is required for multiclass classification. - type: integer - default: null - nullable: true - classification_positive_class: + To access these endpoints please generate an Admin API Key through the [API Platform Organization overview](/organization/admin-keys). Admin API keys cannot be used for non-administration endpoints. + + For best practices on setting up your organization, please refer to this [guide](/docs/guides/production-best-practices/setting-up-your-organization) + navigationGroup: administration + + - id: invite + title: Invites + description: Invite and manage invitations for an organization. Invited users are automatically added to the Default project. + navigationGroup: administration + sections: + - type: endpoint + key: list-invites + path: list + - type: endpoint + key: inviteUser + path: create + - type: endpoint + key: retrieve-invite + path: retrieve + - type: endpoint + key: delete-invite + path: delete + - type: object + key: Invite + path: object + + - id: users + title: Users description: | - The positive class in binary classification. - - This parameter is needed to generate precision, recall, and F1 - metrics when doing binary classification. - type: string - default: null - nullable: true - classification_betas: + Manage users and their role in an organization. Users will be automatically added to the Default project. + navigationGroup: administration + sections: + - type: endpoint + key: list-users + path: list + - type: endpoint + key: modify-user + path: modify + - type: endpoint + key: retrieve-user + path: retrieve + - type: endpoint + key: delete-user + path: delete + - type: object + key: User + path: object + + - id: projects + title: Projects description: | - If this is provided, we calculate F-beta scores at the specified - beta values. The F-beta score is a generalization of F-1 score. - This is only used for binary classification. - - With a beta of 1 (i.e. the F-1 score), precision and recall are - given the same weight. A larger beta score puts more weight on - recall and less on precision. A smaller beta score puts more weight - on precision and less on recall. - type: array - items: - type: number - example: [0.6, 1, 1.5, 2] - default: null - nullable: true - suffix: + Manage the projects within an orgnanization includes creation, updating, and archiving or projects. + The Default project cannot be modified or archived. + navigationGroup: administration + sections: + - type: endpoint + key: list-projects + path: list + - type: endpoint + key: create-project + path: create + - type: endpoint + key: retrieve-project + path: retrieve + - type: endpoint + key: modify-project + path: modify + - type: endpoint + key: archive-project + path: archive + - type: object + key: Project + path: object + + - id: project-users + title: Project Users description: | - A string of up to 40 characters that will be added to your fine-tuned model name. - - For example, a `suffix` of "custom-model-name" would produce a model name like `ada:ft-your-org:custom-model-name-2022-02-15-04-21-04`. - type: string - minLength: 1 - maxLength: 40 - default: null - nullable: true - required: - - training_file - - ListFineTunesResponse: - type: object - properties: - object: - type: string - data: - type: array - items: - $ref: '#/components/schemas/FineTune' - required: - - object - - data - - ListFineTuneEventsResponse: - type: object - properties: - object: - type: string - data: - type: array - items: - $ref: '#/components/schemas/FineTuneEvent' - required: - - object - - data - - CreateEmbeddingRequest: - type: object - additionalProperties: false - properties: - model: *model_configuration - input: + Manage users within a project, including adding, updating roles, and removing users. + Users cannot be removed from the Default project, unless they are being removed from the organization. + navigationGroup: administration + sections: + - type: endpoint + key: list-project-users + path: list + - type: endpoint + key: create-project-user + path: creeate + - type: endpoint + key: retrieve-project-user + path: retrieve + - type: endpoint + key: modify-project-user + path: modify + - type: endpoint + key: delete-project-user + path: delete + - type: object + key: ProjectUser + path: object + + - id: project-service-accounts + title: Project Service Accounts + description: | + Manage service accounts within a project. A service account is a bot user that is not associated with a user. + If a user leaves an organization, their keys and membership in projects will no longer work. Service accounts + do not have this limitation. However, service accounts can also be deleted from a project. + navigationGroup: administration + sections: + - type: endpoint + key: list-project-service-accounts + path: list + - type: endpoint + key: create-project-service-account + path: create + - type: endpoint + key: retrieve-project-service-account + path: retrieve + - type: endpoint + key: delete-project-service-account + path: delete + - type: object + key: ProjectServiceAccount + path: object + + - id: project-api-keys + title: Project API Keys description: | - Input text to get embeddings for, encoded as a string or array of tokens. To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays. Each input must not exceed 2048 tokens in length. - - Unless you are embedding code, we suggest replacing newlines (`\n`) in your input with a single space, as we have observed inferior results when newlines are present. - example: "The quick brown fox jumped over the lazy dog" - oneOf: - - type: string - default: '' - example: "This is a test." - - type: array - items: - type: string - default: '' - example: "This is a test." - - type: array - minItems: 1 - items: - type: integer - example: "[1212, 318, 257, 1332, 13]" - - type: array - minItems: 1 - items: - type: array - minItems: 1 - items: - type: integer - example: "[[1212, 318, 257, 1332, 13]]" - user: *end_user_param_configuration - required: - - model - - input - - CreateEmbeddingResponse: - type: object - properties: - object: - type: string - model: - type: string - data: - type: array - items: - type: object - properties: - index: - type: integer - object: - type: string - embedding: - type: array - items: - type: number - required: - - index - - object - - embedding - usage: - type: object - properties: - prompt_tokens: - type: integer - total_tokens: - type: integer - required: - - prompt_tokens - - total_tokens - required: - - object - - model - - data - - usage - - Engine: - title: Engine - properties: - id: - type: string - object: - type: string - created: - type: integer - nullable: true - ready: - type: boolean - required: - - id - - object - - created - - ready - - Model: - title: Model - properties: - id: - type: string - object: - type: string - created: - type: integer - owned_by: - type: string - required: - - id - - object - - created - - owned_by - - OpenAIFile: - title: OpenAIFile - properties: - id: - type: string - object: - type: string - bytes: - type: integer - created_at: - type: integer - filename: - type: string - purpose: - type: string - status: - type: string - status_details: - type: object - nullable: true - required: - - id - - object - - bytes - - created_at - - filename - - purpose - - FineTune: - title: FineTune - properties: - id: - type: string - object: - type: string - created_at: - type: integer - updated_at: - type: integer - model: - type: string - fine_tuned_model: - type: string - nullable: true - organization_id: - type: string - status: - type: string - hyperparams: - type: object - training_files: - type: array - items: - $ref: '#/components/schemas/OpenAIFile' - validation_files: - type: array - items: - $ref: '#/components/schemas/OpenAIFile' - result_files: - type: array - items: - $ref: '#/components/schemas/OpenAIFile' - events: - type: array - items: - $ref: '#/components/schemas/FineTuneEvent' - required: - - id - - object - - created_at - - updated_at - - model - - fine_tuned_model - - organization_id - - status - - hyperparams - - training_files - - validation_files - - result_files - - FineTuneEvent: - title: FineTuneEvent - properties: - object: - type: string - created_at: - type: integer - level: - type: string - message: - type: string - required: - - object - - created_at - - level - - message + Manage API keys for a given project. Supports listing and deleting keys for users. + This API does not allow issuing keys for users, as users need to authorize themselves to generate keys. + navigationGroup: administration + sections: + - type: endpoint + key: list-project-api-keys + path: list + - type: endpoint + key: retrieve-project-api-key + path: retrieve + - type: endpoint + key: delete-project-api-key + path: delete + - type: object + key: ProjectApiKey + path: object -x-oaiMeta: - groups: - - id: models - title: Models - description: | - List and describe the various models available in the API. You can refer to the [Models](/docs/models) documentation to understand what models are available and the differences between them. - - id: completions - title: Completions - description: | - Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. - - id: edits - title: Edits - description: | - Given a prompt and an instruction, the model will return an edited version of the prompt. - - id: images - title: Images - description: | - Given a prompt and/or an input image, the model will generate a new image. - - id: embeddings - title: Embeddings - description: | - Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. - - Related guide: [Embeddings](/docs/guides/embeddings) - - id: files - title: Files - description: | - Files are used to upload documents that can be used with features like [Fine-tuning](/docs/api-reference/fine-tunes). - - id: fine-tunes - title: Fine-tunes - description: | - Manage fine-tuning jobs to tailor a model to your specific training data. - - Related guide: [Fine-tune models](/docs/guides/fine-tuning) - - id: moderations - title: Moderations - description: | - Given a input text, outputs if the model classifies it as violating OpenAI's content policy. - - Related guide: [Moderations](/docs/guides/moderation) - - id: searches - title: Searches - warning: - title: This endpoint is deprecated and will be removed on December 3rd, 2022 - message: We’ve developed new methods with better performance. [Learn more](https://help.openai.com/en/articles/6272952-search-transition-guide). - description: | - Given a query and a set of documents or labels, the model ranks each document based on its semantic similarity to the provided query. - - Related guide: [Search](/docs/guides/search) - - id: classifications - title: Classifications - warning: - title: This endpoint is deprecated and will be removed on December 3rd, 2022 - message: We’ve developed new methods with better performance. [Learn more](https://help.openai.com/en/articles/6272941-classifications-transition-guide). - description: | - Given a query and a set of labeled examples, the model will predict the most likely label for the query. Useful as a drop-in replacement for any ML classification or text-to-label task. - - Related guide: [Classification](/docs/guides/classifications) - - id: answers - title: Answers - warning: - title: This endpoint is deprecated and will be removed on December 3rd, 2022 - message: We’ve developed new methods with better performance. [Learn more](https://help.openai.com/en/articles/6233728-answers-transition-guide). - description: | - Given a question, a set of documents, and some examples, the API generates an answer to the question based on the information in the set of documents. This is useful for question-answering applications on sources of truth, like company documentation or a knowledge base. - - Related guide: [Question answering](/docs/guides/answers) - - id: engines - title: Engines - description: These endpoints describe and provide access to the various engines available in the API. - warning: - title: The Engines endpoints are deprecated. - message: Please use their replacement, [Models](/docs/api-reference/models), instead. [Learn more](https://help.openai.com/TODO). + - id: audit-logs + title: Audit Logs + description: | + Logs of user actions and configuration changes within this organization. + + To log events, you must activate logging in the [Organization Settings](/settings/organization/general). + Once activated, for security reasons, logging cannot be deactivated. + navigationGroup: administration + sections: + - type: endpoint + key: list-audit-logs + path: list + - type: object + key: AuditLog + path: object + + - id: completions + title: Completions + legacy: true + navigationGroup: legacy + description: | + Given a prompt, the model will return one or more predicted completions along with the probabilities of alternative tokens at each position. Most developer should use our [Chat Completions API](/docs/guides/text-generation/text-generation-models) to leverage our best and newest models. + sections: + - type: endpoint + key: createCompletion + path: create + - type: object + key: CreateCompletionResponse + path: object