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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Cache key generation utilities
//!
//! This module provides functions for generating consistent cache keys
//! from LLM requests, including normalization and hashing.

use super::types::CacheKey;
use crate::core::models::openai::{ChatCompletionRequest, EmbeddingRequest};
use serde::Serialize;
use std::collections::BTreeMap;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use tracing::warn;

/// Prefix for chat completion cache keys
pub const CHAT_KEY_PREFIX: &str = "chat";
/// Prefix for embedding cache keys
pub const EMBEDDING_KEY_PREFIX: &str = "embed";
/// Prefix for completion cache keys
pub const COMPLETION_KEY_PREFIX: &str = "completion";

/// Generate a cache key for a chat completion request
///
/// The key is generated by hashing the normalized request parameters
/// that affect the response (model, messages, temperature, etc.)
pub fn generate_chat_key(request: &ChatCompletionRequest) -> CacheKey {
    generate_chat_key_with_user(request, None)
}

/// Generate a cache key for a chat completion request with optional user ID
pub fn generate_chat_key_with_user(
    request: &ChatCompletionRequest,
    user_id: Option<&str>,
) -> CacheKey {
    let mut hasher = DefaultHasher::new();

    // Hash model name
    request.model.hash(&mut hasher);

    // Hash messages
    for message in &request.messages {
        message.role.hash(&mut hasher);
        if let Some(ref content) = message.content {
            content.hash(&mut hasher);
        }
        if let Some(ref name) = message.name {
            name.hash(&mut hasher);
        }
    }

    // Hash parameters that affect output
    hash_optional_f32(&mut hasher, request.temperature);
    hash_optional_u32(&mut hasher, request.max_tokens);
    hash_optional_u32(&mut hasher, request.max_completion_tokens);
    hash_optional_f32(&mut hasher, request.top_p);
    hash_optional_u32(&mut hasher, request.n);
    hash_optional_f32(&mut hasher, request.presence_penalty);
    hash_optional_f32(&mut hasher, request.frequency_penalty);
    hash_optional_u32(&mut hasher, request.seed);

    // Hash stop sequences (sorted for consistency)
    if let Some(ref stops) = request.stop {
        let mut sorted_stops = stops.clone();
        sorted_stops.sort();
        for stop in sorted_stops {
            stop.hash(&mut hasher);
        }
    }

    // Hash response format
    if let Some(ref format) = request.response_format {
        format.format_type.hash(&mut hasher);
    }

    // Hash tools (sorted for consistency)
    if let Some(ref tools) = request.tools {
        for tool in tools {
            tool.function.name.hash(&mut hasher);
        }
    }

    // Hash user ID if provided
    if let Some(uid) = user_id {
        uid.hash(&mut hasher);
    }

    let hash = hasher.finish();
    let key = format!("{}:{}:{:016x}", CHAT_KEY_PREFIX, request.model, hash);

    CacheKey::new(key)
}

/// Generate a cache key for an embedding request
pub fn generate_embedding_key(request: &EmbeddingRequest) -> CacheKey {
    generate_embedding_key_with_user(request, None)
}

/// Generate a cache key for an embedding request with optional user ID
pub fn generate_embedding_key_with_user(
    request: &EmbeddingRequest,
    user_id: Option<&str>,
) -> CacheKey {
    let mut hasher = DefaultHasher::new();

    // Hash model name
    request.model.hash(&mut hasher);

    // Hash input - normalize to sorted format if array
    match &request.input {
        serde_json::Value::String(s) => {
            "string".hash(&mut hasher);
            s.hash(&mut hasher);
        }
        serde_json::Value::Array(arr) => {
            "array".hash(&mut hasher);
            let mut texts: Vec<String> = arr
                .iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect();
            texts.sort();
            for text in texts {
                text.hash(&mut hasher);
            }
        }
        _ => {
            "other".hash(&mut hasher);
            request.input.to_string().hash(&mut hasher);
        }
    }

    // Hash user ID if provided
    if let Some(uid) = user_id {
        uid.hash(&mut hasher);
    }

    let hash = hasher.finish();
    let key = format!("{}:{}:{:016x}", EMBEDDING_KEY_PREFIX, request.model, hash);

    CacheKey::new(key)
}

