Spaces:
Sleeping
Sleeping
adds filter to query suggestions
Browse files- app.py +6 -4
- suggestion_searcher.py +12 -2
app.py
CHANGED
|
@@ -17,14 +17,16 @@ ALLOWED_API_KEY = str(API_KEY)
|
|
| 17 |
|
| 18 |
@app.get("/api/search")
|
| 19 |
async def search(q: str):
|
| 20 |
-
|
|
|
|
| 21 |
data = await doc_searcher.search(text=xss)
|
| 22 |
return data
|
| 23 |
|
| 24 |
@app.get("/api/suggestions")
|
| 25 |
-
async def get_suggestions(q: str):
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
return data
|
| 29 |
|
| 30 |
|
|
|
|
| 17 |
|
| 18 |
@app.get("/api/search")
|
| 19 |
async def search(q: str):
|
| 20 |
+
query = q.lower()
|
| 21 |
+
xss = nh3.clean(query)
|
| 22 |
data = await doc_searcher.search(text=xss)
|
| 23 |
return data
|
| 24 |
|
| 25 |
@app.get("/api/suggestions")
|
| 26 |
+
async def get_suggestions(q: str, type: int):
|
| 27 |
+
query = q.lower()
|
| 28 |
+
xss = nh3.clean(query)
|
| 29 |
+
data = await suggestion_searcher.search(text=xss,type=type)
|
| 30 |
return data
|
| 31 |
|
| 32 |
|
suggestion_searcher.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from qdrant_client import QdrantClient
|
|
|
|
| 2 |
from fastembed import SparseTextEmbedding, LateInteractionTextEmbedding
|
| 3 |
from qdrant_client import QdrantClient, models
|
| 4 |
from sentence_transformers import SentenceTransformer
|
|
@@ -13,7 +14,7 @@ class SuggestionSearcher:
|
|
| 13 |
self.late_interaction_model = LateInteractionTextEmbedding(LATE_INTERACTION_MODEL)
|
| 14 |
self.qdrant_client = QdrantClient(QDRANT_URL,api_key=QDRANT_API_KEY,timeout=30)
|
| 15 |
|
| 16 |
-
async def search(self, text: str):
|
| 17 |
|
| 18 |
dense_query = self.dense_model.encode(text).tolist()
|
| 19 |
sparse_query = next(self.sparse_model.query_embed(text))
|
|
@@ -41,10 +42,19 @@ class SuggestionSearcher:
|
|
| 41 |
)
|
| 42 |
]
|
| 43 |
|
|
|
|
| 44 |
search_result = self.qdrant_client.query_points(
|
| 45 |
collection_name= self.collection_name,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
search_params=models.SearchParams(
|
| 47 |
-
hnsw_ef=
|
| 48 |
exact=False,
|
| 49 |
quantization=models.QuantizationSearchParams(
|
| 50 |
rescore=True,
|
|
|
|
| 1 |
from qdrant_client import QdrantClient
|
| 2 |
+
from qdrant_client.models import Filter, FieldCondition, MatchValue
|
| 3 |
from fastembed import SparseTextEmbedding, LateInteractionTextEmbedding
|
| 4 |
from qdrant_client import QdrantClient, models
|
| 5 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 14 |
self.late_interaction_model = LateInteractionTextEmbedding(LATE_INTERACTION_MODEL)
|
| 15 |
self.qdrant_client = QdrantClient(QDRANT_URL,api_key=QDRANT_API_KEY,timeout=30)
|
| 16 |
|
| 17 |
+
async def search(self, text: str,type:int):
|
| 18 |
|
| 19 |
dense_query = self.dense_model.encode(text).tolist()
|
| 20 |
sparse_query = next(self.sparse_model.query_embed(text))
|
|
|
|
| 42 |
)
|
| 43 |
]
|
| 44 |
|
| 45 |
+
self.qdrant_client.scroll
|
| 46 |
search_result = self.qdrant_client.query_points(
|
| 47 |
collection_name= self.collection_name,
|
| 48 |
+
query_filter=Filter(
|
| 49 |
+
must=[
|
| 50 |
+
FieldCondition(
|
| 51 |
+
key="tip",
|
| 52 |
+
match=MatchValue(value=type)
|
| 53 |
+
)
|
| 54 |
+
]
|
| 55 |
+
),
|
| 56 |
search_params=models.SearchParams(
|
| 57 |
+
hnsw_ef=64,
|
| 58 |
exact=False,
|
| 59 |
quantization=models.QuantizationSearchParams(
|
| 60 |
rescore=True,
|