1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! Rust library for OpenAI
//!
//! ## Creating client
//!
//! ```
//! use async_openai::{Client, config::OpenAIConfig};
//!
//! // 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");
//!
//! 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::{Client, types::responses::{CreateResponseArgs}};
//!
//! // Create client
//! let client = Client::new();
//!
//! // Create request using builder pattern
//! // Every request struct has companion builder struct with same name + Args suffix
//! let request = CreateResponseArgs::default()
//! .model("gpt-5-mini")
//! .input("tell me the recipe of pav bhaji")
//! .max_output_tokens(512u32)
//! .build()?;
//!
//! // Call API
//! let response = client
//! .responses() // Get the API "group" (responses, images, etc.) from the client
//! .create(request) // Make the API call in that "group"
//! .await?;
//!
//! println!("{:?}", response.output_text());
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! # });
//!```
//!
//! ## 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?;
//!
//! if let Some(content) = response["choices"][0]["message"]["content"].as_str() {
//! println!("{}", content);
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! # });
//!```
//!
//! **References: Borrow Instead of Move**
//!
//! With `byot` use reference to request types
//!
//! ```
//! # #[cfg(feature = "byot")]
//! # tokio_test::block_on(async {
//! # use async_openai::{Client, types::responses::{CreateResponse, Response}};
//! # let client = Client::new();
//! # let request = CreateResponse::default();
//! let response: Response = client
//! .responses()
//! .create_byot(&request).await?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! # });
//! ```
//!
//! ## Rust Types
//!
//! To only use Rust types from the crate - use feature flag `types`.
//!
//! There are granular feature flags like `response-types`, `chat-completion-types`, etc.
//!
//! These granular types are enabled when the corresponding API feature is enabled - for example `responses` will enable `response-types`.
//!
//! ## WASM
//! For WASM targets streaming, retries, file operations are not implemented yet.
//! See [examples/wasm-responses](https://github.com/64bit/async-openai/tree/main/examples/wasm-responses) for a working example.
//!
//! ## Configurable Requests
//!
//! **Individual Request**
//!
//! Certain individual APIs that need additional query or header parameters - these can be provided by chaining `.query()`, `.header()`, `.headers()` on the API group.
//!
//! For example:
//! ```
//! # tokio_test::block_on(async {
//! # use async_openai::Client;
//! # use async_openai::traits::RequestOptionsBuilder;
//! # let client = Client::new();
//! client
//! .chat()
//! // query can be a struct or a map too.
//! .query(&[("limit", "10")])?
//! // header for demo
//! .header("key", "value")?
//! .list()
//! .await?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! # });
//! ```
//!
//! **All Requests**
//!
//! Use `Config`, `OpenAIConfig` etc. for configuring url, headers or query parameters globally for all requests.
//!
//! ## OpenAI-compatible Providers
//!
//! Even though the scope of the crate is official OpenAI APIs, it is very configurable to work with compatible providers.
//!
//! **Configurable Path**
//!
//! In addition to `.query()`, `.header()`, `.headers()` a path for individual request can be changed by using `.path()`, method on the API group.
//!
//! For example:
//! ```
//! # tokio_test::block_on(async {
//! # use async_openai::{Client, types::chat::CreateChatCompletionRequestArgs};
//! # use async_openai::traits::RequestOptionsBuilder;
//! # let client = Client::new();
//! # let request = CreateChatCompletionRequestArgs::default()
//! # .model("gpt-4")
//! # .messages([])
//! # .build()
//! # .unwrap();
//! client
//! .chat()
//! .path("/v1/messages")?
//! .create(request)
//! .await?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! # });
//! ```
//!
//! **Dynamic Dispatch**
//!
//! This allows you to use same code (say a `fn`) to call APIs on different OpenAI-compatible providers.
//!
//! For any struct that implements `Config` trait, wrap it in a smart pointer and cast the pointer to `dyn Config`
//! trait object, then create a client with `Box` or `Arc` wrapped configuration.
//!
//! For example:
//! ```
//! use async_openai::{Client, config::{Config, OpenAIConfig}};
//!
//! // Use `Box` or `std::sync::Arc` to wrap the config
//! let config = Box::new(OpenAIConfig::default()) as Box<dyn Config>;
//! // create client
//! let client: Client<Box<dyn Config>> = Client::with_config(config);
//!
//! // A function can now accept a `&Client<Box<dyn Config>>` parameter
//! // which can invoke any openai compatible api
//! fn chat_completion(client: &Client<Box<dyn Config>>) {
//! todo!()
//! }
//! ```
//!
//! ## 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.
//!
pub use byot;
pub use byot_passthrough as byot;
// #[cfg(all(not(feature = "_api"), not(feature = "byot")))]
// #[macro_export]
// macro_rules! byot {
// ($($tt:tt)*) => {
// $($tt)*
// };
// }
// admin::* would be good - however its expanded here so that docs.rs shows the feature flags
pub use ;
pub use ;
pub use ;
pub use Batches;
pub use Chat;
pub use Chatkit;
pub use Client;
pub use Completions;
pub use ;
pub use Embeddings;
pub use ;
pub use Files;
pub use FineTuning;
pub use Images;
pub use Models;
pub use Moderations;
pub use Realtime;
pub use RequestOptions;
pub use ;
pub use ;
pub use Uploads;
pub use ;
pub use Videos;