/// Generate a cache key from arbitrary serializable request
pub fn generate_key_from_json<T: Serialize>(prefix: &str, request: &T) -> CacheKey {
    let json = serde_json::to_string(request).unwrap_or_else(|e| {
        warn!(
            "Failed to serialize request for cache key generation: {}",
            e
        );
        String::new()
    });
    let normalized = normalize_json_string(&json);

    let mut hasher = DefaultHasher::new();
    normalized.hash(&mut hasher);
    let hash = hasher.finish();

    CacheKey::new(format!("{}:{:016x}", prefix, hash))
}

/// Generate a cache key from raw string content
pub fn generate_key_from_content(prefix: &str, content: &str) -> CacheKey {
    let mut hasher = DefaultHasher::new();
    content.hash(&mut hasher);
    let hash = hasher.finish();

    CacheKey::new(format!("{}:{:016x}", prefix, hash))
}

/// Generate a cache key from multiple parts
pub fn generate_key_from_parts(prefix: &str, parts: &[&str]) -> CacheKey {
    let mut hasher = DefaultHasher::new();
    for part in parts {
        part.hash(&mut hasher);
    }
    let hash = hasher.finish();

    CacheKey::new(format!("{}:{:016x}", prefix, hash))
}

// ==================== Helper Functions ====================

/// Hash an optional f32 value
fn hash_optional_f32<H: Hasher>(hasher: &mut H, value: Option<f32>) {
    if let Some(v) = value {
        1u8.hash(hasher);
        v.to_bits().hash(hasher);
    } else {
        0u8.hash(hasher);
    }
}

/// Hash an optional u32 value
fn hash_optional_u32<H: Hasher>(hasher: &mut H, value: Option<u32>) {
    if let Some(v) = value {
        1u8.hash(hasher);
        v.hash(hasher);
    } else {
        0u8.hash(hasher);
    }
}

/// Normalize a JSON string for consistent hashing
///
/// - Sorts object keys
/// - Removes whitespace
/// - Removes timestamps and request IDs
fn normalize_json_string(json: &str) -> String {
    if let Ok(value) = serde_json::from_str::<serde_json::Value>(json) {
        normalize_json_value(&value)
    } else {
        json.to_string()
    }
}

/// Normalize a JSON value recursively
fn normalize_json_value(value: &serde_json::Value) -> String {
    match value {
        serde_json::Value::Object(map) => {
            // Sort keys and filter out non-deterministic fields
            let sorted: BTreeMap<_, _> = map
                .iter()
                .filter(|(k, _)| !is_non_deterministic_field(k))
                .map(|(k, v)| (k.clone(), normalize_json_value(v)))
                .collect();

            serde_json::to_string(&sorted).unwrap_or_default()
        }
        serde_json::Value::Array(arr) => {
            let normalized: Vec<String> = arr.iter().map(normalize_json_value).collect();
            format!("[{}]", normalized.join(","))
        }
        _ => value.to_string(),
    }
}

/// Check if a field should be excluded from cache key generation
fn is_non_deterministic_field(field: &str) -> bool {
    matches!(
        field,
        "timestamp"
            | "request_id"
            | "trace_id"
            | "span_id"
            | "created_at"
            | "updated_at"
            | "id"
            | "stream"
            | "stream_options"
    )
}

/// Builder for constructing cache keys
pub struct CacheKeyBuilder {
    parts: Vec<String>,
    prefix: String,
}

impl CacheKeyBuilder {
    /// Create a new cache key builder with a prefix
    pub fn new(prefix: impl Into<String>) -> Self {
        Self {
            parts: Vec::new(),
            prefix: prefix.into(),
        }
    }

    /// Add a string part to the key
    pub fn with_part(mut self, part: impl Into<String>) -> Self {
        self.parts.push(part.into());
        self
    }

    /// Add an optional string part to the key
    pub fn add_optional(mut self, part: Option<impl Into<String>>) -> Self {
        if let Some(p) = part {
            self.parts.push(p.into());
        }
        self
    }

    /// Add a numeric part to the key
    pub fn add_num<N: std::fmt::Display>(mut self, num: N) -> Self {
        self.parts.push(num.to_string());
        self
    }

    /// Build the cache key
    pub fn build(self) -> CacheKey {
        let mut hasher = DefaultHasher::new();
        for part in &self.parts {
            part.hash(&mut hasher);
        }
        let hash = hasher.finish();

        CacheKey::new(format!("{}:{:016x}", self.prefix, hash))
    }

