vLLM RankerCompatible with Milvus 2.6.x

vLLM Ranker 利用vLLM推理框架,透過語意重新排序來增強搜尋的相關性。它代表了一種先進的搜尋結果排序方法,超越了傳統的向量相似性。

vLLM Ranker 對於精確度和上下文非常重要的應用程式特別有價值,例如

  • 需要深入瞭解概念的技術文件搜尋

  • 語義關係比關鍵字匹配更重要的研究資料庫

  • 需要將使用者問題與相關解決方案相匹配的客戶支援系統

  • 必須瞭解產品屬性和使用者意圖的電子商務搜尋

先決條件

在 Milvus 中實施 vLLM Ranker 之前,請確保您擁有

  • 具有VARCHAR 欄位的 Milvus 集合,其中包含要重新排名的文字

  • 具有重新排名功能的正在運行的 vLLM 服務。有关设置 vLLM 服务的详细说明,请参阅官方 vLLM 文档。驗證 vLLM 服務的可用性:

    # Replace YOUR_VLLM_ENDPOINT_URL with the actual URL (e.g., http://<service-ip>:<port>/v1/rerank)
    # Replace 'BAAI/bge-reranker-base' if you deployed a different model
    
    curl -X 'POST' \
      'YOUR_VLLM_ENDPOINT_URL' \
      -H 'accept: application/json' \
      -H 'Content-Type: application/json' \
      -d '{
      "model": "BAAI/bge-reranker-base",
      "query": "What is the capital of France?",
      "documents": [
        "The capital of Brazil is Brasilia.",
        "The capital of France is Paris.",
        "Horses and cows are both animals"
      ]
    }'
    

    成功的回應應該會傳回依相關性分數排序的文件,類似於 OpenAI rerank API 的回應。

    有關更多伺服器參數與選項,請參閱vLLM OpenAI Compatible Server 文件

创建 vLLM ranker 函数

要在您的 Milvus 應用程式中使用 vLLM 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
)

# Create a vLLM Ranker function
vllm_ranker = Function(
    name="vllm_semantic_ranker",    # Choose a descriptive name
    input_field_names=["document"],  # Field containing text to rerank
    function_type=FunctionType.RERANK,  # Must be RERANK
    params={
        "reranker": "model",        # Specifies model-based reranking
        "provider": "vllm",         # Specifies vLLM service
        "queries": ["renewable energy developments"],  # Query text
        "endpoint": "http://localhost:8080",  # vLLM service address
        "max_client_batch_size": 32,              # Optional: batch size
        "truncate_prompt_tokens": 256,  # Optional: Use last 256 tokens
    }
)
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("document"))
                       .param("reranker", "model")
                       .param("provider", "vllm")
                       .param("queries", "[\"renewable energy developments\"]")
                       .param("endpoint", "http://localhost:8080")
                       .param("max_client_batch_size", "32")
                       .param("truncate_prompt_tokens", "256")
                       .build();
// nodejs
// go
# restful

vLLM 排序器特定參數

以下參數是 vLLM ranker 特有的:

參數

需要嗎?

說明

值 / 示例

reranker

必須設定為"model" ,才能啟用模型重排。

"model"

provider

用於重排的模型服務提供者。

"vllm"

queries

rerank 模型用來計算相關性分數的查詢字串清單。查詢字串的數量必須與您的搜尋作業中的查詢字串數量完全相同(即使使用查詢向量來取代文字),否則會報錯。

[「搜尋查詢」]

endpoint

您的 vLLM 服務位址。

"http://localhost:8080"

max_client_batch_size

由於模型服務可能無法一次處理所有資料,因此這會設定在多次請求中存取模型服務的批次大小。

32 (預設值)

truncate_prompt_tokens

如果設定為整數k,將只使用提示的最後k個字元 (即左截斷)。預設為 None (即不截斷)。

256

關於所有模型排序器共用的一般參數 (例如provider,queries),請參閱建立模型排序器

將 vLLM 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=vllm_ranker,                         # Apply vLLM 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

免費嘗試托管的 Milvus

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?