adk-rag 3.0.0

Retrieval-Augmented Generation for Rust Agent Development Kit (ADK-Rust) agents
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
//! In-process sentence embeddings on the ONNX runtime (feature `onnx`; spec 3.1 §7.15.2,
//! §10 R1).
//!
//! [`OnnxEmbeddingProvider`] runs a sentence-transformer ONNX model — `BAAI/bge-small-en-v1.5`
//! by default, 384 dimensions — on ONNX Runtime through `ort`, at the one version every ADK-Rust
//! crate pins. The model and its tokenizer are fetched once into the Hugging Face hub cache
//! (`HF_HOME`, else `~/.cache/huggingface`) and read from there afterwards, so embedding needs
//! no network and no provider key. It is the recall embedder of the fully local harness.
//!
//! | Step | Default |
//! |---|---|
//! | Tokenize | the model's `tokenizer.json`, truncated to `max_tokens` (512), padded to the longest text of the batch |
//! | Run | `input_ids`, `attention_mask` and, when the model declares it, `token_type_ids` |
//! | Pool | mean of the last hidden states over the attention mask ([`OnnxPooling::Mean`]); [`OnnxPooling::Cls`] takes the first token |
//! | Normalize | L2, so dot products are cosine similarities |
//!
//! # Example
//!
//! ```rust,no_run
//! use adk_rag::{EmbeddingProvider, OnnxEmbeddingConfig, OnnxEmbeddingProvider};
//!
//! # async fn run() -> adk_rag::Result<()> {
//! let provider = OnnxEmbeddingProvider::new(OnnxEmbeddingConfig::default())?;
//! let vector = provider.embed("the tests need --features sqlite").await?;
//! assert_eq!(vector.len(), 384);
//! # Ok(())
//! # }
//! ```

use std::path::{Path, PathBuf};
use std::sync::Mutex;

use async_trait::async_trait;
use ort::session::Session;
use ort::session::builder::GraphOptimizationLevel;
use ort::value::Value;
use tokenizers::{PaddingParams, Tokenizer, TruncationParams};

use crate::embedding::EmbeddingProvider;
use crate::error::{RagError, Result};

/// The default model: `BAAI/bge-small-en-v1.5`, 384 dimensions.
pub const DEFAULT_MODEL_ID: &str = "BAAI/bge-small-en-v1.5";
/// The default model's dimensionality.
pub const DEFAULT_DIMENSIONS: usize = 384;
/// The ONNX file inside the model repository.
pub const DEFAULT_MODEL_FILE: &str = "onnx/model.onnx";
/// The tokenizer file inside the model repository.
pub const DEFAULT_TOKENIZER_FILE: &str = "tokenizer.json";

const PROVIDER: &str = "ONNX";

fn error(message: impl Into<String>) -> RagError {
    RagError::EmbeddingError { provider: PROVIDER.into(), message: message.into() }
}

/// How the token states become one vector.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OnnxPooling {
    /// The mean of the last hidden states over the attention mask.
    #[default]
    Mean,
    /// The first (`[CLS]`) token's state, which BGE models are trained on.
    Cls,
}

/// Configuration of an [`OnnxEmbeddingProvider`].
///
/// # Example
///
/// ```rust
/// use adk_rag::{OnnxEmbeddingConfig, OnnxPooling};
///
/// let config = OnnxEmbeddingConfig::default();
/// assert_eq!(config.model_id, "BAAI/bge-small-en-v1.5");
/// assert_eq!((config.dimensions, config.pooling, config.normalize), (384, OnnxPooling::Mean, true));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OnnxEmbeddingConfig {
    /// The Hugging Face repository of the model.
    pub model_id: String,
    /// The ONNX file inside the repository (or `local_dir`).
    pub model_file: String,
    /// The tokenizer file inside the repository (or `local_dir`).
    pub tokenizer_file: String,
    /// A directory holding the two files, instead of the hub cache: nothing is fetched.
    pub local_dir: Option<PathBuf>,
    /// The vectors' dimensionality; the model's hidden size must match.
    pub dimensions: usize,
    /// How token states become one vector.
    pub pooling: OnnxPooling,
    /// Whether vectors are L2-normalised.
    pub normalize: bool,
    /// The most tokens of one text; longer texts are truncated.
    pub max_tokens: usize,
    /// Threads the runtime uses within one run; `None` is the runtime's default.
    pub threads: Option<usize>,
}

