cognis 0.2.1

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
//! Time-weighted retriever that scores documents by combining similarity with
//! recency via exponential decay.
//!
//! Documents that were added more recently receive a higher time bonus. The
//! combined score is `similarity + (1.0 - decay_rate).powf(hours_since_access)`.

use std::sync::Arc;
use std::time::SystemTime;

use async_trait::async_trait;
use tokio::sync::RwLock;

use cognis_core::documents::Document;
use cognis_core::embeddings::Embeddings;
use cognis_core::error::Result;
use cognis_core::retrievers::BaseRetriever;

/// A document with associated temporal metadata.
#[derive(Debug, Clone)]
pub struct TimedDocument {
    /// The underlying document.
    pub document: Document,
    /// When this document was added to the store.
    pub created_at: SystemTime,
    /// When this document was last accessed (returned as a result).
    pub last_accessed: SystemTime,
    /// Number of times this document has been accessed.
    pub access_count: usize,
}

impl TimedDocument {
    /// Create a new timed document with current timestamps.
    pub fn new(document: Document) -> Self {
        let now = SystemTime::now();
        Self {
            document,
            created_at: now,
            last_accessed: now,
            access_count: 0,
        }
    }

    /// Create a timed document with specific timestamps (useful for testing).
    pub fn with_timestamps(
        document: Document,
        created_at: SystemTime,
        last_accessed: SystemTime,
    ) -> Self {
        Self {
            document,
            created_at,
            last_accessed,
            access_count: 0,
        }
    }
}

/// A retriever that combines vector similarity with time-based decay to favor
/// more recently accessed documents.
///
/// The combined score for a document is:
///   `similarity + (1.0 - decay_rate).powf(hours_since_last_access)`
///
/// # Example
///
/// ```rust,ignore
/// use cognis::retrievers::time_weighted::TimeWeightedRetriever;
/// use std::sync::Arc;
///
/// let retriever = TimeWeightedRetriever::new(embeddings)
///     .with_decay_rate(0.01)
///     .with_k(4);
///
/// retriever.add_documents(vec![doc1, doc2]).await;
/// let results = retriever.get_relevant_documents("query").await?;
/// ```
pub struct TimeWeightedRetriever {
    /// Embeddings model for computing similarity.
    embeddings: Arc<dyn Embeddings>,
    /// The document store with temporal metadata.
    documents: Arc<RwLock<Vec<TimedDocument>>>,
    /// Exponential decay rate (default: 0.01).
    decay_rate: f64,
    /// Number of documents to return (default: 4).
    k: usize,
}

impl TimeWeightedRetriever {
    /// Create a new time-weighted retriever.
    pub fn new(embeddings: Arc<dyn Embeddings>) -> Self {
        Self {
            embeddings,
            documents: Arc::new(RwLock::new(Vec::new())),
            decay_rate: 0.01,
            k: 4,
        }
    }

    /// Set the decay rate (higher values = faster decay of older documents).
    pub fn with_decay_rate(mut self, decay_rate: f64) -> Self {
        self.decay_rate = decay_rate;
        self
    }

    /// Set the number of results to return.
    pub fn with_k(mut self, k: usize) -> Self {
        self.k = k;
        self
    }

    /// Add documents to the store with the current timestamp.
    pub async fn add_documents(&self, docs: Vec<Document>) {
        let mut store = self.documents.write().await;
        for doc in docs {
            store.push(TimedDocument::new(doc));
        }
    }

    /// Add a single timed document directly (useful for testing or restoring state).
    pub async fn add_timed_document(&self, timed_doc: TimedDocument) {
        let mut store = self.documents.write().await;
        store.push(timed_doc);
    }

    /// Get the number of documents in the store.
    pub async fn len(&self) -> usize {
        self.documents.read().await.len()
    }

    /// Check if the document store is empty.
    pub async fn is_empty(&self) -> bool {
        self.documents.read().await.is_empty()
    }

    /// Compute the combined score from similarity and time decay.
    ///
    /// `combined_score = similarity + (1.0 - decay_rate).powf(hours_since)`
    pub fn combined_score(&self, similarity: f64, hours_since: f64) -> f64 {
        let time_score = (1.0 - self.decay_rate).powf(hours_since);
        similarity + time_score
    }

