Skip to content

Commit caf1a2e

Browse files
committed
Added docs for binary quantization with SQLAlchemy [skip ci]
1 parent dc9a8f9 commit caf1a2e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,38 @@ order = func.cast(Item.embedding, HALFVEC(3)).l2_distance([3, 1, 2])
271271
session.scalars(select(Item).order_by(order).limit(5))
272272
```
273273

274+
#### Binary Quantization
275+
276+
Use expression indexing for binary quantization
277+
278+
```python
279+
from pgvector.sqlalchemy import BIT
280+
from sqlalchemy.sql import func
281+
282+
index = Index(
283+
'my_index',
284+
func.cast(func.binary_quantize(Item.embedding), BIT(3)).label('embedding'),
285+
postgresql_using='hnsw',
286+
postgresql_with={'m': 16, 'ef_construction': 64},
287+
postgresql_ops={'embedding': 'bit_hamming_ops'}
288+
)
289+
```
290+
291+
Get the nearest neighbors by Hamming distance
292+
293+
```python
294+
order = func.cast(func.binary_quantize(Item.embedding), BIT(3)).hamming_distance(func.binary_quantize(func.cast([3, -1, 2], VECTOR(3))))
295+
session.scalars(select(Item).order_by(order).limit(5))
296+
```
297+
298+
Re-rank by the original vectors for better recall
299+
300+
```python
301+
order = func.cast(func.binary_quantize(Item.embedding), BIT(3)).hamming_distance(func.binary_quantize(func.cast([3, -1, 2], VECTOR(3))))
302+
subquery = session.query(Item).order_by(order).limit(20).subquery()
303+
session.scalars(select(subquery).order_by(subquery.c.embedding.cosine_distance([3, -1, 2])).limit(5))
304+
```
305+
274306
#### Arrays
275307

276308
Add an array column

0 commit comments

Comments
 (0)