async_openai/lib.rs
1//! Rust library for OpenAI
2//!
3//! ## Creating client
4//!
5//! ```
6//! use async_openai::{Client, config::OpenAIConfig};
7//!
8//! // Create a OpenAI client with api key from env var OPENAI_API_KEY and default base url.
9//! let client = Client::new();
10//!
11//! // Above is shortcut for
12//! let config = OpenAIConfig::default();
13//! let client = Client::with_config(config);
14//!
15//! // OR use API key from different source and a non default organization
16//! let api_key = "sk-..."; // This secret could be from a file, or environment variable.
17//! let config = OpenAIConfig::new()
18//! .with_api_key(api_key)
19//! .with_org_id("the-continental");
20//!
21//! let client = Client::with_config(config);
22//!
23//! // Use custom reqwest client
24//! let http_client = reqwest::ClientBuilder::new().user_agent("async-openai").build().unwrap();
25//! let client = Client::new().with_http_client(http_client);
26//! ```
27//!
28//!
29//! ## Making requests
30//!
31//!```
32//!# tokio_test::block_on(async {
33//!
34//! use async_openai::{Client, types::{CreateCompletionRequestArgs}};
35//!
36//! // Create client
37//! let client = Client::new();
38//!
39//! // Create request using builder pattern
40//! // Every request struct has companion builder struct with same name + Args suffix
41//! let request = CreateCompletionRequestArgs::default()
42//! .model("gpt-3.5-turbo-instruct")
43//! .prompt("Tell me the recipe of alfredo pasta")
44//! .max_tokens(40_u32)
45//! .build()
46//! .unwrap();
47//!
48//! // Call API
49//! let response = client
50//! .completions() // Get the API "group" (completions, images, etc.) from the client
51//! .create(request) // Make the API call in that "group"
52//! .await
53//! .unwrap();
54//!
55//! println!("{}", response.choices.first().unwrap().text);
56//! # });
57//!```
58//!
59//! ## Bring Your Own Types
60//!
61//! To use custom types for inputs and outputs, enable `byot` feature which provides additional generic methods with same name and `_byot` suffix.
62//! This feature is available on methods whose return type is not `Bytes`
63//!
64//!```
65//!# #[cfg(feature = "byot")]
66//!# tokio_test::block_on(async {
67//! use async_openai::Client;
68//! use serde_json::{Value, json};
69//!
70//! let client = Client::new();
71//!
72//! let response: Value = client
73//! .chat()
74//! .create_byot(json!({
75//! "messages": [
76//! {
77//! "role": "developer",
78//! "content": "You are a helpful assistant"
79//! },
80//! {
81//! "role": "user",
82//! "content": "What do you think about life?"
83//! }
84//! ],
85//! "model": "gpt-4o",
86//! "store": false
87//! }))
88//! .await
89//! .unwrap();
90//!
91//! if let Some(content) = response["choices"][0]["message"]["content"].as_str() {
92//! println!("{}", content);
93//! }
94//! # });
95//!```
96//!
97//! ## Dynamic Dispatch for OpenAI-compatible Providers
98//!
99//! For any struct that implements `Config` trait, wrap it in a smart pointer and cast the pointer to `dyn Config`
100//! trait object, then create a client with `Box` or `Arc` wrapped configuration.
101//!
102//! For example:
103//! ```
104//! use async_openai::{Client, config::{Config, OpenAIConfig}};
105//!
106//! // Use `Box` or `std::sync::Arc` to wrap the config
107//! let config = Box::new(OpenAIConfig::default()) as Box<dyn Config>;
108//! // A function can now accept a `&Client<Box<dyn Config>>` parameter
109//! // which can invoke any openai compatible api
110//! let client: Client<Box<dyn Config>> = Client::with_config(config);
111//! ```
112//!
113//! ## Microsoft Azure
114//!
115//! ```
116//! use async_openai::{Client, config::AzureConfig};
117//!
118//! let config = AzureConfig::new()
119//! .with_api_base("https://my-resource-name.openai.azure.com")
120//! .with_api_version("2023-03-15-preview")
121//! .with_deployment_id("deployment-id")
122//! .with_api_key("...");
123//!
124//! let client = Client::with_config(config);
125//!
126//! // Note that `async-openai` only implements OpenAI spec
127//! // and doesn't maintain parity with the spec of Azure OpenAI service.
128//!
129//! ```
130//!
131//!
132//! ## Examples
133//! For full working examples for all supported features see [examples](https://github.com/64bit/async-openai/tree/main/examples) directory in the repository.
134//!
135#![cfg_attr(docsrs, feature(doc_cfg))]
136
137#[cfg(all(feature = "_api", feature = "byot"))]
138pub(crate) use async_openai_macros::byot;
139
140#[cfg(all(feature = "_api", not(feature = "byot")))]
141pub(crate) use async_openai_macros::byot_passthrough as byot;
142
143// #[cfg(all(not(feature = "_api"), not(feature = "byot")))]
144// #[macro_export]
145// macro_rules! byot {
146// ($($tt:tt)*) => {
147// $($tt)*
148// };
149// }
150
151#[cfg(feature = "administration")]
152mod admin;
153#[cfg(feature = "assistant")]
154mod assistants;
155#[cfg(feature = "audio")]
156mod audio;
157#[cfg(feature = "batch")]
158mod batches;
159#[cfg(feature = "chat-completion")]
160mod chat;
161#[cfg(feature = "chatkit")]
162mod chatkit;
163#[cfg(feature = "_api")]
164mod client;
165#[cfg(feature = "completions")]
166mod completion;
167#[cfg(feature = "_api")]
168pub mod config;
169#[cfg(feature = "container")]
170mod containers;
171#[cfg(feature = "image")]
172mod download;
173#[cfg(feature = "embedding")]
174mod embedding;
175pub mod error;
176#[cfg(feature = "evals")]
177mod evals;
178#[cfg(feature = "file")]
179mod file;
180#[cfg(feature = "finetuning")]
181mod fine_tuning;
182#[cfg(feature = "image")]
183mod image;
184#[cfg(feature = "_api")]
185mod impls;
186#[cfg(feature = "model")]
187mod model;
188#[cfg(feature = "moderation")]
189mod moderation;
190#[cfg(feature = "realtime")]
191mod realtime;
192#[cfg(feature = "_api")]
193mod request_options;
194#[cfg(feature = "responses")]
195mod responses;
196#[cfg(feature = "_api")]
197pub mod traits;
198pub mod types;
199#[cfg(feature = "upload")]
200mod uploads;
201#[cfg(any(
202 feature = "audio",
203 feature = "file",
204 feature = "upload",
205 feature = "image",
206 feature = "video",
207 feature = "container"
208))]
209mod util;
210#[cfg(feature = "vectorstore")]
211mod vectorstores;
212#[cfg(feature = "video")]
213mod video;
214#[cfg(feature = "webhook")]
215pub mod webhooks;
216
217#[cfg(feature = "administration")]
218pub use admin::*;
219#[cfg(feature = "assistant")]
220pub use assistants::*;
221#[cfg(feature = "audio")]
222pub use audio::*;
223#[cfg(feature = "batch")]
224pub use batches::Batches;
225#[cfg(feature = "chat-completion")]
226pub use chat::Chat;
227#[cfg(feature = "chatkit")]
228pub use chatkit::Chatkit;
229#[cfg(feature = "_api")]
230pub use client::Client;
231#[cfg(feature = "completions")]
232pub use completion::Completions;
233#[cfg(feature = "container")]
234pub use containers::*;
235#[cfg(feature = "embedding")]
236pub use embedding::Embeddings;
237#[cfg(feature = "evals")]
238pub use evals::*;
239#[cfg(feature = "file")]
240pub use file::Files;
241#[cfg(feature = "finetuning")]
242pub use fine_tuning::FineTuning;
243#[cfg(feature = "image")]
244pub use image::Images;
245#[cfg(feature = "model")]
246pub use model::Models;
247#[cfg(feature = "moderation")]
248pub use moderation::Moderations;
249#[cfg(feature = "realtime")]
250pub use realtime::Realtime;
251#[cfg(feature = "_api")]
252pub use request_options::RequestOptions;
253#[cfg(feature = "responses")]
254pub use responses::*;
255#[cfg(feature = "upload")]
256pub use uploads::Uploads;
257#[cfg(feature = "vectorstore")]
258pub use vectorstores::*;
259#[cfg(feature = "video")]
260pub use video::Videos;