pgvector-haystack 3.1.0__py3-none-any.whl → 3.2.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.
@@ -51,8 +51,12 @@ def _from_pg_to_haystack_documents(documents: List[Dict[str, Any]]) -> List[Docu
51
51
  blob_mime_type = haystack_dict.pop("blob_mime_type")
52
52
 
53
53
  # convert the embedding to a list of floats
54
+ # for strange reasons, halfvec and vector have different methods to convert the embedding to a list
54
55
  if document.get("embedding") is not None:
55
- haystack_dict["embedding"] = document["embedding"].tolist()
56
+ if hasattr(document["embedding"], "tolist"): # vector
57
+ haystack_dict["embedding"] = document["embedding"].tolist()
58
+ else: # halfvec
59
+ haystack_dict["embedding"] = document["embedding"].to_list()
56
60
 
57
61
  haystack_document = Document.from_dict(haystack_dict)
58
62
 
@@ -26,7 +26,7 @@ logger = logging.getLogger(__name__)
26
26
  CREATE_TABLE_STATEMENT = """
27
27
  CREATE TABLE {schema_name}.{table_name} (
28
28
  id VARCHAR(128) PRIMARY KEY,
29
- embedding VECTOR({embedding_dimension}),
29
+ embedding {embedding_col_type}({embedding_dimension}),
30
30
  content TEXT,
31
31
  blob_data BYTEA,
32
32
  blob_meta JSONB,
@@ -64,6 +64,12 @@ VECTOR_FUNCTION_TO_POSTGRESQL_OPS = {
64
64
  "l2_distance": "vector_l2_ops",
65
65
  }
66
66
 
67
+ HALF_VECTOR_FUNCTION_TO_POSTGRESQL_OPS = {
68
+ "cosine_similarity": "halfvec_cosine_ops",
69
+ "inner_product": "halfvec_ip_ops",
70
+ "l2_distance": "halfvec_l2_ops",
71
+ }
72
+
67
73
  HNSW_INDEX_CREATION_VALID_KWARGS = ["m", "ef_construction"]
68
74
 
69
75
 
@@ -81,6 +87,7 @@ class PgvectorDocumentStore:
81
87
  table_name: str = "haystack_documents",
82
88
  language: str = "english",
83
89
  embedding_dimension: int = 768,
90
+ vector_type: Literal["vector", "halfvec"] = "vector",
84
91
  vector_function: Literal["cosine_similarity", "inner_product", "l2_distance"] = "cosine_similarity",
85
92
  recreate_table: bool = False,
86
93
  search_strategy: Literal["exact_nearest_neighbor", "hnsw"] = "exact_nearest_neighbor",
@@ -112,6 +119,11 @@ class PgvectorDocumentStore:
112
119
  `SELECT cfgname FROM pg_ts_config;`.
113
120
  More information can be found in this [StackOverflow answer](https://stackoverflow.com/a/39752553).
114
121
  :param embedding_dimension: The dimension of the embedding.
122
+ :param vector_type: The type of vector used for embedding storage.
123
+ "vector" is the default.
124
+ "halfvec" stores embeddings in half-precision, which is particularly useful for high-dimensional embeddings
125
+ (dimension greater than 2,000 and up to 4,000). Requires pgvector versions 0.7.0 or later. For more
126
+ information, see the [pgvector documentation](https://github.com/pgvector/pgvector?tab=readme-ov-file).
115
127
  :param vector_function: The similarity function to use when searching for similar embeddings.
116
128
  `"cosine_similarity"` and `"inner_product"` are similarity functions and
117
129
  higher scores indicate greater similarity between the documents.
@@ -145,6 +157,10 @@ class PgvectorDocumentStore:
145
157
  self.table_name = table_name
146
158
  self.schema_name = schema_name
147
159
  self.embedding_dimension = embedding_dimension
160
+ if vector_type not in ["vector", "halfvec"]:
161
+ msg = "vector_type must be one of ['vector', 'halfvec']"
162
+ raise ValueError(msg)
163
+ self.vector_type = vector_type
148
164
  if vector_function not in VALID_VECTOR_FUNCTIONS:
149
165
  msg = f"vector_function must be one of {VALID_VECTOR_FUNCTIONS}, but got {vector_function}"
150
166
  raise ValueError(msg)
@@ -180,6 +196,7 @@ class PgvectorDocumentStore:
180
196
  schema_name=self.schema_name,
181
197
  table_name=self.table_name,
182
198
  embedding_dimension=self.embedding_dimension,
199
+ vector_type=self.vector_type,
183
200
  vector_function=self.vector_function,
184
201
  recreate_table=self.recreate_table,
185
202
  search_strategy=self.search_strategy,
@@ -376,6 +393,7 @@ class PgvectorDocumentStore:
376
393
  schema_name=Identifier(self.schema_name),
377
394
  table_name=Identifier(self.table_name),
378
395
  embedding_dimension=SQLLiteral(self.embedding_dimension),
396
+ embedding_col_type=SQL(self.vector_type),
379
397
  )
380
398
 
381
399
  sql_keyword_index_exists = SQL(
@@ -516,7 +534,16 @@ class PgvectorDocumentStore:
516
534
  index_name=Identifier(self.hnsw_index_name),
517
535
  )
518
536
 
519
- pg_ops = VECTOR_FUNCTION_TO_POSTGRESQL_OPS[self.vector_function]
537
+ if self.vector_type == "halfvec":
538
+ if self.vector_function not in HALF_VECTOR_FUNCTION_TO_POSTGRESQL_OPS:
539
+ msg = f"Unsupported vector_function '{self.vector_function}' for halfvec type."
540
+ raise ValueError(msg)
541
+ pg_ops = HALF_VECTOR_FUNCTION_TO_POSTGRESQL_OPS[self.vector_function]
542
+ else:
543
+ if self.vector_function not in VECTOR_FUNCTION_TO_POSTGRESQL_OPS:
544
+ msg = f"Unsupported vector_function '{self.vector_function}' for vector type."
545
+ raise ValueError(msg)
546
+ pg_ops = VECTOR_FUNCTION_TO_POSTGRESQL_OPS[self.vector_function]
520
547
 
521
548
  sql_create_hnsw_index = SQL(
522
549
  "CREATE INDEX {index_name} ON {schema_name}.{table_name} USING hnsw (embedding {ops})"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pgvector-haystack
3
- Version: 3.1.0
3
+ Version: 3.2.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,10 +2,10 @@ haystack_integrations/components/retrievers/pgvector/__init__.py,sha256=n4q4pUAp
2
2
  haystack_integrations/components/retrievers/pgvector/embedding_retriever.py,sha256=uK-ak1ZASjprix-5wgKoqP8rLnQfpQzExw_6X8fXUxg,9131
3
3
  haystack_integrations/components/retrievers/pgvector/keyword_retriever.py,sha256=5NECAn5qlLqcFTvRf_c-x1crdqdgTCeLHsmCxtD3pHk,6784
4
4
  haystack_integrations/document_stores/pgvector/__init__.py,sha256=Ge-7ggarfr3x8UqNdFHUuFGiu52u49sOVe3l16WEwZU,197
5
- haystack_integrations/document_stores/pgvector/converters.py,sha256=ECNF04RcStH_xpjtY125xB00co0wN8OWNI1fFiv0fUA,2448
6
- haystack_integrations/document_stores/pgvector/document_store.py,sha256=2Mphtj5K7yRVroEy_esvkxhzTN90glgl_WeAyWIrI5Q,44981
5
+ haystack_integrations/document_stores/pgvector/converters.py,sha256=TQ85iX3IGkcAEo_WnnwvZl7q9RYwmZBoXXQHKWejQu0,2733
6
+ haystack_integrations/document_stores/pgvector/document_store.py,sha256=YjD_ZYZ0lrB3vG1EKkPZCWkNmOuhoG_D_EFMq521rQA,46547
7
7
  haystack_integrations/document_stores/pgvector/filters.py,sha256=1xfU-DJcqFUFQtBezZCA8UsYFN0D9znXtsick1KANoU,8998
8
- pgvector_haystack-3.1.0.dist-info/METADATA,sha256=fhF5qTdD0UkVTtLRjgwVJui26CzrqASA6UNdhiwEhMY,2235
9
- pgvector_haystack-3.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
- pgvector_haystack-3.1.0.dist-info/licenses/LICENSE.txt,sha256=B05uMshqTA74s-0ltyHKI6yoPfJ3zYgQbvcXfDVGFf8,10280
11
- pgvector_haystack-3.1.0.dist-info/RECORD,,
8
+ pgvector_haystack-3.2.0.dist-info/METADATA,sha256=0lpw_V7hdm1Dop_P5_QDsJORq9H36d9DgIo1M8gU49o,2235
9
+ pgvector_haystack-3.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
+ pgvector_haystack-3.2.0.dist-info/licenses/LICENSE.txt,sha256=B05uMshqTA74s-0ltyHKI6yoPfJ3zYgQbvcXfDVGFf8,10280
11
+ pgvector_haystack-3.2.0.dist-info/RECORD,,