-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhybrid_retrieval.py
69 lines (58 loc) · 2.26 KB
/
hybrid_retrieval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import logging
import os
from dotenv import load_dotenv
from fastembed import SparseTextEmbedding, TextEmbedding
from qdrant_client import QdrantClient, models
# Load environment variables
load_dotenv()
QDRANT_API_KEY = os.getenv("QDRANT_API_KEY")
QDRANT_HOST = os.getenv("QDRANT_HOST")
collection_name = "genezio"
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class HybridSearch:
"""
class for performing hybrid search using dense and sparse embeddings.
"""
def __init__(self) -> None:
"""
Initialize the Hybrid_search object with dense and sparse embedding models and a Qdrant client.
"""
self.embedding_model = TextEmbedding(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
self.sparse_embedding_model = SparseTextEmbedding(
model_name="Qdrant/bm42-all-minilm-l6-v2-attentions"
)
self.qdrant_client = QdrantClient(
url=QDRANT_HOST, api_key=QDRANT_API_KEY, timeout=30
)
def query_hybrid_search(self, query, metadata_filter=None, limit=5):
# Embed the query using the dense embedding model
dense_query = list(self.embedding_model.embed([query]))[0].tolist()
# Embed the query using the sparse embedding model
sparse_query = list(self.sparse_embedding_model.embed([query]))[0]
results = self.qdrant_client.query_points(
collection_name=collection_name,
prefetch=[
models.Prefetch(
query=models.SparseVector(
indices=sparse_query.indices.tolist(),
values=sparse_query.values.tolist(),
),
using="sparse",
limit=limit,
),
models.Prefetch(
query=dense_query,
using="dense",
limit=limit,
),
],
query_filter=metadata_filter,
query=models.FusionQuery(fusion=models.Fusion.RRF),
)
# Extract the document number, score, and text from the payload of each scored point
documents = [point.payload["text"] for point in results.points]
return documents