Created
April 9, 2024 04:47
-
-
Save NirantK/b806c5c9a4812304f47693b641233f6e to your computer and use it in GitHub Desktop.
Qdrant Python for Hybrid Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Recommended Imports | |
from qdrant_client import QdrantClient | |
from qdrant_client.models import ( | |
Distance, | |
NamedSparseVector, | |
NamedVector, | |
SparseVector, | |
PointStruct, | |
SearchRequest, | |
SparseIndexParams, | |
SparseVectorParams, | |
VectorParams, | |
ScoredPoint, | |
) | |
## Creating a collection | |
client.create_collection( | |
collection_name, | |
vectors_config={ | |
"text-dense": VectorParams( | |
size=1024, # OpenAI Embeddings | |
distance=Distance.COSINE, | |
) | |
}, | |
sparse_vectors_config={ | |
"text-sparse": SparseVectorParams( | |
index=SparseIndexParams( | |
on_disk=False, | |
) | |
) | |
}, | |
) | |
## Creating Points | |
points = [] | |
for idx, (text, sparse_vector, dense_vector) in enumerate(zip(product_texts, sparse_vectors, dense_vectors)): | |
sparse_vector = SparseVector(indices=sparse_vector.indices.tolist(), values=sparse_vector.values.tolist()) | |
point = PointStruct( | |
id=idx, | |
payload={"text": text, "product_id": rows[idx]["product_id"]}, # Add any additional payload if necessary | |
vector={ | |
"text-sparse": sparse_vector, | |
"text-dense": dense_vector, | |
}, | |
) | |
points.append(point) | |
## Upsert | |
client.upsert(collection_name, points) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment