| 1 | // Copyright 2024 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use std::sync::Mutex; |
| 16 | |
| 17 | use lancedb::index::scalar::FtsIndexBuilder; |
| 18 | use lancedb::{ |
| 19 | index::{ |
| 20 | scalar::BTreeIndexBuilder, |
| 21 | vector::{IvfHnswPqIndexBuilder, IvfHnswSqIndexBuilder, IvfPqIndexBuilder}, |
| 22 | Index as LanceDbIndex, |
| 23 | }, |
| 24 | DistanceType, |
| 25 | }; |
| 26 | use pyo3::{ |
| 27 | exceptions::{PyRuntimeError, PyValueError}, |
| 28 | pyclass, pymethods, PyResult, |
| 29 | }; |
| 30 | |
| 31 | use crate::util::parse_distance_type; |
| 32 | |
| 33 | #[pyclass] |
| 34 | pub struct Index { |
| 35 | inner: Mutex<Option<LanceDbIndex>>, |
| 36 | } |
| 37 | |
| 38 | impl Index { |
| 39 | pub fn consume(&self) -> PyResult<LanceDbIndex> { |
| 40 | self.inner |
| 41 | .lock() |
| 42 | .unwrap() |
| 43 | .take() |
| 44 | .ok_or_else(|| PyRuntimeError::new_err("cannot use an Index more than once")) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[pymethods] |
| 49 | impl Index { |
| 50 | #[staticmethod] |
| 51 | pub fn ivf_pq( |
| 52 | distance_type: Option<String>, |
| 53 | num_partitions: Option<u32>, |
| 54 | num_sub_vectors: Option<u32>, |
| 55 | max_iterations: Option<u32>, |
| 56 | sample_rate: Option<u32>, |
| 57 | ) -> PyResult<Self> { |
| 58 | let mut ivf_pq_builder = IvfPqIndexBuilder::default(); |
| 59 | if let Some(distance_type) = distance_type { |
| 60 | let distance_type = match distance_type.as_str() { |
| 61 | "l2" => Ok(DistanceType::L2), |
| 62 | "cosine" => Ok(DistanceType::Cosine), |
| 63 | "dot" => Ok(DistanceType::Dot), |
| 64 | _ => Err(PyValueError::new_err(format!( |
| 65 | "Invalid distance type '{}'. Must be one of l2, cosine, or dot", |
| 66 | distance_type |
| 67 | ))), |
| 68 | }?; |
| 69 | ivf_pq_builder = ivf_pq_builder.distance_type(distance_type); |
| 70 | } |
| 71 | if let Some(num_partitions) = num_partitions { |
| 72 | ivf_pq_builder = ivf_pq_builder.num_partitions(num_partitions); |
| 73 | } |
| 74 | if let Some(num_sub_vectors) = num_sub_vectors { |
| 75 | ivf_pq_builder = ivf_pq_builder.num_sub_vectors(num_sub_vectors); |
| 76 | } |
| 77 | if let Some(max_iterations) = max_iterations { |
| 78 | ivf_pq_builder = ivf_pq_builder.max_iterations(max_iterations); |
| 79 | } |
| 80 | if let Some(sample_rate) = sample_rate { |
| 81 | ivf_pq_builder = ivf_pq_builder.sample_rate(sample_rate); |
| 82 | } |
| 83 | Ok(Self { |
| 84 | inner: Mutex::new(Some(LanceDbIndex::IvfPq(ivf_pq_builder))), |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | #[staticmethod] |
| 89 | pub fn btree() -> PyResult<Self> { |
| 90 | Ok(Self { |
| 91 | inner: Mutex::new(Some(LanceDbIndex::BTree(BTreeIndexBuilder::default()))), |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | #[staticmethod] |
| 96 | pub fn bitmap() -> PyResult<Self> { |
| 97 | Ok(Self { |
| 98 | inner: Mutex::new(Some(LanceDbIndex::Bitmap(Default::default()))), |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | #[staticmethod] |
| 103 | pub fn label_list() -> PyResult<Self> { |
| 104 | Ok(Self { |
| 105 | inner: Mutex::new(Some(LanceDbIndex::LabelList(Default::default()))), |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | #[staticmethod] |
| 110 | pub fn fts(with_position: Option<bool>) -> Self { |
| 111 | let mut opts = FtsIndexBuilder::default(); |
| 112 | if let Some(with_position) = with_position { |
| 113 | opts = opts.with_position(with_position); |
| 114 | } |
| 115 | Self { |
| 116 | inner: Mutex::new(Some(LanceDbIndex::FTS(opts))), |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[staticmethod] |
| 121 | pub fn hnsw_pq( |
| 122 | distance_type: Option<String>, |
| 123 | num_partitions: Option<u32>, |
| 124 | num_sub_vectors: Option<u32>, |
| 125 | max_iterations: Option<u32>, |
| 126 | sample_rate: Option<u32>, |
| 127 | m: Option<u32>, |
| 128 | ef_construction: Option<u32>, |
| 129 | ) -> PyResult<Self> { |
| 130 | let mut hnsw_pq_builder = IvfHnswPqIndexBuilder::default(); |
| 131 | if let Some(distance_type) = distance_type { |
| 132 | let distance_type = parse_distance_type(distance_type)?; |
| 133 | hnsw_pq_builder = hnsw_pq_builder.distance_type(distance_type); |
| 134 | } |
| 135 | if let Some(num_partitions) = num_partitions { |
| 136 | hnsw_pq_builder = hnsw_pq_builder.num_partitions(num_partitions); |
| 137 | } |
| 138 | if let Some(num_sub_vectors) = num_sub_vectors { |
| 139 | hnsw_pq_builder = hnsw_pq_builder.num_sub_vectors(num_sub_vectors); |
| 140 | } |
| 141 | if let Some(max_iterations) = max_iterations { |
| 142 | hnsw_pq_builder = hnsw_pq_builder.max_iterations(max_iterations); |
| 143 | } |
| 144 | if let Some(sample_rate) = sample_rate { |
| 145 | hnsw_pq_builder = hnsw_pq_builder.sample_rate(sample_rate); |
| 146 | } |
| 147 | if let Some(m) = m { |
| 148 | hnsw_pq_builder = hnsw_pq_builder.num_edges(m); |
| 149 | } |
| 150 | if let Some(ef_construction) = ef_construction { |
| 151 | hnsw_pq_builder = hnsw_pq_builder.ef_construction(ef_construction); |
| 152 | } |
| 153 | Ok(Self { |
| 154 | inner: Mutex::new(Some(LanceDbIndex::IvfHnswPq(hnsw_pq_builder))), |
| 155 | }) |
| 156 | } |
| 157 | |
| 158 | #[staticmethod] |
| 159 | pub fn hnsw_sq( |
| 160 | distance_type: Option<String>, |
| 161 | num_partitions: Option<u32>, |
| 162 | max_iterations: Option<u32>, |
| 163 | sample_rate: Option<u32>, |
| 164 | m: Option<u32>, |
| 165 | ef_construction: Option<u32>, |
| 166 | ) -> PyResult<Self> { |
| 167 | let mut hnsw_sq_builder = IvfHnswSqIndexBuilder::default(); |
| 168 | if let Some(distance_type) = distance_type { |
| 169 | let distance_type = parse_distance_type(distance_type)?; |
| 170 | hnsw_sq_builder = hnsw_sq_builder.distance_type(distance_type); |
| 171 | } |
| 172 | if let Some(num_partitions) = num_partitions { |
| 173 | hnsw_sq_builder = hnsw_sq_builder.num_partitions(num_partitions); |
| 174 | } |
| 175 | if let Some(max_iterations) = max_iterations { |
| 176 | hnsw_sq_builder = hnsw_sq_builder.max_iterations(max_iterations); |
| 177 | } |
| 178 | if let Some(sample_rate) = sample_rate { |
| 179 | hnsw_sq_builder = hnsw_sq_builder.sample_rate(sample_rate); |
| 180 | } |
| 181 | if let Some(m) = m { |
| 182 | hnsw_sq_builder = hnsw_sq_builder.num_edges(m); |
| 183 | } |
| 184 | if let Some(ef_construction) = ef_construction { |
| 185 | hnsw_sq_builder = hnsw_sq_builder.ef_construction(ef_construction); |
| 186 | } |
| 187 | Ok(Self { |
| 188 | inner: Mutex::new(Some(LanceDbIndex::IvfHnswSq(hnsw_sq_builder))), |
| 189 | }) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #[pyclass(get_all)] |
| 194 | /// A description of an index currently configured on a column |
| 195 | pub struct IndexConfig { |
| 196 | /// The type of the index |
| 197 | pub index_type: String, |
| 198 | /// The columns in the index |
| 199 | /// |
| 200 | /// Currently this is always a list of size 1. In the future there may |
| 201 | /// be more columns to represent composite indices. |
| 202 | pub columns: Vec<String>, |
| 203 | /// Name of the index. |
| 204 | pub name: String, |
| 205 | } |
| 206 | |
| 207 | #[pymethods] |
| 208 | impl IndexConfig { |
| 209 | pub fn __repr__(&self) -> String { |
| 210 | format!("Index({}, columns={:?})", self.index_type, self.columns) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | impl From<lancedb::index::IndexConfig> for IndexConfig { |
| 215 | fn from(value: lancedb::index::IndexConfig) -> Self { |
| 216 | let index_type = format!("{:?}", value.index_type); |
| 217 | Self { |
| 218 | index_type, |
| 219 | columns: value.columns, |
| 220 | name: value.name, |
| 221 | } |
| 222 | } |
| 223 | } |