lc-rag 0.21.0

RAG (Retrieval-Augmented Generation) module for langchainrust — BM25, Hybrid Retrieval, GraphRAG, HyDE, Reranking, MultiQuery, Document Loaders
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
// lc-rag/src/semantic_cache.rs
//! Semantic cache for retrieval results (0.21.0 S4.2).
//!
//! A semantic cache answers repeated (or paraphrased) queries from cached
//! retrieval results instead of re-running embedding + retrieval:
//! - **lexical hit**: the query text is byte-identical to a cached query —
//!   always preferred (product codes, IDs: lexical is more reliable);
//! - **semantic hit**: the query embedding's cosine similarity against a
//!   cached query's embedding reaches the threshold.
//!
//! The cache is a [`RetrieverTrait`] decorator ([`CachedRetriever`]); it is
//! off-by-default (wrap your retriever explicitly) and eviction/TTL keep the
//! footprint bounded. `k` is part of the cached result set: a hit requires the
//! same `k` (a different `k` recomputes rather than slicing padded results).
//!
//! The inner bookkeeping ([`SemanticCacheCore`]) is a pure, embedder-free
//! unit so the threshold/FIFO/TTL/exact-priority semantics are testable with
//! synthetic vectors.

use crate::retriever::{RetrieverError, RetrieverTrait};
use lc_embeddings::Embeddings;
use lc_vector_stores::{Document, SearchResult};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// Semantic cache configuration.
#[derive(Debug, Clone)]
pub struct SemanticCacheConfig {
    /// Minimum cosine similarity for a semantic hit. Conservative default:
    /// a too-low threshold serves semantically-nearbut-different queries.
    pub threshold: f32,
    /// Max entries; the oldest are evicted FIFO (same policy as lc-agents'
    /// `MemoryCache`).
    pub max_entries: usize,
    /// Optional TTL: entries older than this are treated as misses (corpus
    /// updates invalidate results; pair with `invalidate()` for explicit
    /// invalidation).
    pub ttl: Option<Duration>,
}

impl Default for SemanticCacheConfig {
    fn default() -> Self {
        Self {
            threshold: 0.95,
            max_entries: 256,
            ttl: None,
        }
    }
}

impl SemanticCacheConfig {
    /// Creates a config with defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the semantic-hit threshold.
    pub fn with_threshold(mut self, threshold: f32) -> Self {
        self.threshold = threshold;
        self
    }

    /// Sets the max entry count (FIFO eviction).
    pub fn with_max_entries(mut self, max_entries: usize) -> Self {
        self.max_entries = max_entries.max(1);
        self
    }

    /// Sets an optional TTL.
    pub fn with_ttl(mut self, ttl: Option<Duration>) -> Self {
        self.ttl = ttl;
        self
    }
}

/// One cache entry: query text + its embedding + the result set for `k`.
#[derive(Debug, Clone)]
struct CacheEntry {
    query: String,
    query_vector: Vec<f32>,
    k: usize,
    results: Vec<SearchResult>,
    inserted_at: Instant,
}

/// Hit kind returned by [`SemanticCacheCore::lookup`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CacheHitKind {
    /// Byte-identical query text.
    Lexical,
    /// Similarity reached the threshold.
    Semantic,
}

/// Pure cache bookkeeping (no embedder, no retriever — fully unit-testable).
#[derive(Debug)]
pub struct SemanticCacheCore {
    config: SemanticCacheConfig,
    entries: Mutex<CacheInner>,
}

#[derive(Debug, Default)]
struct CacheInner {
    map: Vec<CacheEntry>,
    order: VecDeque<String>,
}

impl SemanticCacheCore {
    /// Creates a core with the given config.
    pub fn new(config: SemanticCacheConfig) -> Self {
        Self {
            config,
            entries: Mutex::new(CacheInner::default()),
        }
    }

    /// Number of live entries.
    pub fn len(&self) -> usize {
        self.entries
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .map
            .len()
    }

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

    /// Looks up `query` (optionally with its embedding for semantic matching)
    /// for result sets of size `k`. Returns the cached results and the hit kind.
    ///
    /// `query_vector` should be the freshly embedded query; pass `None` to
    /// restrict to lexical hits only.
    pub fn lookup(
        &self,
        query: &str,
        query_vector: Option<&[f32]>,
        k: usize,
        now: Instant,
    ) -> Option<(Vec<SearchResult>, CacheHitKind)> {
        let inner = self.entries.lock().unwrap_or_else(|e| e.into_inner());

        // 1. Lexical exact match wins.
        for entry in &inner.map {
            if entry.query == query && entry.k == k && !self.is_expired(&entry.inserted_at, now) {
                return Some((entry.results.clone(), CacheHitKind::Lexical));
            }
        }

        // 2. Semantic match above threshold.
        let query_vector = query_vector?;
        for entry in &inner.map {
            if entry.k != k || self.is_expired(&entry.inserted_at, now) {
                continue;
            }
            if lc_embeddings::cosine_similarity(query_vector, &entry.query_vector).unwrap_or(0.0)
                >= self.config.threshold
            {
                return Some((entry.results.clone(), CacheHitKind::Semantic));
            }
        }
        None
    }