    /// Build the cache key with explicit parts (no hashing)
    pub fn build_explicit(self) -> CacheKey {
        let key = std::iter::once(self.prefix)
            .chain(self.parts)
            .collect::<Vec<_>>()
            .join(":");

        CacheKey::new(key)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::models::openai::messages::{ChatMessage, MessageContent, MessageRole};

    // Helper to create a test chat message
    fn create_user_message(content: &str) -> ChatMessage {
        ChatMessage {
            role: MessageRole::User,
            content: Some(MessageContent::Text(content.to_string())),
            name: None,
            function_call: None,
            tool_calls: None,
            tool_call_id: None,
            audio: None,
        }
    }

    // ==================== Chat Key Generation Tests ====================

    #[test]
    fn test_generate_chat_key_basic() {
        let request = ChatCompletionRequest {
            model: "gpt-4".to_string(),
            messages: vec![create_user_message("Hello")],
            ..Default::default()
        };

        let key = generate_chat_key(&request);
        assert!(key.as_str().starts_with("chat:gpt-4:"));
    }

    #[test]
    fn test_generate_chat_key_consistency() {
        let request = ChatCompletionRequest {
            model: "gpt-4".to_string(),
            messages: vec![create_user_message("Hello")],
            temperature: Some(0.7),
            ..Default::default()
        };

        let key1 = generate_chat_key(&request);
        let key2 = generate_chat_key(&request);

        assert_eq!(key1, key2);
    }

    #[test]
    fn test_generate_chat_key_different_messages() {
        let request1 = ChatCompletionRequest {
            model: "gpt-4".to_string(),
            messages: vec![create_user_message("Hello")],
            ..Default::default()
        };

        let request2 = ChatCompletionRequest {
            model: "gpt-4".to_string(),
            messages: vec![create_user_message("Goodbye")],
            ..Default::default()
        };

        let key1 = generate_chat_key(&request1);
        let key2 = generate_chat_key(&request2);

        assert_ne!(key1, key2);
    }

    #[test]
    fn test_generate_chat_key_different_models() {
        let request1 = ChatCompletionRequest {
            model: "gpt-4".to_string(),
            messages: vec![],
            ..Default::default()
        };

        let request2 = ChatCompletionRequest {
            model: "gpt-3.5-turbo".to_string(),
            messages: vec![],
            ..Default::default()
        };

        let key1 = generate_chat_key(&request1);
        let key2 = generate_chat_key(&request2);

        assert_ne!(key1, key2);
    }

    #[test]
    fn test_generate_chat_key_with_user() {
        let request = ChatCompletionRequest {
            model: "gpt-4".to_string(),
            messages: vec![],
            ..Default::default()
        };

        let key1 = generate_chat_key_with_user(&request, Some("user-123"));
        let key2 = generate_chat_key_with_user(&request, Some("user-456"));
        let key3 = generate_chat_key_with_user(&request, None);

        assert_ne!(key1, key2);
        assert_ne!(key1, key3);
    }

    #[test]
    fn test_generate_chat_key_with_parameters() {
        let request1 = ChatCompletionRequest {
            model: "gpt-4".to_string(),
            messages: vec![],
            temperature: Some(0.7),
            ..Default::default()
        };

        let request2 = ChatCompletionRequest {
            model: "gpt-4".to_string(),
            messages: vec![],
            temperature: Some(0.9),
            ..Default::default()
        };

        let key1 = generate_chat_key(&request1);
        let key2 = generate_chat_key(&request2);

        assert_ne!(key1, key2);
    }

    // ==================== Embedding Key Generation Tests ====================

    #[test]
    fn test_generate_embedding_key_string() {
        let request = EmbeddingRequest {
            model: "text-embedding-ada-002".to_string(),
            input: serde_json::json!("Hello world"),
            user: None,
        };

        let key = generate_embedding_key(&request);
        assert!(key.as_str().starts_with("embed:text-embedding-ada-002:"));
    }

    #[test]
    fn test_generate_embedding_key_array() {
        let request = EmbeddingRequest {
            model: "text-embedding-ada-002".to_string(),
            input: serde_json::json!(["Hello", "World"]),
            user: None,
        };

        let key = generate_embedding_key(&request);
        assert!(key.as_str().starts_with("embed:text-embedding-ada-002:"));
    }

    #[test]
    fn test_generate_embedding_key_consistency() {
        let request = EmbeddingRequest {
            model: "text-embedding-3-small".to_string(),
            input: serde_json::json!("Test input"),
            user: None,
        };

        let key1 = generate_embedding_key(&request);
        let key2 = generate_embedding_key(&request);

        assert_eq!(key1, key2);
    }

    #[test]
    fn test_generate_embedding_key_array_order_normalized() {
        // Array order should be normalized (sorted) for consistent keys
        let request1 = EmbeddingRequest {
            model: "text-embedding-ada-002".to_string(),
            input: serde_json::json!(["Alpha", "Beta"]),
            user: None,
        };

        let request2 = EmbeddingRequest {
            model: "text-embedding-ada-002".to_string(),
            input: serde_json::json!(["Beta", "Alpha"]),
            user: None,
        };

        let key1 = generate_embedding_key(&request1);
        let key2 = generate_embedding_key(&request2);

        // Keys should be the same due to sorting
        assert_eq!(key1, key2);
    }

    // ==================== Generic Key Generation Tests ====================

    #[test]
    fn test_generate_key_from_content() {
        let key = generate_key_from_content("test", "some content");
        assert!(key.as_str().starts_with("test:"));
    }

    #[test]
    fn test_generate_key_from_parts() {
        let key = generate_key_from_parts("prefix", &["part1", "part2", "part3"]);
        assert!(key.as_str().starts_with("prefix:"));
    }

    #[test]
    fn test_generate_key_from_json() {
        #[derive(Serialize)]
        struct TestRequest {
            field1: String,
            field2: i32,
        }

        let request = TestRequest {
            field1: "value".to_string(),
            field2: 42,
        };

        let key = generate_key_from_json("test", &request);
        assert!(key.as_str().starts_with("test:"));
    }

    // ==================== CacheKeyBuilder Tests ====================

    #[test]
    fn test_cache_key_builder_basic() {
        let key = CacheKeyBuilder::new("chat")
            .with_part("gpt-4")
            .with_part("user-123")
            .build();

        assert!(key.as_str().starts_with("chat:"));
    }

    #[test]
    fn test_cache_key_builder_with_nums() {
        let key = CacheKeyBuilder::new("session")
            .with_part("user")
            .add_num(123)
            .add_num(456)
            .build();

        assert!(key.as_str().starts_with("session:"));
    }

    #[test]
    fn test_cache_key_builder_with_optional() {
        let key1 = CacheKeyBuilder::new("test")
            .with_part("base")
            .add_optional(Some("optional"))
            .build();

        let key2 = CacheKeyBuilder::new("test")
            .with_part("base")
            .add_optional(None::<String>)
            .build();

        assert_ne!(key1, key2);
    }

    #[test]
    fn test_cache_key_builder_explicit() {
        let key = CacheKeyBuilder::new("chat")
            .with_part("gpt-4")
            .with_part("conversation-1")
            .build_explicit();

        assert_eq!(key.as_str(), "chat:gpt-4:conversation-1");
    }

    // ==================== JSON Normalization Tests ====================

    #[test]
    fn test_normalize_json_filters_timestamp() {
        let json1 = r#"{"message": "hello", "timestamp": "2024-01-01"}"#;
        let json2 = r#"{"message": "hello", "timestamp": "2024-12-31"}"#;

        let norm1 = normalize_json_string(json1);
        let norm2 = normalize_json_string(json2);

        assert_eq!(norm1, norm2);
    }

    #[test]
    fn test_normalize_json_sorts_keys() {
        let json1 = r#"{"b": 2, "a": 1}"#;
        let json2 = r#"{"a": 1, "b": 2}"#;

        let norm1 = normalize_json_string(json1);
        let norm2 = normalize_json_string(json2);

        assert_eq!(norm1, norm2);
    }

    #[test]
    fn test_is_non_deterministic_field() {
        assert!(is_non_deterministic_field("timestamp"));
        assert!(is_non_deterministic_field("request_id"));
        assert!(is_non_deterministic_field("stream"));
        assert!(!is_non_deterministic_field("model"));
        assert!(!is_non_deterministic_field("messages"));
    }
}