rsllm 0.1.0

Rust-native LLM client library with multi-provider support and streaming capabilities
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
//! # RSLLM Configuration
//! 
//! Configuration types and utilities for the RSLLM client library.
//! Supports environment variables, config files, and programmatic configuration.

use crate::{RsllmError, RsllmResult, Provider};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use url::Url;

/// Main client configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientConfig {
    /// Provider configuration
    pub provider: ProviderConfig,
    
    /// Model configuration
    pub model: ModelConfig,
    
    /// HTTP configuration
    pub http: HttpConfig,
    
    /// Retry configuration
    pub retry: RetryConfig,
    
    /// Custom headers
    pub headers: HashMap<String, String>,
}

impl Default for ClientConfig {
    fn default() -> Self {
        Self {
            provider: ProviderConfig::default(),
            model: ModelConfig::default(),
            http: HttpConfig::default(),
            retry: RetryConfig::default(),
            headers: HashMap::new(),
        }
    }
}

impl ClientConfig {
    /// Create a new configuration builder
    pub fn builder() -> ClientConfigBuilder {
        ClientConfigBuilder::new()
    }

    /// Load configuration from environment variables
    pub fn from_env() -> RsllmResult<Self> {
        dotenv::dotenv().ok(); // Load .env file if present
        
        let mut config = Self::default();
        
        // Provider configuration
        if let Ok(provider_str) = std::env::var("RSLLM_PROVIDER") {
            config.provider.provider = provider_str.parse()?;
        }
        
        if let Ok(api_key) = std::env::var("RSLLM_API_KEY") {
            config.provider.api_key = Some(api_key);
        }
        
        if let Ok(base_url) = std::env::var("RSLLM_BASE_URL") {
            config.provider.base_url = Some(base_url.parse()?);
        }
        
        // Model configuration
        if let Ok(model) = std::env::var("RSLLM_MODEL") {
            config.model.model = model;
        }
        
        if let Ok(temp_str) = std::env::var("RSLLM_TEMPERATURE") {
            config.model.temperature = Some(temp_str.parse().map_err(|_| 
                RsllmError::configuration("Invalid temperature value"))?);
        }
        
        if let Ok(max_tokens_str) = std::env::var("RSLLM_MAX_TOKENS") {
            config.model.max_tokens = Some(max_tokens_str.parse().map_err(|_| 
                RsllmError::configuration("Invalid max_tokens value"))?);
        }
        
        // HTTP configuration
        if let Ok(timeout_str) = std::env::var("RSLLM_TIMEOUT") {
            let timeout_secs: u64 = timeout_str.parse().map_err(|_| 
                RsllmError::configuration("Invalid timeout value"))?;
            config.http.timeout = Duration::from_secs(timeout_secs);
        }
        
        Ok(config)
    }

    /// Validate the configuration
    pub fn validate(&self) -> RsllmResult<()> {
        // Validate provider
        self.provider.validate()?;
        
        // Validate model
        self.model.validate()?;
        
        // Validate HTTP config
        self.http.validate()?;
        
        Ok(())
    }
}

/// Provider-specific configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderConfig {
    /// LLM provider type
    pub provider: Provider,
    
    /// API key for the provider
    pub api_key: Option<String>,
    
    /// Base URL for the provider (if custom)
    pub base_url: Option<Url>,
    
    /// Organization ID (for providers that support it)
    pub organization_id: Option<String>,
    
    /// Custom provider-specific settings
    pub custom_settings: HashMap<String, serde_json::Value>,
}

impl Default for ProviderConfig {
    fn default() -> Self {
        Self {
            provider: Provider::OpenAI,
            api_key: None,
            base_url: None,
            organization_id: None,
            custom_settings: HashMap::new(),
        }
    }
}

impl ProviderConfig {
    /// Validate provider configuration
    pub fn validate(&self) -> RsllmResult<()> {
        // Check if API key is required and present
        match self.provider {
            Provider::OpenAI | Provider::Claude => {
                if self.api_key.is_none() {
                    return Err(RsllmError::configuration(
                        format!("API key required for provider: {:?}", self.provider)
                    ));
                }
            }
            Provider::Ollama => {
                // Ollama typically doesn't require an API key for local instances
            }
        }
        
        // Validate base URL if provided
        if let Some(url) = &self.base_url {
            if url.scheme() != "http" && url.scheme() != "https" {
                return Err(RsllmError::configuration(
                    "Base URL must use HTTP or HTTPS scheme"
                ));
            }
        }
        
        Ok(())
    }

    /// Get the effective base URL for the provider
    pub fn effective_base_url(&self) -> RsllmResult<Url> {
        if let Some(url) = &self.base_url {
            Ok(url.clone())
        } else {
            Ok(self.provider.default_base_url())
        }
    }
}

