メインコンテンツまでスキップ
バージョン: User Guides (Cloud)

Cohere

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

モデルの選択肢

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

モデル名

次元

最大トークン数

説明

embed-english-v3.0

1,024

512

テキストを分類したり、埋め込みに変換したりできるモデル。英語のみ。

embed-multilingual-v3.0

1,024

512

多言語分類と埋め込みをサポートします。サポートされている言語はこちら

embed-english-light-v3.0

384

512

embed-english-v3.0 の小型で高速なバージョン。ほぼ同等の機能を持つが、はるかに高速。英語のみ。

embed-multilingual-light-v3.0

384

512

embed-multilingual-v3.0 の小型で高速なバージョン。ほぼ同等の機能を持つが、はるかに高速。多言語をサポート。

embed-english-v2.0

4,096

512

テキストを分類したり、埋め込みに変換したりできる古い埋め込みモデル。英語のみ。

embed-english-light-v2.0

1,024

512

embed-english-v2.0 の小型で高速なバージョン。ほぼ同等の機能を持つが、はるかに高速。英語のみ。

embed-multilingual-v2.0

768

256

多言語分類と埋め込みをサポートします。サポートされている言語はこちら

詳細については、Cohere の Embed Models を参照してください。

開始する前に

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

  • 埋め込みモデルを選択

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

  • Cohere と連携し、統合IDを取得する

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

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

    コレクションスキーマには、以下を含めるように計画してください。

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

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

  • 挿入時および検索時に生のテキストを扱う準備をする

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

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

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

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

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

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

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

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

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は指定された埋め込み関数を使用してテキストデータから埋め込みを処理および保存するように指示されます。

# 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がデータの構造に基づいて最も適切なインデックスタイプを選択し、設定できるようにするオプションです。

# 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"
)

コレクションの作成

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

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

ステップ2: データの挿入

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

# 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は自動的にクエリを埋め込みベクトルに変換し、類似性に基づいて関連ドキュメントを取得し、最も一致する結果を返します。

# 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)