litellm-rs 0.8.2

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
use super::*;
use crate::core::models::openai::messages::{ChatMessage, MessageContent, MessageRole};
use crate::core::models::openai::{ChatChoice, Usage};
use std::sync::Arc;

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,
    }
}

fn create_assistant_message(content: &str) -> ChatMessage {
    ChatMessage {
        role: MessageRole::Assistant,
        content: Some(MessageContent::Text(content.to_string())),
        name: None,
        function_call: None,
        tool_calls: None,
        tool_call_id: None,
        audio: None,
    }
}

fn create_test_request() -> ChatCompletionRequest {
    ChatCompletionRequest {
        model: "gpt-4".to_string(),
        messages: vec![create_user_message("Hello")],
        ..Default::default()
    }
}

fn create_test_response() -> ChatCompletionResponse {
    ChatCompletionResponse {
        id: "chatcmpl-123".to_string(),
        object: "chat.completion".to_string(),
        created: 1234567890,
        model: "gpt-4".to_string(),
        choices: vec![ChatChoice {
            index: 0,
            message: create_assistant_message("Hello! How can I help you?"),
            finish_reason: Some("stop".to_string()),
            logprobs: None,
        }],
        usage: Some(Usage {
            prompt_tokens: 10,
            completion_tokens: 8,
            total_tokens: 18,
            prompt_tokens_details: None,
            completion_tokens_details: None,
            thinking_usage: None,
        }),
        system_fingerprint: None,
    }
}

// ==================== LLMCache Tests ====================

#[tokio::test]
async fn test_llm_cache_creation() {
    let cache = LLMCache::memory_only();
    assert!(!cache.is_redis_available().await);
}

#[tokio::test]
async fn test_llm_cache_chat_miss() {
    let cache = LLMCache::memory_only();
    let request = create_test_request();

    let result = cache.get_chat_response(&request).await.unwrap();
    assert!(result.is_none());
}

#[tokio::test]
async fn test_llm_cache_chat_hit() {
    let cache = LLMCache::memory_only();
    let request = create_test_request();
    let response = create_test_response();

    cache
        .cache_chat_response(&request, response.clone())
        .await
        .unwrap();

    let result = cache.get_chat_response(&request).await.unwrap();
    assert!(result.is_some());
    assert_eq!(result.as_ref().unwrap().id.as_str(), response.id.as_str());
}

#[tokio::test]
async fn test_llm_cache_chat_hit_reuses_shared_payload() {
    let cache = LLMCache::memory_only();
    let request = create_test_request();
    let response = create_test_response();

    cache.cache_chat_response(&request, response).await.unwrap();

    let first = cache
        .get_chat_response(&request)
        .await
        .unwrap()
        .expect("first cache hit");
    let second = cache
        .get_chat_response(&request)
        .await
        .unwrap()
        .expect("second cache hit");

    assert!(Arc::ptr_eq(&first, &second));
}

#[tokio::test]
async fn test_llm_cache_chat_invalidate() {
    let cache = LLMCache::memory_only();
    let request = create_test_request();
    let response = create_test_response();

    cache.cache_chat_response(&request, response).await.unwrap();

    let invalidated = cache.invalidate_chat(&request).await.unwrap();
    assert!(invalidated);

    let result = cache.get_chat_response(&request).await.unwrap();
    assert!(result.is_none());
}

#[tokio::test]
async fn test_llm_cache_streaming_not_cached() {
    let cache = LLMCache::memory_only();
    let mut request = create_test_request();
    request.stream = Some(true);
    let response = create_test_response();

    cache.cache_chat_response(&request, response).await.unwrap();

    // Streaming requests should not be cached
    let result = cache.get_chat_response(&request).await.unwrap();
    assert!(result.is_none());
}

#[tokio::test]
async fn test_llm_cache_user_specific() {
    let config = LLMCacheConfig::memory_only().with_user_specific();
    let cache = LLMCache::new(config, None);
    let request = create_test_request();
    let response = create_test_response();

    // Cache with user1
    cache
        .cache_chat_response_with_user(&request, response.clone(), Some("user1"))
        .await
        .unwrap();

    // user1 should get a hit
    let result = cache
        .get_chat_response_with_user(&request, Some("user1"))
        .await
        .unwrap();
    assert!(result.is_some());

    // user2 should get a miss
    let result = cache
        .get_chat_response_with_user(&request, Some("user2"))
        .await
        .unwrap();
    assert!(result.is_none());
}

// ==================== Embedding Cache Tests ====================

