cognis 0.2.0

LLM application framework built on cognis-core
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
//! Embedding cache layer for avoiding redundant API calls.
//!
//! Provides [`CachedEmbeddings`], a wrapper around any [`Embeddings`] implementation
//! that caches results to avoid repeated calls for the same text inputs.

use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, VecDeque};
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, RwLock};

use async_trait::async_trait;

use cognis_core::embeddings::Embeddings;
use cognis_core::error::Result;

/// Statistics about cache performance.
#[derive(Debug, Clone)]
pub struct CacheStats {
    /// Number of cache hits.
    pub hits: usize,
    /// Number of cache misses.
    pub misses: usize,
    /// Hit rate as a fraction (0.0 to 1.0). Returns 0.0 if no lookups have occurred.
    pub hit_rate: f64,
}

/// Trait for embedding caches.
///
/// Implementations store and retrieve embedding vectors keyed by a string
/// derived from the input text (typically a hash).
pub trait EmbeddingCache: Send + Sync {
    /// Retrieve a cached embedding by key.
    fn get(&self, key: &str) -> Option<Vec<f32>>;

    /// Store an embedding under the given key.
    fn put(&self, key: &str, embedding: Vec<f32>);

    /// Retrieve multiple cached embeddings at once.
    ///
    /// Returns a vector of the same length as `keys`, with `None` for cache misses.
    fn get_many(&self, keys: &[String]) -> Vec<Option<Vec<f32>>>;

    /// Store multiple embeddings at once.
    fn put_many(&self, entries: &[(String, Vec<f32>)]);

    /// Remove all entries from the cache.
    fn clear(&self);

    /// Return the number of entries currently in the cache.
    fn len(&self) -> usize;

    /// Return true if the cache is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Thread-safe in-memory embedding cache backed by a `HashMap`.
///
/// Supports optional LRU eviction when a `max_size` is set.
pub struct InMemoryEmbeddingCache {
    store: Arc<RwLock<HashMap<String, Vec<f32>>>>,
    /// Insertion order for LRU eviction. Front = oldest.
    order: Arc<RwLock<VecDeque<String>>>,
    /// Maximum number of entries. `None` means unlimited.
    max_size: Option<usize>,
}

impl InMemoryEmbeddingCache {
    /// Create a new unbounded in-memory cache.
    pub fn new() -> Self {
        Self {
            store: Arc::new(RwLock::new(HashMap::new())),
            order: Arc::new(RwLock::new(VecDeque::new())),
            max_size: None,
        }
    }

    /// Create a new in-memory cache with a maximum number of entries.
    ///
    /// When the cache exceeds this size, the least recently inserted entries
    /// are evicted.
    pub fn with_max_size(max_size: usize) -> Self {
        Self {
            store: Arc::new(RwLock::new(HashMap::new())),
            order: Arc::new(RwLock::new(VecDeque::new())),
            max_size: Some(max_size),
        }
    }

    /// Evict entries until the cache is at or below `max_size`.
    fn evict_if_needed(&self) {
        if let Some(max) = self.max_size {
            let mut store = self.store.write().unwrap();
            let mut order = self.order.write().unwrap();
            while store.len() > max {
                if let Some(oldest_key) = order.pop_front() {
                    store.remove(&oldest_key);
                } else {
                    break;
                }
            }
        }
    }
}

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

impl EmbeddingCache for InMemoryEmbeddingCache {
    fn get(&self, key: &str) -> Option<Vec<f32>> {
        let store = self.store.read().unwrap();
        store.get(key).cloned()
    }

    fn put(&self, key: &str, embedding: Vec<f32>) {
        {
            let mut store = self.store.write().unwrap();
            let is_new = !store.contains_key(key);
            store.insert(key.to_string(), embedding);
            if is_new {
                let mut order = self.order.write().unwrap();
                order.push_back(key.to_string());
            }
        }
        self.evict_if_needed();
    }

