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
use futures::StreamExt;
use mongodb::bson::doc;

use rig::{
    embeddings::{DocumentEmbeddings, Embedding, EmbeddingModel},
    vector_store::{VectorStore, VectorStoreError, VectorStoreIndex},
};

pub struct MongoDbVectorStore {
    collection: mongodb::Collection<DocumentEmbeddings>,
}

fn mongodb_to_rig_error(e: mongodb::error::Error) -> VectorStoreError {
    VectorStoreError::DatastoreError(Box::new(e))
}

impl VectorStore for MongoDbVectorStore {
    type Q = mongodb::bson::Document;

    async fn add_documents(
        &mut self,
        documents: Vec<DocumentEmbeddings>,
    ) -> Result<(), VectorStoreError> {
        self.collection
            .insert_many(documents, None)
            .await
            .map_err(mongodb_to_rig_error)?;
        Ok(())
    }

    async fn get_document_embeddings(
        &self,
        id: &str,
    ) -> Result<Option<DocumentEmbeddings>, VectorStoreError> {
        self.collection
            .find_one(doc! { "_id": id }, None)
            .await
            .map_err(mongodb_to_rig_error)
    }

    async fn get_document<T: for<'a> serde::Deserialize<'a>>(
        &self,
        id: &str,
    ) -> Result<Option<T>, VectorStoreError> {
        Ok(self
            .collection
            .clone_with_type::<String>()
            .aggregate(
                [
                    doc! {"$match": { "_id": id}},
                    doc! {"$project": { "document": 1 }},
                    doc! {"$replaceRoot": { "newRoot": "$document" }},
                ],
                None,
            )
            .await
            .map_err(mongodb_to_rig_error)?
            .with_type::<String>()
            .next()
            .await
            .transpose()
            .map_err(mongodb_to_rig_error)?
            .map(|doc| serde_json::from_str(&doc))
            .transpose()?)
    }

    async fn get_document_by_query(
        &self,
        query: Self::Q,
    ) -> Result<Option<DocumentEmbeddings>, VectorStoreError> {
        self.collection
            .find_one(query, None)
            .await
            .map_err(mongodb_to_rig_error)
    }
}

impl MongoDbVectorStore {
    pub fn new(collection: mongodb::Collection<DocumentEmbeddings>) -> Self {
        Self { collection }
    }

    pub fn index<M: EmbeddingModel>(
        &self,
        model: M,
        index_name: &str,
        filter: mongodb::bson::Document,
    ) -> MongoDbVectorIndex<M> {
        MongoDbVectorIndex::new(self.collection.clone(), model, index_name, filter)
    }
}

pub struct MongoDbVectorIndex<M: EmbeddingModel> {
    collection: mongodb::Collection<DocumentEmbeddings>,
    model: M,
    index_name: String,
    filter: mongodb::bson::Document,
}

impl<M: EmbeddingModel> MongoDbVectorIndex<M> {
    pub fn new(
        collection: mongodb::Collection<DocumentEmbeddings>,
        model: M,
        index_name: &str,
        filter: mongodb::bson::Document,
    ) -> Self {
        Self {
            collection,
            model,
            index_name: index_name.to_string(),
            filter,
        }
    }
}

impl<M: EmbeddingModel + std::marker::Sync + Send> VectorStoreIndex for MongoDbVectorIndex<M> {
    async fn top_n_from_query(
        &self,
        query: &str,
        n: usize,
    ) -> Result<Vec<(f64, DocumentEmbeddings)>, VectorStoreError> {
        let prompt_embedding = self.model.embed_document(query).await?;
        self.top_n_from_embedding(&prompt_embedding, n).await
    }

    async fn top_n_from_embedding(
        &self,
        prompt_embedding: &Embedding,
        n: usize,
    ) -> Result<Vec<(f64, DocumentEmbeddings)>, VectorStoreError> {
        let mut cursor = self
            .collection
            .aggregate(
                [
                    doc! {
                      "$vectorSearch": {
                        "index": &self.index_name,
                        "path": "embeddings.vec",
                        "queryVector": &prompt_embedding.vec,
                        "numCandidates": (n * 10) as u32,
                        "limit": n as u32,
                        "filter": &self.filter,
                      }
                    },
                    doc! {
                      "$addFields": {
                        "score": { "$meta": "vectorSearchScore" }
                      }
                    },
                ],
                None,
            )
            .await
            .map_err(mongodb_to_rig_error)?
            .with_type::<serde_json::Value>();

        let mut results = Vec::new();
        while let Some(doc) = cursor.next().await {
            let doc = doc.map_err(mongodb_to_rig_error)?;
            let score = doc.get("score").expect("score").as_f64().expect("f64");
            let document: DocumentEmbeddings = serde_json::from_value(doc).expect("document");
            results.push((score, document));
        }

        tracing::info!(target: "rig",
            "Selected documents: {}",
            results.iter()
                .map(|(distance, doc)| format!("{} ({})", doc.id, distance))
                .collect::<Vec<String>>()
                .join(", ")
        );

        Ok(results)
    }
}