    /// Compute cosine similarity between two vectors.
    fn cosine_similarity(a: &[f32], b: &[f32]) -> f64 {
        if a.len() != b.len() || a.is_empty() {
            return 0.0;
        }

        let dot: f64 = a
            .iter()
            .zip(b.iter())
            .map(|(x, y)| *x as f64 * *y as f64)
            .sum();
        let norm_a: f64 = a
            .iter()
            .map(|x| (*x as f64) * (*x as f64))
            .sum::<f64>()
            .sqrt();
        let norm_b: f64 = b
            .iter()
            .map(|x| (*x as f64) * (*x as f64))
            .sum::<f64>()
            .sqrt();

        if norm_a == 0.0 || norm_b == 0.0 {
            return 0.0;
        }

        dot / (norm_a * norm_b)
    }

    /// Compute hours elapsed since the given time.
    fn hours_since(time: SystemTime) -> f64 {
        SystemTime::now()
            .duration_since(time)
            .unwrap_or_default()
            .as_secs_f64()
            / 3600.0
    }
}

#[async_trait]
impl BaseRetriever for TimeWeightedRetriever {
    async fn get_relevant_documents(&self, query: &str) -> Result<Vec<Document>> {
        let query_embedding = self.embeddings.embed_query(query).await?;

        let store = self.documents.read().await;
        if store.is_empty() {
            return Ok(vec![]);
        }

        // Embed all document contents.
        let doc_texts: Vec<String> = store
            .iter()
            .map(|td| td.document.page_content.clone())
            .collect();
        let doc_embeddings = self.embeddings.embed_documents(doc_texts).await?;

        // Score each document.
        let mut scored: Vec<(usize, f64)> = store
            .iter()
            .enumerate()
            .zip(doc_embeddings.iter())
            .map(|((idx, timed_doc), embedding)| {
                let similarity = Self::cosine_similarity(&query_embedding, embedding);
                let hours = Self::hours_since(timed_doc.last_accessed);
                let score = self.combined_score(similarity, hours);
                (idx, score)
            })
            .collect();

        // Sort by score descending.
        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        // Take top k.
        let top_indices: Vec<usize> = scored.iter().take(self.k).map(|(idx, _)| *idx).collect();

        drop(store);

        // Update access metadata.
        let mut store = self.documents.write().await;
        let now = SystemTime::now();
        let mut results = Vec::with_capacity(top_indices.len());
        for idx in &top_indices {
            if let Some(timed_doc) = store.get_mut(*idx) {
                timed_doc.last_accessed = now;
                timed_doc.access_count += 1;
                results.push(timed_doc.document.clone());
            }
        }

        Ok(results)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    /// A mock embeddings model that returns simple embeddings based on
    /// document content for testing purposes.
    struct MockEmbeddings;

    #[async_trait]
    impl Embeddings for MockEmbeddings {
        async fn embed_documents(&self, texts: Vec<String>) -> Result<Vec<Vec<f32>>> {
            Ok(texts.iter().map(|t| text_to_embedding(t)).collect())
        }

        async fn embed_query(&self, text: &str) -> Result<Vec<f32>> {
            Ok(text_to_embedding(text))
        }
    }

    /// Convert text to a simple deterministic embedding for testing.
    /// Uses the sum of byte values in different positions to create a 3D vector.
    fn text_to_embedding(text: &str) -> Vec<f32> {
        let bytes = text.as_bytes();
        let dim0: f32 = bytes.iter().map(|b| *b as f32).sum::<f32>() / 1000.0;
        let dim1: f32 = bytes
            .iter()
            .enumerate()
            .map(|(i, b)| (i as f32 + 1.0) * (*b as f32))
            .sum::<f32>()
            / 10000.0;
        let dim2: f32 = bytes.len() as f32 / 100.0;
        vec![dim0, dim1, dim2]
    }

    #[tokio::test]
    async fn test_empty_store_returns_empty() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings);

