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
//! End-to-end RAG pipeline integration tests.
//!
//! These tests exercise the full Retrieval-Augmented Generation pipeline:
//! document loading, text splitting, embedding, vector storage, retrieval,
//! and chain-based question answering -- all using fake/in-memory components
//! so no external services are required.

use std::collections::HashMap;
use std::io::Write;
use std::sync::Arc;

use cognis::chains::conversation_retrieval::ConversationalRetrievalChain;
use cognis::chains::retrieval::RetrievalQAChain;
use cognis::document_loaders::text::TextLoader;
use cognis::text_splitter::{CharacterTextSplitter, RecursiveCharacterTextSplitter, TextSplitter};
use cognis::vectorstores::in_memory::InMemoryVectorStore;
use cognis_core::document_loaders::BaseLoader;
use cognis_core::documents::Document;
use cognis_core::embeddings::Embeddings;
use cognis_core::embeddings_fake::DeterministicFakeEmbedding;
use cognis_core::language_models::chat_model::BaseChatModel;
use cognis_core::language_models::fake::{FakeListChatModel, ParrotFakeChatModel};
use cognis_core::vectorstores::base::{SearchType, VectorStore, VectorStoreRetriever};
use serde_json::Value;
use tempfile::NamedTempFile;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn fake_embeddings() -> Arc<dyn Embeddings> {
    Arc::new(DeterministicFakeEmbedding::new(128))
}

fn fake_llm(responses: Vec<&str>) -> Arc<dyn BaseChatModel> {
    Arc::new(FakeListChatModel::new(
        responses.into_iter().map(String::from).collect(),
    ))
}

fn parrot_llm() -> Arc<dyn BaseChatModel> {
    Arc::new(ParrotFakeChatModel::new())
}

fn make_retriever(store: Arc<dyn VectorStore>) -> Arc<VectorStoreRetriever> {
    Arc::new(VectorStoreRetriever::from_vectorstore(store))
}

fn make_retriever_with_k(store: Arc<dyn VectorStore>, k: usize) -> Arc<VectorStoreRetriever> {
    Arc::new(VectorStoreRetriever::new(store, SearchType::Similarity, k))
}

// ---------------------------------------------------------------------------
// 1. Full RAG pipeline with TextLoader
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_full_rag_pipeline_with_text_loader() {
    // Create a temporary text file
    let mut tmp = NamedTempFile::new().unwrap();
    write!(
        tmp,
        "Rust is a systems programming language focused on safety and performance.\n\
         It was first released in 2015.\n\
         Rust uses a borrow checker to ensure memory safety without a garbage collector."
    )
    .unwrap();

    // Step 1: Load the document
    let loader = TextLoader::new(tmp.path());
    let docs = loader.load().await.unwrap();
    assert_eq!(docs.len(), 1);
    assert!(docs[0].page_content.contains("Rust"));

    // Step 2: Split into chunks
    let splitter = RecursiveCharacterTextSplitter::new()
        .with_chunk_size(80)
        .with_chunk_overlap(10);
    let chunks = splitter.split_documents(&docs);
    assert!(
        chunks.len() > 1,
        "Expected multiple chunks, got {}",
        chunks.len()
    );

    // Step 3: Embed and store in InMemoryVectorStore
    let embeddings = fake_embeddings();
    let store = Arc::new(
        InMemoryVectorStore::from_documents(chunks.clone(), embeddings)
            .await
            .unwrap(),
    );

    // Step 4: Verify similarity search works
    let results = store.similarity_search("borrow checker", 2).await.unwrap();
    assert!(!results.is_empty());

    // Step 5: Create RetrievalQAChain and query
    let retriever = make_retriever(store.clone() as Arc<dyn VectorStore>);
    let llm = fake_llm(vec!["Rust uses a borrow checker for memory safety."]);
    let chain = RetrievalQAChain::new(retriever, llm).with_k(2);
    let answer = chain
        .call("How does Rust ensure memory safety?")
        .await
        .unwrap();
    assert_eq!(answer, "Rust uses a borrow checker for memory safety.");
}

// ---------------------------------------------------------------------------
// 2. RAG with multiple documents
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_rag_with_multiple_documents() {
    let docs = vec![
        Document::new("Python is an interpreted, high-level programming language."),
        Document::new("Rust is a systems programming language focused on safety."),
        Document::new("JavaScript is the language of the web browser."),
        Document::new("Go is a statically typed language designed at Google."),
    ];

    let embeddings = fake_embeddings();
    let store = Arc::new(
        InMemoryVectorStore::from_documents(docs, embeddings)
            .await
            .unwrap(),
    );

    // The deterministic fake embeddings are hash-based, so searching for a text
    // that exactly matches a stored document should return that document first.
    let results = store
        .similarity_search(
            "Rust is a systems programming language focused on safety.",
            1,
        )
        .await
        .unwrap();
    assert_eq!(results.len(), 1);
    assert!(results[0].page_content.contains("Rust"));

    // Full chain query
    let retriever = make_retriever_with_k(store.clone() as Arc<dyn VectorStore>, 2);
    let llm = fake_llm(vec!["Rust focuses on safety and performance."]);
    let chain = RetrievalQAChain::new(retriever, llm);
    let result = chain.call_with_sources("Tell me about Rust").await.unwrap();
    assert_eq!(result.answer, "Rust focuses on safety and performance.");
    assert_eq!(result.source_documents.len(), 2);
}