#[tokio::test]
async fn test_llm_cache_embedding() {
    let cache = LLMCache::memory_only();

    let request = EmbeddingRequest {
        model: "text-embedding-ada-002".to_string(),
        input: serde_json::json!("Test input"),
        user: None,
        encoding_format: None,
        dimensions: None,
        input_type: None,
        truncation: None,
    };

    let response = EmbeddingResponse {
        object: "list".to_string(),
        data: vec![],
        model: "text-embedding-ada-002".to_string(),
        usage: crate::core::models::openai::EmbeddingUsage {
            prompt_tokens: 3,
            total_tokens: 3,
        },
    };

    cache
        .cache_embedding_response(&request, response.clone(), None, "deployment")
        .await
        .unwrap();

    let result = cache
        .get_embedding_response(&request, None, "deployment")
        .await
        .unwrap();
    assert!(result.is_some());
    assert_eq!(
        result.as_ref().unwrap().model.as_str(),
        response.model.as_str()
    );
}

#[tokio::test]
async fn test_llm_cache_embedding_hit_reuses_shared_payload() {
    let cache = LLMCache::memory_only();

    let request = EmbeddingRequest {
        model: "text-embedding-ada-002".to_string(),
        input: serde_json::json!("Test input"),
        user: None,
        encoding_format: None,
        dimensions: None,
        input_type: None,
        truncation: None,
    };

    let response = EmbeddingResponse {
        object: "list".to_string(),
        data: vec![],
        model: "text-embedding-ada-002".to_string(),
        usage: crate::core::models::openai::EmbeddingUsage {
            prompt_tokens: 3,
            total_tokens: 3,
        },
    };

    cache
        .cache_embedding_response(&request, response, None, "deployment")
        .await
        .unwrap();

    let first = cache
        .get_embedding_response(&request, None, "deployment")
        .await
        .unwrap()
        .expect("first cache hit");
    let second = cache
        .get_embedding_response(&request, None, "deployment")
        .await
        .unwrap()
        .expect("second cache hit");

    assert!(Arc::ptr_eq(&first, &second));
}

// ==================== Statistics Tests ====================

#[tokio::test]
async fn test_llm_cache_stats() {
    let cache = LLMCache::memory_only();
    let request = create_test_request();
    let response = create_test_response();

    // Generate some activity
    let _ = cache.get_chat_response(&request).await; // miss
    cache.cache_chat_response(&request, response).await.unwrap(); // write
    let _ = cache.get_chat_response(&request).await; // hit

    let stats = cache.chat_stats();
    assert_eq!(stats.memory_hits, 1);
    assert_eq!(stats.memory_misses, 1);
}

#[tokio::test]
async fn test_llm_cache_combined_stats() {
    let cache = LLMCache::memory_only();

    let combined = cache.combined_stats();
    assert_eq!(combined.total_hits(), 0);
    assert_eq!(combined.hit_rate(), 0.0);
}

// ==================== Clear Tests ====================

#[tokio::test]
async fn test_llm_cache_clear() {
    let cache = LLMCache::memory_only();
    let request = create_test_request();
    let response = create_test_response();

    cache.cache_chat_response(&request, response).await.unwrap();

    cache.clear().await.unwrap();

    let result = cache.get_chat_response(&request).await.unwrap();
    assert!(result.is_none());
}

// ==================== CachedChatResponse Tests ====================

#[test]
fn test_cached_chat_response() {
    let response = create_test_response();
    let cached = CachedChatResponse::new(response.clone(), "gpt-4".to_string());

    assert!(cached.cached);
    assert_eq!(cached.model, "gpt-4");
    assert!(cached.cached_at > 0);

    let shared = cached.response_arc();
    assert!(Arc::ptr_eq(&shared, &cached.response));
    assert_eq!(shared.id.as_str(), response.id.as_str());
}

// ==================== LLMCacheConfig Tests ====================

#[test]
fn test_llm_cache_config_default() {
    let config = LLMCacheConfig::default();
    assert_eq!(config.chat_ttl, Duration::from_secs(3600));
    assert_eq!(config.embedding_ttl, Duration::from_secs(86400));
    assert!(!config.user_specific);
}

#[test]
fn test_llm_cache_config_builder() {
    let config = LLMCacheConfig::default()
        .with_chat_ttl(Duration::from_secs(1800))
        .with_embedding_ttl(Duration::from_secs(7200))
        .with_user_specific();

    assert_eq!(config.chat_ttl, Duration::from_secs(1800));
    assert_eq!(config.embedding_ttl, Duration::from_secs(7200));
    assert!(config.user_specific);
}

