annikwag commited on
Commit
f49fa9f
·
verified ·
1 Parent(s): 59e8a6b

Update appStore/search.py

Browse files
Files changed (1) hide show
  1. 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, RetrievalMode
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
- model_kwargs = {'device': device},
13
- encode_kwargs = {'normalize_embeddings': True},
14
- model_name='BAAI/bge-m3'
15
  )
16
-
17
  sparse_embeddings = FastEmbedSparse(model_name="Qdrant/bm25")
18
 
19
- # embed query
20
  q_dense = embeddings.embed_query(query)
21
  q_sparse = sparse_embeddings.embed_query(query)
22
 
23
- results = client.search_batch(collection_name=collection_name,
24
- requests=[
25
- models.SearchRequest(
26
- vector=models.NamedVector(
27
- name="text-dense",
28
- vector=q_dense,
29
- ),
30
- limit=limit, # was 10, now uses the parameter
31
- with_payload = True,
32
- ),
33
- models.SearchRequest(
34
- vector=models.NamedSparseVector(
35
- name="text-sparse",
36
- vector=models.SparseVector(
37
- indices=q_sparse.indices,
38
- values=q_sparse.values,
39
- ),
40
- ),
41
- limit=10,
42
- with_payload = True,
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