1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
"""
Defines the 'Client' class
Importing from the `vecs.client` directly is not supported.
All public classes, enums, and functions are re-exported by the top level `vecs` module.
"""
from __future__ import annotations
import logging
import time
from typing import TYPE_CHECKING, List, Optional
import sqlalchemy
from deprecated import deprecated
from sqlalchemy import MetaData, create_engine, text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import QueuePool
from .adapter import Adapter
from .exc import CollectionNotFound
if TYPE_CHECKING:
from r2r.vecs.collection import Collection
logger = logging.getLogger(__name__)
class Client:
"""
The `vecs.Client` class serves as an interface to a PostgreSQL database with pgvector support. It facilitates
the creation, retrieval, listing and deletion of vector collections, while managing connections to the
database.
A `Client` instance represents a connection to a PostgreSQL database. This connection can be used to create
and manipulate vector collections, where each collection is a group of vector records in a PostgreSQL table.
The `vecs.Client` class can be also supports usage as a context manager to ensure the connection to the database
is properly closed after operations, or used directly.
Example usage:
DB_CONNECTION = "postgresql://<user>:<password>@<host>:<port>/<db_name>"
with vecs.create_client(DB_CONNECTION) as vx:
# do some work
pass
# OR
vx = vecs.create_client(DB_CONNECTION)
# do some work
vx.disconnect()
"""
def __init__(
self,
connection_string: str,
pool_size: int = 1,
max_retries: int = 3,
retry_delay: int = 1,
):
self.engine = create_engine(
connection_string,
pool_size=pool_size,
poolclass=QueuePool,
pool_recycle=300, # Recycle connections after 5 min
)
self.meta = MetaData(schema="vecs")
self.Session = sessionmaker(self.engine)
self.max_retries = max_retries
self.retry_delay = retry_delay
self.vector_version: Optional[str] = None
self._initialize_database()
def _initialize_database(self):
retries = 0
error = None
while retries < self.max_retries:
try:
with self.Session() as sess:
with sess.begin():
self._create_schema(sess)
self._create_extension(sess)
self._get_vector_version(sess)
return
except Exception as e:
logger.warning(
f"Database connection error: {str(e)}. Retrying in {self.retry_delay} seconds..."
)
retries += 1
time.sleep(self.retry_delay)
error = e
error_message = f"Failed to initialize database after {self.max_retries} retries with error: {str(error)}"
logger.error(error_message)
raise RuntimeError(error_message)
def _create_schema(self, sess):
try:
sess.execute(text("CREATE SCHEMA IF NOT EXISTS vecs;"))
except Exception as e:
logger.warning(f"Failed to create schema: {str(e)}")
def _create_extension(self, sess):
try:
sess.execute(text("CREATE EXTENSION IF NOT EXISTS vector;"))
sess.execute(text("CREATE EXTENSION IF NOT EXISTS pg_trgm;"))
sess.execute(text("CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;"))
except Exception as e:
logger.warning(f"Failed to create extension: {str(e)}")
def _get_vector_version(self, sess):
try:
self.vector_version = sess.execute(
text(
"SELECT installed_version FROM pg_available_extensions WHERE name = 'vector' LIMIT 1;"
)
).scalar_one()
except sqlalchemy.exc.InternalError as e:
logger.error(f"Failed with internal alchemy error: {str(e)}")
import psycopg2
if isinstance(e.orig, psycopg2.errors.InFailedSqlTransaction):
sess.rollback()
self.vector_version = sess.execute(
text(
"SELECT installed_version FROM pg_available_extensions WHERE name = 'vector' LIMIT 1;"
)
).scalar_one()
else:
raise e
except Exception as e:
logger.error(f"Failed to retrieve vector version: {str(e)}")
raise e
def _supports_hnsw(self):
return (
not self.vector_version.startswith("0.4")
and not self.vector_version.startswith("0.3")
and not self.vector_version.startswith("0.2")
and not self.vector_version.startswith("0.1")
and not self.vector_version.startswith("0.0")
)
def get_or_create_collection(
self,
name: str,
*,
dimension: Optional[int] = None,
adapter: Optional[Adapter] = None,
) -> Collection:
"""
Get a vector collection by name, or create it if no collection with
*name* exists.
Args:
name (str): The name of the collection.
Keyword Args:
dimension (int): The dimensionality of the vectors in the collection.
pipeline (int): The dimensionality of the vectors in the collection.
Returns:
Collection: The created collection.
Raises:
CollectionAlreadyExists: If a collection with the same name already exists
"""
from r2r.vecs.collection import Collection
adapter_dimension = adapter.exported_dimension if adapter else None
collection = Collection(
name=name,
dimension=dimension or adapter_dimension, # type: ignore
client=self,
adapter=adapter,
)
return collection._create_if_not_exists()
@deprecated("use Client.get_or_create_collection")
def create_collection(self, name: str, dimension: int) -> Collection:
"""
Create a new vector collection.
Args:
name (str): The name of the collection.
dimension (int): The dimensionality of the vectors in the collection.
Returns:
Collection: The created collection.
Raises:
CollectionAlreadyExists: If a collection with the same name already exists
"""
from r2r.vecs.collection import Collection
return Collection(name, dimension, self)._create()
@deprecated("use Client.get_or_create_collection")
def get_collection(self, name: str) -> Collection:
"""
Retrieve an existing vector collection.
Args:
name (str): The name of the collection.
Returns:
Collection: The retrieved collection.
Raises:
CollectionNotFound: If no collection with the given name exists.
"""
from r2r.vecs.collection import Collection
query = text(
f"""
select
relname as table_name,
atttypmod as embedding_dim
from
pg_class pc
join pg_attribute pa
on pc.oid = pa.attrelid
where
pc.relnamespace = 'vecs'::regnamespace
and pc.relkind = 'r'
and pa.attname = 'vec'
and not pc.relname ^@ '_'
and pc.relname = :name
"""
).bindparams(name=name)
with self.Session() as sess:
query_result = sess.execute(query).fetchone()
if query_result is None:
raise CollectionNotFound(
"No collection found with requested name"
)
name, dimension = query_result
return Collection(
name,
dimension,
self,
)
def list_collections(self) -> List["Collection"]:
"""
List all vector collections.
Returns:
list[Collection]: A list of all collections.
"""
from r2r.vecs.collection import Collection
return Collection._list_collections(self)
def delete_collection(self, name: str) -> None:
"""
Delete a vector collection.
If no collection with requested name exists, does nothing.
Args:
name (str): The name of the collection.
Returns:
None
"""
from r2r.vecs.collection import Collection
Collection(name, -1, self)._drop()
return
def disconnect(self) -> None:
"""
Disconnect the client from the database.
Returns:
None
"""
self.engine.dispose()
logger.info("Disconnected from the database.")
return
def __enter__(self) -> "Client":
"""
Enable use of the 'with' statement.
Returns:
Client: The current instance of the Client.
"""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""
Disconnect the client on exiting the 'with' statement context.
Args:
exc_type: The exception type, if any.
exc_val: The exception value, if any.
exc_tb: The traceback, if any.
Returns:
None
"""
self.disconnect()
return
|