pgvector-haystack 0.2.0__py3-none-any.whl → 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- haystack_integrations/document_stores/pgvector/document_store.py +50 -23
- {pgvector_haystack-0.2.0.dist-info → pgvector_haystack-0.3.0.dist-info}/METADATA +1 -1
- {pgvector_haystack-0.2.0.dist-info → pgvector_haystack-0.3.0.dist-info}/RECORD +5 -5
- {pgvector_haystack-0.2.0.dist-info → pgvector_haystack-0.3.0.dist-info}/WHEEL +0 -0
- {pgvector_haystack-0.2.0.dist-info → pgvector_haystack-0.3.0.dist-info}/licenses/LICENSE.txt +0 -0
|
@@ -145,26 +145,53 @@ class PgvectorDocumentStore:
|
|
|
145
145
|
self.hnsw_index_creation_kwargs = hnsw_index_creation_kwargs or {}
|
|
146
146
|
self.hnsw_ef_search = hnsw_ef_search
|
|
147
147
|
self.language = language
|
|
148
|
+
self._connection = None
|
|
149
|
+
self._cursor = None
|
|
150
|
+
self._dict_cursor = None
|
|
148
151
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
self.
|
|
152
|
+
@property
|
|
153
|
+
def cursor(self):
|
|
154
|
+
if self._cursor is None:
|
|
155
|
+
self._create_connection()
|
|
156
|
+
|
|
157
|
+
return self._cursor
|
|
158
|
+
|
|
159
|
+
@property
|
|
160
|
+
def dict_cursor(self):
|
|
161
|
+
if self._dict_cursor is None:
|
|
162
|
+
self._create_connection()
|
|
163
|
+
|
|
164
|
+
return self._dict_cursor
|
|
152
165
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
self.
|
|
166
|
+
@property
|
|
167
|
+
def connection(self):
|
|
168
|
+
if self._connection is None:
|
|
169
|
+
self._create_connection()
|
|
156
170
|
|
|
171
|
+
return self._connection
|
|
172
|
+
|
|
173
|
+
def _create_connection(self):
|
|
174
|
+
conn_str = self.connection_string.resolve_value() or ""
|
|
175
|
+
connection = connect(conn_str)
|
|
176
|
+
connection.autocommit = True
|
|
157
177
|
connection.execute("CREATE EXTENSION IF NOT EXISTS vector")
|
|
158
|
-
register_vector(connection)
|
|
178
|
+
register_vector(connection) # Note: this must be called before creating the cursors.
|
|
159
179
|
|
|
160
|
-
|
|
180
|
+
self._connection = connection
|
|
181
|
+
self._cursor = self._connection.cursor()
|
|
182
|
+
self._dict_cursor = self._connection.cursor(row_factory=dict_row)
|
|
183
|
+
|
|
184
|
+
# Init schema
|
|
185
|
+
if self.recreate_table:
|
|
161
186
|
self.delete_table()
|
|
162
187
|
self._create_table_if_not_exists()
|
|
163
188
|
self._create_keyword_index_if_not_exists()
|
|
164
189
|
|
|
165
|
-
if search_strategy == "hnsw":
|
|
190
|
+
if self.search_strategy == "hnsw":
|
|
166
191
|
self._handle_hnsw()
|
|
167
192
|
|
|
193
|
+
return self._connection
|
|
194
|
+
|
|
168
195
|
def to_dict(self) -> Dict[str, Any]:
|
|
169
196
|
"""
|
|
170
197
|
Serializes the component to a dictionary.
|
|
@@ -208,11 +235,11 @@ class PgvectorDocumentStore:
|
|
|
208
235
|
:param sql_query: The SQL query to execute.
|
|
209
236
|
:param params: The parameters to pass to the SQL query.
|
|
210
237
|
:param error_msg: The error message to use if an exception is raised.
|
|
211
|
-
:param cursor: The cursor to use to execute the SQL query. Defaults to self.
|
|
238
|
+
:param cursor: The cursor to use to execute the SQL query. Defaults to self.cursor.
|
|
212
239
|
"""
|
|
213
240
|
|
|
214
241
|
params = params or ()
|
|
215
|
-
cursor = cursor or self.
|
|
242
|
+
cursor = cursor or self.cursor
|
|
216
243
|
|
|
217
244
|
sql_query_str = sql_query.as_string(cursor) if not isinstance(sql_query, str) else sql_query
|
|
218
245
|
logger.debug("SQL query: %s\nParameters: %s", sql_query_str, params)
|
|
@@ -220,7 +247,7 @@ class PgvectorDocumentStore:
|
|
|
220
247
|
try:
|
|
221
248
|
result = cursor.execute(sql_query, params)
|
|
222
249
|
except Error as e:
|
|
223
|
-
self.
|
|
250
|
+
self.connection.rollback()
|
|
224
251
|
detailed_error_msg = f"{error_msg}.\nYou can find the SQL query and the parameters in the debug logs."
|
|
225
252
|
raise DocumentStoreError(detailed_error_msg) from e
|
|
226
253
|
|
|
@@ -371,7 +398,7 @@ class PgvectorDocumentStore:
|
|
|
371
398
|
sql_filter,
|
|
372
399
|
params,
|
|
373
400
|
error_msg="Could not filter documents from PgvectorDocumentStore.",
|
|
374
|
-
cursor=self.
|
|
401
|
+
cursor=self.dict_cursor,
|
|
375
402
|
)
|
|
376
403
|
|
|
377
404
|
records = result.fetchall()
|
|
@@ -408,16 +435,16 @@ class PgvectorDocumentStore:
|
|
|
408
435
|
|
|
409
436
|
sql_insert += SQL(" RETURNING id")
|
|
410
437
|
|
|
411
|
-
sql_query_str = sql_insert.as_string(self.
|
|
438
|
+
sql_query_str = sql_insert.as_string(self.cursor) if not isinstance(sql_insert, str) else sql_insert
|
|
412
439
|
logger.debug("SQL query: %s\nParameters: %s", sql_query_str, db_documents)
|
|
413
440
|
|
|
414
441
|
try:
|
|
415
|
-
self.
|
|
442
|
+
self.cursor.executemany(sql_insert, db_documents, returning=True)
|
|
416
443
|
except IntegrityError as ie:
|
|
417
|
-
self.
|
|
444
|
+
self.connection.rollback()
|
|
418
445
|
raise DuplicateDocumentError from ie
|
|
419
446
|
except Error as e:
|
|
420
|
-
self.
|
|
447
|
+
self.connection.rollback()
|
|
421
448
|
error_msg = (
|
|
422
449
|
"Could not write documents to PgvectorDocumentStore. \n"
|
|
423
450
|
"You can find the SQL query and the parameters in the debug logs."
|
|
@@ -428,9 +455,9 @@ class PgvectorDocumentStore:
|
|
|
428
455
|
# https://www.psycopg.org/psycopg3/docs/api/cursors.html#psycopg.Cursor.executemany
|
|
429
456
|
written_docs = 0
|
|
430
457
|
while True:
|
|
431
|
-
if self.
|
|
458
|
+
if self.cursor.fetchone():
|
|
432
459
|
written_docs += 1
|
|
433
|
-
if not self.
|
|
460
|
+
if not self.cursor.nextset():
|
|
434
461
|
break
|
|
435
462
|
|
|
436
463
|
return written_docs
|
|
@@ -483,8 +510,8 @@ class PgvectorDocumentStore:
|
|
|
483
510
|
|
|
484
511
|
# postgresql returns the embedding as a string
|
|
485
512
|
# so we need to convert it to a list of floats
|
|
486
|
-
if document.get("embedding"):
|
|
487
|
-
haystack_dict["embedding"] =
|
|
513
|
+
if document.get("embedding") is not None:
|
|
514
|
+
haystack_dict["embedding"] = document["embedding"].tolist()
|
|
488
515
|
|
|
489
516
|
haystack_document = Document.from_dict(haystack_dict)
|
|
490
517
|
|
|
@@ -555,7 +582,7 @@ class PgvectorDocumentStore:
|
|
|
555
582
|
sql_query,
|
|
556
583
|
(query, *where_params),
|
|
557
584
|
error_msg="Could not retrieve documents from PgvectorDocumentStore.",
|
|
558
|
-
cursor=self.
|
|
585
|
+
cursor=self.dict_cursor,
|
|
559
586
|
)
|
|
560
587
|
|
|
561
588
|
records = result.fetchall()
|
|
@@ -633,7 +660,7 @@ class PgvectorDocumentStore:
|
|
|
633
660
|
sql_query,
|
|
634
661
|
params,
|
|
635
662
|
error_msg="Could not retrieve documents from PgvectorDocumentStore.",
|
|
636
|
-
cursor=self.
|
|
663
|
+
cursor=self.dict_cursor,
|
|
637
664
|
)
|
|
638
665
|
|
|
639
666
|
records = result.fetchall()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: pgvector-haystack
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: An integration of pgvector (vector search extension for Postgres) with Haystack
|
|
5
5
|
Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations
|
|
6
6
|
Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/pgvector/README.md
|
|
@@ -2,9 +2,9 @@ haystack_integrations/components/retrievers/pgvector/__init__.py,sha256=n4q4pUAp
|
|
|
2
2
|
haystack_integrations/components/retrievers/pgvector/embedding_retriever.py,sha256=6LuQmF7JmPOCU_I10ksPcbDC6QMu-2SOq0--BQxBxpg,6523
|
|
3
3
|
haystack_integrations/components/retrievers/pgvector/keyword_retriever.py,sha256=fAJ3M2l_Pt8zKqHES1l1L4gM16BOHeZWq8xc18Bhq7A,4624
|
|
4
4
|
haystack_integrations/document_stores/pgvector/__init__.py,sha256=Ge-7ggarfr3x8UqNdFHUuFGiu52u49sOVe3l16WEwZU,197
|
|
5
|
-
haystack_integrations/document_stores/pgvector/document_store.py,sha256=
|
|
5
|
+
haystack_integrations/document_stores/pgvector/document_store.py,sha256=eXfHx1hAQu_s4CN8rBMK-3WYDVYFQOIAOazBqA3wG_s,27629
|
|
6
6
|
haystack_integrations/document_stores/pgvector/filters.py,sha256=751_Z1QnXJCG0GWC2jcfeZ25NNYfJewQEIUCeJS70PA,8212
|
|
7
|
-
pgvector_haystack-0.
|
|
8
|
-
pgvector_haystack-0.
|
|
9
|
-
pgvector_haystack-0.
|
|
10
|
-
pgvector_haystack-0.
|
|
7
|
+
pgvector_haystack-0.3.0.dist-info/METADATA,sha256=7TIp_Uibizl86fDfdGDqbUCnxSR9IUepEepNJQE7Fm4,2219
|
|
8
|
+
pgvector_haystack-0.3.0.dist-info/WHEEL,sha256=zEMcRr9Kr03x1ozGwg5v9NQBKn3kndp6LSoSlVg-jhU,87
|
|
9
|
+
pgvector_haystack-0.3.0.dist-info/licenses/LICENSE.txt,sha256=B05uMshqTA74s-0ltyHKI6yoPfJ3zYgQbvcXfDVGFf8,10280
|
|
10
|
+
pgvector_haystack-0.3.0.dist-info/RECORD,,
|
|
File without changes
|
{pgvector_haystack-0.2.0.dist-info → pgvector_haystack-0.3.0.dist-info}/licenses/LICENSE.txt
RENAMED
|
File without changes
|