vLLM ランカーCompatible with Milvus 2.6.x

vLLM Rankerは、vLLM推論フレームワークを活用し、セマンティック・リランキングによって検索結果の関連性を高めます。vLLM Rankerは、従来のベクトル類似度を超える、検索結果の順序付けのための高度なアプローチです。

vLLM Rankerは、以下のような精度と文脈が重要なアプリケーションに特に有効です:

  • 概念の深い理解を必要とする技術文書検索

  • 意味的関係がキーワードのマッチングを凌駕する研究用データベース

  • ユーザーの問題と適切な解決策をマッチングさせる必要のあるカスタマーサポートシステム

  • 商品属性とユーザーの意図を理解する必要があるEコマース検索

前提条件

MilvusにvLLMランカーを実装する前に、以下を確認してください:

  • MilvusコレクションにVARCHAR 、ランク付けされるテキストが含まれていること。

  • リランキング機能を持つ実行中の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 ランカー関数の作成

milvusアプリケーションでvLLMランカーを使用するには、再ランクの動作方法を指定する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
)

# 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ランカー固有のものです:

パラメータ

必須か?

説明

値 / 例

reranker

必須

モデルの再ランキングを有効にするには、"model" に設定する必要がある。

"model"

provider

Yes

リランキングに使用するモデルサービスプロバイダ。

"vllm"

queries

はい

リランクモデルが関連性スコアの算出に使用するクエリ文字列のリスト。クエリ文字列の数は、検索操作のクエリの数と正確に一致する必要があります(テキストの代わりにクエリベクタを使用する場合でも)。

[検索クエリ]

endpoint

はい

vLLMサービスのアドレス。

"http://localhost:8080"

max_client_batch_size

いいえ

モデルサービスは一度にすべてのデータを処理しない場合があるため、複数のリクエストでモデルサービスにアクセスする際のバッチサイズを設定します。

32 (デフォルト)

truncate_prompt_tokens

いいえ

整数k に設定すると、プロンプトの最後のk 個のトークンのみを使用します (つまり、左の切り捨て)。デフォルトはNone(切り捨てなし)。

256

すべてのモデル・ランカーで共有される一般的なパラメータ(例:providerqueries )については、モデル・ランカーを作成するを参照。

vLLM ランカーを標準的なベクトル探索に適用する:

# 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

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
フィードバック

このページは役に立ちましたか ?