post_cortex_embeddings/vector_db/config.rs
1// Copyright (c) 2025, 2026 Julius ML
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12
13//! Vector database configuration and search quality presets.
14
15use std::path::PathBuf;
16
17/// Search mode for vector similarity search
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
19pub enum SearchMode {
20 /// Exact search using full linear scan - highest accuracy, slowest
21 Exact,
22 /// Approximate search using HNSW - fastest, good accuracy
23 Approximate,
24 /// Balanced search with optimized HNSW parameters - good speed/accuracy tradeoff
25 #[default]
26 Balanced,
27}
28
29/// Search quality preset for automatic parameter tuning
30#[derive(Debug, Clone, Copy)]
31pub enum SearchQualityPreset {
32 /// Fast search with lower accuracy (ef_search = 32)
33 Fast,
34 /// Balanced search (ef_search = 64)
35 Balanced,
36 /// Accurate search with higher recall (ef_search = 128)
37 Accurate,
38 /// Maximum accuracy search (ef_search = 256)
39 Maximum,
40}
41
42impl SearchQualityPreset {
43 /// Get ef_search value for this preset
44 pub fn ef_search(&self) -> usize {
45 match self {
46 SearchQualityPreset::Fast => 32,
47 SearchQualityPreset::Balanced => 64,
48 SearchQualityPreset::Accurate => 128,
49 SearchQualityPreset::Maximum => 256,
50 }
51 }
52}
53
54/// Configuration for the vector database
55#[derive(Debug, Clone)]
56pub struct VectorDbConfig {
57 /// Vector dimension (must match embedding model)
58 pub dimension: usize,
59 /// Maximum number of connections per node in HNSW
60 pub max_connections: usize,
61 /// Size of the dynamic candidate list
62 pub ef_construction: usize,
63 /// Size of the search candidate list
64 pub ef_search: usize,
65 /// Number of layers in HNSW hierarchy
66 pub num_layers: usize,
67 /// Enable vector quantization for memory optimization
68 pub enable_quantization: bool,
69 /// Quantization buckets (power of 2)
70 pub quantization_buckets: usize,
71 /// Enable HNSW indexing
72 pub enable_hnsw_index: bool,
73 /// Distance threshold for considering vectors similar
74 pub distance_threshold: f32,
75 /// Maximum number of vectors to store
76 pub max_vectors: usize,
77 /// Auto-save interval (number of operations)
78 pub auto_save_interval: usize,
79 /// Enable persistent storage
80 pub persistent_storage: bool,
81 /// Storage path
82 pub storage_path: Option<PathBuf>,
83 /// Enable Product Quantization for memory optimization
84 pub enable_product_quantization: bool,
85 /// Number of subvectors for PQ (must divide dimension evenly)
86 pub pq_subvectors: usize,
87 /// Bits per PQ code (2^bits centroids per subvector)
88 pub pq_bits: usize,
89}
90
91impl Default for VectorDbConfig {
92 fn default() -> Self {
93 Self {
94 dimension: 384, // Default for MiniLM model
95 max_connections: 16,
96 ef_construction: 200,
97 ef_search: 50,
98 num_layers: 4,
99 enable_quantization: true,
100 quantization_buckets: 256,
101 enable_hnsw_index: true,
102 distance_threshold: 0.7,
103 max_vectors: 100_000,
104 auto_save_interval: 1000,
105 persistent_storage: false,
106 storage_path: None,
107 enable_product_quantization: false, // Disabled by default (experimental)
108 pq_subvectors: 8, // 384/8 = 48 dims per subvector
109 pq_bits: 8, // 256 centroids per subvector
110 }
111 }
112}