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
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
// 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.

//! Integration tests for the reranking feature.
//!
//! Tests that require a real reranking model are gated behind the
//! `EMBELLAMA_TEST_RERANK_MODEL` environment variable.
//! Use a cross-encoder GGUF model such as bge-reranker-v2-m3,
//! jina-reranker-v1-turbo-en, or jina-reranker-v2-base-multilingual.

mod common;

use embellama::{EmbeddingEngine, EngineConfig, NormalizationMode, PoolingStrategy, RerankResult};
use serial_test::serial;
use std::path::PathBuf;

/// Gets the path to a reranking test model if available
fn get_rerank_model_path() -> Option<PathBuf> {
    std::env::var("EMBELLAMA_TEST_RERANK_MODEL")
        .ok()
        .map(PathBuf::from)
        .filter(|p| p.exists())
}

/// Checks if reranking model tests should be run
fn should_run_rerank_tests() -> bool {
    get_rerank_model_path().is_some()
}

// ============================================================================
// Unit-style tests (no model required)
// ============================================================================

#[test]
fn test_rerank_result_struct() {
    let result = RerankResult {
        index: 2,
        relevance_score: 0.95,
    };
    assert_eq!(result.index, 2);
    assert!((result.relevance_score - 0.95).abs() < f32::EPSILON);
}

#[test]
fn test_rerank_result_serialization() {
    let result = RerankResult {
        index: 0,
        relevance_score: 0.75,
    };
    let json = serde_json::to_string(&result).unwrap();
    assert!(json.contains("\"index\":0"));
    assert!(json.contains("\"relevance_score\":0.75"));

    // Deserialize back
    let deserialized: RerankResult = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.index, result.index);
    assert!((deserialized.relevance_score - result.relevance_score).abs() < f32::EPSILON);
}

#[test]
fn test_rerank_result_equality() {
    let a = RerankResult {
        index: 1,
        relevance_score: 0.5,
    };
    let b = RerankResult {
        index: 1,
        relevance_score: 0.5,
    };
    assert_eq!(a, b);
}

#[test]
fn test_pooling_strategy_rank_is_not_default() {
    assert_ne!(PoolingStrategy::default(), PoolingStrategy::Rank);
    assert_eq!(PoolingStrategy::default(), PoolingStrategy::Mean);
}

#[test]
fn test_pooling_strategy_rank_serde() {
    let strategy = PoolingStrategy::Rank;
    let json = serde_json::to_string(&strategy).unwrap();
    let deserialized: PoolingStrategy = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, PoolingStrategy::Rank);
}

#[test]
fn test_sigmoid_normalization_properties() {
    // Sigmoid properties: f(0) = 0.5, f(large) -> 1, f(-large) -> 0
    let sigmoid = |x: f32| 1.0 / (1.0 + (-x).exp());

    assert!((sigmoid(0.0) - 0.5).abs() < 1e-6);
    assert!(sigmoid(10.0) > 0.999);
    assert!(sigmoid(-10.0) < 0.001);
    assert!(sigmoid(100.0) > 0.999_999);
    assert!(sigmoid(-100.0) < 0.000_001);

    // Monotonically increasing
    assert!(sigmoid(-1.0) < sigmoid(0.0));
    assert!(sigmoid(0.0) < sigmoid(1.0));
    assert!(sigmoid(1.0) < sigmoid(2.0));
}

#[test]
fn test_engine_config_with_rank_pooling() {
    let (_dir, model_path) = common::create_dummy_model();

    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker")
        .with_pooling_strategy(PoolingStrategy::Rank)
        .with_normalization_mode(NormalizationMode::None)
        .build()
        .unwrap();

    assert_eq!(
        config.model_config.pooling_strategy,
        Some(PoolingStrategy::Rank)
    );
    assert_eq!(
        config.model_config.normalization_mode,
        Some(NormalizationMode::None)
    );
}

// ============================================================================
// Integration tests (require real reranking model)
// ============================================================================

#[test]
#[serial]
fn test_rerank_basic() {
    if !should_run_rerank_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_RERANK_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = get_rerank_model_path().unwrap();
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker")
        .with_pooling_strategy(PoolingStrategy::Rank)
        .with_normalization_mode(NormalizationMode::None)
        .with_n_seq_max(4)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    let query = "What is the capital of France?";
    let documents = [
        "Paris is the capital and largest city of France.",
        "Berlin is the capital of Germany.",
        "The weather is nice today.",
    ];

    let results = engine
        .rerank(Some("reranker"), query, &documents, None, true)
        .unwrap();

    // Should return all 3 documents
    assert_eq!(results.len(), 3);

    // Results should be sorted by relevance (descending)
    for i in 1..results.len() {
        assert!(
            results[i - 1].relevance_score >= results[i].relevance_score,
            "Results should be sorted descending: {} >= {}",
            results[i - 1].relevance_score,
            results[i].relevance_score
        );
    }

    // The most relevant document should be about Paris (index 0)
    assert_eq!(results[0].index, 0, "Paris document should be ranked first");

    // All normalized scores should be in [0, 1]
    for r in &results {
        assert!(
            r.relevance_score >= 0.0 && r.relevance_score <= 1.0,
            "Normalized score should be in [0, 1]: {}",
            r.relevance_score
        );
    }
}

#[test]
#[serial]
fn test_rerank_top_n() {
    if !should_run_rerank_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_RERANK_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = get_rerank_model_path().unwrap();
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker")
        .with_pooling_strategy(PoolingStrategy::Rank)
        .with_normalization_mode(NormalizationMode::None)
        .with_n_seq_max(4)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    let query = "machine learning";
    let documents = [
        "Deep learning is a subset of machine learning.",
        "The stock market rose today.",
        "Neural networks power modern AI.",
        "I went to the grocery store.",
    ];

    let results = engine
        .rerank(Some("reranker"), query, &documents, Some(2), true)
        .unwrap();

    assert_eq!(results.len(), 2, "Should return only top 2 results");

    // Top results should be sorted descending
    assert!(results[0].relevance_score >= results[1].relevance_score);
}