    fn get_many(&self, keys: &[String]) -> Vec<Option<Vec<f32>>> {
        let store = self.store.read().unwrap();
        keys.iter().map(|k| store.get(k).cloned()).collect()
    }

    fn put_many(&self, entries: &[(String, Vec<f32>)]) {
        {
            let mut store = self.store.write().unwrap();
            let mut order = self.order.write().unwrap();
            for (key, embedding) in entries {
                let is_new = !store.contains_key(key);
                store.insert(key.clone(), embedding.clone());
                if is_new {
                    order.push_back(key.clone());
                }
            }
        }
        self.evict_if_needed();
    }

    fn clear(&self) {
        let mut store = self.store.write().unwrap();
        let mut order = self.order.write().unwrap();
        store.clear();
        order.clear();
    }

    fn len(&self) -> usize {
        let store = self.store.read().unwrap();
        store.len()
    }
}

/// Compute a deterministic cache key from text content using hashing.
pub fn cache_key(text: &str) -> String {
    let mut hasher = DefaultHasher::new();
    text.hash(&mut hasher);
    format!("{:016x}", hasher.finish())
}

/// Wrapper around an [`Embeddings`] implementation that caches results.
///
/// On each call to `embed_documents` or `embed_query`, the cache is consulted
/// first. Only texts not found in the cache are forwarded to the inner embeddings
/// provider. Results are cached for future use.
///
/// # Example
///
/// ```no_run
/// use cognis::embeddings::cached::{CachedEmbeddings, InMemoryEmbeddingCache};
///
/// # fn example(inner: Box<dyn cognis_core::embeddings::Embeddings>) {
/// let cached = CachedEmbeddings::new(
///     inner,
///     Box::new(InMemoryEmbeddingCache::new()),
/// );
/// # }
/// ```
pub struct CachedEmbeddings {
    inner: Box<dyn Embeddings>,
    cache: Box<dyn EmbeddingCache>,
    hits: AtomicUsize,
    misses: AtomicUsize,
}

impl CachedEmbeddings {
    /// Create a new cached embeddings wrapper.
    pub fn new(inner: Box<dyn Embeddings>, cache: Box<dyn EmbeddingCache>) -> Self {
        Self {
            inner,
            cache,
            hits: AtomicUsize::new(0),
            misses: AtomicUsize::new(0),
        }
    }

    /// Return current cache performance statistics.
    pub fn cache_stats(&self) -> CacheStats {
        let hits = self.hits.load(Ordering::Relaxed);
        let misses = self.misses.load(Ordering::Relaxed);
        let total = hits + misses;
        let hit_rate = if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        };
        CacheStats {
            hits,
            misses,
            hit_rate,
        }
    }

    /// Reset cache statistics counters to zero.
    pub fn reset_stats(&self) {
        self.hits.store(0, Ordering::Relaxed);
        self.misses.store(0, Ordering::Relaxed);
    }

    /// Clear the cache and reset statistics.
    pub fn clear(&self) {
        self.cache.clear();
        self.reset_stats();
    }

    /// Return a reference to the underlying cache.
    pub fn cache(&self) -> &dyn EmbeddingCache {
        self.cache.as_ref()
    }
}

