embellama 0.10.1

High-performance Rust library for generating text embeddings using llama-cpp
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
// Copyright 2025 Embellama Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests for context size auto-detection and utilization
//!
//! These tests verify that:
//! 1. GGUF metadata extraction correctly reads context sizes
//! 2. Models auto-detect context size from GGUF when not explicitly configured
//! 3. Models can utilize their full context size
//! 4. Proper errors are returned when context size is exceeded

use embellama::{EmbeddingEngine, EngineConfig, extract_gguf_metadata};
use serial_test::serial;
use std::path::PathBuf;

mod common;

/// Get the Jina test model path
fn get_jina_model_path() -> Option<PathBuf> {
    let path = PathBuf::from(
        std::env::var("HOME").unwrap()
            + "/Library/Caches/roothalia/models/jina-embeddings-v2-base-code-Q4_K_M.gguf",
    );
    if path.exists() {
        Some(path)
    } else {
        // Also try the EMBELLAMA_TEST_MODEL env var
        std::env::var("EMBELLAMA_TEST_MODEL")
            .ok()
            .map(PathBuf::from)
            .filter(|p| p.exists() && p.to_string_lossy().contains("jina"))
    }
}

// ============================================================================
// 1. GGUF Metadata Extraction Tests
// ============================================================================

#[test]
fn test_extract_gguf_metadata_jina_model() {
    common::init_test_logger();

    let Some(model_path) = get_jina_model_path() else {
        eprintln!("⚠️  Skipping test: Jina model not found");
        eprintln!(
            "   Expected at: ~/Library/Caches/roothalia/models/jina-embeddings-v2-base-code-Q4_K_M.gguf"
        );
        return;
    };

    println!(
        "Testing GGUF metadata extraction from: {}",
        model_path.display()
    );

    let result = extract_gguf_metadata(&model_path);
    assert!(
        result.is_ok(),
        "Failed to extract GGUF metadata: {:?}",
        result.err()
    );

    let metadata = result.unwrap();

    // PRIMARY TEST: Jina model should have 8192 context size
    assert_eq!(
        metadata.context_size, 8192,
        "Expected Jina model to have 8192 context size from GGUF metadata"
    );
    println!(
        "✓ Successfully extracted context size: {}",
        metadata.context_size
    );

    // SECONDARY: Dimensions (optional - model.n_embd() is the source of truth)
    println!(
        "  Dimensions from GGUF: {} ({})",
        metadata.embedding_dimensions,
        if metadata.embedding_dimensions > 0 {
            "found"
        } else {
            "not found - will use model.n_embd()"
        }
    );
}

#[test]
fn test_extract_gguf_metadata_invalid_file() {
    use tempfile::NamedTempFile;

    // Create a file that's not a valid GGUF
    let temp_file = NamedTempFile::new().unwrap();
    std::fs::write(temp_file.path(), b"not a gguf file").unwrap();

    let result = extract_gguf_metadata(temp_file.path());
    assert!(result.is_err(), "Should fail on invalid GGUF file");

    println!("✓ Correctly rejects invalid GGUF file");
}

// ============================================================================
// 2. Context Size Auto-Detection Tests
// ============================================================================

#[test]
#[serial]
fn test_model_autodetect_context_size_from_gguf() {
    common::init_test_logger();

    let Some(model_path) = get_jina_model_path() else {
        eprintln!("⚠️  Skipping test: Jina model not found");
        return;
    };

    println!("Testing auto-detection of context size from GGUF");

    // Create config WITHOUT explicit n_ctx - should auto-detect from GGUF
    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("jina-test")
        // Note: NOT setting with_context_size() or with_n_ctx()
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // The engine should have detected 8192 from GGUF metadata
    // We can verify this by checking the model's max_sequence_length
    // Note: We'll need to expose this through the engine API or test via embedding

    println!("✓ Engine created successfully with auto-detected context size");

    // Try to generate an embedding to ensure it works
    let embedding = engine.embed(Some("jina-test"), "Test text").unwrap();
    assert!(!embedding.is_empty());

    println!("✓ Auto-detected context size allows embeddings to work");

    engine.cleanup_thread_models();
}

#[test]
#[serial]
fn test_model_explicit_config_overrides_gguf() {
    common::init_test_logger();

    let Some(model_path) = get_jina_model_path() else {
        eprintln!("⚠️  Skipping test: Jina model not found");
        return;
    };

    println!("Testing that explicit config overrides GGUF metadata");

    // Explicitly set context size to 2048 (even though Jina GGUF says 8192)
    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("jina-test")
        .with_context_size(2048) // Explicit override (increased from 512 to accommodate embedding overhead)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Generate some embeddings to verify it works with the explicit size
    let embedding = engine.embed(Some("jina-test"), "Test text").unwrap();
    assert!(!embedding.is_empty());

    println!("✓ Explicit config successfully overrides GGUF metadata");

    engine.cleanup_thread_models();
}

// ============================================================================
// 3. Context Size Utilization Tests
// ============================================================================