#[test]
#[serial]
fn test_rerank_without_normalization() {
    if !should_run_rerank_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_RERANK_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = get_rerank_model_path().unwrap();
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker")
        .with_pooling_strategy(PoolingStrategy::Rank)
        .with_normalization_mode(NormalizationMode::None)
        .with_n_seq_max(4)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    let query = "What is the capital of France?";
    let documents = ["Paris is the capital of France.", "Berlin is in Germany."];

    let results = engine
        .rerank(Some("reranker"), query, &documents, None, false)
        .unwrap();

    // Raw scores can be any value (not necessarily in [0, 1])
    assert_eq!(results.len(), 2);

    // Still sorted descending
    assert!(results[0].relevance_score >= results[1].relevance_score);
}

#[test]
#[serial]
fn test_rerank_empty_documents_returns_empty() {
    if !should_run_rerank_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_RERANK_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = get_rerank_model_path().unwrap();
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker")
        .with_pooling_strategy(PoolingStrategy::Rank)
        .with_normalization_mode(NormalizationMode::None)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    let documents: &[&str] = &[];
    let results = engine
        .rerank(Some("reranker"), "query", documents, None, true)
        .unwrap();

    assert!(results.is_empty());
}

#[test]
#[serial]
fn test_rerank_single_document() {
    if !should_run_rerank_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_RERANK_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = get_rerank_model_path().unwrap();
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker")
        .with_pooling_strategy(PoolingStrategy::Rank)
        .with_normalization_mode(NormalizationMode::None)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    let results = engine
        .rerank(
            Some("reranker"),
            "test query",
            &["single document"],
            None,
            true,
        )
        .unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].index, 0);
    assert!(results[0].relevance_score >= 0.0 && results[0].relevance_score <= 1.0);
}

#[test]
#[serial]
fn test_rerank_batch_exceeds_n_seq_max() {
    if !should_run_rerank_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_RERANK_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = get_rerank_model_path().unwrap();
    // Use n_seq_max=2 to force chunking with 5 documents
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker")
        .with_pooling_strategy(PoolingStrategy::Rank)
        .with_normalization_mode(NormalizationMode::None)
        .with_n_seq_max(2)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    let query = "What is machine learning?";
    let documents = [
        "Machine learning is a branch of AI.",
        "The sun is a star.",
        "Deep learning uses neural networks.",
        "I like pizza.",
        "Supervised learning uses labeled data.",
    ];

    let results = engine
        .rerank(Some("reranker"), query, &documents, None, true)
        .unwrap();

    // All 5 documents should be scored despite n_seq_max=2
    assert_eq!(results.len(), 5);

    // Each original index should appear exactly once
    let mut seen_indices: Vec<usize> = results.iter().map(|r| r.index).collect();
    seen_indices.sort();
    assert_eq!(seen_indices, vec![0, 1, 2, 3, 4]);
}

#[test]
#[serial]
fn test_embed_on_rank_model_fails() {
    if !should_run_rerank_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_RERANK_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = get_rerank_model_path().unwrap();
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker")
        .with_pooling_strategy(PoolingStrategy::Rank)
        .with_normalization_mode(NormalizationMode::None)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    // embed() on a Rank model should fail
    let result = engine.embed(Some("reranker"), "test text");
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("PoolingStrategy::Rank")
    );
}

#[test]
#[serial]
fn test_rerank_on_embedding_model_fails() {
    if !common::should_run_model_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = common::get_test_model_path().unwrap();
    // Load an embedding model with Mean pooling
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("embedder")
        .with_pooling_strategy(PoolingStrategy::Mean)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    // rerank() on a non-Rank model should fail
    let result = engine.rerank(Some("embedder"), "query", &["document"], None, true);
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("PoolingStrategy::Rank")
    );
}

// ============================================================================
// Auto-detection tests (require real reranking model with GGUF pooling_type=4)
// ============================================================================

#[test]
#[serial]
fn test_rerank_auto_detect_from_gguf() {
    if !should_run_rerank_tests() {
        eprintln!("Skipping: EMBELLAMA_TEST_RERANK_MODEL not set");
        return;
    }
    common::init_test_logger();

    let model_path = get_rerank_model_path().unwrap();

    // Do NOT set pooling_strategy or normalization_mode — let auto-detection work
    let config = EngineConfig::builder()
        .with_model_path(&model_path)
        .with_model_name("reranker-auto")
        .with_n_seq_max(4)
        .build()
        .unwrap();

    let engine = EmbeddingEngine::new(config).unwrap();

    let query = "What is the capital of France?";
    let documents = [
        "Paris is the capital and largest city of France.",
        "Berlin is the capital of Germany.",
        "The weather is nice today.",
    ];

    // rerank() should work without explicit PoolingStrategy::Rank
    // because GGUF metadata pooling_type=4 is auto-detected
    let results = engine
        .rerank(Some("reranker-auto"), query, &documents, None, true)
        .unwrap();

    assert_eq!(results.len(), 3);

    // Results should be sorted by relevance (descending)
    for i in 1..results.len() {
        assert!(
            results[i - 1].relevance_score >= results[i].relevance_score,
            "Results should be sorted descending"
        );
    }

    // The most relevant document should be about Paris (index 0)
    assert_eq!(results[0].index, 0, "Paris document should be ranked first");
}