mirror of
https://github.com/james-m-jordan/morphik-core.git
synced 2025-05-09 19:32:38 +00:00
Speed up multivector store
Co-authored-by: Arnav Agrawal <aa779@cornell.edu>
This commit is contained in:
parent
89633ce761
commit
bce0e1cfe1
@ -181,7 +181,8 @@ class MultiVectorStore(BaseVectorStore):
|
||||
"""
|
||||
CREATE OR REPLACE FUNCTION max_sim(document bit[], query bit[]) RETURNS double precision AS $$
|
||||
WITH queries AS (
|
||||
SELECT row_number() OVER () AS query_number, * FROM (SELECT unnest(query) AS query) AS foo
|
||||
SELECT row_number() OVER () AS query_number, *
|
||||
FROM (SELECT unnest(query) AS query) AS foo
|
||||
),
|
||||
documents AS (
|
||||
SELECT unnest(document) AS document
|
||||
@ -189,7 +190,8 @@ class MultiVectorStore(BaseVectorStore):
|
||||
similarities AS (
|
||||
SELECT
|
||||
query_number,
|
||||
1.0 - (bit_count(document # query)::float / greatest(bit_length(query), 1)::float) AS similarity
|
||||
1.0 - (bit_count(document # query)::float /
|
||||
greatest(bit_length(query), 1)::float) AS similarity
|
||||
FROM queries CROSS JOIN documents
|
||||
),
|
||||
max_similarities AS (
|
||||
@ -217,10 +219,6 @@ class MultiVectorStore(BaseVectorStore):
|
||||
if isinstance(embeddings, list) and not isinstance(embeddings[0], np.ndarray):
|
||||
embeddings = np.array(embeddings)
|
||||
|
||||
# Add this check to ensure pgvector is registered for the connection
|
||||
with self.get_connection() as conn:
|
||||
register_vector(conn)
|
||||
|
||||
return [Bit(embedding > 0) for embedding in embeddings]
|
||||
|
||||
async def store_embeddings(self, chunks: List[DocumentChunk]) -> Tuple[bool, List[str]]:
|
||||
@ -230,7 +228,7 @@ class MultiVectorStore(BaseVectorStore):
|
||||
return True, []
|
||||
|
||||
stored_ids = []
|
||||
|
||||
with self.get_connection() as conn:
|
||||
for chunk in chunks:
|
||||
# Ensure embeddings exist
|
||||
if not hasattr(chunk, "embedding") or chunk.embedding is None:
|
||||
@ -244,7 +242,7 @@ class MultiVectorStore(BaseVectorStore):
|
||||
binary_embeddings = self._binary_quantize(embeddings)
|
||||
|
||||
# Insert into database with retry logic
|
||||
with self.get_connection() as conn:
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO multi_vector_embeddings
|
||||
|
Loading…
x
Reference in New Issue
Block a user