    /// Inserts a result set for `(query, k)`.
    pub fn insert(
        &self,
        query: &str,
        query_vector: Vec<f32>,
        k: usize,
        results: Vec<SearchResult>,
        now: Instant,
    ) {
        let mut inner = self.entries.lock().unwrap_or_else(|e| e.into_inner());
        // Replace existing entry for the same (query, k).
        if let Some(pos) = inner.map.iter().position(|e| e.query == query && e.k == k) {
            inner.map.remove(pos);
            if let Some(p) = inner.order.iter().position(|q| q == query) {
                inner.order.remove(p);
            }
        }
        // FIFO eviction.
        while inner.map.len() >= self.config.max_entries {
            if let Some(oldest) = inner.order.pop_front() {
                if let Some(pos) = inner.map.iter().position(|e| e.query == oldest) {
                    inner.map.remove(pos);
                }
            } else {
                break;
            }
        }
        inner.order.push_back(query.to_string());
        inner.map.push(CacheEntry {
            query: query.to_string(),
            query_vector,
            k,
            results,
            inserted_at: now,
        });
    }

    /// Clears all entries (corpus update invalidation).
    pub fn invalidate(&self) {
        let mut inner = self.entries.lock().unwrap_or_else(|e| e.into_inner());
        inner.map.clear();
        inner.order.clear();
    }

    fn is_expired(&self, inserted_at: &Instant, now: Instant) -> bool {
        match self.config.ttl {
            Some(ttl) => now.duration_since(*inserted_at) > ttl,
            None => false,
        }
    }
}

/// A [`RetrieverTrait`] decorator backed by a [`SemanticCacheCore`].
///
/// Query flow: embed once → lexical hit → semantic hit → miss (call inner,
/// insert). On a hit no retrieval call (and only the embedding call) happens;
/// the embedding itself could be skipped only for lexical hits — which skip
/// embedding too. Failures of the inner retriever propagate (never cached).
pub struct CachedRetriever {
    inner: Arc<dyn RetrieverTrait>,
    embeddings: Arc<dyn Embeddings>,
    cache: Arc<SemanticCacheCore>,
}

impl std::fmt::Debug for CachedRetriever {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CachedRetriever")
            .field("entries", &self.cache.len())
            .finish()
    }
}

impl CachedRetriever {
    /// Wraps `inner` with a semantic cache using `config`.
    pub fn new(
        inner: Arc<dyn RetrieverTrait>,
        embeddings: Arc<dyn Embeddings>,
        config: SemanticCacheConfig,
    ) -> Self {
        Self {
            inner,
            embeddings,
            cache: Arc::new(SemanticCacheCore::new(config)),
        }
    }

    /// The underlying cache core (for inspection / invalidation).
    pub fn cache(&self) -> &Arc<SemanticCacheCore> {
        &self.cache
    }

    async fn lookup_or_retrieve(
        &self,
        query: &str,
        k: usize,
    ) -> Result<(Vec<SearchResult>, Option<CacheHitKind>), RetrieverError> {
        let now = Instant::now();
        // Lexical hits need no embedding at all.
        if let Some((results, kind)) = self.cache.lookup(query, None, k, now) {
            return Ok((results, Some(kind)));
        }
        let qvec = self
            .embeddings
            .embed_query(query)
            .await
            .map_err(|e| RetrieverError::EmbeddingError(e.to_string()))?;
        if let Some((results, kind)) = self.cache.lookup(query, Some(&qvec), k, now) {
            return Ok((results, Some(kind)));
        }
        let results = self.inner.retrieve_with_scores(query, k).await?;
        self.cache
            .insert(query, qvec, k, results.clone(), Instant::now());
        Ok((results, None))
    }
}

#[async_trait::async_trait]
impl RetrieverTrait for CachedRetriever {
    async fn retrieve(&self, query: &str, k: usize) -> Result<Vec<Document>, RetrieverError> {
        let (results, _) = self.lookup_or_retrieve(query, k).await?;
        Ok(results.into_iter().map(|r| r.document).collect())
    }

    async fn retrieve_with_scores(
        &self,
        query: &str,
        k: usize,
    ) -> Result<Vec<SearchResult>, RetrieverError> {
        let (results, _) = self.lookup_or_retrieve(query, k).await?;
        Ok(results)
    }