#[tokio::test]
async fn test_invalidate_chat_with_user_honors_user_specific_key() {
    let config = LLMCacheConfig::memory_only().with_user_specific();
    let cache = LLMCache::new(config, None);
    let request = create_test_request();
    let response = create_test_response();

    cache
        .cache_chat_response_with_user(&request, response, Some("user1"))
        .await
        .unwrap();
    assert!(
        cache
            .get_chat_response_with_user(&request, Some("user1"))
            .await
            .unwrap()
            .is_some()
    );

    // Pre-fix bug: invalidate_chat ignored user_specific and deleted the
    // wrong (no-user) key, leaving the per-user entry live.
    let invalidated = cache
        .invalidate_chat_with_user(&request, Some("user1"))
        .await
        .unwrap();
    assert!(invalidated);
    assert!(
        cache
            .get_chat_response_with_user(&request, Some("user1"))
            .await
            .unwrap()
            .is_none()
    );
}

#[tokio::test]
async fn test_invalidate_chat_with_user_matching_skips_replacement() {
    let cache = LLMCache::memory_only();
    let request = create_test_request();
    let poisoned = create_test_response();
    let mut replacement = create_test_response();
    replacement.id = "chatcmpl-safe".to_string();
    replacement.created = 1234567891;

    cache
        .cache_chat_response(&request, poisoned.clone())
        .await
        .unwrap();
    cache
        .cache_chat_response(&request, replacement.clone())
        .await
        .unwrap();

    let deleted = cache
        .invalidate_chat_with_user_matching(&request, None, &poisoned)
        .await
        .unwrap();
    assert!(!deleted);

    let still_there = cache.get_chat_response(&request).await.unwrap().unwrap();
    assert_eq!(still_there.id, "chatcmpl-safe");
}

#[tokio::test]
async fn test_invalidate_matching_compares_full_payload_not_just_metadata() {
    // Azure-style defaults: empty id + second-resolution created can collide for
    // distinct completions. Matching must include choice bodies so a safe
    // replacement with the same metadata is not deleted.
    let cache = LLMCache::memory_only();
    let request = create_test_request();
    let mut poisoned = create_test_response();
    poisoned.id = String::new();
    poisoned.created = 1_700_000_000;
    poisoned.choices[0].message = create_assistant_message("blocked later");

    let mut replacement = poisoned.clone();
    replacement.choices[0].message = create_assistant_message("safe replacement");

    cache
        .cache_chat_response(&request, poisoned.clone())
        .await
        .unwrap();
    cache
        .cache_chat_response(&request, replacement.clone())
        .await
        .unwrap();

    let deleted = cache
        .invalidate_chat_with_user_matching(&request, None, &poisoned)
        .await
        .unwrap();
    assert!(!deleted);

    let still_there = cache.get_chat_response(&request).await.unwrap().unwrap();
    let content = still_there.choices[0]
        .message
        .content
        .as_ref()
        .and_then(|c| match c {
            MessageContent::Text(text) => Some(text.as_str()),
            _ => None,
        });
    assert_eq!(content, Some("safe replacement"));
}

#[tokio::test]
async fn test_invalidate_matching_is_atomic_against_concurrent_replacement() {
    use std::sync::Arc;

    let cache = Arc::new(LLMCache::memory_only());
    let request = create_test_request();
    let poisoned = create_test_response();
    let mut replacement = create_test_response();
    replacement.id = "chatcmpl-safe".to_string();
    replacement.created = 1234567891;

    for _ in 0..64 {
        cache
            .cache_chat_response(&request, poisoned.clone())
            .await
            .unwrap();

        let cache_invalidate = Arc::clone(&cache);
        let request_invalidate = request.clone();
        let poisoned_invalidate = poisoned.clone();
        let invalidate = tokio::spawn(async move {
            cache_invalidate
                .invalidate_chat_with_user_matching(&request_invalidate, None, &poisoned_invalidate)
                .await
        });

        let cache_replace = Arc::clone(&cache);
        let request_replace = request.clone();
        let replacement_store = replacement.clone();
        let replace = tokio::spawn(async move {
            cache_replace
                .cache_chat_response(&request_replace, replacement_store)
                .await
        });

        let deleted = invalidate.await.unwrap().unwrap();
        replace.await.unwrap().unwrap();

        let live = cache.get_chat_response(&request).await.unwrap();
        if deleted {
            // Invalidate won the race against the original poison; replacement may
            // or may not have been written afterward.
            if let Some(live) = live {
                assert_eq!(live.id, "chatcmpl-safe");
            }
        } else {
            // Invalidate observed a non-matching live value and must not have
            // deleted the concurrent replacement.
            let live = live.expect("replacement must remain after skipped invalidate");
            assert_eq!(live.id, "chatcmpl-safe");
        }
    }
}