seasoning 0.1.4

Embedding and reranking infrastructure with rate limiting and retry logic
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
//! Text embedding execution with retrieval-aware rendering helpers.
//!
//! ```rust,no_run
//! use std::time::Duration;
//!
//! use secrecy::SecretString;
//! use seasoning::EmbeddingProvider;
//! use seasoning::embedding::{
//!     Client, Dialect, EmbedderConfig, EmbeddingInput, EmbeddingRole, ModelFamily,
//!     PreparedEmbeddingInput,
//! };
//!
//! # async fn example() -> seasoning::Result<()> {
//! let client = Client::new(EmbedderConfig {
//!     api_key: Some(SecretString::from("your-api-key")),
//!     base_url: "https://api.deepinfra.com/v1/openai".to_string(),
//!     timeout: Duration::from_secs(30),
//!     dialect: Dialect::DeepInfra,
//!     model_family: ModelFamily::Qwen3,
//!     model: "Qwen/Qwen3-Embedding-0.6B".to_string(),
//!     query_instruction: None,
//!     embedding_dim: 1024,
//!     requests_per_minute: 1000,
//!     max_concurrent_requests: 50,
//!     tokens_per_minute: 1_000_000,
//! })?;
//!
//! let semantic = EmbeddingInput {
//!     role: EmbeddingRole::Query,
//!     text: "memory safety".to_string(),
//!     title: None,
//! };
//! let rendered = client.render_input(&semantic);
//! let _ = rendered;
//!
//! // Tokenize `rendered` with the tokenizer for the target embedding model,
//! // then execute with the resulting token ids.
//! let prepared = vec![PreparedEmbeddingInput::new(vec![1, 2, 3])?];
//! let _ = client.embed(&prepared).await?;
//! # Ok(())
//! # }
//! ```

use std::time::Duration;

use async_trait::async_trait;
use secrecy::SecretString;
use serde::{Deserialize, Serialize};
use tracing::debug;

use crate::EmbeddingProvider;
use crate::Result;
pub use crate::api::PreparedEmbeddingInput;
#[cfg(feature = "local")]
use crate::local::LocalEmbeddingClient;
use crate::reqwestx::{ApiClient, ApiClientConfig};
pub use crate::{
    Dialect, EmbedOutput, EmbeddingInput, EmbeddingRole, ModelFamily, ProviderDialect,
};

/// Configuration for the embedding client.
#[derive(Debug, Clone)]
pub struct EmbedderConfig {
    /// Optional API key for authentication.
    pub api_key: Option<SecretString>,
    /// Base URL for the embedding API endpoint.
    pub base_url: String,
    /// Request timeout duration.
    pub timeout: Duration,
    /// Backend dialect used for execution.
    pub dialect: Dialect,
    /// Retrieval-model family used by rendering helpers.
    pub model_family: ModelFamily,
    /// Model identifier.
    pub model: String,
    /// Optional query instruction or task text used by rendering helpers.
    pub query_instruction: Option<String>,
    /// Dimension of the embedding vectors returned by the model.
    pub embedding_dim: usize,
    /// Maximum number of requests per minute.
    pub requests_per_minute: usize,
    /// Maximum number of concurrent requests allowed.
    pub max_concurrent_requests: usize,
    /// Maximum number of tokens per minute.
    pub tokens_per_minute: u32,
}

#[derive(Clone)]
pub struct Client {
    model_family: ModelFamily,
    query_instruction: Option<String>,
    backend: Backend,
}

#[derive(Clone)]
enum Backend {
    Remote(RemoteClient),
    #[cfg(feature = "local")]
    Local(LocalEmbeddingClient),
}

#[derive(Clone)]
struct RemoteClient {
    client: ApiClient,
    model: String,
    dimension: usize,
    dialect: Dialect,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct EmbeddingRequest<'a> {
    input: Vec<&'a [u32]>,
    model: &'a str,
    encoding_format: &'static str,
    dimensions: usize,
}

/// Internal representation of a single embedding from the API response.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct EmbeddingObject {
    index: usize,
    embedding: Vec<f32>,
}

