rig-s3vectors 0.42.0

AWS S3Vectors vector store implementation for the rig framework
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
// Tests assert on filter shapes, where a failed conversion should panic loudly
// rather than be handled; matches the other vector-store crates (e.g. rig-lancedb).
#![cfg_attr(test, allow(clippy::expect_used))]
//! AWS S3Vectors vector store integration for Rig.
//!
//! This crate provides [`S3VectorsVectorStore`], a Rig vector store backed by
//! AWS S3Vectors indexes. It uses the AWS SDK client supplied by the caller and
//! maps Rig search filters to S3Vectors filter documents through
//! [`S3SearchFilter`].
//!
//! The root `rig` facade re-exports this crate as `rig::s3vectors` when the
//! `s3vectors` feature is enabled.

use aws_sdk_s3vectors::{
    Client,
    types::{PutInputVector, VectorData},
};
use aws_smithy_types::Document;
use rig_core::{
    embeddings::EmbeddingModel,
    vector_store::{
        InsertDocuments, VectorStoreError, VectorStoreIndex,
        request::{DynamicSearchFilter, Filter, FilterError, SearchFilter, VectorSearchRequest},
    },
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use uuid::Uuid;

#[derive(Debug, Serialize, Deserialize)]
pub struct CreateRecord {
    document: serde_json::Value,
    embedded_text: String,
}

/// S3Vectors filter backed by the AWS SDK's native document type.
#[derive(Clone, Debug)]
pub struct S3SearchFilter(aws_smithy_types::Document);

impl SearchFilter for S3SearchFilter {
    type Value = aws_smithy_types::Document;

    fn eq(key: impl AsRef<str>, value: Self::Value) -> Self {
        Self(document_comparison(key, "$eq", value))
    }

    fn gt(key: impl AsRef<str>, value: Self::Value) -> Self {
        Self(document_comparison(key, "$gt", value))
    }

    fn lt(key: impl AsRef<str>, value: Self::Value) -> Self {
        Self(document_comparison(key, "$lt", value))
    }

    fn and(self, rhs: Self) -> Self {
        Self(document_object([(
            "$and",
            Document::Array(vec![self.0, rhs.0]),
        )]))
    }

    fn or(self, rhs: Self) -> Self {
        Self(document_object([(
            "$or",
            Document::Array(vec![self.0, rhs.0]),
        )]))
    }
}

/// Builds a `Document::Object` from the given entries.
fn document_object<K>(entries: impl IntoIterator<Item = (K, Document)>) -> Document
where
    K: Into<String>,
{
    Document::Object(
        entries
            .into_iter()
            .map(|(key, value)| (key.into(), value))
            .collect(),
    )
}

/// Builds the `{ key: { op: value } }` shape S3Vectors uses for comparison
/// operators such as `$eq` and `$gt`.
fn document_comparison(key: impl AsRef<str>, op: &str, value: Document) -> Document {
    document_object([(key.as_ref(), document_object([(op, value)]))])
}

impl DynamicSearchFilter for S3SearchFilter {
    fn from_dynamic_filter(filter: Filter<serde_json::Value>) -> Result<Self, FilterError> {
        Ok(filter.interpret_with(|value| json_value_to_document(&value)))
    }
}

impl S3SearchFilter {
    pub fn inner(&self) -> &aws_smithy_types::Document {
        &self.0
    }

    pub fn into_inner(self) -> aws_smithy_types::Document {
        self.0
    }

    pub fn gte(key: String, value: <Self as SearchFilter>::Value) -> Self {
        Self(document_comparison(key, "$gte", value))
    }

    pub fn lte(key: String, value: <Self as SearchFilter>::Value) -> Self {
        Self(document_comparison(key, "$lte", value))
    }

    pub fn exists(key: String) -> Self {
        Self(document_object([(
            "$exists",
            document_object([(key, Document::Bool(true))]),
        )]))
    }

    #[allow(clippy::should_implement_trait)]
    pub fn not(self) -> Self {
        Self(document_object([("$not", self.0)]))
    }
}

pub struct S3VectorsVectorStore<M> {
    embedding_model: M,
    client: Client,
    bucket_name: String,
    index_name: String,
}

impl<M> S3VectorsVectorStore<M>
where
    M: EmbeddingModel,
{
    pub fn new(
        embedding_model: M,
        client: aws_sdk_s3vectors::Client,
        bucket_name: &str,
        index_name: &str,
    ) -> Self {
        Self {
            embedding_model,
            client,
            bucket_name: bucket_name.to_string(),
            index_name: index_name.to_string(),
        }
    }

    pub fn bucket_name(&self) -> &str {
        &self.bucket_name
    }

    pub fn set_bucket_name(&mut self, bucket_name: &str) {
        self.bucket_name = bucket_name.to_string();
    }

    pub fn index_name(&self) -> &str {
        &self.index_name
    }

    pub fn set_index_name(&mut self, index_name: &str) {
        self.index_name = index_name.to_string();
    }

    pub fn client(&self) -> &Client {
        &self.client
    }

    /// Validates the sample count, embeds the query, and runs the S3Vectors
    /// query, returning the `(distance, vector)` pairs passing the threshold.
    async fn run_query(
        &self,
        req: &VectorSearchRequest<S3SearchFilter>,
        return_metadata: bool,
    ) -> Result<Vec<(f64, aws_sdk_s3vectors::types::QueryOutputVector)>, VectorStoreError> {
        if req.samples() > i32::MAX as u64 {
            return Err(VectorStoreError::DatastoreError(format!("The number of samples to return with the `rig` AWS S3Vectors integration cannot be higher than {}", i32::MAX).into()));
        }

        let embedding = self
            .embedding_model
            .embed_text(req.query())
            .await?
            .vec
            .into_iter()
            .map(|x| x as f32)
            .collect();

        let mut query_builder = self
            .client
            .query_vectors()
            .query_vector(VectorData::Float32(embedding))
            .top_k(req.samples() as i32)
            .return_distance(true)
            .vector_bucket_name(self.bucket_name())
            .index_name(self.index_name());

        if return_metadata {
            query_builder = query_builder.return_metadata(true);
        }

        if let Some(filter) = req.filter() {
            query_builder = query_builder.filter(filter.inner().clone())
        }

        let query = query_builder
            .send()
            .await
            .map_err(VectorStoreError::datastore)?;

        Ok(query
            .vectors
            .into_iter()
            .map(|x| {
                let distance = x.distance.ok_or_else(|| {
                    VectorStoreError::DatastoreError("S3Vectors response missing distance".into())
                })? as f64;

                Ok((distance, x))
            })
            .collect::<Result<Vec<_>, VectorStoreError>>()?
            .into_iter()
            .filter(|(distance, _)| {
                !req.threshold()
                    .is_some_and(|threshold| *distance < threshold)
            })
            .collect())
    }
}

impl<M> InsertDocuments for S3VectorsVectorStore<M>
where
    M: EmbeddingModel,
{
    async fn insert_documents<Doc: serde::Serialize + rig_core::Embed + Send>(
        &self,
        documents: Vec<(Doc, Vec<rig_core::embeddings::Embedding>)>,
    ) -> Result<(), rig_core::vector_store::VectorStoreError> {
        let docs: Vec<PutInputVector> =
            rig_core::vector_store::flatten_embedded(documents, |json_value, y| {
                let document = CreateRecord {
                    document: json_value.clone(),
                    embedded_text: y.document,
                };
                let document =
                    serde_json::to_value(&document).map_err(VectorStoreError::JsonError)?;
                let document = json_value_to_document(&document);
                let vec = y.vec.into_iter().map(|item| item as f32).collect();
                PutInputVector::builder()
                    .metadata(document.clone())
                    .data(VectorData::Float32(vec))
                    .key(Uuid::new_v4())
                    .build()
                    .map_err(|x| {
                        VectorStoreError::DatastoreError(
                            format!("Couldn't build vector input: {x}").into(),
                        )
                    })
            })
            .map_err(|x| {
                VectorStoreError::DatastoreError(
                    format!("Could not build vector store data: {x}").into(),
                )
            })?;

        self.client
            .put_vectors()
            .vector_bucket_name(self.bucket_name())
            .set_vectors(Some(docs))
            .set_index_name(Some(self.index_name.clone()))
            .send()
            .await
            .map_err(|x| {
                VectorStoreError::DatastoreError(
                    format!("Error while submitting document insertion request: {x}").into(),
                )
            })?;

        Ok(())
    }
}

fn json_value_to_document(value: &Value) -> Document {
    match value {
        Value::Null => Document::Null,
        Value::Bool(b) => Document::Bool(*b),
        Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Document::Number(aws_smithy_types::Number::NegInt(i))
            } else if let Some(u) = n.as_u64() {
                Document::Number(aws_smithy_types::Number::PosInt(u))
            } else if let Some(f) = n.as_f64() {
                Document::Number(aws_smithy_types::Number::Float(f))
            } else {
                Document::Null // fallback, should never happen
            }
        }
        Value::String(s) => Document::String(s.clone()),
        Value::Array(arr) => Document::Array(arr.iter().map(json_value_to_document).collect()),
        Value::Object(obj) => Document::Object(
            obj.iter()
                .map(|(k, v)| (k.clone(), json_value_to_document(v)))
                .collect::<HashMap<_, _>>(),
        ),
    }
}

