TEI 排行榜Compatible with Milvus 2.6.x
TEI Ranker 利用 Hugging Face 的Text Embedding Inference (TEI)服務,透過語意重新排序來提升搜尋相關性。它代表了一種先進的搜尋結果排序方法,超越了傳統的向量相似性。
先決條件
在 Milvus 中實作 TEI Ranker 之前,請確保您擁有
具有
VARCHAR欄位的 Milvus 資料庫,其中包含要重新排序的文字。具有重新排名功能的執行中 TEI 服務。有關設定 TEI 服務的詳細說明,請參閱TEI 官方文件。
建立 TEI Ranker 函式
要在您的 Milvus 應用程式中使用 TEI Ranker,請建立一個 Function 物件,指定重排的操作方式。這個函數將會傳給 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 TEI Ranker
tei_ranker = Function(
name="tei_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": "tei", # Specifies TEI as the service provider
"queries": ["renewable energy developments"], # Query text for relevance evaluation
"endpoint": "http://localhost:8080", # Your TEI service URL
"max_client_batch_size": 32, # Optional: batch size for processing (default: 32)
"truncate": True, # Optional: Truncate the inputs that are longer than the maximum supported size
"truncation_direction": "Right", # Optional: Direction to truncate the inputs
}
)
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("vllm_semantic_ranker")
.inputFieldNames(Collections.singletonList(NAME_FIELD))
.param("reranker", "model")
.param("provider", "tei")
.param("queries", "[\"renewable energy developments\"]")
.param("endpoint", "http://localhost:8080")
.param("max_client_batch_size", "32")
.param("truncate", "true")
.param("truncation_direction", "Right")
.build();
searchWithRanker(scientists, ranker);
// nodejs
// go
# restful
TEI 排序器特定參數
以下參數是 TEI 排序器的特定參數:
參數 |
需要嗎? |
說明 |
值/範例 |
|---|---|---|---|
|
是 |
必須設定為 |
|
|
是 |
用於重排的模型服務提供者。 |
|
|
是 |
rerank 模型用來計算相關性分數的查詢字串清單。查詢字串的數量必須與您的搜尋作業中的查詢字串數量完全相同(即使使用查詢向量來取代文字),否則會報錯。 |
[「搜尋查詢」] |
|
是 |
您的 TEI 服務 URL。 |
|
|
否 |
由於模型服務可能無法一次處理所有資料,因此這會設定在多次請求中存取模型服務的批次大小。 |
|
|
否 |
是否截斷超過最大序列長度的輸入。如果 |
|
|
否 |
當輸入太長時要截斷的方向:
|
|
關於所有模型排序器共用的一般參數 (例如provider,queries),請參閱建立模型排序器。
套用至標準向量搜尋
將 TEI Ranker 應用於標準向量搜尋:
# Execute search with vLLM 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=tei_ranker, # Apply tei 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