// ---------------------------------------------------------------------------
// 3. RAG with text splitter
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_rag_with_text_splitter() {
    let long_text = "Artificial intelligence is transforming every industry. \
        Machine learning models can now generate text, images, and code. \
        Large language models like GPT and Claude have billions of parameters. \
        Transformer architectures use self-attention mechanisms. \
        Embeddings map words and sentences into dense vector spaces. \
        Retrieval-augmented generation combines search with language models. \
        Vector databases store and index high-dimensional embeddings. \
        Fine-tuning allows adapting pre-trained models to specific tasks.";

    // Split the long text into smaller chunks
    let splitter = CharacterTextSplitter::new()
        .with_separator(". ")
        .with_chunk_size(120)
        .with_chunk_overlap(0);
    let chunks = splitter.split_text(long_text);
    assert!(chunks.len() > 1, "Expected multiple chunks from splitter");

    // Convert chunks into Documents
    let docs: Vec<Document> = chunks.iter().map(|c| Document::new(c.clone())).collect();

    let embeddings = fake_embeddings();
    let store = Arc::new(
        InMemoryVectorStore::from_documents(docs, embeddings)
            .await
            .unwrap(),
    );

    let retriever = make_retriever_with_k(store.clone() as Arc<dyn VectorStore>, 2);
    let llm = fake_llm(vec!["RAG combines search with LLMs."]);
    let chain = RetrievalQAChain::new(retriever, llm).with_k(2);
    let result = chain.call_with_sources("What is RAG?").await.unwrap();

    assert_eq!(result.answer, "RAG combines search with LLMs.");
    assert!(result.source_documents.len() <= 2);
}

// ---------------------------------------------------------------------------
// 4. Conversational RAG multi-turn
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_conversational_rag_multi_turn() {
    let docs = vec![
        Document::new("Rust was created by Graydon Hoare at Mozilla."),
        Document::new("Rust 1.0 was released on May 15, 2015."),
        Document::new("Rust has won Stack Overflow's most loved language award multiple times."),
    ];

    let embeddings = fake_embeddings();
    let store = Arc::new(
        InMemoryVectorStore::from_documents(docs, embeddings)
            .await
            .unwrap(),
    );
    let retriever = make_retriever_with_k(store.clone() as Arc<dyn VectorStore>, 2);

    // Responses: first QA answer, then condensation output, then second QA answer
    let llm = fake_llm(vec![
        "Rust was created by Graydon Hoare.",     // 1st call: QA answer
        "When was Rust 1.0 released?",            // 2nd call: condensation
        "Rust 1.0 was released on May 15, 2015.", // 3rd call: QA answer
    ]);

    let chain = ConversationalRetrievalChain::new(retriever, llm).with_k(2);

    // First turn -- no history, so condensation is skipped
    let r1 = chain.call_with_sources("Who created Rust?").await.unwrap();
    assert_eq!(r1.condensed_question, "Who created Rust?");
    assert_eq!(r1.answer, "Rust was created by Graydon Hoare.");

    // Second turn -- history exists, so question is condensed
    let r2 = chain
        .call_with_sources("When was version 1.0 released?")
        .await
        .unwrap();
    assert_eq!(r2.condensed_question, "When was Rust 1.0 released?");
    assert_eq!(r2.answer, "Rust 1.0 was released on May 15, 2015.");
    assert!(!r2.source_documents.is_empty());
}

// ---------------------------------------------------------------------------
// 5. RAG with metadata filtering
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_rag_with_metadata_filtering() {
    let mut meta_rust = HashMap::new();
    meta_rust.insert("language".to_string(), Value::String("rust".into()));
    meta_rust.insert("category".to_string(), Value::String("systems".into()));

    let mut meta_python = HashMap::new();
    meta_python.insert("language".to_string(), Value::String("python".into()));
    meta_python.insert("category".to_string(), Value::String("scripting".into()));

    let docs = vec![
        Document::new("Rust has zero-cost abstractions.").with_metadata(meta_rust.clone()),
        Document::new("Python is great for data science.").with_metadata(meta_python.clone()),
        Document::new("Rust guarantees memory safety.").with_metadata(meta_rust.clone()),
    ];

    let embeddings = fake_embeddings();
    let store = Arc::new(
        InMemoryVectorStore::from_documents(docs, embeddings)
            .await
            .unwrap(),
    );

    // Search for Rust-related content
    let results = store
        .similarity_search("Rust has zero-cost abstractions.", 2)
        .await
        .unwrap();
    assert_eq!(results.len(), 2);

    // Verify metadata is preserved through the pipeline
    for doc in &results {
        assert!(
            doc.metadata.contains_key("language"),
            "Expected metadata key 'language' on document: {}",
            doc.page_content
        );
        assert!(
            doc.metadata.contains_key("category"),
            "Expected metadata key 'category' on document: {}",
            doc.page_content
        );
    }

    // Use in a chain and verify source metadata is returned
    let retriever = make_retriever_with_k(store.clone() as Arc<dyn VectorStore>, 3);
    let llm = fake_llm(vec!["Rust has zero-cost abstractions and memory safety."]);
    let chain = RetrievalQAChain::new(retriever, llm).with_k(3);
    let result = chain
        .call_with_sources("What are Rust's features?")
        .await
        .unwrap();

    // All source documents should have metadata
    for doc in &result.source_documents {
        assert!(
            !doc.metadata.is_empty(),
            "Source document metadata should not be empty"
        );
    }
}

