Skip to main content

sql_rs/vector/
mod.rs

1mod collection;
2mod hnsw;
3mod quantization;
4mod similarity;
5mod store;
6
7pub use collection::VectorCollection;
8pub use hnsw::HnswIndex;
9pub use quantization::QuantizedVector;
10pub use similarity::{Distance, DistanceMetric};
11pub use store::VectorStore;
12
13use serde::{Deserialize, Serialize};
14use std::collections::HashMap;
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct Vector {
18    pub id: String,
19    pub embedding: Vec<f32>,
20    pub metadata: HashMap<String, String>,
21}
22
23impl Vector {
24    pub fn new(id: impl Into<String>, embedding: Vec<f32>) -> Self {
25        Self {
26            id: id.into(),
27            embedding,
28            metadata: HashMap::new(),
29        }
30    }
31
32    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
33        self.metadata.insert(key.into(), value.into());
34        self
35    }
36
37    pub fn dimension(&self) -> usize {
38        self.embedding.len()
39    }
40
41    /// Quantize this vector to 8-bit
42    pub fn quantize_8bit(&self) -> QuantizedVector {
43        QuantizedVector::quantize_8bit(&self.embedding)
44    }
45
46    /// Quantize this vector to 16-bit
47    pub fn quantize_16bit(&self) -> QuantizedVector {
48        QuantizedVector::quantize_16bit(&self.embedding)
49    }
50}
51
52#[derive(Debug, Clone)]
53pub struct SearchResult {
54    pub id: String,
55    pub distance: f32,
56    pub metadata: HashMap<String, String>,
57}