openai-ergonomic 0.5.2

Ergonomic Rust wrapper for OpenAI API
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Configuration for the `OpenAI` ergonomic client.
//!
//! This module provides configuration options for the `OpenAI` client,
//! including API key management, base URLs, timeouts, and retry settings.

use crate::{errors::Result, Error};
use reqwest_middleware::ClientWithMiddleware;
use std::env;

/// Configuration for the `OpenAI` client.
///
/// The configuration can be created from environment variables or
/// manually constructed with the builder pattern.
///
/// # Environment Variables
///
/// ## Standard `OpenAI`
/// - `OPENAI_API_KEY`: The `OpenAI` API key (required)
/// - `OPENAI_API_BASE`: Custom base URL for the API (optional)
/// - `OPENAI_ORGANIZATION`: Organization ID (optional)
/// - `OPENAI_PROJECT`: Project ID (optional)
/// - `OPENAI_MAX_RETRIES`: Maximum number of retries (optional, default: 3)
///
/// ## Azure `OpenAI`
/// - `AZURE_OPENAI_API_KEY`: The Azure `OpenAI` API key (alternative to `OPENAI_API_KEY`)
/// - `AZURE_OPENAI_ENDPOINT`: Azure `OpenAI` endpoint (e.g., `<https://my-resource.openai.azure.com>`)
/// - `AZURE_OPENAI_DEPLOYMENT`: Deployment name (required for Azure)
/// - `AZURE_OPENAI_API_VERSION`: API version (optional, default: 2024-02-01)
///
/// # Example
///
/// ```rust,ignore
/// # use openai_ergonomic::Config;
/// // From environment variables
/// let config = Config::from_env().unwrap();
///
/// // Manual configuration for OpenAI
/// let config = Config::builder()
///     .api_key("your-api-key")
///     .max_retries(5)
///     .build();
///
/// // Manual configuration for Azure OpenAI
/// let config = Config::builder()
///     .api_key("your-azure-api-key")
///     .api_base("https://my-resource.openai.azure.com")
///     .azure_deployment("my-deployment")
///     .azure_api_version("2024-02-01")
///     .build();
/// ```
#[derive(Clone)]
pub struct Config {
    api_key: String,
    api_base: String,
    organization: Option<String>,
    project: Option<String>,
    max_retries: u32,
    default_model: String,
    http_client: Option<ClientWithMiddleware>,
    azure_deployment: Option<String>,
    azure_api_version: Option<String>,
}

impl std::fmt::Debug for Config {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Config")
            .field("api_key", &"***")
            .field("api_base", &self.api_base)
            .field("organization", &self.organization)
            .field("project", &self.project)
            .field("max_retries", &self.max_retries)
            .field("default_model", &self.default_model)
            .field(
                "http_client",
                &self.http_client.as_ref().map(|_| "<ClientWithMiddleware>"),
            )
            .field("azure_deployment", &self.azure_deployment)
            .field("azure_api_version", &self.azure_api_version)
            .finish()
    }
}

