llm_chain_milvus/
lib.rs

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
use async_trait::async_trait;
use errors::MilvusError;
use llm_chain::{
    schema::Document,
    traits::{Embeddings, VectorStore},
};
use milvus::{
    client::Client as MilvusClient,
    collection::SearchOption,
    data::FieldColumn,
    proto::{milvus::MutationResult, schema::i_ds::IdField},
    value::ValueVec,
};
use serde::{de::DeserializeOwned, Serialize};
use std::{collections::HashMap, marker::PhantomData, sync::Arc};

pub mod errors;
const DEFAULT_CONTENT_PAYLOAD_KEY: &str = "page_content";
const DEFAULT_METADATA_PAYLOAD_KEY: &str = "metadata";

pub struct Milvus<E, M>
where
    E: Embeddings,
    M: Serialize + DeserializeOwned + Send + Sync,
{
    client: Arc<MilvusClient>,
    collection_name: String,
    vector_field_name: String,
    payload_field_name: Option<String>,
    content_payload_key: String,
    metadata_payload_key: String,
    embeddings: E,
    _marker: PhantomData<M>,
}

impl<E, M> Milvus<E, M>
where
    E: Embeddings,
    M: Serialize + DeserializeOwned + Send + Sync,
{
    pub fn new(
        client: Arc<MilvusClient>,
        collection_name: String,
        vector_field_name: String,
        payload_field_name: Option<String>,
        content_payload_key: Option<String>,
        metadata_payload_key: Option<String>,
        embeddings: E,
    ) -> Self {
        Self {
            client,
            collection_name,
            vector_field_name,
            payload_field_name,
            embeddings,
            content_payload_key: content_payload_key
                .unwrap_or(DEFAULT_CONTENT_PAYLOAD_KEY.to_string()),
            metadata_payload_key: metadata_payload_key
                .unwrap_or(DEFAULT_METADATA_PAYLOAD_KEY.to_string()),
            _marker: Default::default(),
        }
    }

    fn ids_from_milvus_results(
        &self,
        res: MutationResult,
    ) -> Result<Vec<String>, MilvusError<E::Error>> {
        let ids = res.i_ds.ok_or(errors::MilvusError::InsertionError)?;
        match ids.id_field {
            Some(IdField::IntId(arr)) => Ok(arr
                .data
                .into_iter()
                .map(|x| x.to_string())
                .collect::<Vec<String>>()),
            Some(IdField::StrId(string_arr)) => Ok(string_arr.data),
            None => Err(errors::MilvusError::InsertionError),
        }
    }
}

