Skip to main content

sim_lib_openai_server/storage/
vector.rs

1use sim_kernel::Expr;
2
3/// Record kind tag designating a gateway vector store.
4pub const GATEWAY_VECTOR_STORE_KIND: &str = "openai-gateway/vector-store";
5/// Record kind tag designating a single item within a gateway vector store.
6pub const GATEWAY_VECTOR_STORE_ITEM_KIND: &str = "openai-gateway/vector-store-item";
7
8/// A named gateway vector store holding embedded text items.
9#[derive(Clone, Debug, PartialEq)]
10pub struct GatewayVectorStore {
11    id: String,
12    name: String,
13    created_at_ms: u64,
14    items: Vec<GatewayVectorStoreItem>,
15}
16
17impl GatewayVectorStore {
18    /// Creates a vector store with the given id, name, creation time, and items.
19    pub fn new(
20        id: impl Into<String>,
21        name: impl Into<String>,
22        created_at_ms: u64,
23        items: Vec<GatewayVectorStoreItem>,
24    ) -> Self {
25        Self {
26            id: id.into(),
27            name: name.into(),
28            created_at_ms,
29            items,
30        }
31    }
32
33    /// Returns the vector store's identifier.
34    pub fn id(&self) -> &str {
35        &self.id
36    }
37
38    /// Returns the vector store's display name.
39    pub fn name(&self) -> &str {
40        &self.name
41    }
42
43    /// Returns the creation timestamp in milliseconds since the Unix epoch.
44    pub fn created_at_ms(&self) -> u64 {
45        self.created_at_ms
46    }
47
48    /// Returns the items held by this vector store.
49    pub fn items(&self) -> &[GatewayVectorStoreItem] {
50        &self.items
51    }
52
53    /// Encodes the vector store as a SIM [`Expr`] map record.
54    pub fn to_expr(&self) -> Expr {
55        Expr::Map(vec![
56            field("kind", Expr::String(GATEWAY_VECTOR_STORE_KIND.to_owned())),
57            field("id", Expr::String(self.id.clone())),
58            field("name", Expr::String(self.name.clone())),
59            field(
60                "created-at-ms",
61                Expr::String(self.created_at_ms.to_string()),
62            ),
63            field(
64                "items",
65                Expr::Vector(
66                    self.items
67                        .iter()
68                        .map(GatewayVectorStoreItem::to_expr)
69                        .collect(),
70                ),
71            ),
72        ])
73    }
74}
75
76/// A single embedded text item within a [`GatewayVectorStore`].
77#[derive(Clone, Debug, PartialEq)]
78pub struct GatewayVectorStoreItem {
79    id: String,
80    text: String,
81    embedding: Vec<f64>,
82}
83
84impl GatewayVectorStoreItem {
85    /// Creates an item with the given id, text, and embedding vector.
86    pub fn new(id: impl Into<String>, text: impl Into<String>, embedding: Vec<f64>) -> Self {
87        Self {
88            id: id.into(),
89            text: text.into(),
90            embedding,
91        }
92    }
93
94    /// Returns the item's identifier.
95    pub fn id(&self) -> &str {
96        &self.id
97    }
98
99    /// Returns the item's source text.
100    pub fn text(&self) -> &str {
101        &self.text
102    }
103
104    /// Returns the item's embedding vector.
105    pub fn embedding(&self) -> &[f64] {
106        &self.embedding
107    }
108
109    /// Encodes the item as a SIM [`Expr`] map record.
110    pub fn to_expr(&self) -> Expr {
111        Expr::Map(vec![
112            field(
113                "kind",
114                Expr::String(GATEWAY_VECTOR_STORE_ITEM_KIND.to_owned()),
115            ),
116            field("id", Expr::String(self.id.clone())),
117            field("text", Expr::String(self.text.clone())),
118            field(
119                "embedding",
120                Expr::Vector(
121                    self.embedding
122                        .iter()
123                        .map(|value| Expr::String(format!("{value:.6}")))
124                        .collect(),
125                ),
126            ),
127        ])
128    }
129}
130
131use sim_value::build::entry as field;