litellm-rs 0.4.16

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
//! Anthropic Configuration
//!
//! Configuration

use std::collections::HashMap;
use std::env;

use crate::core::providers::unified_provider::ProviderError;
use crate::core::traits::provider::ProviderConfig;

/// Configuration
#[derive(Debug, Clone)]
pub struct AnthropicConfig {
    /// API key
    pub api_key: Option<String>,
    /// Base URL
    pub base_url: String,
    /// APIversion
    pub api_version: String,
    /// Request
    pub request_timeout: u64,
    /// Connection
    pub connect_timeout: u64,
    /// maximumNumber of retries
    pub max_retries: u32,
    /// Retry delay base (milliseconds)
    pub retry_delay_base: u64,
    /// Proxy URL (optional)
    pub proxy_url: Option<String>,
    /// Request
    pub custom_headers: HashMap<String, String>,
    /// Enable multimodal support
    pub enable_multimodal: bool,
    /// Enable cache control
    pub enable_cache_control: bool,
    /// Enable computer tools
    pub enable_computer_use: bool,
    /// Enable experimental features
    pub enable_experimental: bool,
}

impl Default for AnthropicConfig {
    fn default() -> Self {
        Self {
            api_key: None,
            base_url: "https://api.anthropic.com".to_string(),
            api_version: "2023-06-01".to_string(),
            request_timeout: 120,
            connect_timeout: 10,
            max_retries: 3,
            retry_delay_base: 1000,
            proxy_url: None,
            custom_headers: HashMap::new(),
            enable_multimodal: true,
            enable_cache_control: true,
            enable_computer_use: false, // Default disabled
            enable_experimental: false,
        }
    }
}

impl AnthropicConfig {
    /// Create
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: Some(api_key.into()),
            ..Default::default()
        }
    }

    /// Create
    pub fn new_test(api_key: impl Into<String>) -> Self {
        Self {
            api_key: Some(api_key.into()),
            base_url: "https://api.anthropic.com".to_string(),
            ..Default::default()
        }
    }

    /// Configuration
    pub fn from_env() -> Result<Self, ProviderError> {
        let api_key = env::var("ANTHROPIC_API_KEY")
            .or_else(|_| env::var("CLAUDE_API_KEY"))
            .map(Some)
            .map_err(|_| {
                ProviderError::configuration(
                    "anthropic",
                    "ANTHROPIC_API_KEY or CLAUDE_API_KEY environment variable is required",
                )
            })?;

        let mut config = Self {
            api_key,
            ..Default::default()
        };

        // Configuration
        if let Ok(base_url) = env::var("ANTHROPIC_BASE_URL") {
            config.base_url = base_url;
        }

        if let Ok(api_version) = env::var("ANTHROPIC_API_VERSION") {
            config.api_version = api_version;
        }

        if let Ok(timeout) = env::var("ANTHROPIC_TIMEOUT") {
            config.request_timeout = timeout.parse().unwrap_or(120);
        }

        if let Ok(proxy) = env::var("ANTHROPIC_PROXY") {
            config.proxy_url = Some(proxy);
        }

        // Feature switches
        if let Ok(multimodal) = env::var("ANTHROPIC_ENABLE_MULTIMODAL") {
            config.enable_multimodal = multimodal.parse().unwrap_or(true);
        }

        if let Ok(cache) = env::var("ANTHROPIC_ENABLE_CACHE") {
            config.enable_cache_control = cache.parse().unwrap_or(true);
        }

        if let Ok(computer) = env::var("ANTHROPIC_ENABLE_COMPUTER_USE") {
            config.enable_computer_use = computer.parse().unwrap_or(false);
        }

        if let Ok(experimental) = env::var("ANTHROPIC_ENABLE_EXPERIMENTAL") {
            config.enable_experimental = experimental.parse().unwrap_or(false);
        }

        Ok(config)
    }

    /// Settings
    pub fn with_api_key(mut self, api_key: impl Into<String>) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// Settings
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into();
        self
    }

    /// Settings
    pub fn with_api_version(mut self, version: impl Into<String>) -> Self {
        self.api_version = version.into();
        self
    }

    /// Settings
    pub fn with_timeout(mut self, timeout: u64) -> Self {
        self.request_timeout = timeout;
        self
    }

    /// Settings
    pub fn with_proxy(mut self, proxy_url: impl Into<String>) -> Self {
        self.proxy_url = Some(proxy_url.into());
        self
    }

    /// Request
    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.custom_headers.insert(key.into(), value.into());
        self
    }

    /// Enable multimodal support
    pub fn with_multimodal(mut self, enabled: bool) -> Self {
        self.enable_multimodal = enabled;
        self
    }

    /// Enable cache control
    pub fn with_cache_control(mut self, enabled: bool) -> Self {
        self.enable_cache_control = enabled;
        self
    }

    /// Enable computer tools
    pub fn with_computer_use(mut self, enabled: bool) -> Self {
        self.enable_computer_use = enabled;
        self
    }

    /// Enable experimental features
    pub fn with_experimental(mut self, enabled: bool) -> Self {
        self.enable_experimental = enabled;
        self
    }

    /// Get
    pub fn get_api_url(&self, endpoint: &str) -> String {
        format!("{}{}", self.base_url.trim_end_matches('/'), endpoint)
    }

    /// Check
    pub fn is_feature_enabled(&self, feature: &str) -> bool {
        match feature {
            "multimodal" => self.enable_multimodal,
            "cache_control" => self.enable_cache_control,
            "computer_use" => self.enable_computer_use,
            "experimental" => self.enable_experimental,
            _ => false,
        }
    }
}

