Skip to main content

siumai/
defaults.rs

1//! Default Configuration Values
2//!
3//! This module centralizes all default values used throughout the Siumai SDK.
4//! Having defaults in one place makes them easier to maintain, document, and adjust.
5
6use std::time::Duration;
7
8/// HTTP client default configurations
9pub mod http {
10    use super::*;
11
12    /// Default request timeout for HTTP requests
13    ///
14    /// Set to 60 seconds to accommodate large language models that may take
15    /// 10-20 seconds to respond, plus network latency and proxy delays.
16    pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
17
18    /// Default connection timeout for establishing HTTP connections
19    ///
20    /// Set to 10 seconds which is sufficient for most network conditions
21    /// while not being too aggressive.
22    pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
23
24    /// Default User-Agent string for HTTP requests
25    pub const USER_AGENT: &str = "siumai/0.1.0";
26
27    /// Default maximum number of idle connections per host
28    pub const MAX_IDLE_PER_HOST: usize = 10;
29
30    /// Default maximum total idle connections
31    pub const MAX_IDLE_TOTAL: usize = 100;
32
33    /// Default keep-alive timeout for HTTP connections
34    pub const KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(90);
35
36    /// Default TCP keep-alive interval
37    pub const TCP_KEEP_ALIVE: Duration = Duration::from_secs(60);
38}
39
40/// Timeout configurations for different use cases
41pub mod timeouts {
42    use super::*;
43
44    /// Fast response timeout for interactive applications
45    ///
46    /// Suitable for small to medium models (7B-32B parameters)
47    /// that typically respond within 1-5 seconds.
48    pub const FAST: Duration = Duration::from_secs(30);
49
50    /// Standard timeout for production applications
51    ///
52    /// Suitable for most models including large ones (72B-235B parameters)
53    /// that may take 5-15 seconds to respond.
54    pub const STANDARD: Duration = Duration::from_secs(60);
55
56    /// Extended timeout for complex operations
57    ///
58    /// Suitable for very large models, reasoning models, or batch processing
59    /// that may take 15-60 seconds to complete.
60    pub const EXTENDED: Duration = Duration::from_secs(120);
61
62    /// Long-running timeout for batch processing
63    ///
64    /// Suitable for complex reasoning tasks, long document processing,
65    /// or operations that may take several minutes.
66    pub const LONG_RUNNING: Duration = Duration::from_secs(300);
67
68    /// Maximum reasonable timeout
69    ///
70    /// Upper bound for any operation to prevent indefinite hanging.
71    pub const MAXIMUM: Duration = Duration::from_secs(600);
72}
73
74/// Model-specific timeout recommendations
75pub mod model_timeouts {
76    use super::*;
77
78    /// Timeout for small models (7B-14B parameters)
79    ///
80    /// These models typically respond very quickly (1-3 seconds)
81    pub const SMALL_MODELS: Duration = timeouts::FAST;
82
83    /// Timeout for medium models (32B-72B parameters)
84    ///
85    /// These models typically respond within 3-8 seconds
86    pub const MEDIUM_MODELS: Duration = timeouts::STANDARD;
87
88    /// Timeout for large models (235B+ parameters)
89    ///
90    /// These models may take 8-20 seconds to respond
91    pub const LARGE_MODELS: Duration = timeouts::EXTENDED;
92
93    /// Timeout for reasoning models (DeepSeek R1, QwQ, etc.)
94    ///
95    /// These models perform complex reasoning and may take 10-30 seconds
96    pub const REASONING_MODELS: Duration = timeouts::EXTENDED;
97
98    /// Timeout for code generation models
99    ///
100    /// Code generation may involve complex analysis and take 5-20 seconds
101    pub const CODE_MODELS: Duration = timeouts::EXTENDED;
102
103    /// Timeout for multimodal models (vision + text)
104    ///
105    /// Processing images along with text may take additional time
106    pub const MULTIMODAL_MODELS: Duration = timeouts::EXTENDED;
107
108    /// Timeout for embedding models
109    ///
110    /// Embedding generation is typically fast (1-5 seconds)
111    pub const EMBEDDING_MODELS: Duration = timeouts::FAST;
112
113    /// Timeout for reranking models
114    ///
115    /// Reranking is typically fast (1-5 seconds)
116    pub const RERANK_MODELS: Duration = timeouts::FAST;
117}
118
119/// Rate limiting and retry defaults
120pub mod rate_limiting {
121    use super::*;
122
123    /// Default maximum number of retry attempts
124    pub const MAX_RETRIES: u32 = 3;
125
126    /// Default base delay for exponential backoff (in milliseconds)
127    pub const BASE_RETRY_DELAY_MS: u64 = 1000;
128
129    /// Default maximum delay for exponential backoff
130    pub const MAX_RETRY_DELAY: Duration = Duration::from_secs(30);
131
132    /// Default jitter factor for retry delays (0.0 to 1.0)
133    pub const RETRY_JITTER: f64 = 0.1;
134
135    /// Default requests per minute for rate limiting
136    pub const REQUESTS_PER_MINUTE: u32 = 60;
137
138    /// Default burst size for rate limiting
139    pub const BURST_SIZE: u32 = 10;
140}
141
142/// Streaming and real-time defaults
143pub mod streaming {
144    use super::*;
145
146    /// Default buffer size for streaming responses
147    pub const BUFFER_SIZE: usize = 8192;
148
149    /// Default timeout for streaming chunk reception
150    pub const CHUNK_TIMEOUT: Duration = Duration::from_secs(30);
151
152    /// Default keep-alive interval for Server-Sent Events
153    pub const SSE_KEEP_ALIVE: Duration = Duration::from_secs(15);
154
155    /// Default maximum time to wait for stream start
156    pub const STREAM_START_TIMEOUT: Duration = Duration::from_secs(10);
157}
158
159/// Performance and optimization defaults
160pub mod performance {
161    /// Default batch size for batch processing
162    pub const BATCH_SIZE: usize = 10;
163
164    /// Default concurrency limit for parallel requests
165    pub const CONCURRENCY_LIMIT: usize = 5;
166
167    /// Default cache size for response caching
168    pub const CACHE_SIZE: usize = 1000;
169
170    /// Default cache TTL (time to live)
171    pub const CACHE_TTL_SECONDS: u64 = 3600; // 1 hour
172}
173
174/// Logging and tracing defaults
175pub mod logging {
176    /// Default log level
177    pub const LOG_LEVEL: &str = "info";
178
179    /// Default maximum log file size (in bytes)
180    pub const MAX_LOG_FILE_SIZE: u64 = 100 * 1024 * 1024; // 100MB
181
182    /// Default number of log files to keep in rotation
183    pub const LOG_FILE_ROTATION_COUNT: u32 = 5;
184
185    /// Default maximum size for logged request/response bodies
186    pub const MAX_BODY_LOG_SIZE: usize = 1024; // 1KB
187
188    /// Default sampling rate for tracing (0.0 to 1.0)
189    pub const TRACING_SAMPLING_RATE: f64 = 1.0;
190}
191
192/// Provider-specific defaults
193pub mod providers {
194    use super::*;
195
196    /// OpenAI-specific defaults
197    pub mod openai {
198        use super::*;
199
200        /// Default base URL for OpenAI API
201        pub const BASE_URL: &str = "https://api.openai.com/v1";
202
203        /// Default model for OpenAI
204        pub const DEFAULT_MODEL: &str = "gpt-4o-mini";
205
206        /// Default timeout for OpenAI requests
207        pub const TIMEOUT: Duration = timeouts::STANDARD;
208    }
209
210    /// Anthropic-specific defaults
211    pub mod anthropic {
212        use super::*;
213
214        /// Default base URL for Anthropic API
215        pub const BASE_URL: &str = "https://api.anthropic.com";
216
217        /// Default model for Anthropic
218        pub const DEFAULT_MODEL: &str = "claude-3-5-haiku-20241022";
219
220        /// Default timeout for Anthropic requests
221        pub const TIMEOUT: Duration = timeouts::STANDARD;
222    }
223
224    /// SiliconFlow-specific defaults
225    pub mod siliconflow {
226        use super::*;
227
228        /// Default base URL for SiliconFlow API
229        pub const BASE_URL: &str = "https://api.siliconflow.cn/v1";
230
231        /// Default model for SiliconFlow
232        pub const DEFAULT_MODEL: &str = "deepseek-ai/DeepSeek-V3.1";
233
234        /// Default timeout for SiliconFlow requests (longer due to large models)
235        pub const TIMEOUT: Duration = timeouts::EXTENDED;
236    }
237
238    /// Groq-specific defaults
239    pub mod groq {
240        use super::*;
241
242        /// Default base URL for Groq API
243        pub const BASE_URL: &str = "https://api.groq.com/openai/v1";
244
245        /// Default model for Groq
246        pub const DEFAULT_MODEL: &str = "llama-3.3-70b-versatile";
247
248        /// Default timeout for Groq requests (fast inference)
249        pub const TIMEOUT: Duration = timeouts::FAST;
250    }
251}
252
253/// Model parameter defaults
254pub mod model_params {
255    /// Default temperature for text generation
256    pub const TEMPERATURE: f32 = 0.7;
257
258    /// Default top-p for nucleus sampling
259    pub const TOP_P: f32 = 0.9;
260
261    /// Default top-k for top-k sampling
262    pub const TOP_K: u32 = 50;
263
264    /// Default maximum tokens to generate
265    pub const MAX_TOKENS: u32 = 2048;
266
267    /// Default presence penalty
268    pub const PRESENCE_PENALTY: f32 = 0.0;
269
270    /// Default frequency penalty
271    pub const FREQUENCY_PENALTY: f32 = 0.0;
272}
273
274#[cfg(test)]
275mod tests {
276    use super::*;
277
278    #[test]
279    fn test_timeout_hierarchy() {
280        // Ensure timeouts are in logical order
281        assert!(timeouts::FAST < timeouts::STANDARD);
282        assert!(timeouts::STANDARD < timeouts::EXTENDED);
283        assert!(timeouts::EXTENDED < timeouts::LONG_RUNNING);
284        assert!(timeouts::LONG_RUNNING < timeouts::MAXIMUM);
285    }
286
287    #[test]
288    fn test_http_defaults() {
289        assert_eq!(http::REQUEST_TIMEOUT, Duration::from_secs(60));
290        assert_eq!(http::CONNECT_TIMEOUT, Duration::from_secs(10));
291        assert_eq!(http::USER_AGENT, "siumai/0.1.0");
292    }
293
294    #[test]
295    fn test_model_timeout_assignments() {
296        // Small models should use fast timeout
297        assert_eq!(model_timeouts::SMALL_MODELS, timeouts::FAST);
298
299        // Large models should use extended timeout
300        assert_eq!(model_timeouts::LARGE_MODELS, timeouts::EXTENDED);
301
302        // Reasoning models need extended time
303        assert_eq!(model_timeouts::REASONING_MODELS, timeouts::EXTENDED);
304    }
305}