#[async_trait]
impl<E, M> VectorStore<E, M> for Milvus<E, M>
where
    E: Embeddings + Send + Sync,
    M: Send + Sync + Serialize + DeserializeOwned,
{
    type Error = errors::MilvusError<E::Error>;

    async fn add_texts(&self, texts: Vec<String>) -> Result<Vec<String>, Self::Error> {
        let embedding_vecs = self.embeddings.embed_texts(texts.clone()).await?;
        let collection = self
            .client
            .get_collection(&self.collection_name)
            .await
            .map_err(errors::MilvusError::Client)?;

        let embed_column = FieldColumn::new(
            collection
                .schema()
                .get_field(&self.vector_field_name)
                .unwrap(),
            embedding_vecs.into_iter().flatten().collect::<Vec<_>>(),
        );

        let milvus_results = collection.insert(vec![embed_column], None).await.unwrap();
        collection
            .flush()
            .await
            .map_err(|_| errors::MilvusError::InsertionError)?;
        self.ids_from_milvus_results(milvus_results)
    }

    async fn add_documents(&self, documents: Vec<Document<M>>) -> Result<Vec<String>, Self::Error> {
        let collection = self
            .client
            .get_collection(&self.collection_name)
            .await
            .map_err(errors::MilvusError::Client)?;

        // Embedding documents' text
        let texts = documents.iter().map(|d| d.page_content.clone()).collect();
        let embedding_vecs = self.embeddings.embed_texts(texts).await?;

        // Construct Milvus vector column
        let embed_column = FieldColumn::new(
            collection
                .schema()
                .get_field(&self.vector_field_name)
                .unwrap(),
            embedding_vecs.into_iter().flatten().collect::<Vec<_>>(),
        );
        // Inserting document in Milvus collection
        // Note: To insert document metadata we need to be sure that
        // the collection has a column `Datatype.JSON`
        match &self.payload_field_name {
            Some(payload_field_name) => {
                let payload_column_name = collection
                    .schema()
                    .get_field(&payload_field_name)
                    .ok_or(errors::MilvusError::InvalidColumnName)?;
                let payloads: Vec<String> = documents
                    .into_iter()
                    .map(|document| {
                        // Construct the
                        let mut payload: HashMap<String, Option<String>> = HashMap::new();

                        if let Some(metadata) = document.metadata {
                            let val =
                                serde_json::to_string(&metadata).map_err(Self::Error::Serde)?;

                            payload.insert(self.metadata_payload_key.clone(), val.into());
                        } else {
                            payload.insert(self.metadata_payload_key.clone(), None);
                        }
                        payload.insert(
                            self.content_payload_key.clone(),
                            document.page_content.clone().into(),
                        );
                        let payload =
                            serde_json::to_string(&payload).map_err(Self::Error::Serde)?;
                        Ok(payload)
                    })
                    .collect::<Result<Vec<_>, errors::MilvusError<_>>>()?;
                let payload_column = FieldColumn::new(payload_column_name, payloads);
                let milvus_results = collection
                    .insert(vec![embed_column, payload_column], None)
                    .await
                    .unwrap();

                collection
                    .flush()
                    .await
                    .map_err(|_| errors::MilvusError::InsertionError)?;

                self.ids_from_milvus_results(milvus_results)
            }
            None => {
                let milvus_results = collection.insert(vec![embed_column], None).await.unwrap();
                self.ids_from_milvus_results(milvus_results)
            }
        }
    }

    async fn similarity_search(
        &self,
        query: String,
        limit: u32,
    ) -> Result<Vec<Document<M>>, Self::Error> {
        let collection = self
            .client
            .get_collection(&self.collection_name)
            .await
            .map_err(errors::MilvusError::Client)?;

        let embedded_query = self.embeddings.embed_query(query).await?;

        let indexes = collection
            .describe_index(self.vector_field_name.clone())
            .await
            .unwrap();

        // Take the first index for now
        let index = indexes
            .first()
            .ok_or(errors::MilvusError::EmptyIndexError)?;

        match &self.payload_field_name {
            Some(out_field) => {
                let results = collection
                    .search(
                        vec![embedded_query.into()],
                        self.vector_field_name.clone(),
                        limit as i32,
                        index.params().metric_type(),
                        vec![out_field],
                        &SearchOption::default(),
                    )
                    .await
                    .map_err(Self::Error::Client)?;

                // Convert Results to docs
                let mut docs: Vec<Document<M>> = Vec::new();
                for res in results {
                    for field in res.field.iter().filter(|f| &f.name == out_field) {
                        match &field.value {
                            ValueVec::String(val) => {
                                let payload: HashMap<String, Option<String>> =
                                    serde_json::from_str(&val[0])
                                        .map_err(errors::MilvusError::Serde)?;

                                let _metadata: Option<String> = payload // XXX: temp fix since the
                                                                       // var is not used rn
                                    .get(&self.metadata_payload_key)
                                    .unwrap()
                                    .clone()
                                    .into();

                                let page_content = payload
                                    .get(&self.content_payload_key)
                                    .unwrap()
                                    .clone()
                                    .unwrap_or("".to_string());

                                docs.push(Document {
                                    page_content: page_content,
                                    metadata: None,
                                });
                            }
                            _ => return Err(errors::MilvusError::QueryError),
                        }
                    }
                }
                Ok(docs)
            }
            None => return Err(errors::MilvusError::QueryError),
        }
    }
}