fn document_to_json_value(value: &Document) -> Value {
    match value {
        Document::Null => Value::Null,
        Document::Bool(b) => Value::Bool(*b),
        Document::Number(n) => match n {
            aws_smithy_types::Number::Float(f) => serde_json::Number::from_f64(*f)
                .map(Value::Number)
                .unwrap_or_else(|| Value::String(f.to_string())),
            aws_smithy_types::Number::NegInt(i) => {
                serde_json::Value::Number(serde_json::Number::from(*i))
            }
            aws_smithy_types::Number::PosInt(u) => {
                serde_json::Value::Number(serde_json::Number::from(*u))
            }
        },
        Document::String(s) => Value::String(s.clone()),
        Document::Array(arr) => Value::Array(arr.iter().map(document_to_json_value).collect()),
        Document::Object(obj) => {
            let res = obj
                .iter()
                .map(|(k, v)| (k.clone(), document_to_json_value(v)))
                .collect::<serde_json::Map<String, serde_json::Value>>();

            serde_json::Value::Object(res)
        }
    }
}

impl<M> VectorStoreIndex for S3VectorsVectorStore<M>
where
    M: EmbeddingModel,
{
    type Filter = S3SearchFilter;

    async fn top_n<T: for<'a> serde::Deserialize<'a> + Send>(
        &self,
        req: VectorSearchRequest<S3SearchFilter>,
    ) -> Result<Vec<(f64, String, T)>, VectorStoreError> {
        self.run_query(&req, true)
            .await?
            .into_iter()
            .map(|(distance, x)| {
                let metadata_document = x.metadata.ok_or_else(|| {
                    VectorStoreError::DatastoreError("S3Vectors response missing metadata".into())
                })?;
                let val = document_to_json_value(&metadata_document);
                let metadata: T = serde_json::from_value(val)?;

                Ok((distance, x.key, metadata))
            })
            .collect()
    }

    async fn top_n_ids(
        &self,
        req: VectorSearchRequest<S3SearchFilter>,
    ) -> Result<Vec<(f64, String)>, VectorStoreError> {
        Ok(self
            .run_query(&req, false)
            .await?
            .into_iter()
            .map(|(distance, x)| (distance, x.key))
            .collect())
    }
}

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

    #[test]
    fn dynamic_filter_compiles_to_native_aws_documents() {
        let filter = Filter::eq("status", serde_json::json!("ready"))
            .and(Filter::eq("tags", serde_json::json!(["rust", "ai"])))
            .and(Filter::gt("score", serde_json::json!(4.5)));

        let compiled = S3SearchFilter::from_dynamic_filter(filter)
            .expect("JSON values should compile to AWS documents");

        assert_eq!(
            document_to_json_value(compiled.inner()),
            serde_json::json!({
                "$and": [{
                    "$and": [
                        { "status": { "$eq": "ready" } },
                        { "tags": { "$eq": ["rust", "ai"] } }
                    ]
                }, {
                    "score": { "$gt": 4.5 }
                }]
            })
        );
    }

    #[test]
    fn extension_operators_build_the_documented_filter_shapes() {
        let number = |n| Document::Number(aws_smithy_types::Number::PosInt(n));
        let filter = S3SearchFilter::gte("score".into(), number(5))
            .or(S3SearchFilter::lte("score".into(), number(1)))
            .or(S3SearchFilter::exists("status".into()))
            .not();

        assert_eq!(
            document_to_json_value(filter.inner()),
            serde_json::json!({
                "$not": {
                    "$or": [
                        { "$or": [
                            { "score": { "$gte": 5 } },
                            { "score": { "$lte": 1 } }
                        ]},
                        { "$exists": { "status": true } }
                    ]
                }
            })
        );
    }
}