Cohere ランカーCompatible with Milvus 2.6.x
Cohere Ranker は、Cohere の強力なリランク・モデルを活用し、セマンティック・リランキングによって検索の関連性を高めます。Cohere Ranker は、堅牢な 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 ランカー固有のものです:
パラメータ |
必須か? |
説明 |
値 / 例 |
|---|---|---|---|
|
Yes |
モデルの再ランキングを有効にするには、 |
|
|
Yes |
リランキングに使用するモデルサービスプロバイダ。 |
|
|
はい |
Cohere プラットフォームでサポートされているモデルの中から使用する Cohere リランク・モデル。 使用可能なリランク・モデルのリストについては、Cohere のドキュメントを参照してください。 |
|
|
はい |
クエリ文字列のリスト。クエリ文字列の数は、検索操作のクエリ数と正確に一致する必要があります (テキストの代わりにクエリベクトルを使用する場合も同様)。 |
[検索クエリ] |
|
いいえ |
モデルサービスは一度にすべてのデータを処理しない可能性があるため、複数のリクエストでモデルサービスにアクセスする際のバッチサイズを設定します。 |
|
|
いいえ |
ドキュメントあたりの最大トークン数。長い文書は自動的に指定されたトークン数に切り詰められます。 |
|
|
いいえ |
Cohere API サービスにアクセスするための認証クレデンシャル。指定しない場合、システムは |
"your-cohere-api-key" |
すべてのモデル・ランカー(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