Skip to content

Commit 7d75517

Browse files
authored
enhance: [2.4] Update hello_hybrid_sparse_dense.py example to include BGE reranker (#2032)
pr: milvus-io/milvus#31062 Signed-off-by: Buqian Zheng <[email protected]>
1 parent 9f11b48 commit 7d75517

1 file changed

Lines changed: 44 additions & 8 deletions

File tree

‎examples/hello_hybrid_sparse_dense.py‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
# A demo showing hybrid semantic search with dense and sparse vectors using Milvus.
2+
#
23
# You can optionally choose to use the BGE-M3 model to embed the text as dense
3-
# and sparse vectors, or simply use random generated vectors as the example.
4-
5-
# To use BGE-M3 model, you need to install the optional `model` module in pymilvus:
4+
# and sparse vectors, or simply use random generated vectors as an example.
5+
#
6+
# You can also use the BGE CrossEncoder model to rerank the search results.
7+
#
8+
# Note that the sparse vector search feature is only available in Milvus 2.4.0 or
9+
# higher version. Make sure you follow https://milvus.io/docs/install_standalone-docker.md
10+
# to set up the latest version of Milvus in your local environment.
11+
12+
# To connect to Milvus server, you need the python client library called pymilvus.
13+
# To use BGE-M3 model, you need to install the optional `model` module in pymilvus.
14+
# You can get them by simply running the following commands:
15+
#
16+
# pip install pymilvus
617
# pip install pymilvus[model]
18+
19+
# If true, use BGE-M3 model to generate dense and sparse vectors.
20+
# If false, use random numbers to compose dense and sparse vectors.
721
use_bge_m3 = True
22+
# If true, the search result will be reranked using BGE CrossEncoder model.
23+
use_reranker = True
824

925
# The overall steps are as follows:
1026
# 1. embed the text as dense and sparse vectors
@@ -104,12 +120,32 @@ def random_embedding(texts):
104120
# Currently Milvus only support 1 query in the same hybrid search request, so
105121
# we inspect res[0] directly. In future release Milvus will accept batch
106122
# hybrid search queries in the same call.
107-
for hit in res[0]:
108-
print(f'text: {hit.fields["text"]} distance {hit.distance}')
109-
110-
# If you are using BGE-M3 to generate the embedding, you should see the following:
123+
res = res[0]
124+
125+
if use_reranker:
126+
result_texts = [hit.fields["text"] for hit in res]
127+
from pymilvus.model.reranker import BGERerankFunction
128+
bge_rf = BGERerankFunction(device='cpu')
129+
# rerank the results using BGE CrossEncoder model
130+
results = bge_rf(query, result_texts, top_k=2)
131+
for hit in results:
132+
print(f'text: {hit.text} distance {hit.score}')
133+
else:
134+
for hit in res:
135+
print(f'text: {hit.fields["text"]} distance {hit.distance}')
136+
137+
# If you used both BGE-M3 and the reranker, you should see the following:
138+
# text: Alan Turing was the first person to conduct substantial research in AI. distance 0.9306981017573297
139+
# text: Artificial intelligence was founded as an academic discipline in 1956. distance 0.03217001154515051
140+
#
141+
# If you used only BGE-M3, you should see the following:
111142
# text: Alan Turing was the first person to conduct substantial research in AI. distance 0.032786883413791656
112143
# text: Artificial intelligence was founded as an academic discipline in 1956. distance 0.016129031777381897
113144

145+
# In this simple example the reranker yields the same result as the embedding based hybrid search, but in more complex
146+
# scenarios the reranker can provide more accurate results.
147+
148+
# If you used random vectors, the result will be different each time you run the script.
149+
114150
# Drop the collection to clean up the data.
115-
utility.drop_collection(col_name)
151+
utility.drop_collection(col_name)

0 commit comments

Comments
 (0)