impl Default for OnnxEmbeddingConfig {
    fn default() -> Self {
        Self {
            model_id: DEFAULT_MODEL_ID.into(),
            model_file: DEFAULT_MODEL_FILE.into(),
            tokenizer_file: DEFAULT_TOKENIZER_FILE.into(),
            local_dir: None,
            dimensions: DEFAULT_DIMENSIONS,
            pooling: OnnxPooling::Mean,
            normalize: true,
            max_tokens: 512,
            threads: None,
        }
    }
}

impl OnnxEmbeddingConfig {
    /// The default files of the hub repository `model_id`, with `dimensions`.
    pub fn new(model_id: impl Into<String>, dimensions: usize) -> Self {
        Self { model_id: model_id.into(), dimensions, ..Self::default() }
    }

    /// Reads the model and tokenizer files from `dir` instead of the hub cache.
    #[must_use]
    pub fn with_local_dir(mut self, dir: impl Into<PathBuf>) -> Self {
        self.local_dir = Some(dir.into());
        self
    }

    /// Whether the model and tokenizer files are on this machine, in `local_dir` or the
    /// Hugging Face hub cache, so [`OnnxEmbeddingProvider::new`] fetches nothing. A caller
    /// that is about to wait on a first fetch (about 130 MB for the default model) can say
    /// so first.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_rag::OnnxEmbeddingConfig;
    ///
    /// let config = OnnxEmbeddingConfig::default().with_local_dir("/nonexistent");
    /// assert!(!config.is_fetched());
    /// ```
    pub fn is_fetched(&self) -> bool {
        match &self.local_dir {
            Some(dir) => {
                dir.join(&self.model_file).is_file() && dir.join(&self.tokenizer_file).is_file()
            }
            None => {
                let repo = hf_hub::Cache::from_env().model(self.model_id.clone());
                repo.get(&self.model_file).is_some() && repo.get(&self.tokenizer_file).is_some()
            }
        }
    }

    /// Sets the pooling.
    #[must_use]
    pub fn with_pooling(mut self, pooling: OnnxPooling) -> Self {
        self.pooling = pooling;
        self
    }

    /// Sets the thread count of one run.
    #[must_use]
    pub fn with_threads(mut self, threads: usize) -> Self {
        self.threads = Some(threads);
        self
    }
}

/// An [`EmbeddingProvider`] running a sentence-transformer ONNX model in process.
///
/// See the [module documentation](self).
pub struct OnnxEmbeddingProvider {
    session: Mutex<Session>,
    tokenizer: Tokenizer,
    config: OnnxEmbeddingConfig,
    input_names: Vec<String>,
    model_path: PathBuf,
}

impl std::fmt::Debug for OnnxEmbeddingProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OnnxEmbeddingProvider")
            .field("model_id", &self.config.model_id)
            .field("model_path", &self.model_path)
            .field("dimensions", &self.config.dimensions)
            .field("pooling", &self.config.pooling)
            .finish()
    }
}

impl OnnxEmbeddingProvider {
    /// Loads the model and tokenizer of `config`, fetching them into the hub cache on the
    /// first use unless `local_dir` is set.
    ///
    /// # Errors
    ///
    /// Returns [`RagError::EmbeddingError`] when a file cannot be fetched (no network and
    /// not cached) or loaded.
    pub fn new(config: OnnxEmbeddingConfig) -> Result<Self> {
        let (model_path, tokenizer_path) = match &config.local_dir {
            Some(dir) => (dir.join(&config.model_file), dir.join(&config.tokenizer_file)),
            None => fetch(&config)?,
        };
        let mut tokenizer = Tokenizer::from_file(&tokenizer_path).map_err(|e| {
            error(format!("failed to load the tokenizer {}: {e}", tokenizer_path.display()))
        })?;
        tokenizer
            .with_truncation(Some(TruncationParams {
                max_length: config.max_tokens,
                ..TruncationParams::default()
            }))
            .map_err(|e| error(format!("failed to configure truncation: {e}")))?;
        tokenizer.with_padding(Some(PaddingParams::default()));

        let mut builder = adk_core::onnx::ort_environment()
            .and_then(|environment| Session::builder(&environment))
            .and_then(|builder| {
                builder.with_optimization_level(GraphOptimizationLevel::Level3).map_err(Into::into)
            })
            .map_err(|e| error(format!("failed to create the session builder: {e}")))?;
        if let Some(threads) = config.threads {
            builder = builder
                .with_intra_threads(threads)
                .map_err(|e| error(format!("failed to set the thread count: {e}")))?;
        }
        let session = builder.commit_from_file(&model_path).map_err(|e| {
            error(format!("failed to load the ONNX model {}: {e}", model_path.display()))
        })?;
        let input_names = session.inputs().iter().map(|input| input.name().to_string()).collect();
        tracing::info!(
            model.id = %config.model_id,
            model.path = %model_path.display(),
            dimensions = config.dimensions,
            "loaded the ONNX embedding model"
        );
        Ok(Self { session: Mutex::new(session), tokenizer, config, input_names, model_path })
    }