impl Config {
    /// Create a new configuration builder.
    #[must_use]
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder::default()
    }

    /// Create configuration from environment variables.
    ///
    /// Supports both standard `OpenAI` and Azure `OpenAI` configurations.
    /// For Azure `OpenAI`, set `AZURE_OPENAI_ENDPOINT`, `AZURE_OPENAI_API_KEY`, and `AZURE_OPENAI_DEPLOYMENT`.
    pub fn from_env() -> Result<Self> {
        // Check for Azure OpenAI configuration first
        let azure_endpoint = env::var("AZURE_OPENAI_ENDPOINT").ok();
        let azure_deployment = env::var("AZURE_OPENAI_DEPLOYMENT").ok();
        let azure_api_version = env::var("AZURE_OPENAI_API_VERSION").ok();

        let (api_key, api_base) = if let Some(endpoint) = azure_endpoint {
            // Azure OpenAI configuration
            let key = env::var("AZURE_OPENAI_API_KEY")
                .or_else(|_| env::var("OPENAI_API_KEY"))
                .map_err(|_| {
                    Error::Config(
                        "AZURE_OPENAI_API_KEY or OPENAI_API_KEY environment variable is required"
                            .to_string(),
                    )
                })?;
            // Trim trailing slash from Azure endpoint
            let endpoint = endpoint.trim_end_matches('/').to_string();
            (key, endpoint)
        } else {
            // Standard OpenAI configuration
            let key = env::var("OPENAI_API_KEY").map_err(|_| {
                Error::Config("OPENAI_API_KEY environment variable is required".to_string())
            })?;
            let base = env::var("OPENAI_API_BASE")
                .unwrap_or_else(|_| "https://api.openai.com/v1".to_string());
            (key, base)
        };

        let organization = env::var("OPENAI_ORGANIZATION").ok();
        let project = env::var("OPENAI_PROJECT").ok();

        let max_retries = env::var("OPENAI_MAX_RETRIES")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(3);

        let default_model =
            env::var("OPENAI_DEFAULT_MODEL").unwrap_or_else(|_| "gpt-4".to_string());

        Ok(Self {
            api_key,
            api_base,
            organization,
            project,
            max_retries,
            default_model,
            http_client: None,
            azure_deployment,
            azure_api_version,
        })
    }

    /// Get the API key.
    pub fn api_key(&self) -> &str {
        &self.api_key
    }

    /// Get the API base URL.
    pub fn api_base(&self) -> &str {
        &self.api_base
    }

    /// Get the organization ID, if set.
    pub fn organization(&self) -> Option<&str> {
        self.organization.as_deref()
    }

    /// Get the project ID, if set.
    pub fn project(&self) -> Option<&str> {
        self.project.as_deref()
    }

    /// Get the maximum number of retries.
    pub fn max_retries(&self) -> u32 {
        self.max_retries
    }

    /// Get the default model to use.
    pub fn default_model(&self) -> Option<&str> {
        if self.default_model.is_empty() {
            None
        } else {
            Some(&self.default_model)
        }
    }

    /// Get the base URL, if different from default.
    pub fn base_url(&self) -> Option<&str> {
        if self.api_base == "https://api.openai.com/v1" {
            None
        } else {
            Some(&self.api_base)
        }
    }

    /// Get the organization ID, if set.
    pub fn organization_id(&self) -> Option<&str> {
        self.organization.as_deref()
    }

    /// Create an authorization header value.
    pub fn auth_header(&self) -> String {
        format!("Bearer {}", self.api_key)
    }

    /// Get the custom HTTP client, if set.
    pub fn http_client(&self) -> Option<&ClientWithMiddleware> {
        self.http_client.as_ref()
    }

    /// Get the Azure deployment name, if set.
    pub fn azure_deployment(&self) -> Option<&str> {
        self.azure_deployment.as_deref()
    }

    /// Get the Azure API version, if set.
    pub fn azure_api_version(&self) -> Option<&str> {
        self.azure_api_version.as_deref()
    }

    /// Check if this configuration is for Azure `OpenAI`.
    pub fn is_azure(&self) -> bool {
        self.azure_deployment.is_some() || self.api_base.contains(".openai.azure.com")
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            api_key: String::new(),
            api_base: "https://api.openai.com/v1".to_string(),
            organization: None,
            project: None,
            max_retries: 3,
            default_model: "gpt-4".to_string(),
            http_client: None,
            azure_deployment: None,
            azure_api_version: None,
        }
    }
}

/// Builder for creating `OpenAI` client configuration.
#[derive(Clone, Default)]
pub struct ConfigBuilder {
    api_key: Option<String>,
    api_base: Option<String>,
    organization: Option<String>,
    project: Option<String>,
    max_retries: Option<u32>,
    default_model: Option<String>,
    http_client: Option<ClientWithMiddleware>,
    azure_deployment: Option<String>,
    azure_api_version: Option<String>,
}

impl ConfigBuilder {
    /// Set the API key.
    #[must_use]
    pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// Set the API base URL.
    #[must_use]
    pub fn api_base(mut self, api_base: impl Into<String>) -> Self {
        self.api_base = Some(api_base.into());
        self
    }

    /// Set the organization ID.
    #[must_use]
    pub fn organization(mut self, organization: impl Into<String>) -> Self {
        self.organization = Some(organization.into());
        self
    }

