Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update appStore/search.py
Browse files- appStore/search.py +34 -33
appStore/search.py
CHANGED
@@ -1,48 +1,49 @@
|
|
1 |
from appStore.prep_utils import get_client
|
2 |
-
from langchain_qdrant import FastEmbedSparse
|
3 |
from torch import cuda
|
4 |
from qdrant_client.http import models
|
5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
6 |
-
# get the device to be used eithe gpu or cpu
|
7 |
-
device = 'cuda' if cuda.is_available() else 'cpu'
|
8 |
|
|
|
9 |
|
10 |
-
def hybrid_search(client, query, collection_name):
|
11 |
embeddings = HuggingFaceEmbeddings(
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
)
|
16 |
-
|
17 |
sparse_embeddings = FastEmbedSparse(model_name="Qdrant/bm25")
|
18 |
|
19 |
-
#
|
20 |
q_dense = embeddings.embed_query(query)
|
21 |
q_sparse = sparse_embeddings.embed_query(query)
|
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 |
return results
|
48 |
-
|
|
|
1 |
from appStore.prep_utils import get_client
|
2 |
+
from langchain_qdrant import FastEmbedSparse
|
3 |
from torch import cuda
|
4 |
from qdrant_client.http import models
|
5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
|
|
6 |
|
7 |
+
device = 'cuda' if cuda.is_available() else 'cpu'
|
8 |
|
9 |
+
def hybrid_search(client, query, collection_name, limit=300):
|
10 |
embeddings = HuggingFaceEmbeddings(
|
11 |
+
model_name='BAAI/bge-m3',
|
12 |
+
model_kwargs={'device': device},
|
13 |
+
encode_kwargs={'normalize_embeddings': True}
|
14 |
)
|
|
|
15 |
sparse_embeddings = FastEmbedSparse(model_name="Qdrant/bm25")
|
16 |
|
17 |
+
# 1) Embed the query
|
18 |
q_dense = embeddings.embed_query(query)
|
19 |
q_sparse = sparse_embeddings.embed_query(query)
|
20 |
|
21 |
+
# 2) Request more than 10 items
|
22 |
+
results = client.search_batch(
|
23 |
+
collection_name=collection_name,
|
24 |
+
requests=[
|
25 |
+
# Dense request
|
26 |
+
models.SearchRequest(
|
27 |
+
vector=models.NamedVector(
|
28 |
+
name="text-dense",
|
29 |
+
vector=q_dense,
|
30 |
+
),
|
31 |
+
limit=limit, # was 10, now uses the parameter
|
32 |
+
with_payload=True,
|
33 |
+
),
|
34 |
+
# Sparse request
|
35 |
+
models.SearchRequest(
|
36 |
+
vector=models.NamedSparseVector(
|
37 |
+
name="text-sparse",
|
38 |
+
vector=models.SparseVector(
|
39 |
+
indices=q_sparse.indices,
|
40 |
+
values=q_sparse.values,
|
41 |
+
),
|
42 |
+
),
|
43 |
+
limit=limit, # was 10, now uses the parameter
|
44 |
+
with_payload=True,
|
45 |
+
),
|
46 |
+
]
|
47 |
+
)
|
48 |
|
49 |
return results
|
|