    /// The configuration.
    pub fn config(&self) -> &OnnxEmbeddingConfig {
        &self.config
    }

    /// Where the model was loaded from.
    pub fn model_path(&self) -> &Path {
        &self.model_path
    }

    fn embed_sync(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }
        let encodings = self
            .tokenizer
            .encode_batch(texts.to_vec(), true)
            .map_err(|e| error(format!("tokenization failed: {e}")))?;
        let batch = encodings.len();
        let seq = encodings.iter().map(|encoding| encoding.get_ids().len()).max().unwrap_or(0);
        let mut ids = Vec::with_capacity(batch * seq);
        let mut mask = Vec::with_capacity(batch * seq);
        let mut types = Vec::with_capacity(batch * seq);
        for encoding in &encodings {
            let pad = seq - encoding.get_ids().len();
            ids.extend(encoding.get_ids().iter().map(|id| i64::from(*id)));
            ids.extend(std::iter::repeat_n(0, pad));
            mask.extend(encoding.get_attention_mask().iter().map(|m| i64::from(*m)));
            mask.extend(std::iter::repeat_n(0, pad));
            types.extend(encoding.get_type_ids().iter().map(|t| i64::from(*t)));
            types.extend(std::iter::repeat_n(0, pad));
        }
        let shape = [batch as i64, seq as i64];
        let tensor = |data: Vec<i64>| {
            Value::from_array((shape, data))
                .map(Value::into_dyn)
                .map_err(|e| error(format!("failed to create an input tensor: {e}")))
        };
        let mut inputs = vec![
            ("input_ids".to_string(), tensor(ids)?),
            ("attention_mask".to_string(), tensor(mask.clone())?),
        ];
        if self.input_names.iter().any(|name| name == "token_type_ids") {
            inputs.push(("token_type_ids".to_string(), tensor(types)?));
        }
        let mut session = self.session.lock().unwrap_or_else(|e| e.into_inner());
        let outputs = session.run(inputs).map_err(|e| error(format!("inference failed: {e}")))?;
        let (out_shape, data) = outputs[0]
            .try_extract_tensor::<f32>()
            .map_err(|e| error(format!("failed to read the hidden states: {e}")))?;
        let out_shape: Vec<i64> = out_shape.iter().copied().collect();
        pool(
            data,
            &out_shape,
            &mask,
            self.config.pooling,
            self.config.normalize,
            self.config.dimensions,
        )
    }
}

/// Pools `data` of shape `[batch, seq, hidden]` over `mask` (`[batch, seq]`, 1 for real
/// tokens) into one vector per text.
fn pool(
    data: &[f32],
    shape: &[i64],
    mask: &[i64],
    pooling: OnnxPooling,
    normalize: bool,
    dimensions: usize,
) -> Result<Vec<Vec<f32>>> {
    let [batch, seq, hidden] = shape else {
        return Err(error(format!(
            "expected hidden states of shape [batch, seq, hidden], got {shape:?}"
        )));
    };
    let (batch, seq, hidden) = (*batch as usize, *seq as usize, *hidden as usize);
    if hidden != dimensions {
        return Err(error(format!(
            "the model produces {hidden}-dimensional states but the configuration says {dimensions}"
        )));
    }
    if data.len() != batch * seq * hidden || mask.len() != batch * seq {
        return Err(error("the hidden states do not match the batch"));
    }
    let mut vectors = Vec::with_capacity(batch);
    for b in 0..batch {
        let mut vector = vec![0.0f32; hidden];
        match pooling {
            OnnxPooling::Mean => {
                let mut count = 0.0f32;
                for t in 0..seq {
                    if mask[b * seq + t] == 0 {
                        continue;
                    }
                    count += 1.0;
                    let row = &data[(b * seq + t) * hidden..(b * seq + t + 1) * hidden];
                    for (sum, value) in vector.iter_mut().zip(row) {
                        *sum += *value;
                    }
                }
                if count > 0.0 {
                    for value in &mut vector {
                        *value /= count;
                    }
                }
            }
            OnnxPooling::Cls => {
                vector.copy_from_slice(&data[b * seq * hidden..b * seq * hidden + hidden]);
            }
        }
        if normalize {
            let norm = vector.iter().map(|v| v * v).sum::<f32>().sqrt();
            if norm > 0.0 {
                for value in &mut vector {
                    *value /= norm;
                }
            }
        }
        vectors.push(vector);
    }
    Ok(vectors)
}

