Skip to content

Instantly share code, notes, and snippets.

@NirantK
Created April 9, 2024 04:47
Show Gist options
  • Save NirantK/b806c5c9a4812304f47693b641233f6e to your computer and use it in GitHub Desktop.
Save NirantK/b806c5c9a4812304f47693b641233f6e to your computer and use it in GitHub Desktop.
Qdrant Python for Hybrid Search
## 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