|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import click |
| 4 | +from pymilvus import ( |
| 5 | + CollectionSchema, |
| 6 | + DataType, |
| 7 | + FieldSchema, |
| 8 | + MilvusClient, |
| 9 | +) |
| 10 | +from pymilvus.milvus_client import IndexParams |
| 11 | + |
| 12 | + |
| 13 | +def get_schema() -> CollectionSchema: |
| 14 | + id_field = FieldSchema( |
| 15 | + name="id", dtype=DataType.INT64, descrition="primary field", is_primary=True, auto_id=True |
| 16 | + ) |
| 17 | + embeddings_field = FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=384) |
| 18 | + |
| 19 | + schema = CollectionSchema( |
| 20 | + enable_dynamic_field=True, |
| 21 | + fields=[ |
| 22 | + id_field, |
| 23 | + embeddings_field, |
| 24 | + ], |
| 25 | + ) |
| 26 | + |
| 27 | + return schema |
| 28 | + |
| 29 | + |
| 30 | +def get_index_params() -> IndexParams: |
| 31 | + index_params = IndexParams() |
| 32 | + index_params.add_index(field_name="embeddings", index_type="AUTOINDEX", metric_type="COSINE") |
| 33 | + return index_params |
| 34 | + |
| 35 | + |
| 36 | +def create_database(client: MilvusClient, db_name: str) -> None: |
| 37 | + databases = client._get_connection().list_database() |
| 38 | + if db_name in databases: |
| 39 | + drop_database(client=client, db_name=db_name) |
| 40 | + print(f"Creating database {db_name}") |
| 41 | + client._get_connection().create_database(db_name=db_name) |
| 42 | + client.using_database(db_name=db_name) |
| 43 | + |
| 44 | + |
| 45 | +def drop_database(client: MilvusClient, db_name: str) -> None: |
| 46 | + print("Dropping existing database first") |
| 47 | + client.using_database(db_name=db_name) |
| 48 | + collections = client.list_collections() |
| 49 | + for collection in collections: |
| 50 | + print(f"Dropping existing collection {collection} first") |
| 51 | + client.drop_collection(collection_name=collection) |
| 52 | + client._get_connection().drop_database(db_name=db_name) |
| 53 | + |
| 54 | + |
| 55 | +def create_collection(client: MilvusClient, collection_name: str) -> None: |
| 56 | + collections = client.list_collections() |
| 57 | + if collection_name in collections: |
| 58 | + print("Dropping existing collection first") |
| 59 | + client.drop_collection(collection_name=collection_name) |
| 60 | + schema = get_schema() |
| 61 | + index_params = get_index_params() |
| 62 | + print(f"Creating collection {collection_name}") |
| 63 | + client.create_collection( |
| 64 | + collection_name=collection_name, schema=schema, index_params=index_params |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +@click.command("milvus-init") |
| 69 | +@click.option("--host", type=str, default="localhost") |
| 70 | +@click.option("--port", type=int, default=19530) |
| 71 | +@click.option("--db-name", type=str, default="milvus") |
| 72 | +def create(host: str, port: int, db_name: str): |
| 73 | + client = MilvusClient(uri=f"http://{host}:{port}") |
| 74 | + create_database(client=client, db_name=db_name) |
| 75 | + create_collection(client=client, collection_name="ingest_test") |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + create() |
0 commit comments