clia_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//! ## Microsoft Azure
98//!
99//! ```
100//! use async_openai::{Client, config::AzureConfig};
101//!
102//! let config = AzureConfig::new()
103//! .with_api_base("https://my-resource-name.openai.azure.com")
104//! .with_api_version("2023-03-15-preview")
105//! .with_deployment_id("deployment-id")
106//! .with_api_key("...");
107//!
108//! let client = Client::with_config(config);
109//!
110//! // Note that `async-openai` only implements OpenAI spec
111//! // and doesn't maintain parity with the spec of Azure OpenAI service.
112//!
113//! ```
114//!
115//!
116//! ## Examples
117//! For full working examples for all supported features see [examples](https://github.com/64bit/async-openai/tree/main/examples) directory in the repository.
118//!
119#![cfg_attr(docsrs, feature(doc_cfg))]
120
121#[cfg(feature = "byot")]
122pub(crate) use async_openai_macros::byot;
123
124#[cfg(not(feature = "byot"))]
125pub(crate) use async_openai_macros::byot_passthrough as byot;
126
127mod assistants;
128mod audio;
129mod audit_logs;
130mod batches;
131mod chat;
132mod client;
133mod completion;
134pub mod config;
135mod download;
136mod embedding;
137pub mod error;
138mod file;
139mod fine_tuning;
140mod image;
141mod invites;
142mod messages;
143mod model;
144mod moderation;
145mod project_api_keys;
146mod project_service_accounts;
147mod project_users;
148mod projects;
149mod runs;
150mod steps;
151mod threads;
152pub mod traits;
153pub mod types;
154mod uploads;
155mod users;
156mod util;
157mod vector_store_file_batches;
158mod vector_store_files;
159mod vector_stores;
160
161pub use assistants::Assistants;
162pub use audio::Audio;
163pub use audit_logs::AuditLogs;
164pub use batches::Batches;
165pub use chat::Chat;
166pub use client::Client;
167pub use completion::Completions;
168pub use embedding::Embeddings;
169pub use file::Files;
170pub use fine_tuning::FineTuning;
171pub use image::Images;
172pub use invites::Invites;
173pub use messages::Messages;
174pub use model::Models;
175pub use moderation::Moderations;
176pub use project_api_keys::ProjectAPIKeys;
177pub use project_service_accounts::ProjectServiceAccounts;
178pub use project_users::ProjectUsers;
179pub use projects::Projects;
180pub use runs::Runs;
181pub use steps::Steps;
182pub use threads::Threads;
183pub use uploads::Uploads;
184pub use users::Users;
185pub use vector_store_file_batches::VectorStoreFileBatches;
186pub use vector_store_files::VectorStoreFiles;
187pub use vector_stores::VectorStores;