    /// Set the project ID.
    #[must_use]
    pub fn project(mut self, project: impl Into<String>) -> Self {
        self.project = Some(project.into());
        self
    }

    /// Set the maximum number of retries.
    #[must_use]
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = Some(max_retries);
        self
    }

    /// Set the default model to use.
    #[must_use]
    pub fn default_model(mut self, default_model: impl Into<String>) -> Self {
        self.default_model = Some(default_model.into());
        self
    }

    /// Set a custom HTTP client.
    ///
    /// This allows you to provide a pre-configured `ClientWithMiddleware` with
    /// custom settings like retry policies, connection pooling, proxies, etc.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use reqwest_middleware::ClientBuilder;
    /// use reqwest_retry::{RetryTransientMiddleware, policies::ExponentialBackoff};
    ///
    /// let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
    /// let client = ClientBuilder::new(reqwest::Client::new())
    ///     .with(RetryTransientMiddleware::new_with_policy(retry_policy))
    ///     .build();
    ///
    /// let config = Config::builder()
    ///     .api_key("sk-...")
    ///     .http_client(client)
    ///     .build();
    /// ```
    #[must_use]
    pub fn http_client(mut self, client: ClientWithMiddleware) -> Self {
        self.http_client = Some(client);
        self
    }

    /// Set the Azure deployment name.
    ///
    /// Required when using Azure `OpenAI`.
    #[must_use]
    pub fn azure_deployment(mut self, deployment: impl Into<String>) -> Self {
        self.azure_deployment = Some(deployment.into());
        self
    }

    /// Set the Azure API version.
    ///
    /// Defaults to "2024-02-01" if not specified.
    #[must_use]
    pub fn azure_api_version(mut self, version: impl Into<String>) -> Self {
        self.azure_api_version = Some(version.into());
        self
    }

    /// Build the configuration.
    #[must_use]
    pub fn build(self) -> Config {
        Config {
            api_key: self.api_key.unwrap_or_default(),
            api_base: self
                .api_base
                .unwrap_or_else(|| "https://api.openai.com/v1".to_string()),
            organization: self.organization,
            project: self.project,
            max_retries: self.max_retries.unwrap_or(3),
            default_model: self.default_model.unwrap_or_else(|| "gpt-4".to_string()),
            http_client: self.http_client,
            azure_deployment: self.azure_deployment,
            azure_api_version: self.azure_api_version,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn test_config_builder() {
        let config = Config::builder().api_key("test-key").max_retries(5).build();

        assert_eq!(config.api_key(), "test-key");
        assert_eq!(config.max_retries(), 5);
        assert_eq!(config.api_base(), "https://api.openai.com/v1");
    }

    #[test]
    fn test_auth_header() {
        let config = Config::builder().api_key("test-key").build();

        assert_eq!(config.auth_header(), "Bearer test-key");
    }

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert_eq!(config.max_retries(), 3);
        assert_eq!(config.default_model(), Some("gpt-4"));
    }

    #[test]
    fn test_config_with_custom_http_client() {
        let http_client = reqwest_middleware::ClientBuilder::new(
            reqwest::Client::builder()
                .timeout(Duration::from_secs(30))
                .build()
                .unwrap(),
        )
        .build();

        let config = Config::builder()
            .api_key("test-key")
            .http_client(http_client)
            .build();

        assert!(config.http_client().is_some());
    }

    #[test]
    fn test_config_without_custom_http_client() {
        let config = Config::builder().api_key("test-key").build();

        assert!(config.http_client().is_none());
    }

    #[test]
    fn test_config_debug_hides_sensitive_data() {
        let config = Config::builder().api_key("secret-key-12345").build();

        let debug_output = format!("{config:?}");

        // Should not contain the actual API key
        assert!(!debug_output.contains("secret-key-12345"));
        // Should contain the masked version
        assert!(debug_output.contains("***"));
    }

    #[test]
    fn test_config_debug_with_http_client() {
        let http_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build();
        let config = Config::builder()
            .api_key("test-key")
            .http_client(http_client)
            .build();

        let debug_output = format!("{config:?}");

        // Should show placeholder for HTTP client
        assert!(debug_output.contains("<ClientWithMiddleware>"));
    }
}