File size: 19,845 Bytes
776f7d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SPTAG python wrapper tutorial \n",
    "\n",
    "To end-to-end build a vector search online service, it contains two steps:\n",
    "- Offline build SPTAG index for database vectors\n",
    "- Online serve the index to support vector search requests from the clients\n",
    "\n",
    "## Offline build SPTAG index\n",
    "\n",
    "> Prepare input vectors and metadatas for SPTAG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "import numpy as np\n",
    "\n",
    "vector_number = 1000\n",
    "vector_dimension = 100\n",
    "\n",
    "# Randomly generate the database vectors. Currently SPTAG only support int8, int16 and float32 data type.\n",
    "x = np.random.rand(vector_number, vector_dimension).astype(np.float32) \n",
    "\n",
    "# Prepare metadata for each vectors, separate them by '\\n'. Currently SPTAG python wrapper only support '\\n' as the separator\n",
    "m = ''\n",
    "for i in range(vector_number):\n",
    "    m += str(i) + '\\n'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Build SPTAG index for database vectors **x**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['deletes.bin',\n",
       " 'graph.bin',\n",
       " 'indexloader.ini',\n",
       " 'metadata.bin',\n",
       " 'metadataIndex.bin',\n",
       " 'tree.bin',\n",
       " 'vectors.bin']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import SPTAG\n",
    "\n",
    "index = SPTAG.AnnIndex('BKT', 'Float', vector_dimension)\n",
    "\n",
    "# Set the thread number to speed up the build procedure in parallel \n",
    "index.SetBuildParam(\"NumberOfThreads\", '4', \"Index\")\n",
    "\n",
    "# Set the distance type. Currently SPTAG only support Cosine and L2 distances. Here Cosine distance is not the Cosine similarity. The smaller Cosine distance it is, the better.\n",
    "index.SetBuildParam(\"DistCalcMethod\", 'L2', \"Index\") \n",
    "\n",
    "if (os.path.exists(\"sptag_index\")):\n",
    "    shutil.rmtree(\"sptag_index\")\n",
    "if index.BuildWithMetaData(x, m, vector_number, False, False):\n",
    "    index.Save(\"sptag_index\") # Save the index to the disk\n",
    "\n",
    "os.listdir('sptag_index')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[450, 247, 150, 832, 626, 828, 253, 471, 243, 365]\n",
      "[11.603649139404297, 12.164414405822754, 12.225785255432129, 12.336504936218262, 12.416375160217285, 12.481977462768555, 12.573633193969727, 12.616722106933594, 12.708147048950195, 12.821012496948242]\n",
      "[b'450\\n', b'247\\n', b'150\\n', b'832\\n', b'626\\n', b'828\\n', b'253\\n', b'471\\n', b'243\\n', b'365\\n']\n",
      "[(450, 11.603649139404297, b'450\\n'), (247, 12.164414405822754, b'247\\n'), (150, 12.225785255432129, b'150\\n'), (832, 12.336504936218262, b'832\\n'), (626, 12.416375160217285, b'626\\n'), (828, 12.481977462768555, b'828\\n'), (253, 12.573633193969727, b'253\\n'), (471, 12.616722106933594, b'471\\n'), (243, 12.708147048950195, b'243\\n'), (365, 12.821012496948242, b'365\\n')]\n"
     ]
    }
   ],
   "source": [
    "# Local index test on the vector search\n",
    "index = SPTAG.AnnIndex.Load('sptag_index')\n",
    "\n",
    "# prepare query vector\n",
    "q = np.random.rand(vector_dimension).astype(np.float32)\n",
    "\n",
    "result = index.SearchWithMetaData(q, 10) # Search k=3 nearest vectors for query vector q\n",
    "print (result[0]) # nearest k vector ids\n",
    "print (result[1]) # nearest k vector distances\n",
    "print (result[2]) # nearest k vector metadatas\n",
    "\n",
    "results = []\n",
    "iterator = index.GetIterator(q)\n",
    "while True:\n",
    "    result = iterator.Next(10)\n",
    "    #print (result[0]) # vector ids of the batch index scan candidates\n",
    "    #print (result[1]) # vector distances of the batch index scan candidates\n",
    "    #print (result[2]) # vector metadatas of the batch index scan candidates\n",
    "    #print (result[3]) # relaxed monotonicity of the batch index scan candidates\n",
    "    for j in range(len(result[0])):\n",
    "        results.append((result[0][j], result[1][j], result[2][j]))\n",
    "    if len(result[0]) < 10 or result[3][0] == True: break\n",
    "iterator.Close()\n",
    "\n",
    "results.sort(key = lambda item: item[1])\n",
    "print (results[0:10])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Build SPTAG index for database vector **x** with PQ quantization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CompletedProcess(args='Quantizer.exe -d 100 -v Float -f DEFAULT -i sptag_index\\\\vectors.bin -o quan_doc_vectors.bin -oq quantizer.bin -qt PQQuantizer -qd 50 -ts 1000 -norm false', returncode=0)\n",
      "(1000, 50)\n"
     ]
    }
   ],
   "source": [
    "import subprocess\n",
    "import struct\n",
    "import numpy as np\n",
    "\n",
    "if (os.path.exists(\"quantizer.bin\")):\n",
    "    os.remove(\"quantizer.bin\")\n",
    "if (os.path.exists(\"quan_doc_vectors.bin\")):\n",
    "    os.remove(\"quan_doc_vectors.bin\")\n",
    "cmd = \"Quantizer.exe -d %d -v Float -f DEFAULT -i sptag_index\\\\vectors.bin -o quan_doc_vectors.bin -oq quantizer.bin -qt PQQuantizer -qd %d -ts %d -norm false\" % (vector_dimension, int(vector_dimension / 2), vector_number)\n",
    "result = subprocess.run(cmd)\n",
    "print (result) \n",
    "# For SPTAG index with quantization:\n",
    "#     Cosine distance: norm -> true and use L2 for index build\n",
    "#     L2 distance: norm -> false and use L2 for index build\n",
    "\n",
    "f = open('quan_doc_vectors.bin', 'rb')\n",
    "r = struct.unpack('i', f.read(4))[0]\n",
    "c = struct.unpack('i', f.read(4))[0]\n",
    "quan_x = np.frombuffer(f.read(r*c), dtype=np.uint8).reshape((r,c))\n",
    "f.close()\n",
    "print (quan_x.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['deletes.bin',\n",
       " 'graph.bin',\n",
       " 'indexloader.ini',\n",
       " 'metadata.bin',\n",
       " 'metadataIndex.bin',\n",
       " 'quantizer.bin',\n",
       " 'tree.bin',\n",
       " 'vectors.bin']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import SPTAG\n",
    "\n",
    "index = SPTAG.AnnIndex('BKT', 'UInt8', int(vector_dimension / 2))\n",
    "\n",
    "# Set the thread number to speed up the build procedure in parallel \n",
    "index.SetBuildParam(\"NumberOfThreads\", '4', \"Index\")\n",
    "\n",
    "# Set the distance type. Currently SPTAG only support Cosine and L2 distances. Here Cosine distance is not the Cosine similarity. The smaller Cosine distance it is, the better.\n",
    "index.SetBuildParam(\"DistCalcMethod\", 'L2', \"Index\") \n",
    "\n",
    "if (os.path.exists(\"quan_sptag_index\")):\n",
    "    shutil.rmtree(\"quan_sptag_index\")\n",
    "if index.LoadQuantizer(\"quantizer.bin\") and index.BuildWithMetaData(quan_x, m, vector_number, False, False):\n",
    "    index.Save(\"quan_sptag_index\") # Save the index to the disk\n",
    "\n",
    "os.listdir('quan_sptag_index')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[450, 247, 832, 626, 150, 253, 471, 828, 243, 207, 365, 823]\n",
      "[11.497368812561035, 12.21573257446289, 12.328060150146484, 12.369790077209473, 12.420766830444336, 12.47612476348877, 12.48221206665039, 12.526467323303223, 12.802266120910645, 12.819355010986328, 12.876933097839355, 12.913871765136719]\n",
      "[b'450\\n', b'247\\n', b'832\\n', b'626\\n', b'150\\n', b'253\\n', b'471\\n', b'828\\n', b'243\\n', b'207\\n', b'365\\n', b'823\\n']\n"
     ]
    }
   ],
   "source": [
    "# Local index test on the vector search\n",
    "index = SPTAG.AnnIndex.Load('quan_sptag_index')\n",
    "index.SetQuantizerADC(True)\n",
    "result = index.SearchWithMetaData(q, 12) # Search k=3 nearest vectors for query vector q\n",
    "print (result[0]) # nearest k vector ids\n",
    "print (result[1]) # nearest k vector distances\n",
    "print (result[2]) # nearest k vector metadatas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['HeadIndex',\n",
       " 'indexloader.ini',\n",
       " 'metadata.bin',\n",
       " 'metadataIndex.bin',\n",
       " 'SPTAGFullList.bin',\n",
       " 'SPTAGHeadVectorIDs.bin',\n",
       " 'SPTAGHeadVectors.bin']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import SPTAG\n",
    "\n",
    "index = SPTAG.AnnIndex('SPANN', 'Float', vector_dimension)\n",
    "\n",
    "# Set the thread number to speed up the build procedure in parallel \n",
    "index.SetBuildParam(\"IndexAlgoType\", \"BKT\", \"Base\")\n",
    "index.SetBuildParam(\"IndexDirectory\", \"spann_index\", \"Base\")\n",
    "index.SetBuildParam(\"DistCalcMethod\", \"L2\", \"Base\")\n",
    "\n",
    "index.SetBuildParam(\"isExecute\", \"true\", \"SelectHead\")\n",
    "index.SetBuildParam(\"NumberOfThreads\", \"4\", \"SelectHead\")\n",
    "index.SetBuildParam(\"Ratio\", \"0.2\", \"SelectHead\") # index.SetBuildParam(\"Count\", \"200\", \"SelectHead\")\n",
    "\n",
    "index.SetBuildParam(\"isExecute\", \"true\", \"BuildHead\")\n",
    "index.SetBuildParam(\"RefineIterations\", \"3\", \"BuildHead\")\n",
    "index.SetBuildParam(\"NumberOfThreads\", \"4\", \"BuildHead\")\n",
    "\n",
    "index.SetBuildParam(\"isExecute\", \"true\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"BuildSsdIndex\", \"true\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"PostingPageLimit\", \"12\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"SearchPostingPageLimit\", \"12\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"NumberOfThreads\", \"4\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"InternalResultNum\", \"32\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"SearchInternalResultNum\", \"64\", \"BuildSSDIndex\")\n",
    "\n",
    "if (os.path.exists(\"spann_index\")):\n",
    "    shutil.rmtree(\"spann_index\")\n",
    "\n",
    "if index.BuildWithMetaData(x, m, vector_number, False, False):\n",
    "    index.Save(\"spann_index\") # Save the index to the disk\n",
    "\n",
    "os.listdir('spann_index')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[450, 247, 150, 832, 626, 828, 253, 471, 243, 365, 207, 823]\n",
      "[11.603649139404297, 12.164414405822754, 12.225785255432129, 12.336504936218262, 12.416375160217285, 12.481977462768555, 12.573633193969727, 12.616722106933594, 12.708147048950195, 12.821012496948242, 12.878414154052734, 13.01927375793457]\n",
      "[b'450\\n', b'247\\n', b'150\\n', b'832\\n', b'626\\n', b'828\\n', b'253\\n', b'471\\n', b'243\\n', b'365\\n', b'207\\n', b'823\\n']\n"
     ]
    }
   ],
   "source": [
    "index = SPTAG.AnnIndex.Load('spann_index')\n",
    "result = index.SearchWithMetaData(q, 12) # Search k=3 nearest vectors for query vector q\n",
    "print (result[0]) # nearest k vector ids\n",
    "print (result[1]) # nearest k vector distances\n",
    "print (result[2]) # nearest k vector metadatas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['HeadIndex',\n",
       " 'indexloader.ini',\n",
       " 'metadata.bin',\n",
       " 'metadataIndex.bin',\n",
       " 'quantizer.bin',\n",
       " 'SPTAGFullList.bin',\n",
       " 'SPTAGHeadVectorIDs.bin',\n",
       " 'SPTAGHeadVectors.bin']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import SPTAG\n",
    "\n",
    "index = SPTAG.AnnIndex('SPANN', 'UInt8', int(vector_dimension / 2))\n",
    "\n",
    "# Set the thread number to speed up the build procedure in parallel \n",
    "index.SetBuildParam(\"IndexAlgoType\", \"BKT\", \"Base\")\n",
    "index.SetBuildParam(\"IndexDirectory\", \"spann_quan_index\", \"Base\")\n",
    "index.SetBuildParam(\"DistCalcMethod\", \"L2\", \"Base\")\n",
    "index.SetBuildParam(\"QuantizerFilePath\", \"quantizer.bin\", \"Base\")\n",
    "\n",
    "index.SetBuildParam(\"isExecute\", \"true\", \"SelectHead\")\n",
    "index.SetBuildParam(\"NumberOfThreads\", \"4\", \"SelectHead\")\n",
    "index.SetBuildParam(\"Ratio\", \"0.2\", \"SelectHead\") # index.SetBuildParam(\"Count\", \"200\", \"SelectHead\")\n",
    "\n",
    "index.SetBuildParam(\"isExecute\", \"true\", \"BuildHead\")\n",
    "index.SetBuildParam(\"RefineIterations\", \"3\", \"BuildHead\")\n",
    "index.SetBuildParam(\"NumberOfThreads\", \"4\", \"BuildHead\")\n",
    "\n",
    "index.SetBuildParam(\"isExecute\", \"true\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"BuildSsdIndex\", \"true\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"PostingPageLimit\", \"12\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"SearchPostingPageLimit\", \"12\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"NumberOfThreads\", \"4\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"InternalResultNum\", \"32\", \"BuildSSDIndex\")\n",
    "index.SetBuildParam(\"SearchInternalResultNum\", \"64\", \"BuildSSDIndex\")\n",
    "\n",
    "if (os.path.exists(\"spann_quan_index\")):\n",
    "    shutil.rmtree(\"spann_quan_index\")\n",
    "\n",
    "if index.LoadQuantizer(\"quantizer.bin\") and index.BuildWithMetaData(quan_x, m, vector_number, False, False):\n",
    "    index.Save(\"spann_quan_index\") # Save the index to the disk\n",
    "\n",
    "os.listdir('spann_quan_index')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[450, 247, 832, 626, 150, 253, 471, 828, 243, 207, 365, 823]\n",
      "[11.497368812561035, 12.21573257446289, 12.328060150146484, 12.369790077209473, 12.420766830444336, 12.47612476348877, 12.48221206665039, 12.526467323303223, 12.802266120910645, 12.819355010986328, 12.876933097839355, 12.913871765136719]\n",
      "[b'450\\n', b'247\\n', b'832\\n', b'626\\n', b'150\\n', b'253\\n', b'471\\n', b'828\\n', b'243\\n', b'207\\n', b'365\\n', b'823\\n']\n"
     ]
    }
   ],
   "source": [
    "index = SPTAG.AnnIndex.Load('spann_quan_index')\n",
    "index.SetQuantizerADC(True)\n",
    "result = index.SearchWithMetaData(q, 12) # Search k=3 nearest vectors for query vector q\n",
    "print (result[0]) # nearest k vector ids\n",
    "print (result[1]) # nearest k vector distances\n",
    "print (result[2]) # nearest k vector metadatas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Online serve the index\n",
    "\n",
    "Start the vector search service on the host machine which listens for the client requests on the port 8000\n",
    "\n",
    "> Write a server configuration file **service.ini** as follows:\n",
    "\n",
    "```bash\n",
    "[Service]\n",
    "ListenAddr=0.0.0.0\n",
    "ListenPort=8000\n",
    "ThreadNumber=8\n",
    "SocketThreadNumber=8\n",
    "\n",
    "[QueryConfig]\n",
    "DefaultMaxResultNumber=6\n",
    "DefaultSeparator=|\n",
    "\n",
    "[Index]\n",
    "List=MyIndex\n",
    "\n",
    "[Index_MyIndex]\n",
    "IndexFolder=sptag_index\n",
    "```\n",
    "\n",
    "> Start the server on the host machine\n",
    "\n",
    "```bash\n",
    "Server.exe -m socket -c service.ini\n",
    "```\n",
    "\n",
    "It will print the follow messages:\n",
    "\n",
    "```bash\n",
    "Setting TreeFilePath with value tree.bin\n",
    "Setting GraphFilePath with value graph.bin\n",
    "Setting VectorFilePath with value vectors.bin\n",
    "Setting DeleteVectorFilePath with value deletes.bin\n",
    "Setting BKTNumber with value 1\n",
    "Setting BKTKmeansK with value 32\n",
    "Setting BKTLeafSize with value 8\n",
    "Setting Samples with value 1000\n",
    "Setting TPTNumber with value 32\n",
    "Setting TPTLeafSize with value 2000\n",
    "Setting NumTopDimensionTpTreeSplit with value 5\n",
    "Setting NeighborhoodSize with value 32\n",
    "Setting GraphNeighborhoodScale with value 2\n",
    "Setting GraphCEFScale with value 2\n",
    "Setting RefineIterations with value 2\n",
    "Setting CEF with value 1000\n",
    "Setting MaxCheckForRefineGraph with value 8192\n",
    "Setting NumberOfThreads with value 4\n",
    "Setting DistCalcMethod with value Cosine\n",
    "Setting DeletePercentageForRefine with value 0.400000\n",
    "Setting AddCountForRebuild with value 1000\n",
    "Setting MaxCheck with value 8192\n",
    "Setting ThresholdOfNumberOfContinuousNoBetterPropagation with value 3\n",
    "Setting NumberOfInitialDynamicPivots with value 50\n",
    "Setting NumberOfOtherDynamicPivots with value 4\n",
    "Load Vector From sptag_index\\vectors.bin\n",
    "Load Vector (100, 10) Finish!\n",
    "Load BKT From sptag_index\\tree.bin\n",
    "Load BKT (1,101) Finish!\n",
    "Load Graph From sptag_index\\graph.bin\n",
    "Load Graph (100, 32) Finish!\n",
    "Load DeleteID From sptag_index\\deletes.bin\n",
    "Load DeleteID (100, 1) Finish!\n",
    "Start to listen 0.0.0.0:8000 ...\n",
    "```\n",
    "\n",
    "> Start python client to connect to the server and send vector search request."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import SPTAGClient\n",
    "import time\n",
    "\n",
    "# connect to the server\n",
    "client = SPTAGClient.AnnClient('127.0.0.1', '8000')\n",
    "while not client.IsConnected():\n",
    "    time.sleep(1)\n",
    "client.SetTimeoutMilliseconds(18000)\n",
    "\n",
    "k = 3\n",
    "vector_dimension = 10\n",
    "# prepare query vector\n",
    "q = np.random.rand(vector_dimension).astype(np.float32)\n",
    "\n",
    "result = client.Search(q, k, 'Float', True) # AnnClient.Search(query_vector, knn, data_type, with_metadata)\n",
    "\n",
    "print (result[0]) # nearest k vector ids\n",
    "print (result[1]) # nearest k vector distances\n",
    "print (result[2]) # nearest k vector metadatas\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "file_extension": ".py",
  "kernelspec": {
   "display_name": "Python 3.9.12 64-bit",
   "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.9.3"
  },
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython2",
  "version": 2,
  "vscode": {
   "interpreter": {
    "hash": "81794d4967e6c3204c66dcd87b604927b115b27c00565d3d43f05ba2f3a2cb0d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}