この章では、Python を使用して Milvus 開発を迅速に開始する方法について紹介します。

提供されているサンプルコードを実行することで、Milvus の機能について初步の理解が得られます。

バージョン要件

  • Milvus 2.3.0
  • Python 3 (3.7.1 以上)
  • PyMilvus 2.3.x

Milvus Python SDK のインストール

python3 -m pip install pymilvus==

サンプルコードのダウンロード

次のコマンドを使用して hello_milvus.py を直接ダウンロードするか、以下のコマンドを使用します。

$ wget https://www.tizi365.com/storage/hello_milvus.py

サンプルコードの説明

サンプルコードでは、次の手順が実行されます。

  • PyMilvus パッケージをインポート:
from pymilvus import (
    connections,
    utility,
    FieldSchema,
    CollectionSchema,
    DataType,
    Collection,
)
  • サーバに接続:
connections.connect("default", host="localhost", port="19530")
  • コレクションを作成:
fields = [
    FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=False),
    FieldSchema(name="random", dtype=DataType.DOUBLE),
    FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=8)
]
schema = CollectionSchema(fields, "hello_milvus は最も簡単なデモ例であり、API の紹介に使用されます")
hello_milvus = Collection("hello_milvus", schema)
  • コレクションにベクトルを挿入:
import random
entities = [
    [i for i in range(3000)],  # field pk
    [float(random.randrange(-20, -10)) for _ in range(3000)],  # field random
    [[random.random() for _ in range(8)] for _ in range(3000)],  # field embeddings
]
insert_result = hello_milvus.insert(entities)
hello_milvus.flush()
  • エンティティにインデックスを作成:
index = {
    "index_type": "IVF_FLAT",
    "metric_type": "L2",
    "params": {"nlist": 128},
}
hello_milvus.create_index("embeddings", index)
  • コレクションをメモリにロードし、ベクトルの類似検索を実行:
hello_milvus.load()
vectors_to_search = entities[-1][-2:]
search_params = {
    "metric_type": "L2",
    "params": {"nprobe": 10},
}
result = hello_milvus.search(vectors_to_search, "embeddings", search_params, limit=3, output_fields=["random"])
  • ベクトルクエリを実行:
result = hello_milvus.query(expr="random > -14", output_fields=["random", "embeddings"])
  • ミックス検索を実行:
result = hello_milvus.search(vectors_to_search, "embeddings", search_params, limit=3, expr="random > -12", output_fields=["random"])
  • 主キーに基づいてエンティティを削除:
expr = f"pk in [{entities[0][0]}, {entities[0][1]}]"
hello_milvus.delete(expr)
  • コレクションを削除:
utility.drop_collection("hello_milvus")

サンプルコードの実行

以下のコマンドを実行して、サンプルコードを実行します。

$ python3 hello_milvus.py

以下に、返された結果とクエリの遅延時間が表示されます:

=== Milvusへの接続 ===

Milvusに名前がhello_milvusのコレクションが存在するか:False

=== `hello_milvus`コレクションの作成 ===

=== エンティティの挿入 ===

Milvus内のエンティティ数:3000

=== IVF_FLATインデックスの作成 ===

=== ローディング ===

=== ベクトル類似性に基づいて検索 ===

一致:(距離:0.0、ID:2998)、 ランダムフィールド:-11.0
一致:(距離:0.11455299705266953、ID:1581)、 ランダムフィールド:-18.0
一致:(距離:0.1232629269361496、ID:2647)、 ランダムフィールド:-13.0
一致:(距離:0.0、ID:2999)、 ランダムフィールド:-11.0
一致:(距離:0.10560893267393112、ID:2430)、 ランダムフィールド:-18.0
一致:(距離:0.13938161730766296、ID:377)、 ランダムフィールド:-14.0
検索遅延 = 0.2796秒

=== `random > -14`を使ったクエリ ===

クエリ結果:
-{'pk': 9, 'random': -13.0, 'embeddings': [0.298433, 0.931987, 0.949756, 0.598713, 0.290125, 0.094323, 0.064444, 0.306993]}
検索遅延 = 0.2970秒

=== `random > -12`を使った混合検索 ===

一致:(距離:0.0、ID:2998)、 ランダムフィールド:-11.0
一臈:(距離:0.15773043036460876、ID:472)、 ランダムフィールド:-11.0
一致:(距離:0.3273330628871918、ID:2146)、 ランダムフィールド:-11.0
一致:(距離:0.0、ID:2999)、 ランダムフィールド:-11.0
一致:(距離:0.15844076871871948、ID:2218)、 ランダムフィールド:-11.0
一致:(距離:0.1622171700000763、ID:1403)、 ランダムフィールド:-11.0
検索遅延 = 0.3028秒

=== `pk in [0, 1]`を使った削除の実行 ===

削除前の`pk in [0, 1]`をベースにしたクエリ結果:
-{'pk': 0, 'random': -18.0, 'embeddings': [0.142279, 0.414248, 0.378628, 0.971863, 0.535941, 0.107011, 0.207052, 0.98182]}
-{'pk': 1, 'random': -15.0, 'embeddings': [0.57512, 0.358512, 0.439131, 0.862369, 0.083284, 0.294493, 0.004961, 0.180082]}

削除後の`pk in [0, 1]`をベースにしたクエリ結果:[]
  
=== `hello_milvus`コレクションの削除 ===