Spaces:
Sleeping
Sleeping
dinhquangson
commited on
Create hybrid_searcher.py
Browse files- hybrid_searcher.py +39 -0
hybrid_searcher.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from qdrant_client import QdrantClient
|
2 |
+
|
3 |
+
|
4 |
+
class HybridSearcher:
|
5 |
+
DENSE_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
6 |
+
SPARSE_MODEL = "prithivida/Splade_PP_en_v1"
|
7 |
+
def __init__(self, collection_name):
|
8 |
+
self.collection_name = collection_name
|
9 |
+
# initialize Qdrant client
|
10 |
+
self.qdrant_client = QdrantClient("http://localhost:6333")
|
11 |
+
self.qdrant_client.set_model(self.DENSE_MODEL)
|
12 |
+
# comment this line to use dense vectors only
|
13 |
+
self.qdrant_client.set_sparse_model(self.SPARSE_MODEL)
|
14 |
+
|
15 |
+
def search(self, text: str, city: str):
|
16 |
+
city_of_interest = city
|
17 |
+
|
18 |
+
# Define a filter for cities
|
19 |
+
city_filter = models.Filter(
|
20 |
+
must=[
|
21 |
+
models.FieldCondition(
|
22 |
+
key="city",
|
23 |
+
match=models.MatchValue(value=city_of_interest)
|
24 |
+
)
|
25 |
+
]
|
26 |
+
)
|
27 |
+
|
28 |
+
search_result = self.qdrant_client.query(
|
29 |
+
collection_name=self.collection_name,
|
30 |
+
query_text=text,
|
31 |
+
query_filter=city_filter,
|
32 |
+
limit=5
|
33 |
+
)
|
34 |
+
# `search_result` contains found vector ids with similarity scores
|
35 |
+
# along with the stored payload
|
36 |
+
|
37 |
+
# Select and return metadata
|
38 |
+
metadata = [hit.metadata for hit in search_result]
|
39 |
+
return metadata
|