File size: 1,606 Bytes
e5034c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use chrono::Utc;
use derive_setters::Setters;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PointId(Uuid);

impl PointId {
    pub fn generate() -> Self {
        Self(Uuid::new_v4())
    }

    pub fn into_uuid(self) -> Uuid {
        self.0
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Point<C> {
    pub id: PointId,
    pub content: C,
    pub embedding: Vec<f32>,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

impl<C> Point<C> {
    /// Embedding can be created from a part or more of the actual content.
    pub fn new(content: C, embedding: Vec<f32>) -> Self {
        let now = Utc::now();
        Self {
            id: PointId::generate(),
            content,
            embedding,
            created_at: now,
            updated_at: now,
        }
    }

    pub fn try_map<D, E>(
        self,
        f: impl FnOnce(C) -> std::result::Result<D, E>,
    ) -> std::result::Result<Point<D>, E> {
        Ok(Point {
            content: f(self.content)?,
            id: self.id,
            embedding: self.embedding,
            created_at: self.created_at,
            updated_at: self.updated_at,
        })
    }
}

#[derive(Debug, Clone, Setters)]
#[setters(strip_option, into)]
pub struct Query {
    pub embedding: Vec<f32>,
    pub limit: Option<u64>,
    pub distance: Option<f32>,
}

impl Query {
    pub fn new(embedding: Vec<f32>) -> Self {
        Self { embedding, limit: None, distance: None }
    }
}