sim_lib_openai_server/storage/
vector.rs1use sim_kernel::Expr;
2
3pub const GATEWAY_VECTOR_STORE_KIND: &str = "openai-gateway/vector-store";
5pub const GATEWAY_VECTOR_STORE_ITEM_KIND: &str = "openai-gateway/vector-store-item";
7
8#[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 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 pub fn id(&self) -> &str {
35 &self.id
36 }
37
38 pub fn name(&self) -> &str {
40 &self.name
41 }
42
43 pub fn created_at_ms(&self) -> u64 {
45 self.created_at_ms
46 }
47
48 pub fn items(&self) -> &[GatewayVectorStoreItem] {
50 &self.items
51 }
52
53 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#[derive(Clone, Debug, PartialEq)]
78pub struct GatewayVectorStoreItem {
79 id: String,
80 text: String,
81 embedding: Vec<f64>,
82}
83
84impl GatewayVectorStoreItem {
85 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 pub fn id(&self) -> &str {
96 &self.id
97 }
98
99 pub fn text(&self) -> &str {
101 &self.text
102 }
103
104 pub fn embedding(&self) -> &[f64] {
106 &self.embedding
107 }
108
109 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;