{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Test" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pymilvus import connections, db\n", "\n", "conn = connections.connect(host=\"127.0.0.1\", port=19530)\n", "\n", "database = db.create_database(\"my_database\")\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['default', 'my_database']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.list_database()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'auto_id': False, 'description': '', 'fields': [{'name': 'my_id', 'description': '', 'type': , 'is_primary': True, 'auto_id': False}, {'name': 'my_vector', 'description': '', 'type': , 'params': {'dim': 5}}, {'name': 'my_varchar', 'description': '', 'type': , 'params': {'max_length': 512}}], 'enable_dynamic_field': True}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pymilvus import MilvusClient, DataType\n", "\n", "client = MilvusClient(\n", " uri=\"http://localhost:19530\",\n", " token=\"root:Milvus\"\n", ")\n", "\n", "schema = MilvusClient.create_schema(\n", " auto_id=False,\n", " enable_dynamic_field=True,\n", ")\n", "\n", "schema.add_field(field_name=\"my_id\", datatype=DataType.INT64, is_primary=True)\n", "schema.add_field(field_name=\"my_vector\", datatype=DataType.FLOAT_VECTOR, dim=5)\n", "schema.add_field(field_name=\"my_varchar\", datatype=DataType.VARCHAR, max_length=512)\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "index_params = client.prepare_index_params()\n", "index_params.add_index(\n", " field_name=\"my_vector\", \n", " index_type=\"AUTOINDEX\",\n", " metric_type=\"COSINE\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'state': }\n" ] } ], "source": [ "client.create_collection(\n", " collection_name=\"customized_setup_1\",\n", " schema=schema,\n", " index_params=index_params\n", ")\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'collection_name': 'customized_setup_1', 'auto_id': False, 'num_shards': 1, 'description': '', 'fields': [{'field_id': 100, 'name': 'my_id', 'description': '', 'type': , 'params': {}, 'is_primary': True}, {'field_id': 101, 'name': 'my_vector', 'description': '', 'type': , 'params': {'dim': 5}}, {'field_id': 102, 'name': 'my_varchar', 'description': '', 'type': , 'params': {'max_length': 512}}], 'functions': [], 'aliases': [], 'collection_id': 456238279677978972, 'consistency_level': 2, 'properties': {}, 'num_partitions': 1, 'enable_dynamic_field': True}\n" ] } ], "source": [ "res = client.describe_collection(\n", " collection_name=\"customized_setup_1\"\n", ")\n", "\n", "print(res)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "client.release_collection(\n", " collection_name=\"customized_setup_1\"\n", ")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "client.load_collection(\\\n", " collection_name=\"customized_setup_1\",\n", " # highlight-next-line\n", " load_fields=[\"my_id\", \"my_vector\"], # Load only the specified fields\n", " skip_load_dynamic_field=True # Skip loading the dynamic field\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tạo db với vector nhị phân" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'auto_id': True, 'description': '', 'fields': [{'name': 'pk', 'description': '', 'type': , 'params': {'max_length': 100}, 'is_primary': True, 'auto_id': False}, {'name': 'ingredients', 'description': '', 'type': , 'params': {'dim': 128}}, {'name': 'recipes', 'description': '', 'type': , 'params': {'max_length': 1000}}], 'enable_dynamic_field': False}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "schema = client.create_schema(\n", " auto_id=True,\n", " enable_dynamic_fields=True,\n", ")\n", "\n", "schema.add_field(field_name=\"pk\", datatype=DataType.VARCHAR, is_primary=True, max_length=100)\n", "schema.add_field(field_name=\"ingredients\", datatype=DataType.BINARY_VECTOR, dim=128)\n", "schema.add_field(field_name=\"recipes\", datatype=DataType.VARCHAR, max_length=1000)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "index_params = client.prepare_index_params()\n", "\n", "index_params.add_index(\n", " field_name=\"ingredients\",\n", " index_name=\"ingredients_index\",\n", " index_type=\"BIN_IVF_FLAT\",\n", " metric_type=\"HAMMING\",\n", " params={\"nlist\": 128}\n", ")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "client.create_collection(\n", " collection_name=\"cookbook_db\",\n", " schema=schema,\n", " index_params=index_params\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def convert_bool_list_to_bytes(bool_list):\n", " if len(bool_list) % 8 != 0:\n", " raise ValueError(\"The length of a boolean list must be a multiple of 8\")\n", "\n", " byte_array = bytearray(len(bool_list) // 8)\n", " for i, bit in enumerate(bool_list):\n", " if bit == 1:\n", " index = i // 8\n", " shift = i % 8\n", " byte_array[index] |= (1 << shift)\n", " return bytes(byte_array)\n", "\n", "\n", "bool_vectors = [\n", " [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0] + [0] * 112,\n", " [0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1] + [0] * 112,\n", "]\n", "\n", "data = [{\"ingredients\": convert_bool_list_to_bytes(bool_vector), \"recipes\": f\"xin chao cac ban {i}\" } for i, bool_vector in enumerate(bool_vectors)]\n", "\n", "client.insert(\n", " collection_name=\"cookbook_db\",\n", " data=data\n", ")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "search\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data: [\"[{'id': '456238279677992491', 'distance': 0.0, 'entity': {'pk': '456238279677992491', 'recipes': 'xin chao cac ban'}}]\"]\n" ] } ], "source": [ "search_params = {\n", " \"params\": {\"nprobe\": 10}\n", "}\n", "\n", "query_bool_list = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0] + [0] * 112\n", "query_vector = convert_bool_list_to_bytes(query_bool_list)\n", "\n", "res = client.search(\n", " collection_name=\"cookbook_db\",\n", " data=[query_vector],\n", " anns_field=\"ingredients\",\n", " search_params=search_params,\n", " limit=1,\n", " output_fields=[\"pk\", \"recipes\"]\n", ")\n", "\n", "print(res)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" } }, "nbformat": 4, "nbformat_minor": 2 }