        let docs = retriever.get_relevant_documents("query").await.unwrap();
        assert!(docs.is_empty());
    }

    #[tokio::test]
    async fn test_add_documents() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings);

        retriever
            .add_documents(vec![Document::new("doc1"), Document::new("doc2")])
            .await;

        assert_eq!(retriever.len().await, 2);
        assert!(!retriever.is_empty().await);
    }

    #[tokio::test]
    async fn test_retrieve_returns_up_to_k() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings).with_k(2);

        retriever
            .add_documents(vec![
                Document::new("alpha"),
                Document::new("beta"),
                Document::new("gamma"),
                Document::new("delta"),
            ])
            .await;

        let docs = retriever.get_relevant_documents("test").await.unwrap();
        assert_eq!(docs.len(), 2);
    }

    #[tokio::test]
    async fn test_retrieve_returns_all_when_fewer_than_k() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings).with_k(10);

        retriever
            .add_documents(vec![Document::new("only_one")])
            .await;

        let docs = retriever.get_relevant_documents("test").await.unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].page_content, "only_one");
    }

    #[tokio::test]
    async fn test_combined_score_calculation() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings).with_decay_rate(0.01);

        // At 0 hours, time component is 1.0
        let score = retriever.combined_score(0.5, 0.0);
        assert!((score - 1.5).abs() < 1e-10);

        // At some hours, time component decays
        let score_later = retriever.combined_score(0.5, 24.0);
        assert!(score_later < score);
        assert!(score_later > 0.5); // Still positive
    }

    #[tokio::test]
    async fn test_high_decay_rate_penalizes_old_documents() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings).with_decay_rate(0.5);

        // With 50% decay rate, after 1 hour the time score is 0.5
        let score = retriever.combined_score(0.0, 1.0);
        assert!((score - 0.5).abs() < 1e-10);

        // After 2 hours: (0.5)^2 = 0.25
        let score2 = retriever.combined_score(0.0, 2.0);
        assert!((score2 - 0.25).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_zero_decay_rate_no_time_penalty() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings).with_decay_rate(0.0);

        // With 0 decay, time component is always 1.0
        let score = retriever.combined_score(0.5, 1000.0);
        assert!((score - 1.5).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_cosine_similarity_identical_vectors() {
        let a = vec![1.0, 2.0, 3.0];
        let similarity = TimeWeightedRetriever::cosine_similarity(&a, &a);
        assert!((similarity - 1.0).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_cosine_similarity_orthogonal_vectors() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![0.0, 1.0, 0.0];
        let similarity = TimeWeightedRetriever::cosine_similarity(&a, &b);
        assert!(similarity.abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_cosine_similarity_empty_vectors() {
        let a: Vec<f32> = vec![];
        let b: Vec<f32> = vec![];
        let similarity = TimeWeightedRetriever::cosine_similarity(&a, &b);
        assert_eq!(similarity, 0.0);
    }

    #[tokio::test]
    async fn test_cosine_similarity_zero_vector() {
        let a = vec![0.0, 0.0, 0.0];
        let b = vec![1.0, 2.0, 3.0];
        let similarity = TimeWeightedRetriever::cosine_similarity(&a, &b);
        assert_eq!(similarity, 0.0);
    }

    #[tokio::test]
    async fn test_access_count_increments() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings).with_k(10);

        retriever.add_documents(vec![Document::new("doc")]).await;

        retriever.get_relevant_documents("q1").await.unwrap();
        retriever.get_relevant_documents("q2").await.unwrap();

        let store = retriever.documents.read().await;
        assert_eq!(store[0].access_count, 2);
    }

    #[tokio::test]
    async fn test_older_documents_scored_lower() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings)
            .with_k(2)
            .with_decay_rate(0.99);

        // Add a document with an old timestamp.
        let old_doc = TimedDocument::with_timestamps(
            Document::new("old_doc"),
            SystemTime::now() - Duration::from_secs(3600 * 24), // 24 hours ago
            SystemTime::now() - Duration::from_secs(3600 * 24),
        );
        retriever.add_timed_document(old_doc).await;

        // Add a recent document with similar content.
        retriever
            .add_documents(vec![Document::new("new_doc")])
            .await;

        let docs = retriever.get_relevant_documents("doc").await.unwrap();
        // The new doc should be ranked first due to less time decay.
        assert_eq!(docs.len(), 2);
        assert_eq!(docs[0].page_content, "new_doc");
    }

    #[tokio::test]
    async fn test_with_k_builder() {
        let embeddings = Arc::new(MockEmbeddings);
        let retriever = TimeWeightedRetriever::new(embeddings).with_k(7);

        retriever
            .add_documents((0..10).map(|i| Document::new(format!("doc{i}"))).collect())
            .await;

        let docs = retriever.get_relevant_documents("test").await.unwrap();
        assert_eq!(docs.len(), 7);
    }

    #[tokio::test]
    async fn test_timed_document_creation() {
        let doc = Document::new("test content");
        let timed = TimedDocument::new(doc.clone());

        assert_eq!(timed.document.page_content, "test content");
        assert_eq!(timed.access_count, 0);

        // created_at and last_accessed should be very close to now.
        let elapsed = timed.created_at.elapsed().unwrap();
        assert!(elapsed < Duration::from_secs(1));
    }
}