/// Fetches the model files into the hub cache, or reads them from it.
fn fetch(config: &OnnxEmbeddingConfig) -> Result<(PathBuf, PathBuf)> {
    // `from_env` honours `HF_HOME` (and `HF_ENDPOINT`); `new` always uses the home cache.
    let api = hf_hub::api::sync::ApiBuilder::from_env()
        .with_progress(false)
        .build()
        .map_err(|e| error(format!("the Hugging Face hub client failed: {e}")))?;
    let repo = api.model(config.model_id.clone());
    let get = |file: &str| {
        repo.get(file).map_err(|e| {
            error(format!(
                "could not fetch {file} of {} into the Hugging Face hub cache: {e}; the first \
                 use needs network, or point `local_dir` at a directory holding the files",
                config.model_id
            ))
        })
    };
    Ok((get(&config.model_file)?, get(&config.tokenizer_file)?))
}

#[async_trait]
impl EmbeddingProvider for OnnxEmbeddingProvider {
    async fn embed(&self, text: &str) -> Result<Vec<f32>> {
        self.embed_sync(&[text])?.into_iter().next().ok_or_else(|| error("no vector produced"))
    }

    async fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>> {
        self.embed_sync(texts)
    }

    fn dimensions(&self) -> usize {
        self.config.dimensions
    }
}

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

    #[test]
    fn mean_pooling_averages_real_tokens_and_normalizes() {
        // batch 1, seq 3, hidden 2; the third token is padding.
        let data = [1.0, 0.0, 3.0, 4.0, 100.0, 100.0];
        let vectors = pool(&data, &[1, 3, 2], &[1, 1, 0], OnnxPooling::Mean, false, 2).unwrap();
        assert_eq!(vectors, vec![vec![2.0, 2.0]]);
        let normalized = pool(&data, &[1, 3, 2], &[1, 1, 0], OnnxPooling::Mean, true, 2).unwrap();
        let norm: f32 = normalized[0].iter().map(|v| v * v).sum::<f32>().sqrt();
        assert!((norm - 1.0).abs() < 1e-6);
        let cls = pool(&data, &[1, 3, 2], &[1, 1, 0], OnnxPooling::Cls, false, 2).unwrap();
        assert_eq!(cls, vec![vec![1.0, 0.0]]);
    }

    #[test]
    fn a_dimension_mismatch_is_an_error() {
        let error = pool(&[0.0; 4], &[1, 2, 2], &[1, 1], OnnxPooling::Mean, true, 384).unwrap_err();
        assert!(error.to_string().contains("384"), "{error}");
    }

    /// Needs the model in the hub cache or network to fetch it: run with `--ignored`.
    #[tokio::test]
    #[ignore = "fetches BAAI/bge-small-en-v1.5 (133 MB) into the Hugging Face hub cache"]
    async fn the_default_model_embeds_related_texts_closer_than_unrelated_ones() {
        let provider = OnnxEmbeddingProvider::new(OnnxEmbeddingConfig::default()).unwrap();
        let vectors = provider
            .embed_batch(&[
                "the tests need --features sqlite",
                "run the test suite with the sqlite feature",
                "deploy on Fridays only",
            ])
            .await
            .unwrap();
        assert_eq!(vectors.len(), 3);
        assert!(vectors.iter().all(|v| v.len() == DEFAULT_DIMENSIONS));
        let dot = |a: &[f32], b: &[f32]| a.iter().zip(b).map(|(x, y)| x * y).sum::<f32>();
        assert!(dot(&vectors[0], &vectors[1]) > dot(&vectors[0], &vectors[2]));
    }
}