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
use std::error::Error;

use async_trait::async_trait;

use crate::schemas::{self, Document};

use super::VecStoreOptions;

// VectorStore is the trait for saving and querying documents in the
// form of vector embeddings.
#[async_trait]
pub trait VectorStore: Send + Sync {
    async fn add_documents(
        &self,
        docs: &[Document],
        opt: &VecStoreOptions,
    ) -> Result<Vec<String>, Box<dyn Error>>;

    async fn similarity_search(
        &self,
        query: &str,
        limit: usize,
        opt: &VecStoreOptions,
    ) -> Result<Vec<Document>, Box<dyn Error>>;
}

// Retriever is a retriever for vector stores.
pub struct Retriver {
    vstore: Box<dyn VectorStore>,
    num_docs: usize,
    options: VecStoreOptions,
}
impl Retriver {
    pub fn new<V: VectorStore + 'static>(vstore: V, num_docs: usize) -> Self {
        Retriver {
            vstore: Box::new(vstore),
            num_docs,
            options: VecStoreOptions::default(),
        }
    }

    pub fn with_options(mut self, options: VecStoreOptions) -> Self {
        self.options = options;
        self
    }
}

#[async_trait]
impl schemas::Retriever for Retriver {
    async fn get_relevant_documents(&self, query: &str) -> Result<Vec<Document>, Box<dyn Error>> {
        self.vstore
            .similarity_search(query, self.num_docs, &self.options)
            .await
    }
}