/// Internal representation of the embedding API response.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct EmbedApiResponse {
    data: Vec<EmbeddingObject>,
}

impl Client {
    pub fn new(config: EmbedderConfig) -> Result<Self> {
        match config.dialect {
            Dialect::OpenAI | Dialect::DeepInfra => {
                let model_family = config.model_family;
                let query_instruction = config.query_instruction.clone();
                let remote = RemoteClient::new(config)?;
                Ok(Self {
                    model_family,
                    query_instruction,
                    backend: Backend::Remote(remote),
                })
            }
            Dialect::LlamaCpp => {
                #[cfg(feature = "local")]
                {
                    Ok(Self {
                        model_family: config.model_family,
                        query_instruction: config.query_instruction,
                        backend: Backend::Local(LocalEmbeddingClient::new(
                            config.model_family,
                            &config.model,
                        )?),
                    })
                }
                #[cfg(not(feature = "local"))]
                {
                    let _ = config;
                    Err(crate::Error::LocalFeatureRequired {
                        dialect: Dialect::LlamaCpp.to_string(),
                    })
                }
            }
        }
    }

    #[must_use]
    pub fn render_input(&self, input: &EmbeddingInput) -> String {
        self.model_family
            .format_embedding_input(input, self.query_instruction.as_deref())
    }

    #[must_use]
    pub fn render_inputs(&self, input: &[EmbeddingInput]) -> Vec<String> {
        input.iter().map(|item| self.render_input(item)).collect()
    }

    fn estimate_token_count(&self, input: &[PreparedEmbeddingInput]) -> u32 {
        input.iter().fold(0u32, |tokens, item| {
            tokens.saturating_add(item.token_count() as u32)
        })
    }
}

impl RemoteClient {
    fn new(config: EmbedderConfig) -> Result<Self> {
        let api_config = ApiClientConfig {
            base_url: config.base_url.clone(),
            api_key: config.api_key.clone(),
            max_concurrent_requests: config.max_concurrent_requests,
            max_requests_per_minute: config.requests_per_minute,
            max_tokens_per_minute: config.tokens_per_minute as usize,
            max_retries: 3,
            timeout: config.timeout,
        };

        let client = ApiClient::new(api_config)?;

        Ok(Self {
            client,
            model: config.model,
            dimension: config.embedding_dim,
            dialect: config.dialect,
        })
    }

    async fn embed_prepared(
        &self,
        prepared: &[PreparedEmbeddingInput],
        estimated_tokens: u32,
    ) -> Result<EmbedOutput> {
        if prepared.is_empty() {
            return Ok(EmbedOutput {
                embeddings: Vec::new(),
            });
        }

        let payload = match self.dialect {
            Dialect::OpenAI | Dialect::DeepInfra => EmbeddingRequest {
                input: prepared
                    .iter()
                    .map(PreparedEmbeddingInput::token_ids)
                    .collect(),
                model: self.model.as_str(),
                encoding_format: "float",
                dimensions: self.dimension,
            },
            Dialect::LlamaCpp => unreachable!("local execution is handled outside RemoteClient"),
        };

        let response: EmbedApiResponse = self
            .client
            .post_json("/embeddings", &payload, estimated_tokens)
            .await?;

        let embeddings = order_embeddings(response.data, prepared.len())?;

        Ok(EmbedOutput { embeddings })
    }
}

fn order_embeddings(items: Vec<EmbeddingObject>, inputs: usize) -> Result<Vec<Vec<f32>>> {
    if items.len() != inputs {
        return Err(crate::Error::EmbeddingCountMismatch {
            embeddings: items.len(),
            inputs,
        });
    }

    let mut embeddings = vec![None; inputs];
    for item in items {
        let slot = embeddings
            .get_mut(item.index)
            .ok_or(crate::Error::InvalidEmbeddingIndex {
                index: item.index,
                inputs,
            })?;
        if slot.is_some() {
            return Err(crate::Error::InvalidEmbeddingIndex {
                index: item.index,
                inputs,
            });
        }
        *slot = Some(item.embedding);
    }

    embeddings
        .into_iter()
        .enumerate()
        .map(|(index, embedding)| {
            embedding.ok_or(crate::Error::InvalidEmbeddingIndex { index, inputs })
        })
        .collect()
}