// ---------------------------------------------------------------------------
// 6. VectorStore add and delete
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_vectorstore_add_and_delete() {
    let embeddings = fake_embeddings();
    let store = InMemoryVectorStore::new(embeddings);

    // Add documents with explicit IDs
    let docs = vec![
        Document::new("Alpha document").with_id("alpha"),
        Document::new("Beta document").with_id("beta"),
        Document::new("Gamma document").with_id("gamma"),
    ];
    let ids = store.add_documents(docs, None).await.unwrap();
    assert_eq!(ids, vec!["alpha", "beta", "gamma"]);

    // Verify all three are searchable
    let all = store.similarity_search("document", 10).await.unwrap();
    assert_eq!(all.len(), 3);

    // Delete "beta"
    let deleted = store.delete(Some(&["beta".to_string()])).await.unwrap();
    assert!(deleted);

    // Verify only two remain
    let remaining = store.similarity_search("document", 10).await.unwrap();
    assert_eq!(remaining.len(), 2);
    assert!(
        remaining.iter().all(|d| d.page_content != "Beta document"),
        "Deleted document should not appear in search results"
    );

    // Verify get_by_ids no longer returns the deleted doc
    let fetched = store.get_by_ids(&["beta".to_string()]).await.unwrap();
    assert!(
        fetched.is_empty(),
        "Deleted document should not be retrievable by ID"
    );

    // The remaining docs should still be retrievable
    let fetched_alpha = store.get_by_ids(&["alpha".to_string()]).await.unwrap();
    assert_eq!(fetched_alpha.len(), 1);
    assert_eq!(fetched_alpha[0].page_content, "Alpha document");
}

// ---------------------------------------------------------------------------
// 7. RAG with empty vectorstore
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_rag_empty_vectorstore() {
    let embeddings = fake_embeddings();
    let store = Arc::new(InMemoryVectorStore::new(embeddings));

    // Verify empty search returns no results
    let results = store.similarity_search("anything", 5).await.unwrap();
    assert!(results.is_empty());

    // Chain should still work -- the LLM gets an empty context
    let retriever = make_retriever(store.clone() as Arc<dyn VectorStore>);
    let llm = fake_llm(vec!["I don't have enough context to answer."]);
    let chain = RetrievalQAChain::new(retriever, llm);

    let result = chain
        .call_with_sources("What is the meaning of life?")
        .await
        .unwrap();
    assert_eq!(result.answer, "I don't have enough context to answer.");
    assert!(result.source_documents.is_empty());

    // Conversational chain should also handle empty store gracefully
    let embeddings2 = fake_embeddings();
    let store2 = Arc::new(InMemoryVectorStore::new(embeddings2));
    let retriever2 = make_retriever(store2.clone() as Arc<dyn VectorStore>);
    let llm2 = fake_llm(vec!["No information available."]);
    let conv_chain = ConversationalRetrievalChain::new(retriever2, llm2);

    let conv_result = conv_chain.call("question?").await.unwrap();
    assert_eq!(conv_result, "No information available.");
}

// ---------------------------------------------------------------------------
// 8. RAG with custom prompt
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_rag_with_custom_prompt() {
    let docs = vec![
        Document::new("The capital of France is Paris."),
        Document::new("France is a country in Western Europe."),
    ];

    let embeddings = fake_embeddings();
    let store = Arc::new(
        InMemoryVectorStore::from_documents(docs, embeddings)
            .await
            .unwrap(),
    );

    let retriever = make_retriever_with_k(store.clone() as Arc<dyn VectorStore>, 2);

    // Use a parrot LLM so we can verify the prompt was formatted correctly
    let llm = parrot_llm();

    let custom_template =
        "You are a geography expert.\n\nRelevant facts:\n{context}\n\nUser question: {query}\n\nProvide a concise answer:";
    let chain = RetrievalQAChain::new(retriever, llm)
        .with_prompt_template(custom_template)
        .with_k(2);

    let answer = chain.call("What is the capital of France?").await.unwrap();

    // The parrot echoes the formatted prompt back, so we can verify template substitution
    assert!(
        answer.contains("You are a geography expert."),
        "Custom prompt prefix should appear in the formatted output"
    );
    assert!(
        answer.contains("User question: What is the capital of France?"),
        "Query should be substituted in the template"
    );
    assert!(
        answer.contains("The capital of France is Paris."),
        "Context from retrieved docs should appear in the prompt"
    );
    assert!(
        answer.contains("Relevant facts:"),
        "Custom template structure should be preserved"
    );
}