#[test]
#[serial]
fn test_embedding_near_context_limit() {
    common::init_test_logger();

    let Some(model_path) = get_jina_model_path() else {
        eprintln!("⚠️  Skipping test: Jina model not found");
        return;
    };

    println!("Testing embedding generation near effective token limit");

    // Default n_batch=min(ctx,2048)=2048, n_seq_max=2 for encoder models,
    // so effective_max = 2048/2 - 2 = 1022 tokens.
    // Generate text safely under that limit (~900 tokens).
    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("jina-test")
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Aim for ~900 tokens, safely under effective_max of 1022
    let target_tokens = 900;
    let chars_needed = target_tokens * 13 / 10;
    let large_text = "The quick brown fox jumps over the lazy dog. This is a test of the embedding system with a very long context. "
        .repeat(chars_needed / 110);

    println!(
        "Generated text with approximately {} characters (target: ~{} tokens)",
        large_text.len(),
        target_tokens
    );

    // This should succeed within the effective token limit
    let result = engine.embed(Some("jina-test"), &large_text);

    match &result {
        Ok(embedding) => {
            // Verify embedding is valid
            let norm: f32 = embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
            assert!(
                norm > 0.1,
                "Embedding norm should be non-zero, got: {}",
                norm
            );
            println!(
                "✓ Successfully generated embedding for large text (norm: {:.4})",
                norm
            );
        }
        Err(e) => {
            panic!(
                "Failed to generate embedding for text near context limit: {:?}",
                e
            );
        }
    }

    engine.cleanup_thread_models();
}

// ============================================================================
// 4. Context Size Overflow Tests
// ============================================================================

#[test]
#[serial]
fn test_embedding_exceeds_context_size() {
    common::init_test_logger();

    let Some(model_path) = get_jina_model_path() else {
        eprintln!("⚠️  Skipping test: Jina model not found");
        return;
    };

    println!("Testing error handling when text exceeds context limit");

    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("jina-test")
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Generate text that will definitely exceed 8192 tokens
    // Aim for ~8500 tokens to be sure
    let target_tokens = 8500;
    let chars_needed = target_tokens * 13 / 10;
    let oversized_text = "The quick brown fox jumps over the lazy dog. This is a test of the embedding system with an extremely long context that should exceed the limit. "
        .repeat(chars_needed / 150);

    println!(
        "Generated text with approximately {} characters (target: ~{} tokens)",
        oversized_text.len(),
        target_tokens
    );

    // This should fail with InvalidInput error
    let result = engine.embed(Some("jina-test"), &oversized_text);

    assert!(result.is_err(), "Expected error for oversized text");

    let err = result.unwrap_err();
    let err_msg = format!("{:?}", err);

    // Verify the error message contains useful information
    assert!(
        err_msg.contains("exceeds")
            || err_msg.contains("token limit")
            || err_msg.contains("InvalidInput"),
        "Error should mention token limit. Got: {}",
        err_msg
    );

    println!(
        "✓ Correctly rejected oversized text with error: {}",
        err_msg
    );

    engine.cleanup_thread_models();
}

#[test]
#[serial]
fn test_embedding_at_exact_boundary() {
    common::init_test_logger();

    let Some(model_path) = get_jina_model_path() else {
        eprintln!("⚠️  Skipping test: Jina model not found");
        return;
    };

    println!("Testing boundary conditions with context_size=100");

    // Use a smaller context for precise boundary testing.
    // Set n_seq_max=1 so effective_max = n_batch - 2 = 98 tokens
    // (with n_seq_max=2, effective_max would be 100/2 - 2 = 48).
    let test_context_size = 100;
    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("jina-test")
        .with_context_size(test_context_size)
        .with_n_seq_max(1)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Test text that's under the effective limit (98 tokens)
    let safe_text = "word ".repeat(90); // ~90 tokens, under effective_max of 98
    let result = engine.embed(Some("jina-test"), &safe_text);
    assert!(result.is_ok(), "Text under limit should succeed");
    println!("✓ Text under limit succeeded");

    // Test text that exceeds the limit
    let oversized_text = "word ".repeat(150); // Definitely over 98 tokens
    let result = engine.embed(Some("jina-test"), &oversized_text);
    assert!(result.is_err(), "Text over limit should fail");
    println!("✓ Text over limit correctly rejected");

    engine.cleanup_thread_models();
}

#[test]
#[serial]
fn test_batch_with_large_contexts() {
    common::init_test_logger();

    let Some(model_path) = get_jina_model_path() else {
        eprintln!("⚠️  Skipping test: Jina model not found");
        return;
    };

    println!("Testing batch processing with large contexts");

    // Default n_batch=2048, n_seq_max=2 for encoder models,
    // so effective_max per sequence = 2048/2 - 2 = 1022 tokens.
    // Use texts safely under that limit (~900 tokens each).
    let config = EngineConfig::builder()
        .with_model_path(model_path)
        .with_model_name("jina-test")
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).expect("Failed to create engine");

    // Create 3 texts, each around 900 tokens (under effective_max of 1022)
    let target_tokens = 900;
    let chars_needed = target_tokens * 13 / 10;
    let large_text = "The quick brown fox jumps over the lazy dog in this batch test. "
        .repeat(chars_needed / 65);

    let texts: Vec<&str> = vec![&large_text, &large_text, &large_text];

    println!(
        "Testing batch of {} texts, each ~{} tokens",
        texts.len(),
        target_tokens
    );

    let result = engine.embed_batch(Some("jina-test"), &texts);

    assert!(
        result.is_ok(),
        "Batch with large contexts should succeed: {:?}",
        result.err()
    );

    let embeddings = result.unwrap();
    assert_eq!(embeddings.len(), 3, "Should get 3 embeddings");

    for (i, embedding) in embeddings.iter().enumerate() {
        let norm: f32 = embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
        assert!(norm > 0.1, "Embedding {} should have non-zero norm", i);
    }

    println!("✓ Successfully processed batch with large contexts");

    engine.cleanup_thread_models();
}