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
use std::{collections::HashMap, path::PathBuf, time::Instant};

use anyhow::Result;
use glob::glob;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};

use crate::{import, metrics, Embedder};

use super::{Configuration, Document, Embeddings};

#[derive(Serialize, Deserialize)]
struct Store {
    documents: HashMap<String, Document>,
    embeddings: HashMap<String, Embeddings>,
}

impl Store {
    fn new() -> Self {
        let documents = HashMap::new();
        let embeddings = HashMap::new();
        Self {
            documents,
            embeddings,
        }
    }

    fn from_data_path(path: &str) -> Result<Self> {
        let path = PathBuf::from(path).join("rag.bin");
        if path.exists() {
            let raw = std::fs::read(&path)?;
            Ok(bitcode::deserialize(&raw)?)
        } else {
            Ok(Store::new())
        }
    }

    fn to_data_path(&self, path: &str) -> Result<()> {
        let path = PathBuf::from(path).join("rag.bin");
        let raw = bitcode::serialize(&self)?;

        std::fs::write(path, raw)?;

        Ok(())
    }
}

pub struct VectorStore {
    config: Configuration,
    embedder: Box<dyn Embedder>,
    store: Store,
}

impl VectorStore {
    pub fn new(embedder: Box<dyn Embedder>, config: Configuration) -> Result<Self> {
        let store = Store::from_data_path(&config.data_path)?;
        Ok(Self {
            config,
            embedder,
            store,
        })
    }

    pub async fn import_new_documents(&mut self) -> Result<()> {
        let path = std::fs::canonicalize(&self.config.source_path)?
            .display()
            .to_string();

        let expr = format!("{}/**/*.*", path);
        let start = Instant::now();
        let mut new = 0;

        for path in (glob(&expr)?).flatten() {
            match import::import_document_from(&path) {
                Ok(doc) => {
                    let docs = if let Some(chunk_size) = self.config.chunk_size {
                        doc.chunks(chunk_size)?
                    } else {
                        vec![doc]
                    };

                    for doc in docs {
                        match self.add(doc).await {
                            Err(err) => log::error!("storing {}: {}", path.display(), err),
                            Ok(added) => {
                                if added {
                                    new += 1
                                }
                            }
                        }
                    }
                }
                Err(err) => log::warn!("{} {err}", path.display()),
            }
        }

        if new > 0 {
            log::info!("{} new documents indexed in {:?}\n", new, start.elapsed(),);
        }

        Ok(())
    }

    pub async fn add(&mut self, mut document: Document) -> Result<bool> {
        let doc_id = document.get_ident().to_string();
        let doc_path = document.get_path().to_string();

        if self.store.documents.contains_key(&doc_id) {
            log::debug!("document with id '{}' already indexed", &doc_id);
            return Ok(false);
        }

        log::info!(
            "indexing new document '{}' ({} bytes) ...",
            doc_path,
            document.get_byte_size()?
        );

        let start = Instant::now();
        let embeddings: Vec<f64> = self.embedder.embed(document.get_data()?).await?;
        let size = embeddings.len();

        // get rid of the contents once indexed
        document.drop_data();

        self.store.documents.insert(doc_id.to_string(), document);
        self.store.embeddings.insert(doc_id, embeddings);

        self.store.to_data_path(&self.config.data_path)?;

        log::debug!("time={:?} embedding_size={}", start.elapsed(), size);

        Ok(true)
    }

    pub async fn retrieve(&self, query: &str, top_k: usize) -> Result<Vec<(Document, f64)>> {
        log::debug!("{} (top {})", query, top_k);

        let query_vector = self.embedder.embed(query).await?;
        let mut results = vec![];

        let distances: Vec<(&String, f64)> = {
            let mut distances: Vec<(&String, f64)> = self
                .store
                .embeddings
                .par_iter()
                .map(|(doc_id, doc_embedding)| {
                    (doc_id, metrics::cosine(&query_vector, doc_embedding))
                })
                .collect();
            distances.par_sort_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap());
            distances
        };

        for (doc_id, score) in distances {
            let document = self.store.documents.get(doc_id).unwrap();
            results.push((document.clone(), score));
            if results.len() >= top_k {
                break;
            }
        }

        Ok(results)
    }
}