Skip to content

Commit bf5c104

Browse files
feat: Adding Async Vector Store (#16)
* feat: Adding Async Vector Store * Remove cluster from fixtures. * chore: pr comments to remove scann * chore: fix extra args in DistanceStrategy * feat: Adding Vector Store (#17) * feat: Adding Vector Store * remove cluster from fixtures * minor fix in init * chore: address pr comments to remove scann * chore: add more type ignore statements to skip lint checks --------- Co-authored-by: Vishwaraj Anand <vishwarajanand@google.com> * chore: remove IVF Index due to lack of support on Cloud SQL --------- Co-authored-by: Vishwaraj Anand <vishwarajanand@google.com> Co-authored-by: Vishwaraj Anand <vishwaraj.anand00@gmail.com>
1 parent 9f0f0d6 commit bf5c104

File tree

8 files changed

+2731
-0
lines changed

8 files changed

+2731
-0
lines changed

src/llama_index_cloud_sql_pg/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
from .document_store import PostgresDocumentStore
1616
from .engine import Column, PostgresEngine
1717
from .index_store import PostgresIndexStore
18+
from .vector_store import PostgresVectorStore
1819
from .version import __version__
1920

2021
_all = [
2122
"Column",
2223
"PostgresEngine",
2324
"PostgresDocumentStore",
2425
"PostgresIndexStore",
26+
"PostgresVectorStore",
2527
"__version__",
2628
]

src/llama_index_cloud_sql_pg/async_vector_store.py

Lines changed: 675 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)