@@ -103,7 +103,6 @@ class Item(Base):
103103 'sqlalchemy_orm_half_precision_index' ,
104104 func .cast (Item .embedding , HALFVEC (3 )).label ('embedding' ),
105105 postgresql_using = 'hnsw' ,
106- postgresql_with = {'m' : 16 , 'ef_construction' : 64 },
107106 postgresql_ops = {'embedding' : 'halfvec_l2_ops' }
108107)
109108half_precision_index .create (setup_engine )
@@ -112,7 +111,6 @@ class Item(Base):
112111 'sqlalchemy_orm_binary_quantize_index' ,
113112 func .cast (func .binary_quantize (Item .embedding ), BIT (3 )).label ('embedding' ),
114113 postgresql_using = 'hnsw' ,
115- postgresql_with = {'m' : 16 , 'ef_construction' : 64 },
116114 postgresql_ops = {'embedding' : 'bit_hamming_ops' }
117115)
118116binary_quantize_index .create (setup_engine )
@@ -528,6 +526,22 @@ def test_binary_quantize(self, engine):
528526 items = session .query (Item ).order_by (distance ).all ()
529527 assert [v .id for v in items ] == [2 , 3 , 1 ]
530528
529+ def test_binary_quantize_reranking (self , engine ):
530+ # recreate index (could also vacuum table)
531+ binary_quantize_index .drop (setup_engine )
532+ binary_quantize_index .create (setup_engine )
533+
534+ with Session (engine ) as session :
535+ session .add (Item (id = 1 , embedding = [- 1 , - 2 , - 3 ]))
536+ session .add (Item (id = 2 , embedding = [1 , - 2 , 3 ]))
537+ session .add (Item (id = 3 , embedding = [1 , 2 , 3 ]))
538+ session .commit ()
539+
540+ distance = func .cast (func .binary_quantize (Item .embedding ), BIT (3 )).hamming_distance (func .binary_quantize (func .cast ([3 , - 1 , 2 ], VECTOR (3 ))))
541+ subquery = session .query (Item ).order_by (distance ).limit (20 ).subquery ()
542+ items = session .query (subquery ).order_by (subquery .c .embedding .cosine_distance ([3 , - 1 , 2 ])).limit (5 ).all ()
543+ assert [v .id for v in items ] == [2 , 3 , 1 ]
544+
531545
532546@pytest .mark .parametrize ('engine' , array_engines )
533547class TestSqlalchemyArray :
@@ -596,6 +610,11 @@ async def test_bit(self, engine):
596610 item = await session .get (Item , 1 )
597611 assert item .binary_embedding == embedding
598612
613+ if engine == asyncpg_engine :
614+ session .add (Item (id = 2 , binary_embedding = '101' ))
615+ item = await session .get (Item , 2 )
616+ assert item .binary_embedding == embedding
617+
599618 await engine .dispose ()
600619
601620 @pytest .mark .asyncio
0 commit comments