|
| 1 | +# Copyright 2024 Google LLC |
| 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 | +import enum |
| 16 | +from abc import ABC, abstractmethod |
| 17 | +from dataclasses import dataclass, field |
| 18 | +from typing import List, Optional |
| 19 | + |
| 20 | + |
| 21 | +@dataclass |
| 22 | +class StrategyMixin: |
| 23 | + operator: str |
| 24 | + search_function: str |
| 25 | + index_function: str |
| 26 | + |
| 27 | + |
| 28 | +class DistanceStrategy(StrategyMixin, enum.Enum): |
| 29 | + """Enumerator of the Distance strategies.""" |
| 30 | + |
| 31 | + EUCLIDEAN = "<->", "l2_distance", "vector_l2_ops" |
| 32 | + COSINE_DISTANCE = "<=>", "cosine_distance", "vector_cosine_ops" |
| 33 | + INNER_PRODUCT = "<#>", "inner_product", "vector_ip_ops" |
| 34 | + |
| 35 | + |
| 36 | +DEFAULT_DISTANCE_STRATEGY: DistanceStrategy = DistanceStrategy.COSINE_DISTANCE |
| 37 | +DEFAULT_INDEX_NAME_SUFFIX: str = "li_vectorindex" |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class BaseIndex(ABC): |
| 42 | + name: Optional[str] = None |
| 43 | + index_type: str = "base" |
| 44 | + distance_strategy: DistanceStrategy = field( |
| 45 | + default_factory=lambda: DistanceStrategy.COSINE_DISTANCE |
| 46 | + ) |
| 47 | + partial_indexes: Optional[List[str]] = None |
| 48 | + |
| 49 | + @abstractmethod |
| 50 | + def index_options(self) -> str: |
| 51 | + """Set index query options for vector store initialization.""" |
| 52 | + raise NotImplementedError( |
| 53 | + "index_options method must be implemented by subclass" |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +@dataclass |
| 58 | +class ExactNearestNeighbor(BaseIndex): |
| 59 | + index_type: str = "exactnearestneighbor" |
| 60 | + |
| 61 | + |
| 62 | +@dataclass |
| 63 | +class QueryOptions(ABC): |
| 64 | + @abstractmethod |
| 65 | + def to_string(self) -> str: |
| 66 | + """Convert index attributes to string.""" |
| 67 | + raise NotImplementedError("to_string method must be implemented by subclass") |
| 68 | + |
| 69 | + |
| 70 | +@dataclass |
| 71 | +class HNSWIndex(BaseIndex): |
| 72 | + index_type: str = "hnsw" |
| 73 | + m: int = 16 |
| 74 | + ef_construction: int = 64 |
| 75 | + |
| 76 | + def index_options(self) -> str: |
| 77 | + """Set index query options for vector store initialization.""" |
| 78 | + return f"(m = {self.m}, ef_construction = {self.ef_construction})" |
| 79 | + |
| 80 | + |
| 81 | +@dataclass |
| 82 | +class HNSWQueryOptions(QueryOptions): |
| 83 | + ef_search: int = 40 |
| 84 | + |
| 85 | + def to_string(self) -> str: |
| 86 | + """Convert index attributes to string.""" |
| 87 | + return f"hnsw.ef_search = {self.ef_search}" |
| 88 | + |
| 89 | + |
| 90 | +@dataclass |
| 91 | +class IVFFlatIndex(BaseIndex): |
| 92 | + index_type: str = "ivfflat" |
| 93 | + lists: int = 100 |
| 94 | + |
| 95 | + def index_options(self) -> str: |
| 96 | + """Set index query options for vector store initialization.""" |
| 97 | + return f"(lists = {self.lists})" |
| 98 | + |
| 99 | + |
| 100 | +@dataclass |
| 101 | +class IVFFlatQueryOptions(QueryOptions): |
| 102 | + probes: int = 1 |
| 103 | + |
| 104 | + def to_string(self) -> str: |
| 105 | + """Convert index attributes to string.""" |
| 106 | + return f"ivflfat.probes = {self.probes}" |
0 commit comments