impl ProviderConfig for AnthropicConfig {
    fn validate(&self) -> Result<(), String> {
        let api_key = self.api_key.as_ref().ok_or("API key is required")?;
        if api_key.is_empty() {
            return Err("API key cannot be empty".to_string());
        }

        if !api_key.starts_with("sk-ant-") {
            return Err(
                "Invalid Anthropic API key format. Keys should start with 'sk-ant-'".to_string(),
            );
        }

        if api_key.len() < 20 {
            return Err("API key appears to be too short".to_string());
        }

        // Validation
        if self.base_url.is_empty() {
            return Err("Base URL cannot be empty".to_string());
        }

        if !self.base_url.starts_with("http://") && !self.base_url.starts_with("https://") {
            return Err("Base URL must start with http:// or https://".to_string());
        }

        // Settings
        if self.request_timeout == 0 {
            return Err("Request timeout must be greater than 0".to_string());
        }

        if self.connect_timeout == 0 {
            return Err("Connect timeout must be greater than 0".to_string());
        }

        if self.connect_timeout > self.request_timeout {
            return Err("Connect timeout cannot be greater than request timeout".to_string());
        }

        Ok(())
    }

    fn api_key(&self) -> Option<&str> {
        self.api_key.as_deref()
    }

    fn api_base(&self) -> Option<&str> {
        Some(&self.base_url)
    }

    fn timeout(&self) -> std::time::Duration {
        std::time::Duration::from_secs(self.request_timeout)
    }

    fn max_retries(&self) -> u32 {
        self.max_retries
    }
}

/// Configuration
pub struct AnthropicConfigBuilder {
    config: AnthropicConfig,
}

impl AnthropicConfigBuilder {
    /// Create
    pub fn new() -> Self {
        Self {
            config: AnthropicConfig::default(),
        }
    }

    /// Build
    pub fn from_env() -> Result<Self, ProviderError> {
        Ok(Self {
            config: AnthropicConfig::from_env()?,
        })
    }

    /// Settings
    pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
        self.config.api_key = Some(api_key.into());
        self
    }

    /// Settings
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.config.base_url = base_url.into();
        self
    }

    /// Settings
    pub fn timeout(mut self, timeout: u64) -> Self {
        self.config.request_timeout = timeout;
        self
    }

    /// Enable multimodal
    pub fn multimodal(mut self, enabled: bool) -> Self {
        self.config.enable_multimodal = enabled;
        self
    }

    /// Enable experimental features
    pub fn experimental(mut self, enabled: bool) -> Self {
        self.config.enable_experimental = enabled;
        self.config.enable_computer_use = enabled; // Computer tools are part of experimental features
        self
    }

    /// Configuration
    pub fn build(self) -> Result<AnthropicConfig, ProviderError> {
        self.config
            .validate()
            .map_err(|e| ProviderError::configuration("anthropic", e))?;
        Ok(self.config)
    }
}

impl Default for AnthropicConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_default_config() {
        let config = AnthropicConfig::default();
        assert_eq!(config.base_url, "https://api.anthropic.com");
        assert_eq!(config.api_version, "2023-06-01");
        assert!(config.enable_multimodal);
        assert!(config.enable_cache_control);
        assert!(!config.enable_computer_use);
    }

    #[test]
    fn test_config_validation() {
        let mut config = AnthropicConfig::default();

        // Should fail without API key
        assert!(config.validate().is_err());

        // Should pass with valid API key
        config.api_key = Some("sk-ant-api03-test123456".to_string());
        assert!(config.validate().is_ok());

        // Should fail with invalid API key format
        config.api_key = Some("invalid-key".to_string());
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_builder() {
        let config = AnthropicConfigBuilder::new()
            .api_key("sk-ant-test1234567890123")
            .base_url("https://custom.api.com")
            .timeout(60)
            .multimodal(false)
            .build();

        assert!(config.is_ok());
        let config = config.unwrap();
        assert_eq!(config.api_key, Some("sk-ant-test1234567890123".to_string()));
        assert_eq!(config.base_url, "https://custom.api.com");
        assert_eq!(config.request_timeout, 60);
        assert!(!config.enable_multimodal);
    }

    #[test]
    fn test_feature_check() {
        let config = AnthropicConfig::default();
        assert!(config.is_feature_enabled("multimodal"));
        assert!(config.is_feature_enabled("cache_control"));
        assert!(!config.is_feature_enabled("computer_use"));
        assert!(!config.is_feature_enabled("unknown_feature"));
    }

    #[test]
    fn test_api_url_generation() {
        let config = AnthropicConfig::default();
        assert_eq!(
            config.get_api_url("/v1/messages"),
            "https://api.anthropic.com/v1/messages"
        );

        // Test trailing slash removal
        let config = config.with_base_url("https://api.anthropic.com/");
        assert_eq!(
            config.get_api_url("/v1/messages"),
            "https://api.anthropic.com/v1/messages"
        );
    }
}