SiliconFlow RankerCompatible with Milvus 2.6.x
SiliconFlow Ranker 利用SiliconFlow 的全面重排模型,透過語意重排來增強搜尋相關性。它提供了靈活的文件分塊功能,並支援來自不同供應商的各種專門的重排模型。
SiliconFlow Ranker 對需要下列功能的應用程式特別有價值:
進階的文件分塊功能,可設定重疊時間,以處理長文件
存取多種重排模型,包括 BAAI/bge-reranker 系列和其他專門模型
靈活的以分塊為基礎的評分,其中得分最高的分塊代表文件得分
具有成本效益的重排,支援標準和專業模型變體
先決條件
在 Milvus 中實作 SiliconFlow Ranker 之前,請確保您擁有
一個具有
VARCHAR欄位的 Milvus 集合,其中包含要重新排名的文字一個有效的 SiliconFlow API 密鑰,可以存取 reranking 模型。在SiliconFlow 平台註冊以取得您的 API 認證。您可以
設定
SILICONFLOW_API_KEY環境變數,或直接在排名器配置中指定 API 密鑰
創建一個SiliconFlow排名函數
要在您的 Milvus 應用程式中使用 SiliconFlow Ranker,請建立一個 Function 物件,指定 reranking 應該如何操作。這個函數將傳給 Milvus 搜尋作業,以加強結果排名。
from pymilvus import MilvusClient, Function, FunctionType
# Connect to your Milvus server
client = MilvusClient(
uri="http://localhost:19530" # Replace with your Milvus server URI
)
# Configure SiliconFlow Ranker
siliconflow_ranker = Function(
name="siliconflow_semantic_ranker", # Unique identifier for your ranker
input_field_names=["document"], # VARCHAR field containing text to rerank
function_type=FunctionType.RERANK, # Must be RERANK for reranking functions
params={
"reranker": "model", # Enables model-based reranking
"provider": "siliconflow", # Specifies SiliconFlow as the service provider
"model_name": "BAAI/bge-reranker-v2-m3", # SiliconFlow reranking model to use
"queries": ["renewable energy developments"], # Query text for relevance evaluation
"max_client_batch_size": 128, # Optional: batch size for model service requests (default: 128)
"max_chunks_per_doc": 5, # Optional: max chunks per document for supported models
"overlap_tokens": 50, # Optional: token overlap between chunks for supported models
# "credential": "your-siliconflow-api-key" # Optional: if not set, uses SILICONFLOW_API_KEY env var
}
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.common.clientenum.FunctionType;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("http://localhost:19530")
.build());
CreateCollectionReq.Function ranker = CreateCollectionReq.Function.builder()
.functionType(FunctionType.RERANK)
.name("siliconflow_semantic_ranker")
.inputFieldNames(Collections.singletonList("document"))
.param("reranker", "model")
.param("provider", "siliconflow")
.param("model_name", "BAAI/bge-reranker-v2-m3")
.param("queries", "[\"renewable energy developments\"]")
.param("endpoint", "http://localhost:8080")
.param("max_client_batch_size", "32")
.param("max_chunks_per_doc", "5")
.param("overlap_tokens", "50")
.build();
// nodejs
// go
# restful
SiliconFlow 排序器特定參數
以下參數是 SiliconFlow ranker 特有的:
參數 |
需要嗎? |
說明 |
值/範例 |
|---|---|---|---|
|
是 |
必須設定為 |
|
|
是 |
用於重排的模型服務提供者。 |
|
|
是 |
要從 SiliconFlow 平台上支援的模型中使用的 SiliconFlow reranking 模型。 如需可用的重排模型清單,請參閱SiliconFlow 文件。 |
|
|
是 |
rerank 模型用來計算相關性分數的查詢字串清單。查詢字串的數量必須與您的搜尋作業中的查詢字串數量完全相同(即使使用查詢向量來取代文字),否則會報錯。 |
[「搜尋查詢」] |
|
否 |
由於模型服務可能無法一次處理所有資料,因此這會設定在多次請求中存取模型服務的批次大小。 |
|
|
無 |
從文件內部產生的最大分塊數量。長文件會分成多個分塊進行計算,並取分塊中的最高分數作為該文件的分數。僅特定機型支援: |
|
|
無 |
文件分塊時,相鄰分塊之間的符號重疊數量。這可確保跨越分塊邊界的連續性,以便更好地理解語義。僅特定模型支援: |
|
|
無 |
存取 SiliconFlow API 服務的驗證憑證。如果沒有指定,系統會尋找 |
"your-siliconflow-api-key" |
特定機型的功能支援:max_chunks_per_doc 和overlap_tokens 參數僅受特定機型支援。使用其他機型時,這些參數將被忽略。
關於所有模型排序器共用的一般參數 (例如provider,queries),請參閱建立模型排序器。
套用至標準向量搜尋
將 SiliconFlow Ranker 應用於標準向量搜尋:
# Execute search with SiliconFlow reranking
results = client.search(
collection_name="your_collection",
data=[your_query_vector], # Replace with your query vector
anns_field="dense_vector", # Vector field to search
limit=5, # Number of results to return
output_fields=["document"], # Include text field for reranking
ranker=siliconflow_ranker, # Apply SiliconFlow reranking
consistency_level="Bounded"
)
import io.milvus.v2.common.ConsistencyLevel;
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.response.SearchResp;
import io.milvus.v2.service.vector.request.data.EmbeddedText;
SearchReq searchReq = SearchReq.builder()
.collectionName("your_collection")
.data(Arrays.asList(new EmbeddedText("AI Research Progress"), new EmbeddedText("What is AI")))
.annsField("vector_field")
.limit(10)
.outputFields(Collections.singletonList("document"))
.functionScore(FunctionScore.builder()
.addFunction(ranker)
.build())
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build();
SearchResp searchResp = client.search(searchReq);
// nodejs
// go
# restful