メインコンテンツまでスキップ

Cohere

このトピックでは、Milvus で Cohere 埋め込み関数を構成して使用する方法について説明します。

モデルの選択肢​

Milvus は、Cohere が提供する埋め込みモデルをサポートしています。以下は、すぐに参照できるように現在利用可能な埋め込みモデルの一覧です:

モデル名次元数最大トークン数説明
embed-english-v3.01,024512テキストを分類したり埋め込みに変換したりできるモデルです。英語のみに対応しています。
embed-multilingual-v3.01,024512多言語の分類と埋め込みをサポートします。サポートされている言語はこちら を参照してください。
embed-english-light-v3.0384512embed-english-v3.0 のより小さく高速なバージョンです。ほぼ同等の性能ですが、大幅に高速です。英語のみに対応しています。
embed-multilingual-light-v3.0384512embed-multilingual-v3.0 のより小さく高速なバージョンです。ほぼ同等の性能ですが、大幅に高速です。複数の言語をサポートします。
embed-english-v2.04,096512テキストを分類したり埋め込みに変換したりできる旧バージョンの埋め込みモデルです。英語のみに対応しています。
embed-english-light-v2.01,024512embed-english-v2.0 のより小さく高速なバージョンです。ほぼ同等の性能ですが、大幅に高速です。英語のみに対応しています。
embed-multilingual-v2.0768256多言語の分類と埋め込みをサポートします。サポートされている言語はこちら を参照してください。

詳細は、Cohere の埋め込みモデル を参照してください。

事前準備​

テキスト埋め込み関数を使用する前に、以下の前提条件を満たしていることを確認してください。

  • 埋め込みモデルを選択する

    使用する埋め込みモデルを決定します。この選択によって埋め込みの動作と出力形式が決まります。詳細は、埋め込みモデルの選択 を参照してください。

  • Cohere と統合して統合 ID を取得する

    Cohere が提供する埋め込みモデルを使用する前に、Cohere とのモデルプロバイダー統合を作成し、統合 ID を取得する必要があります。詳細は、モデルプロバイダーとの統合 を参照してください。

  • 互換性のあるコレクションスキーマを設計する

    コレクションスキーマに以下を含めるように計画します:

    • 生の入力テキスト用のテキストフィールド(VARCHAR)

    • データ型と次元数が選択した埋め込みモデルと一致する高密度ベクトルフィールド

  • 挿入時と検索時に生テキストを扱う準備をする

    テキスト埋め込み関数を有効にすると、生テキストを直接挿入およびクエリできます。埋め込みはシステムによって自動的に生成されます。

ステップ 1: テキスト埋め込み関数を使用してコレクションを作成する​

スキーマフィールドを定義する​

埋め込み関数を使用するには、特定のスキーマを持つコレクションを作成します。このスキーマには、少なくとも次の 3 つの必須フィールドを含める必要があります:

  • コレクション内の各エンティティを一意に識別するプライマリフィールド。

  • 埋め込み対象の生データを格納する VARCHAR フィールド。

  • テキスト埋め込み関数が VARCHAR フィールドに対して生成する高密度ベクトル埋め込みを格納するために予約されたベクトルフィールド。

次の例では、テキストデータを格納する 1 つのスカラーフィールド "document" と、Function モジュールによって生成される埋め込みを格納する 1 つのベクトルフィールド "dense" を持つスキーマを定義します。選択した埋め込みモデルの出力に合わせてベクトルの次元(dim)を設定することを忘れないでください。

python
from pymilvus import MilvusClient, DataType, Function, FunctionType

# Initialize Milvus client
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)

# Create a new schema for the collection
schema = client.create_schema()

# Add primary field "id"
schema.add_field("id", DataType.INT64, is_primary=True, auto_id=False)

# Add scalar field "document" for storing textual data
schema.add_field("document", DataType.VARCHAR, max_length=9000)

# Add vector field "dense" for storing embeddings.
# IMPORTANT: Set dim to match the exact output dimension of the embedding model.
schema.add_field("dense", DataType.FLOAT_VECTOR, dim=1024)

テキスト埋め込み関数を定義する​

Milvus の Function モジュールは、スカラーフィールドに格納された生データを自動的に埋め込みに変換し、明示的に定義されたベクトルフィールドに格納します。

次の例では、スカラーフィールド "document" を埋め込みに変換し、生成されたベクトルを先に定義した "dense" ベクトルフィールドに格納する Function モジュール(cohere_func)を追加します。

埋め込み関数を定義したら、それをコレクションスキーマに追加します。これにより、Milvus は指定された埋め込み関数を使用して、テキストデータから埋め込みを処理して格納するようになります。

python
# Define embedding function specifically for embedding model provider
text_embedding_function = Function(
name="cohere_func", # Unique identifier for this embedding function
function_type=FunctionType.TEXTEMBEDDING, # Indicates a text embedding function
input_field_names=["document"], # Scalar field(s) containing text data to embed
output_field_names=["dense"], # Vector field(s) for storing embeddings
params={ # Provider-specific embedding parameters (function-level)
"provider": "cohere", # Must be set to "cohere"
"model_name": "embed-english-v3.0", # Specifies the embedding model to use
"integration_id": "YOUR_INTEGRATION_ID", # Integration ID generated in the Zilliz Cloud console for the selected model provider
# "url": "https://api.cohere.com/v2/embed", # Defaults to the official endpoint if omitted
# "truncate": "NONE", # Specifies how the API will handle inputs longer than the maximum token length.
}
)

# Add the configured embedding function to your existing collection schema
schema.add_function(text_embedding_function)

インデックスを構成する​

必要なフィールドと組み込み関数を含むスキーマを定義したら、コレクションのインデックスを設定します。このプロセスを簡素化するには、index_type として AUTOINDEX を使用します。これは、データの構造に基づいて Zilliz Cloud が最適なインデックスタイプを選択して構成できるようにするオプションです。

python
# Prepare index parameters
index_params = client.prepare_index_params()

# Add AUTOINDEX to automatically select optimal indexing method
index_params.add_index(
field_name="dense",
index_type="AUTOINDEX",
metric_type="COSINE"
)

コレクションを作成する​

次に、定義したスキーマとインデックスパラメーターを使用してコレクションを作成します。

python
# Create collection named "demo"
client.create_collection(
collection_name='demo',
schema=schema,
index_params=index_params
)

ステップ 2: データを挿入する​

コレクションとインデックスを設定したら、生データを挿入する準備が整います。このプロセスでは、生テキストを提供するだけで済みます。先ほど定義した Function モジュールが、各テキストエントリに対応するスパースベクトルを自動的に生成します。

python
# Insert sample documents
client.insert('demo', [
{'id': 1, 'document': 'Milvus simplifies semantic search through embeddings.'},
{'id': 2, 'document': 'Vector embeddings convert text into searchable numeric data.'},
{'id': 3, 'document': 'Semantic search helps users find relevant information quickly.'},
])

ステップ 3: テキストで検索する​

データを挿入したら、生のクエリテキストを使用してセマンティック検索を実行します。Milvus はクエリを自動的に埋め込みベクトルに変換し、類似度に基づいて関連するドキュメントを取得して、最も一致する結果を返します。

python
# Perform semantic search
results = client.search(
collection_name='demo',
data=['How does Milvus handle semantic search?'], # Use text query rather than query vector
anns_field='dense', # Use the vector field that stores embeddings
limit=1,
output_fields=['document'],
)

print(results)