sonchuate commited on
Commit
5630b43
·
verified ·
1 Parent(s): 732e1fa

Upload 2 files

Browse files
Files changed (2) hide show
  1. milvus.ipynb +335 -0
  2. standalone.bat +145 -0
milvus.ipynb ADDED
@@ -0,0 +1,335 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Test"
8
+ ]
9
+ },
10
+ {
11
+ "cell_type": "code",
12
+ "execution_count": 1,
13
+ "metadata": {},
14
+ "outputs": [],
15
+ "source": [
16
+ "from pymilvus import connections, db\n",
17
+ "\n",
18
+ "conn = connections.connect(host=\"127.0.0.1\", port=19530)\n",
19
+ "\n",
20
+ "database = db.create_database(\"my_database\")\n"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 2,
26
+ "metadata": {},
27
+ "outputs": [
28
+ {
29
+ "data": {
30
+ "text/plain": [
31
+ "['default', 'my_database']"
32
+ ]
33
+ },
34
+ "execution_count": 2,
35
+ "metadata": {},
36
+ "output_type": "execute_result"
37
+ }
38
+ ],
39
+ "source": [
40
+ "db.list_database()"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": 3,
46
+ "metadata": {},
47
+ "outputs": [
48
+ {
49
+ "data": {
50
+ "text/plain": [
51
+ "{'auto_id': False, 'description': '', 'fields': [{'name': 'my_id', 'description': '', 'type': <DataType.INT64: 5>, 'is_primary': True, 'auto_id': False}, {'name': 'my_vector', 'description': '', 'type': <DataType.FLOAT_VECTOR: 101>, 'params': {'dim': 5}}, {'name': 'my_varchar', 'description': '', 'type': <DataType.VARCHAR: 21>, 'params': {'max_length': 512}}], 'enable_dynamic_field': True}"
52
+ ]
53
+ },
54
+ "execution_count": 3,
55
+ "metadata": {},
56
+ "output_type": "execute_result"
57
+ }
58
+ ],
59
+ "source": [
60
+ "from pymilvus import MilvusClient, DataType\n",
61
+ "\n",
62
+ "client = MilvusClient(\n",
63
+ " uri=\"http://localhost:19530\",\n",
64
+ " token=\"root:Milvus\"\n",
65
+ ")\n",
66
+ "\n",
67
+ "schema = MilvusClient.create_schema(\n",
68
+ " auto_id=False,\n",
69
+ " enable_dynamic_field=True,\n",
70
+ ")\n",
71
+ "\n",
72
+ "schema.add_field(field_name=\"my_id\", datatype=DataType.INT64, is_primary=True)\n",
73
+ "schema.add_field(field_name=\"my_vector\", datatype=DataType.FLOAT_VECTOR, dim=5)\n",
74
+ "schema.add_field(field_name=\"my_varchar\", datatype=DataType.VARCHAR, max_length=512)\n",
75
+ "\n"
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": 4,
81
+ "metadata": {},
82
+ "outputs": [],
83
+ "source": [
84
+ "index_params = client.prepare_index_params()\n",
85
+ "index_params.add_index(\n",
86
+ " field_name=\"my_vector\", \n",
87
+ " index_type=\"AUTOINDEX\",\n",
88
+ " metric_type=\"COSINE\"\n",
89
+ ")"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "code",
94
+ "execution_count": null,
95
+ "metadata": {},
96
+ "outputs": [
97
+ {
98
+ "name": "stdout",
99
+ "output_type": "stream",
100
+ "text": [
101
+ "{'state': <LoadState: Loaded>}\n"
102
+ ]
103
+ }
104
+ ],
105
+ "source": [
106
+ "client.create_collection(\n",
107
+ " collection_name=\"customized_setup_1\",\n",
108
+ " schema=schema,\n",
109
+ " index_params=index_params\n",
110
+ ")\n",
111
+ "\n"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": 8,
117
+ "metadata": {},
118
+ "outputs": [
119
+ {
120
+ "name": "stdout",
121
+ "output_type": "stream",
122
+ "text": [
123
+ "{'collection_name': 'customized_setup_1', 'auto_id': False, 'num_shards': 1, 'description': '', 'fields': [{'field_id': 100, 'name': 'my_id', 'description': '', 'type': <DataType.INT64: 5>, 'params': {}, 'is_primary': True}, {'field_id': 101, 'name': 'my_vector', 'description': '', 'type': <DataType.FLOAT_VECTOR: 101>, 'params': {'dim': 5}}, {'field_id': 102, 'name': 'my_varchar', 'description': '', 'type': <DataType.VARCHAR: 21>, 'params': {'max_length': 512}}], 'functions': [], 'aliases': [], 'collection_id': 456238279677978972, 'consistency_level': 2, 'properties': {}, 'num_partitions': 1, 'enable_dynamic_field': True}\n"
124
+ ]
125
+ }
126
+ ],
127
+ "source": [
128
+ "res = client.describe_collection(\n",
129
+ " collection_name=\"customized_setup_1\"\n",
130
+ ")\n",
131
+ "\n",
132
+ "print(res)"
133
+ ]
134
+ },
135
+ {
136
+ "cell_type": "code",
137
+ "execution_count": 13,
138
+ "metadata": {},
139
+ "outputs": [],
140
+ "source": [
141
+ "client.release_collection(\n",
142
+ " collection_name=\"customized_setup_1\"\n",
143
+ ")"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "code",
148
+ "execution_count": 14,
149
+ "metadata": {},
150
+ "outputs": [],
151
+ "source": [
152
+ "client.load_collection(\\\n",
153
+ " collection_name=\"customized_setup_1\",\n",
154
+ " # highlight-next-line\n",
155
+ " load_fields=[\"my_id\", \"my_vector\"], # Load only the specified fields\n",
156
+ " skip_load_dynamic_field=True # Skip loading the dynamic field\n",
157
+ ")"
158
+ ]
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "execution_count": null,
163
+ "metadata": {},
164
+ "outputs": [],
165
+ "source": []
166
+ },
167
+ {
168
+ "cell_type": "markdown",
169
+ "metadata": {},
170
+ "source": [
171
+ "# Tạo db với vector nhị phân"
172
+ ]
173
+ },
174
+ {
175
+ "cell_type": "code",
176
+ "execution_count": 15,
177
+ "metadata": {},
178
+ "outputs": [
179
+ {
180
+ "data": {
181
+ "text/plain": [
182
+ "{'auto_id': True, 'description': '', 'fields': [{'name': 'pk', 'description': '', 'type': <DataType.VARCHAR: 21>, 'params': {'max_length': 100}, 'is_primary': True, 'auto_id': False}, {'name': 'ingredients', 'description': '', 'type': <DataType.BINARY_VECTOR: 100>, 'params': {'dim': 128}}, {'name': 'recipes', 'description': '', 'type': <DataType.VARCHAR: 21>, 'params': {'max_length': 1000}}], 'enable_dynamic_field': False}"
183
+ ]
184
+ },
185
+ "execution_count": 15,
186
+ "metadata": {},
187
+ "output_type": "execute_result"
188
+ }
189
+ ],
190
+ "source": [
191
+ "schema = client.create_schema(\n",
192
+ " auto_id=True,\n",
193
+ " enable_dynamic_fields=True,\n",
194
+ ")\n",
195
+ "\n",
196
+ "schema.add_field(field_name=\"pk\", datatype=DataType.VARCHAR, is_primary=True, max_length=100)\n",
197
+ "schema.add_field(field_name=\"ingredients\", datatype=DataType.BINARY_VECTOR, dim=128)\n",
198
+ "schema.add_field(field_name=\"recipes\", datatype=DataType.VARCHAR, max_length=1000)"
199
+ ]
200
+ },
201
+ {
202
+ "cell_type": "code",
203
+ "execution_count": 16,
204
+ "metadata": {},
205
+ "outputs": [],
206
+ "source": [
207
+ "index_params = client.prepare_index_params()\n",
208
+ "\n",
209
+ "index_params.add_index(\n",
210
+ " field_name=\"ingredients\",\n",
211
+ " index_name=\"ingredients_index\",\n",
212
+ " index_type=\"BIN_IVF_FLAT\",\n",
213
+ " metric_type=\"HAMMING\",\n",
214
+ " params={\"nlist\": 128}\n",
215
+ ")"
216
+ ]
217
+ },
218
+ {
219
+ "cell_type": "code",
220
+ "execution_count": 17,
221
+ "metadata": {},
222
+ "outputs": [],
223
+ "source": [
224
+ "client.create_collection(\n",
225
+ " collection_name=\"cookbook_db\",\n",
226
+ " schema=schema,\n",
227
+ " index_params=index_params\n",
228
+ ")"
229
+ ]
230
+ },
231
+ {
232
+ "cell_type": "code",
233
+ "execution_count": null,
234
+ "metadata": {},
235
+ "outputs": [],
236
+ "source": [
237
+ "def convert_bool_list_to_bytes(bool_list):\n",
238
+ " if len(bool_list) % 8 != 0:\n",
239
+ " raise ValueError(\"The length of a boolean list must be a multiple of 8\")\n",
240
+ "\n",
241
+ " byte_array = bytearray(len(bool_list) // 8)\n",
242
+ " for i, bit in enumerate(bool_list):\n",
243
+ " if bit == 1:\n",
244
+ " index = i // 8\n",
245
+ " shift = i % 8\n",
246
+ " byte_array[index] |= (1 << shift)\n",
247
+ " return bytes(byte_array)\n",
248
+ "\n",
249
+ "\n",
250
+ "bool_vectors = [\n",
251
+ " [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0] + [0] * 112,\n",
252
+ " [0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1] + [0] * 112,\n",
253
+ "]\n",
254
+ "\n",
255
+ "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",
256
+ "\n",
257
+ "client.insert(\n",
258
+ " collection_name=\"cookbook_db\",\n",
259
+ " data=data\n",
260
+ ")\n",
261
+ "\n"
262
+ ]
263
+ },
264
+ {
265
+ "cell_type": "markdown",
266
+ "metadata": {},
267
+ "source": [
268
+ "search\n"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "execution_count": 28,
274
+ "metadata": {},
275
+ "outputs": [
276
+ {
277
+ "name": "stdout",
278
+ "output_type": "stream",
279
+ "text": [
280
+ "data: [\"[{'id': '456238279677992491', 'distance': 0.0, 'entity': {'pk': '456238279677992491', 'recipes': 'xin chao cac ban'}}]\"]\n"
281
+ ]
282
+ }
283
+ ],
284
+ "source": [
285
+ "search_params = {\n",
286
+ " \"params\": {\"nprobe\": 10}\n",
287
+ "}\n",
288
+ "\n",
289
+ "query_bool_list = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0] + [0] * 112\n",
290
+ "query_vector = convert_bool_list_to_bytes(query_bool_list)\n",
291
+ "\n",
292
+ "res = client.search(\n",
293
+ " collection_name=\"cookbook_db\",\n",
294
+ " data=[query_vector],\n",
295
+ " anns_field=\"ingredients\",\n",
296
+ " search_params=search_params,\n",
297
+ " limit=1,\n",
298
+ " output_fields=[\"pk\", \"recipes\"]\n",
299
+ ")\n",
300
+ "\n",
301
+ "print(res)\n",
302
+ "\n",
303
+ "\n"
304
+ ]
305
+ },
306
+ {
307
+ "cell_type": "code",
308
+ "execution_count": null,
309
+ "metadata": {},
310
+ "outputs": [],
311
+ "source": []
312
+ }
313
+ ],
314
+ "metadata": {
315
+ "kernelspec": {
316
+ "display_name": "Python 3",
317
+ "language": "python",
318
+ "name": "python3"
319
+ },
320
+ "language_info": {
321
+ "codemirror_mode": {
322
+ "name": "ipython",
323
+ "version": 3
324
+ },
325
+ "file_extension": ".py",
326
+ "mimetype": "text/x-python",
327
+ "name": "python",
328
+ "nbconvert_exporter": "python",
329
+ "pygments_lexer": "ipython3",
330
+ "version": "3.10.10"
331
+ }
332
+ },
333
+ "nbformat": 4,
334
+ "nbformat_minor": 2
335
+ }
standalone.bat ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @REM Licensed to the LF AI & Data foundation under one
2
+ @REM or more contributor license agreements. See the NOTICE file
3
+ @REM distributed with this work for additional information
4
+ @REM regarding copyright ownership. The ASF licenses this file
5
+ @REM to you under the Apache License, Version 2.0 (the
6
+ @REM "License"); you may not use this file except in compliance
7
+ @REM with the License. You may obtain a copy of the License at
8
+ @REM
9
+ @REM http://www.apache.org/licenses/LICENSE-2.0
10
+ @REM
11
+ @REM Unless required by applicable law or agreed to in writing, software
12
+ @REM distributed under the License is distributed on an "AS IS" BASIS,
13
+ @REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ @REM See the License for the specific language governing permissions and
15
+ @REM limitations under the License.
16
+
17
+ @echo off
18
+ setlocal enabledelayedexpansion
19
+
20
+ :main
21
+ if "%1"=="" (
22
+ echo Please use standalone_embed.bat restart^|start^|stop^|delete
23
+ exit /b 1
24
+ )
25
+
26
+ if "%1"=="restart" (
27
+ call :stop
28
+ call :start
29
+ ) else if "%1"=="start" (
30
+ call :start
31
+ ) else if "%1"=="stop" (
32
+ call :stop
33
+ ) else if "%1"=="delete" (
34
+ call :delete
35
+ ) else (
36
+ echo Unknown command.
37
+ echo Please use standalone_embed.bat restart^|start^|stop^|upgrade^|delete
38
+ exit /b 1
39
+ )
40
+ goto :eof
41
+
42
+ :run_embed
43
+ (
44
+ echo listen-client-urls: http://0.0.0.0:2379
45
+ echo advertise-client-urls: http://0.0.0.0:2379
46
+ echo quota-backend-bytes: 4294967296
47
+ echo auto-compaction-mode: revision
48
+ echo auto-compaction-retention: '1000'
49
+ ) > embedEtcd.yaml
50
+
51
+ (
52
+ echo # Extra config to override default milvus.yaml
53
+ ) > user.yaml
54
+
55
+ docker run -d ^
56
+ --name milvus-standalone ^
57
+ --security-opt seccomp:unconfined ^
58
+ -e ETCD_USE_EMBED=true ^
59
+ -e ETCD_DATA_DIR=/var/lib/milvus/etcd ^
60
+ -e ETCD_CONFIG_PATH=/milvus/configs/embedEtcd.yaml ^
61
+ -e COMMON_STORAGETYPE=local ^
62
+ -v "%cd%\volumes\milvus:/var/lib/milvus" ^
63
+ -v "%cd%\embedEtcd.yaml:/milvus/configs/embedEtcd.yaml" ^
64
+ -v "%cd%\user.yaml:/milvus/configs/user.yaml" ^
65
+ -p 19530:19530 ^
66
+ -p 9091:9091 ^
67
+ -p 2379:2379 ^
68
+ --health-cmd="curl -f http://localhost:9091/healthz" ^
69
+ --health-interval=30s ^
70
+ --health-start-period=90s ^
71
+ --health-timeout=20s ^
72
+ --health-retries=3 ^
73
+ milvusdb/milvus:v2.4.13 ^
74
+ milvus run standalone >nul
75
+ if %errorlevel% neq 0 (
76
+ echo Failed to start Milvus container.
77
+ exit /b 1
78
+ )
79
+
80
+ goto :eof
81
+
82
+ :wait_for_milvus_running
83
+ echo Wait for Milvus Starting...
84
+ :wait_loop
85
+ for /f "tokens=*" %%A in ('docker ps ^| findstr "milvus-standalone" ^| findstr "healthy"') do set running=1
86
+ if "!running!"=="1" (
87
+ echo Start successfully.
88
+ echo To change the default Milvus configuration, edit user.yaml and restart the service.
89
+ goto :eof
90
+ )
91
+ timeout /t 1 >nul
92
+ goto wait_loop
93
+
94
+ :start
95
+ for /f "tokens=*" %%A in ('docker ps ^| findstr "milvus-standalone" ^| findstr "healthy"') do (
96
+ echo Milvus is running.
97
+ exit /b 0
98
+ )
99
+
100
+ for /f "tokens=*" %%A in ('docker ps -a ^| findstr "milvus-standalone"') do set container_exists=1
101
+ if defined container_exists (
102
+ docker start milvus-standalone >nul
103
+ ) else (
104
+ call :run_embed
105
+ )
106
+
107
+ if %errorlevel% neq 0 (
108
+ echo Start failed.
109
+ exit /b 1
110
+ )
111
+
112
+ call :wait_for_milvus_running
113
+ goto :eof
114
+
115
+ :stop
116
+ docker stop milvus-standalone >nul
117
+ if %errorlevel% neq 0 (
118
+ echo Stop failed.
119
+ exit /b 1
120
+ )
121
+ echo Stop successfully.
122
+ goto :eof
123
+
124
+ :delete_container
125
+ for /f "tokens=*" %%A in ('docker ps ^| findstr "milvus-standalone"') do (
126
+ echo Please stop Milvus service before delete.
127
+ exit /b 1
128
+ )
129
+ docker rm milvus-standalone >nul
130
+ if %errorlevel% neq 0 (
131
+ echo Delete Milvus container failed.
132
+ exit /b 1
133
+ )
134
+ echo Delete Milvus container successfully.
135
+ goto :eof
136
+
137
+ :delete
138
+ call :delete_container
139
+ rmdir /s /q "%cd%\volumes"
140
+ del /q embedEtcd.yaml
141
+ del /q user.yaml
142
+ echo Delete successfully.
143
+ goto :eof
144
+
145
+ :EOF