#[async_trait]
impl EmbeddingProvider for Client {
    async fn embed(&self, input: &[PreparedEmbeddingInput]) -> Result<EmbedOutput> {
        debug!("Embedding input batch_size: {}", input.len());
        let estimated_tokens = self.estimate_token_count(input);

        match &self.backend {
            Backend::Remote(client) => client.embed_prepared(input, estimated_tokens).await,
            #[cfg(feature = "local")]
            Backend::Local(client) => client.embed_prepared(input).await,
        }
    }
}

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

    use secrecy::SecretString;
    use serde_json::json;
    use wiremock::matchers::{body_json, header, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn prepared_input(token_ids: &[u32]) -> PreparedEmbeddingInput {
        PreparedEmbeddingInput::new(token_ids.to_vec()).unwrap()
    }

    #[tokio::test]
    async fn embed_openai_success_reorders_embeddings_from_token_input() {
        let mock_server = MockServer::start().await;
        let input = vec![prepared_input(&[11, 12, 13]), prepared_input(&[21, 22])];

        Mock::given(method("POST"))
            .and(path("/embeddings"))
            .and(body_json(json!({
                "input": [[11, 12, 13], [21, 22]],
                "model": "test-model",
                "encodingFormat": "float",
                "dimensions": 2
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "data": [
                    { "index": 1, "embedding": [0.8, 0.9] },
                    { "index": 0, "embedding": [0.1, 0.2] }
                ]
            })))
            .mount(&mock_server)
            .await;

        let client = Client::new(EmbedderConfig {
            api_key: None,
            base_url: mock_server.uri(),
            timeout: Duration::from_secs(10),
            dialect: Dialect::OpenAI,
            model_family: ModelFamily::Qwen3,
            model: "test-model".to_string(),
            query_instruction: None,
            embedding_dim: 2,
            requests_per_minute: 1000,
            max_concurrent_requests: 10,
            tokens_per_minute: 1_000_000,
        })
        .unwrap();

        let output = client.embed(&input).await.unwrap();
        assert_eq!(output.embeddings, vec![vec![0.1, 0.2], vec![0.8, 0.9]]);
    }

    #[tokio::test]
    async fn embed_deepinfra_success_sets_authorization_header() {
        let mock_server = MockServer::start().await;
        let input = vec![prepared_input(&[5, 8, 13])];

        Mock::given(method("POST"))
            .and(path("/embeddings"))
            .and(header("Authorization", "Bearer test_key"))
            .and(body_json(json!({
                "input": [[5, 8, 13]],
                "model": "test-model",
                "encodingFormat": "float",
                "dimensions": 3
            })))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "data": [
                    { "index": 0, "embedding": [0.2, 0.4, 0.6] }
                ]
            })))
            .mount(&mock_server)
            .await;

        let client = Client::new(EmbedderConfig {
            api_key: Some(SecretString::from("test_key")),
            base_url: mock_server.uri(),
            timeout: Duration::from_secs(10),
            dialect: Dialect::DeepInfra,
            model_family: ModelFamily::Qwen3,
            model: "test-model".to_string(),
            query_instruction: None,
            embedding_dim: 3,
            requests_per_minute: 1000,
            max_concurrent_requests: 10,
            tokens_per_minute: 1_000_000,
        })
        .unwrap();

        let output = client.embed(&input).await.unwrap();
        assert_eq!(output.embeddings, vec![vec![0.2, 0.4, 0.6]]);
    }

    #[test]
    fn embedder_new_should_not_panic_on_invalid_api_key() {
        let result = panic::catch_unwind(|| {
            let _ = Client::new(EmbedderConfig {
                api_key: Some(SecretString::from("bad\nkey")),
                base_url: "http://127.0.0.1:1".to_string(),
                timeout: Duration::from_secs(1),
                dialect: Dialect::OpenAI,
                model_family: ModelFamily::Qwen3,
                model: "test-model".to_string(),
                query_instruction: None,
                embedding_dim: 2,
                requests_per_minute: 1000,
                max_concurrent_requests: 300,
                tokens_per_minute: 1,
            });
        });

        assert!(
            result.is_ok(),
            "Client::new should return Err, not panic, for invalid API keys"
        );
    }

    #[test]
    fn render_input_uses_client_query_instruction() {
        let client = Client::new(EmbedderConfig {
            api_key: None,
            base_url: "http://127.0.0.1:1".to_string(),
            timeout: Duration::from_secs(1),
            dialect: Dialect::OpenAI,
            model_family: ModelFamily::Qwen3,
            model: "test-model".to_string(),
            query_instruction: Some("custom instruction".to_string()),
            embedding_dim: 2,
            requests_per_minute: 1,
            max_concurrent_requests: 1,
            tokens_per_minute: 1,
        })
        .unwrap();

        let rendered = client.render_input(&EmbeddingInput {
            role: EmbeddingRole::Query,
            text: "rust ownership".to_string(),
            title: None,
        });

        assert_eq!(
            rendered,
            "Instruct: custom instruction\nQuery: rust ownership"
        );
    }

    #[cfg(not(feature = "local"))]
    #[test]
    fn llama_cpp_requires_local_feature() {
        let result = Client::new(EmbedderConfig {
            api_key: None,
            base_url: String::new(),
            timeout: Duration::from_secs(1),
            dialect: Dialect::LlamaCpp,
            model_family: ModelFamily::Gemma,
            model: "hf:ggml-org/embeddinggemma-300M-GGUF/embeddinggemma-300M-Q8_0.gguf".to_string(),
            query_instruction: None,
            embedding_dim: 768,
            requests_per_minute: 1,
            max_concurrent_requests: 1,
            tokens_per_minute: 1,
        });

        assert!(matches!(
            result,
            Err(crate::Error::LocalFeatureRequired { .. })
        ));
    }

    #[cfg(feature = "local")]
    #[test]
    fn local_embedder_rejects_unsupported_model_for_family() {
        let result = Client::new(EmbedderConfig {
            api_key: None,
            base_url: String::new(),
            timeout: Duration::from_secs(1),
            dialect: Dialect::LlamaCpp,
            model_family: ModelFamily::Gemma,
            model: "hf:example/unsupported.gguf".to_string(),
            query_instruction: None,
            embedding_dim: 768,
            requests_per_minute: 1,
            max_concurrent_requests: 1,
            tokens_per_minute: 1,
        });

        assert!(matches!(
            result,
            Err(crate::Error::UnsupportedLocalModel {
                kind: "embedding",
                ..
            })
        ));
    }

    #[test]
    fn order_embeddings_rejects_out_of_range_index() {
        let err = order_embeddings(
            vec![EmbeddingObject {
                index: 2,
                embedding: vec![0.1, 0.2],
            }],
            1,
        )
        .unwrap_err();

        assert!(matches!(
            err,
            crate::Error::InvalidEmbeddingIndex {
                index: 2,
                inputs: 1
            }
        ));
    }

    #[test]
    fn order_embeddings_rejects_duplicate_index() {
        let err = order_embeddings(
            vec![
                EmbeddingObject {
                    index: 0,
                    embedding: vec![0.1, 0.2],
                },
                EmbeddingObject {
                    index: 0,
                    embedding: vec![0.3, 0.4],
                },
            ],
            2,
        )
        .unwrap_err();

        assert!(matches!(
            err,
            crate::Error::InvalidEmbeddingIndex {
                index: 0,
                inputs: 2
            }
        ));
    }

    #[test]
    fn order_embeddings_rejects_count_mismatch() {
        let err = order_embeddings(
            vec![
                EmbeddingObject {
                    index: 0,
                    embedding: vec![0.1, 0.2],
                },
                EmbeddingObject {
                    index: 1,
                    embedding: vec![0.3, 0.4],
                },
                EmbeddingObject {
                    index: 2,
                    embedding: vec![0.5, 0.6],
                },
            ],
            4,
        )
        .unwrap_err();

        assert!(matches!(
            err,
            crate::Error::EmbeddingCountMismatch {
                embeddings: 3,
                inputs: 4
            }
        ));
    }
}