    async fn add_documents(&self, documents: Vec<Document>) -> Result<(), RetrieverError> {
        // Corpus changed: previously cached results are potentially stale.
        self.cache.invalidate();
        self.inner.add_documents(documents).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn result(content: &str, score: f32) -> SearchResult {
        SearchResult {
            document: Document::new(content),
            score,
        }
    }

    /// Inner retriever counting calls and returning fixed results.
    struct CountingRetriever {
        calls: AtomicUsize,
        results: Vec<SearchResult>,
    }

    impl CountingRetriever {
        fn new(results: Vec<SearchResult>) -> Self {
            Self {
                calls: AtomicUsize::new(0),
                results,
            }
        }
    }

    #[async_trait]
    impl RetrieverTrait for CountingRetriever {
        async fn retrieve(&self, _query: &str, _k: usize) -> Result<Vec<Document>, RetrieverError> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Ok(self.results.iter().map(|r| r.document.clone()).collect())
        }
        async fn retrieve_with_scores(
            &self,
            _query: &str,
            _k: usize,
        ) -> Result<Vec<SearchResult>, RetrieverError> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Ok(self.results.clone())
        }
        async fn add_documents(&self, _documents: Vec<Document>) -> Result<(), RetrieverError> {
            Ok(())
        }
    }

    /// Identity embedding: unit vector on the axis named by the text ("a"/"b").
    struct AxisEmbeddings;

    #[async_trait]
    impl Embeddings for AxisEmbeddings {
        async fn embed_query(&self, text: &str) -> Result<Vec<f32>, lc_embeddings::EmbeddingError> {
            self.embed_documents(&[text])
                .await
                .map(|mut v| v.pop().unwrap_or_default())
        }
        async fn embed_documents(
            &self,
            texts: &[&str],
        ) -> Result<Vec<Vec<f32>>, lc_embeddings::EmbeddingError> {
            texts
                .iter()
                .map(|t| {
                    if t.starts_with('a') {
                        Ok(vec![1.0, 0.0])
                    } else if t.starts_with('b') {
                        Ok(vec![0.0, 1.0])
                    } else {
                        Err(lc_embeddings::EmbeddingError::EmptyInput)
                    }
                })
                .collect()
        }
        fn dimension(&self) -> usize {
            2
        }
        fn model_name(&self) -> &str {
            "axis"
        }
    }

    #[test]
    fn cosine_similarity_basics() {
        use lc_embeddings::cosine_similarity;
        let a = vec![1.0, 0.0];
        assert!((cosine_similarity(&a, &a).unwrap() - 1.0).abs() < 1e-6);
        assert!(cosine_similarity(&a, &[0.0, 1.0]).unwrap().abs() < 1e-6);
        assert!((cosine_similarity(&a, &[-1.0, 0.0]).unwrap() + 1.0).abs() < 1e-6);
        assert!(
            cosine_similarity(&a, &[]).is_err(),
            "length mismatch errors, never NaN"
        );
    }

    #[test]
    fn lexical_hit_requires_same_k() {
        let core = SemanticCacheCore::new(SemanticCacheConfig::new());
        let now = Instant::now();
        core.insert("q", vec![1.0, 0.0], 5, vec![result("doc", 0.9)], now);

        assert!(
            core.lookup("q", None, 5, now).is_some(),
            "exact (q, k) hits"
        );
        assert!(
            core.lookup("q", None, 10, now).is_none(),
            "different k must not be served from the k=5 result set"
        );
    }

    #[test]
    fn lexical_beats_semantic() {
        let core = SemanticCacheCore::new(SemanticCacheConfig::new());
        let now = Instant::now();
        core.insert(
            "a-query",
            vec![1.0, 0.0],
            5,
            vec![result("from-a", 1.0)],
            now,
        );
        core.insert(
            "a-query ",
            vec![1.0, 0.0],
            5,
            vec![result("from-a2", 0.9)],
            now,
        );

        let (results, kind) = core.lookup("a-query", Some(&[1.0, 0.0]), 5, now).unwrap();
        assert_eq!(kind, CacheHitKind::Lexical, "byte-identical wins");
        assert_eq!(results[0].document.content, "from-a");
    }

    #[test]
    fn semantic_hit_respects_threshold() {
        let config = SemanticCacheConfig::new().with_threshold(0.9);
        let core = SemanticCacheCore::new(config);
        let now = Instant::now();
        core.insert(
            "query a",
            vec![1.0, 0.0],
            5,
            vec![result("cached", 0.8)],
            now,
        );

        // Similarity 0.8 < 0.9 → miss.
        let n: f32 = (0.8f32 * 0.8 + 0.6 * 0.6).sqrt();
        let v = vec![0.8 / n, 0.6 / n];
        assert!(core.lookup("query b", Some(&v), 5, now).is_none());

        // Similarity 1.0 ≥ 0.9 → hit.
        let (results, kind) = core.lookup("query c", Some(&[1.0, 0.0]), 5, now).unwrap();
        assert_eq!(kind, CacheHitKind::Semantic);
        assert_eq!(results[0].document.content, "cached");
    }

    #[test]
    fn fifo_eviction_bounded() {
        let config = SemanticCacheConfig::new().with_max_entries(2);
        let core = SemanticCacheCore::new(config);
        let now = Instant::now();
        core.insert("q1", vec![1.0, 0.0], 5, vec![], now);
        core.insert("q2", vec![1.0, 0.0], 5, vec![], now);
        assert_eq!(core.len(), 2);
        core.insert("q3", vec![1.0, 0.0], 5, vec![], now);
        assert_eq!(core.len(), 2, "FIFO evicts the oldest");
        assert!(core.lookup("q1", None, 5, now).is_none(), "q1 evicted");
        assert!(core.lookup("q3", None, 5, now).is_some());
    }

    #[test]
    fn ttl_expiry_is_a_miss() {
        let config = SemanticCacheConfig::new().with_ttl(Some(Duration::from_millis(50)));
        let core = SemanticCacheCore::new(config);
        let now = Instant::now();
        core.insert("q", vec![1.0, 0.0], 5, vec![result("doc", 1.0)], now);

        assert!(core.lookup("q", None, 5, now).is_some());
        let later = now + Duration::from_millis(51);
        assert!(
            core.lookup("q", None, 5, later).is_none(),
            "expired entries are misses"
        );
    }

    #[test]
    fn insert_replaces_same_query_and_k() {
        let core = SemanticCacheCore::new(SemanticCacheConfig::new());
        let now = Instant::now();
        core.insert("q", vec![1.0], 5, vec![result("old", 1.0)], now);
        core.insert("q", vec![1.0], 5, vec![result("new", 1.0)], now);
        assert_eq!(core.len(), 1, "replace, not duplicate");
        let (results, _) = core.lookup("q", None, 5, now).unwrap();
        assert_eq!(results[0].document.content, "new");
    }

    #[test]
    fn invalidate_clears_all() {
        let core = SemanticCacheCore::new(SemanticCacheConfig::new());
        let now = Instant::now();
        core.insert("q", vec![1.0], 5, vec![], now);
        assert!(!core.is_empty());
        core.invalidate();
        assert!(core.is_empty());
        assert!(core.lookup("q", None, 5, now).is_none());
    }

    /// Decorator: identical query → 1 retrieval call; paraphrase above
    /// threshold → still cached (semantic hit); add_documents invalidates.
    #[tokio::test]
    async fn cached_retriever_skips_inner_on_hits() {
        let inner = Arc::new(CountingRetriever::new(vec![result("doc a", 0.9)]));
        let retriever = CachedRetriever::new(
            inner.clone(),
            Arc::new(AxisEmbeddings),
            SemanticCacheConfig::new(),
        );

        let first = retriever.retrieve("apple", 3).await.unwrap();
        assert_eq!(first.len(), 1);
        assert_eq!(inner.calls.load(Ordering::SeqCst), 1, "miss → inner call");

        let second = retriever.retrieve("apple", 3).await.unwrap();
        assert_eq!(second[0].content, "doc a");
        assert_eq!(
            inner.calls.load(Ordering::SeqCst),
            1,
            "lexical hit → no call"
        );

        // "avocado" embeds to the same axis (starts with 'a') → semantic hit.
        let third = retriever.retrieve("avocado", 3).await.unwrap();
        assert_eq!(third[0].content, "doc a");
        assert_eq!(
            inner.calls.load(Ordering::SeqCst),
            1,
            "semantic hit → no call"
        );

        // Different axis → miss → second inner call.
        let _ = retriever.retrieve("banana", 3).await.unwrap();
        assert_eq!(inner.calls.load(Ordering::SeqCst), 2);

        // k is part of the cache identity: k change → miss → third call.
        let _ = retriever.retrieve("apple", 5).await.unwrap();
        assert_eq!(inner.calls.load(Ordering::SeqCst), 3);

        // add_documents invalidates: "apple" k=3 needs a fresh inner call.
        retriever
            .add_documents(vec![Document::new("new doc")])
            .await
            .unwrap();
        assert!(
            retriever.cache().is_empty(),
            "corpus update invalidates cache"
        );
        let _ = retriever.retrieve("apple", 3).await.unwrap();
        assert_eq!(inner.calls.load(Ordering::SeqCst), 4);
    }

    /// Unknown text (embeddings error) surfaces the error rather than caching.
    #[tokio::test]
    async fn embedding_error_propagates() {
        let inner = Arc::new(CountingRetriever::new(vec![]));
        let retriever =
            CachedRetriever::new(inner, Arc::new(AxisEmbeddings), SemanticCacheConfig::new());
        let err = retriever.retrieve("zebra", 3).await;
        assert!(err.is_err(), "embedding failure must not be swallowed");
    }
}