dinhquangson commited on
Commit
8fd584d
·
verified ·
1 Parent(s): 7c022e4

Create neural_searcher.py

Browse files
Files changed (1) hide show
  1. neural_searcher.py +38 -0
neural_searcher.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from qdrant_client import QdrantClient
2
+ from sentence_transformers import SentenceTransformer
3
+ from qdrant_client.models import Filter
4
+
5
+ class NeuralSearcher:
6
+ def __init__(self, collection_name):
7
+ self.collection_name = collection_name
8
+ # Initialize encoder model
9
+ self.model = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
10
+ # initialize Qdrant client
11
+ self.qdrant_client = QdrantClient("http://localhost:6333")
12
+
13
+ def search(self, text: str, city: str):
14
+ # Convert text query into vector
15
+ vector = self.model.encode(text).tolist()
16
+
17
+ city_of_interest = city
18
+
19
+ # Define a filter for cities
20
+ city_filter = Filter(**{
21
+ "must": [{
22
+ "key": "city", # Store city information in a field of the same name
23
+ "match": { # This condition checks if payload field has the requested value
24
+ "value": city_of_interest
25
+ }
26
+ }]
27
+ })
28
+
29
+ search_result = self.qdrant_client.search(
30
+ collection_name=self.collection_name,
31
+ query_vector=vector,
32
+ query_filter=city_filter,
33
+ limit=5
34
+ )
35
+ # `search_result` contains found vector ids with similarity scores along with the stored payload
36
+ # In this function you are interested in payload only
37
+ payloads = [hit.payload for hit in search_result]
38
+ return payloads