/// Model configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelConfig {
    /// Model name/identifier
    pub model: String,
    
    /// Temperature for sampling (0.0 to 2.0)
    pub temperature: Option<f32>,
    
    /// Maximum tokens to generate
    pub max_tokens: Option<u32>,
    
    /// Top-p sampling parameter
    pub top_p: Option<f32>,
    
    /// Frequency penalty
    pub frequency_penalty: Option<f32>,
    
    /// Presence penalty
    pub presence_penalty: Option<f32>,
    
    /// Stop sequences
    pub stop: Option<Vec<String>>,
    
    /// Whether to stream responses
    pub stream: bool,
}

impl Default for ModelConfig {
    fn default() -> Self {
        Self {
            model: "gpt-3.5-turbo".to_string(),
            temperature: Some(0.7),
            max_tokens: None,
            top_p: None,
            frequency_penalty: None,
            presence_penalty: None,
            stop: None,
            stream: false,
        }
    }
}

impl ModelConfig {
    /// Validate model configuration
    pub fn validate(&self) -> RsllmResult<()> {
        if self.model.is_empty() {
            return Err(RsllmError::validation("model", "Model name cannot be empty"));
        }
        
        if let Some(temp) = self.temperature {
            if !(0.0..=2.0).contains(&temp) {
                return Err(RsllmError::validation(
                    "temperature", 
                    "Temperature must be between 0.0 and 2.0"
                ));
            }
        }
        
        if let Some(top_p) = self.top_p {
            if !(0.0..=1.0).contains(&top_p) {
                return Err(RsllmError::validation(
                    "top_p", 
                    "Top-p must be between 0.0 and 1.0"
                ));
            }
        }
        
        if let Some(freq_penalty) = self.frequency_penalty {
            if !(-2.0..=2.0).contains(&freq_penalty) {
                return Err(RsllmError::validation(
                    "frequency_penalty", 
                    "Frequency penalty must be between -2.0 and 2.0"
                ));
            }
        }
        
        if let Some(pres_penalty) = self.presence_penalty {
            if !(-2.0..=2.0).contains(&pres_penalty) {
                return Err(RsllmError::validation(
                    "presence_penalty", 
                    "Presence penalty must be between -2.0 and 2.0"
                ));
            }
        }
        
        Ok(())
    }
}

/// HTTP configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpConfig {
    /// Request timeout
    pub timeout: Duration,
    
    /// Connection timeout
    pub connect_timeout: Duration,
    
    /// Maximum number of redirects to follow
    pub max_redirects: u32,
    
    /// User agent string
    pub user_agent: String,
    
    /// Whether to use TLS verification
    pub verify_tls: bool,
}

impl Default for HttpConfig {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(30),
            connect_timeout: Duration::from_secs(10),
            max_redirects: 5,
            user_agent: format!("rsllm/{}", crate::VERSION),
            verify_tls: true,
        }
    }
}

impl HttpConfig {
    /// Validate HTTP configuration
    pub fn validate(&self) -> RsllmResult<()> {
        if self.timeout.as_secs() == 0 {
            return Err(RsllmError::validation("timeout", "Timeout must be greater than 0"));
        }
        
        if self.connect_timeout.as_secs() == 0 {
            return Err(RsllmError::validation(
                "connect_timeout", 
                "Connect timeout must be greater than 0"
            ));
        }
        
        Ok(())
    }
}

/// Retry configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryConfig {
    /// Maximum number of retries
    pub max_retries: u32,
    
    /// Base delay between retries
    pub base_delay: Duration,
    
    /// Maximum delay between retries
    pub max_delay: Duration,
    
    /// Backoff multiplier
    pub backoff_multiplier: f32,
    
    /// Whether to add jitter to retry delays
    pub jitter: bool,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            base_delay: Duration::from_millis(500),
            max_delay: Duration::from_secs(30),
            backoff_multiplier: 2.0,
            jitter: true,
        }
    }
}

/// Builder for client configuration
pub struct ClientConfigBuilder {
    config: ClientConfig,
}

impl ClientConfigBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self {
            config: ClientConfig::default(),
        }
    }

    /// Set the provider
    pub fn provider(mut self, provider: Provider) -> Self {
        self.config.provider.provider = provider;
        self
    }

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

    /// Set the base URL
    pub fn base_url(mut self, base_url: impl AsRef<str>) -> RsllmResult<Self> {
        self.config.provider.base_url = Some(base_url.as_ref().parse()?);
        Ok(self)
    }

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

    /// Set the temperature
    pub fn temperature(mut self, temperature: f32) -> Self {
        self.config.model.temperature = Some(temperature);
        self
    }

    /// Set max tokens
    pub fn max_tokens(mut self, max_tokens: u32) -> Self {
        self.config.model.max_tokens = Some(max_tokens);
        self
    }

    /// Enable streaming
    pub fn stream(mut self, stream: bool) -> Self {
        self.config.model.stream = stream;
        self
    }

    /// Set timeout
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.config.http.timeout = timeout;
        self
    }

    /// Add a custom header
    pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.config.headers.insert(key.into(), value.into());
        self
    }

    /// Build the configuration
    pub fn build(self) -> RsllmResult<ClientConfig> {
        self.config.validate()?;
        Ok(self.config)
    }
}

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