TEIランカーCompatible with Milvus 2.6.x

TEI Rankerは、Hugging FaceのText Embedding Inference (TEI)サービスを活用し、セマンティック・リランキングによって検索の関連性を高めます。これは、従来のベクトル類似性を超える、検索結果の順序付けへの高度なアプローチです。

前提条件

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

  • リランキング対象のテキストを含むVARCHAR フィールドを持つMilvusコレクション。

  • リランキング機能を持つTEIサービスが稼働していること。TEIサービスの詳細な設定方法については、TEI公式ドキュメントを参照してください。

TEIランカー関数の作成

MilvusアプリケーションでTEIランカーを使用するには、リランキングの動作を指定する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ランカー固有のものです:

パラメータ

必須か?

説明

値 / 例

reranker

必須

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

"model"

provider

Yes

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

"tei"

queries

はい

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

[検索クエリ]

endpoint

はい

TEIサービスのURL。

"http://localhost:8080"

max_client_batch_size

いいえ

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

32 (デフォルト)

truncate

いいえ

最大シーケンス長を超える入力を切り捨てるかどうか。False の場合、長い入力はエラーになります。

True またはFalse

truncation_direction

いいえ

入力が長すぎる場合に切り捨てる方向:

  • "Right" (デフォルト): サポートされる最大サイズに達するまで、トークンはシーケンスの最後から取り除かれる。

  • "Left":トークンはシーケンスの先頭から削除される。

"Right" または"Left"

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

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

Try Managed Milvus for Free

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

Get Started
フィードバック

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