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

Serving Cluster クイックスタート

Serving Cluster は、リアルタイムの本番サービングのために compute と storage の両方を組み合わせた自己完結型サーバーです。Extract-Transform-Load(ETL)パイプラインでデータをクリーンアップした後、それを serving cluster にインポートすることで、大幅なパフォーマンス向上を実現できます。

始める前に

以下の手順では、すでに serving cluster を作成し、その endpoint とアクセス認証情報を取得していることを前提としています。

ステップ 1: 接続を設定する

cluster の認証情報または API key を取得したら、それを使用して cluster に接続できます。

python
from pymilvus import MilvusClient, DataType

SERVING_CLUSTER_ENDPOINT = "https://{cluster-id}.{region}.vectordb.zillizcloud.com:19530"
TOKEN = "YOUR_ZILLIZ_API_KEY"
# A valid token could be either
# - An API key, or
# - Use your Zilliz Cloud API key

# 1. Set up a Milvus client
client = MilvusClient(
uri=SERVING_CLUSTER_ENDPOINT,
token=TOKEN
)

ステップ 2: (任意)データベースを作成する。

Serving cluster にはデフォルトのデータベースが付属しています。それを使用する場合は、このステップをスキップしてください。以下のようにデータベースを作成することもできます。

python
# connect to the serving cluster
client = MilvusClient(
# a cluster-specific endpoint
uri=SERVING_CLUSTER_ENDPOINT,
token=TOKEN
)

client.create_database(
db_name="my_database"
)

ステップ 3: collection を作成する。

データベースの準備ができたら、その中に managed collection を作成できます。collection のカラムを外部データファイルにマッピングする external collection とは異なり、managed collection では大幅なパフォーマンス向上のためにデータをインポートする必要があります。

次の例では、collection schema を設定して collection を作成する方法を示します。

python
from pymilvus import MilvusClient, DataType

schema = MilvusClient.create_schema()

schema.add_field(
field_name="product_id",
datatype=DataType.INT64,
is_primary=True
)

schema.add_field(
field_name="product_name",
datatype=DataType.VARCHAR,
max_length=512
)

schema.add_field(
field_name="embedding",
datatype=DataType.FLOAT_VECTOR,
dim=768
)

その後、上記の schema を使用して collection を作成できます。デフォルトのデータベースを使用する場合は、db_name パラメータを安全にスキップできます。

python
client.use_database(
db_name="my_database"
)

# create the collection
client.create_collection(
collection_name="prod_collection",
schema=schema
)

ステップ 4: index を作成する。

すべての vector フィールドに対して index を作成する必要があり、必要に応じて選択した scalar フィールドに対しても作成できます。

python
index_params = client.prepare_index_params()

# Add indexes
index_params.add_index(
field_name="embedding",
index_type="AUTOINDEX",
metric_type="COSINE"
)

index_params.add_index(
field_name="product_name",
index_type="AUTOINDEX"
)

client.create_index(
db_name="my_database",
collection_name="prod_collection",
index_params=index_params
)

ステップ 5: collection をロードする。

index の準備ができたら、collection をメモリにロードします。

python
client.load_collection(
db_name="my_database",
collection_name="prod_collection"
)

ステップ 6: データをインポートする。

すべての設定が完了したら、処理済みデータをインポートできます。次の例では、処理済みデータが外部 storage bucket に保存されていることを前提としています。

bucket 内のデータ形式または storage integration については、フォーマットオプション を参照してください。

python
from pymilvus.bulk_writer import bulk_import

# The path should be relative to the root
# of a zilliz cloud volume or an external storage
OBJECT_URLS = [[
"https://s3.us-west-2.amazonaws.com/your-bucket/path/in/external/storage.json"
]]

ACCESS_KEY = "YOUR_STORAGE_ACCESS_KEY"
SECRET_KEY = "YOUR_STORAGE_SECRET_KEY"

res = bulk_import(
api_key="YOUR_ZILLIZ_API_KEY",
url="https://api.cloud.zilliz.com",
cluster_id="inxx-xxxxxxxxxxxxxxxxxxx",
db_name="my_database",
collection_name="prod_collection",
object_urls=OBJECT_URLS,
access_key=ACCESS_KEY,
secret_key=SECRET_KEY
)

# job-xxxxxxxxxxxxxxxxxxxxx

返されたジョブ ID を使用して、進行状況を監視できます。

python
import json
from pymilvus.bulk_writer import get_import_progress

# Get bulk-insert job progress
resp = get_import_progress(
api_key="YOUR_ZILLIZ_API_KEY",
url="https://api.cloud.zilliz.com",
cluster_id="inxx-xxxxxxxxxxxxxxxxxxx",
job_id="job-xxxxxxxxxxxxxxxxxxxxx",
)

print(json.dumps(resp.json(), indent=4))

ステップ 7: データを提供する。

インポートが完了したら、検索、クエリ、ハイブリッド検索を通じてユーザーがデータを利用できるようにできます。

python
query_vector = [0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, ..., 0.9029438446296592]
res = client.search(
db_name="my_database",
collection_name="prod_collection",
anns_field="embedding",
data=[query_vector],
limit=3,
output_fields=["product_name"]
)
Ctrl I