#[async_trait]
impl Embeddings for CachedEmbeddings {
    async fn embed_documents(&self, texts: Vec<String>) -> Result<Vec<Vec<f32>>> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }

        // Build cache keys for all texts
        let keys: Vec<String> = texts.iter().map(|t| cache_key(t)).collect();
        let cached = self.cache.get_many(&keys);

        // Identify misses
        let mut miss_indices: Vec<usize> = Vec::new();
        let mut miss_texts: Vec<String> = Vec::new();
        for (i, entry) in cached.iter().enumerate() {
            if entry.is_none() {
                miss_indices.push(i);
                miss_texts.push(texts[i].clone());
            }
        }

        let hit_count = texts.len() - miss_indices.len();
        self.hits.fetch_add(hit_count, Ordering::Relaxed);
        self.misses.fetch_add(miss_indices.len(), Ordering::Relaxed);

        // Fetch embeddings for misses from the inner provider
        let miss_embeddings = if miss_texts.is_empty() {
            Vec::new()
        } else {
            self.inner.embed_documents(miss_texts).await?
        };

        // Cache the new embeddings
        let new_entries: Vec<(String, Vec<f32>)> = miss_indices
            .iter()
            .zip(miss_embeddings.iter())
            .map(|(&idx, emb)| (keys[idx].clone(), emb.clone()))
            .collect();
        if !new_entries.is_empty() {
            self.cache.put_many(&new_entries);
        }

        // Assemble the full result in original order
        let mut results: Vec<Vec<f32>> = Vec::with_capacity(texts.len());
        let mut miss_iter = miss_embeddings.into_iter();
        for entry in cached {
            match entry {
                Some(emb) => results.push(emb),
                None => results.push(miss_iter.next().unwrap()),
            }
        }

        Ok(results)
    }

    async fn embed_query(&self, text: &str) -> Result<Vec<f32>> {
        let key = cache_key(text);
        if let Some(cached) = self.cache.get(&key) {
            self.hits.fetch_add(1, Ordering::Relaxed);
            return Ok(cached);
        }

        self.misses.fetch_add(1, Ordering::Relaxed);
        let embedding = self.inner.embed_query(text).await?;
        self.cache.put(&key, embedding.clone());
        Ok(embedding)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use cognis_core::embeddings_fake::DeterministicFakeEmbedding;
    use std::sync::Arc;

    /// Helper: create a CachedEmbeddings with DeterministicFakeEmbedding and unbounded cache.
    fn make_cached(size: usize) -> CachedEmbeddings {
        CachedEmbeddings::new(
            Box::new(DeterministicFakeEmbedding::new(size)),
            Box::new(InMemoryEmbeddingCache::new()),
        )
    }

    /// Helper: create a CachedEmbeddings with a bounded cache.
    fn make_cached_bounded(size: usize, max_cache: usize) -> CachedEmbeddings {
        CachedEmbeddings::new(
            Box::new(DeterministicFakeEmbedding::new(size)),
            Box::new(InMemoryEmbeddingCache::with_max_size(max_cache)),
        )
    }

    #[tokio::test]
    async fn test_cache_miss_calls_inner() {
        let cached = make_cached(8);

        let result = cached
            .embed_documents(vec!["hello".to_string()])
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].len(), 8);

        let stats = cached.cache_stats();
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hits, 0);
    }

    #[tokio::test]
    async fn test_cache_hit_returns_cached_value() {
        let cached = make_cached(8);

        // First call: miss
        let first = cached
            .embed_documents(vec!["hello".to_string()])
            .await
            .unwrap();

        // Second call: hit
        let second = cached
            .embed_documents(vec!["hello".to_string()])
            .await
            .unwrap();

        assert_eq!(first, second);

        let stats = cached.cache_stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
    }

    #[tokio::test]
    async fn test_mixed_hits_and_misses_in_batch() {
        let cached = make_cached(8);

        // Populate cache with "hello"
        cached
            .embed_documents(vec!["hello".to_string()])
            .await
            .unwrap();

        // Now request "hello" (hit) and "world" (miss)
        let results = cached
            .embed_documents(vec!["hello".to_string(), "world".to_string()])
            .await
            .unwrap();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].len(), 8);
        assert_eq!(results[1].len(), 8);

        let stats = cached.cache_stats();
        // First call: 1 miss. Second call: 1 hit + 1 miss.
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 2);
    }

    #[tokio::test]
    async fn test_cache_stats_tracking() {
        let cached = make_cached(4);

        // 3 misses
        cached
            .embed_documents(vec!["a".to_string(), "b".to_string(), "c".to_string()])
            .await
            .unwrap();

        // 3 hits
        cached
            .embed_documents(vec!["a".to_string(), "b".to_string(), "c".to_string()])
            .await
            .unwrap();

        let stats = cached.cache_stats();
        assert_eq!(stats.hits, 3);
        assert_eq!(stats.misses, 3);
        assert!((stats.hit_rate - 0.5).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn test_lru_eviction_when_max_size_exceeded() {
        let cached = make_cached_bounded(4, 2);

        // Insert 3 items into a cache that holds 2
        cached.embed_documents(vec!["a".to_string()]).await.unwrap();
        cached.embed_documents(vec!["b".to_string()]).await.unwrap();
        cached.embed_documents(vec!["c".to_string()]).await.unwrap();

        // Cache should have only 2 entries
        assert_eq!(cached.cache().len(), 2);

        // "a" should have been evicted (oldest)
        let key_a = cache_key("a");
        assert!(cached.cache().get(&key_a).is_none());

        // "b" and "c" should still be present
        let key_b = cache_key("b");
        let key_c = cache_key("c");
        assert!(cached.cache().get(&key_b).is_some());
        assert!(cached.cache().get(&key_c).is_some());
    }

    #[tokio::test]
    async fn test_embed_query_caching() {
        let cached = make_cached(8);

        let first = cached.embed_query("test query").await.unwrap();
        let second = cached.embed_query("test query").await.unwrap();

        assert_eq!(first, second);

        let stats = cached.cache_stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
    }

    #[tokio::test]
    async fn test_clear_cache_resets_stats() {
        let cached = make_cached(4);

        cached.embed_query("foo").await.unwrap();
        cached.embed_query("foo").await.unwrap();

        assert_eq!(cached.cache().len(), 1);
        assert_eq!(cached.cache_stats().hits, 1);

        cached.clear();

        assert_eq!(cached.cache().len(), 0);
        let stats = cached.cache_stats();
        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
    }

    #[tokio::test]
    async fn test_thread_safety_concurrent_access() {
        let cached = Arc::new(make_cached(8));

        let mut handles = Vec::new();
        for i in 0..10 {
            let cached_clone = Arc::clone(&cached);
            handles.push(tokio::spawn(async move {
                let text = format!("text_{}", i);
                cached_clone.embed_query(&text).await.unwrap()
            }));
        }

        let mut results = Vec::new();
        for handle in handles {
            results.push(handle.await.unwrap());
        }

        assert_eq!(results.len(), 10);
        // All should be cached now
        assert_eq!(cached.cache().len(), 10);

        let stats = cached.cache_stats();
        assert_eq!(stats.misses, 10);
        assert_eq!(stats.hits, 0);
    }

    #[tokio::test]
    async fn test_empty_input_handling() {
        let cached = make_cached(8);

        let result = cached.embed_documents(vec![]).await.unwrap();
        assert!(result.is_empty());

        let stats = cached.cache_stats();
        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
    }

    #[tokio::test]
    async fn test_cache_key_consistency() {
        // Same text should always produce the same cache key
        let key1 = cache_key("consistent text");
        let key2 = cache_key("consistent text");
        assert_eq!(key1, key2);

        // Different text should produce different keys
        let key3 = cache_key("different text");
        assert_ne!(key1, key3);
    }

    #[tokio::test]
    async fn test_embed_query_and_documents_share_cache() {
        let cached = make_cached(8);

        // embed_query populates cache
        let query_result = cached.embed_query("shared text").await.unwrap();

        // embed_documents should hit cache for same text
        let doc_results = cached
            .embed_documents(vec!["shared text".to_string()])
            .await
            .unwrap();

        assert_eq!(query_result, doc_results[0]);

        let stats = cached.cache_stats();
        // 1 miss from embed_query, 1 hit from embed_documents
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hits, 1);
    }

    #[test]
    fn test_in_memory_cache_is_empty() {
        let cache = InMemoryEmbeddingCache::new();
        assert!(cache.is_empty());
        cache.put("key", vec![1.0, 2.0]);
        assert!(!cache.is_empty());
    }

    #[test]
    fn test_cache_stats_zero_lookups() {
        let cached = make_cached(4);
        let stats = cached.cache_stats();
        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
        assert_eq!(stats.hit_rate, 0.0);
    }
}