Cohere 排名器Compatible with Milvus 2.6.x

Cohere Ranker 利用Cohere強大的重排模型,透過語意重排來提升搜尋相關性。它提供了企業級的重排功能,具有強大的 API 基礎結構,並針對生產環境進行了性能優化。

Cohere Ranker 對於有以下需求的應用程式特別有價值:

  • 利用最先進的重排模型進行高品質的語意理解

  • 適用於生產工作負載的企業級可靠性和可擴展性

  • 跨多種內容類型的多語言重排能力

  • 內建速率限制與錯誤處理功能,提供一致的 API 效能

先決條件

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

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

  • 有效的 Cohere API 密鑰,可存取排名模型。在Cohere 平台註冊以獲得您的 API 認證。您可以

    • 設定COHERE_API_KEY 環境變數,或

    • 直接在排名器配置credential 中指定 API 密鑰

創建一個 Cohere 排名器功能

要在您的 Milvus 應用程式中使用 Cohere 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 Cohere Ranker
cohere_ranker = Function(
    name="cohere_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": "cohere",               # Specifies Cohere as the service provider
        "model_name": "rerank-english-v3.0", # Cohere rerank 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_tokens_per_doc": 4096,         # Optional: max tokens per document (default: 4096)
        # "credential": "your-cohere-api-key" # Optional: authentication credential for Cohere API
    }
)
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("cohere_semantic_ranker")
                       .inputFieldNames(Collections.singletonList("document"))
                       .param("reranker", "model")
                       .param("provider", "cohere")
                       .param("model_name", "rerank-english-v3.0")
                       .param("queries", "[\"renewable energy developments\"]")
                       .param("endpoint", "http://localhost:8080")
                       .param("max_client_batch_size", "128")
                       .param("max_tokens_per_doc", "4096")
                       .build();
// nodejs
// go
# restful

Cohere 排序器特定參數

以下參數是 Cohere 排序器的特定參數:

參數

需要嗎?

說明

值/範例

reranker

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

"model"

provider

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

"cohere"

model_name

要從 Cohere 平台支援的模型中使用的 Cohere rerank 模型。

如需可用的 rerank 模型清單,請參閱Cohere 文件

"rerank-english-v3.0","rerank-multilingual-v3.0"

queries

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

[「搜尋查詢」]

max_client_batch_size

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

128 (預設值)

max_tokens_per_doc

每個文件的最大字元數。長文件將自動截斷為指定的字元數。

4096 (預設)

credential

存取 Cohere API 服務的驗證憑證。如果未指定,系統會尋找COHERE_API_KEY 環境變數。

"your-cohere-api-key" (您的 Cohere API 密鑰)

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

將 Cohere Ranker 應用於標準向量搜尋:

# Execute search with Cohere 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=cohere_ranker,                       # Apply Cohere 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(COLLECTION_NAME)
        .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 倍。

開始使用
反饋

這個頁面有幫助嗎?