1+ import time
2+
3+ import numpy as np
4+ from pymilvus import (
5+ connections ,
6+ utility ,
7+ FieldSchema , CollectionSchema , DataType ,
8+ Collection ,
9+ MilvusClient
10+ )
11+
12+ fmt = "\n === {:30} ===\n "
13+ search_latency_fmt = "search latency = {:.4f}s"
14+ num_entities , dim = 3000 , 8
15+
16+
17+ print (fmt .format ("start connecting to Milvus" ))
18+ # this is milvus standalone
19+ connection = connections .connect (
20+ alias = "default" ,
21+ host = 'localhost' , # or '0.0.0.0' or 'localhost'
22+ port = '19530'
23+ )
24+
25+ client = MilvusClient (connections = connection )
26+
27+ has = utility .has_collection ("hello_milvus" )
28+ print (f"Does collection hello_milvus exist in Milvus: { has } " )
29+ if has :
30+ utility .drop_collection ("hello_milvus" )
31+
32+ fields = [
33+ FieldSchema (name = "pk" , dtype = DataType .VARCHAR , is_primary = True , auto_id = False , max_length = 100 ),
34+ FieldSchema (name = "random" , dtype = DataType .DOUBLE ),
35+ FieldSchema (name = "embeddings1" , dtype = DataType .FLOAT_VECTOR , dim = dim ),
36+ FieldSchema (name = "embeddings2" , dtype = DataType .FLOAT_VECTOR , dim = dim )
37+ ]
38+
39+ schema = CollectionSchema (fields , "hello_milvus is the simplest demo to introduce the APIs" )
40+
41+ print (fmt .format ("Create collection `hello_milvus`" ))
42+
43+ print (fmt .format ("Message for handling an invalid format in the normalization_fields value" )) # you can try with other value like: dict,...
44+ try :
45+ hello_milvus = Collection ("hello_milvus" , schema , consistency_level = "Strong" , normalization_fields = 'embeddings1' )
46+ except BaseException as e :
47+ print (e )
48+
49+
50+ print (fmt .format ("Message for handling the invalid vector fields" ))
51+ try :
52+ hello_milvus = Collection ("hello_milvus" , schema , consistency_level = "Strong" , normalization_fields = ['embddings' ])
53+ except BaseException as e :
54+ print (e )
55+
56+ print (fmt .format ("Insert data, with conversion to standard form" ))
57+
58+ hello_milvus = Collection ("hello_milvus" , schema , consistency_level = "Strong" , normalization_fields = ['embeddings1' ])
59+
60+ print (fmt .format ("Start inserting a row" ))
61+ rng = np .random .default_rng (seed = 19530 )
62+
63+ row = {
64+ "pk" : "19530" ,
65+ "random" : 0.5 ,
66+ "embeddings1" : rng .random ((1 , dim ), np .float32 )[0 ],
67+ "embeddings2" : rng .random ((1 , dim ), np .float32 )[0 ]
68+ }
69+ _row = row .copy ()
70+ hello_milvus .insert (row )
71+
72+ index_param = {"index_type" : "FLAT" , "metric_type" : "L2" , "params" : {}}
73+ hello_milvus .create_index ("embeddings1" , index_param )
74+ hello_milvus .create_index ("embeddings2" , index_param )
75+ hello_milvus .load ()
76+
77+ original_vector = _row ['embeddings1' ]
78+ insert_vector = hello_milvus .query (
79+ expr = "pk == '19530'" ,
80+ output_fields = ["embeddings1" ],
81+ )[0 ]['embeddings1' ]
82+
83+ print (fmt .format ("Mean and standard deviation before normalization." ))
84+ print ("Mean: " , np .mean (original_vector ))
85+ print ("Std: " , np .std (original_vector ))
86+
87+ print (fmt .format ("Mean and standard deviation after normalization." ))
88+ print ("Mean: " , np .mean (insert_vector ))
89+ print ("Std: " , np .std (insert_vector ))
90+
91+
92+ print (fmt .format ("Start inserting entities" ))
93+
94+ entities = [
95+ [str (i ) for i in range (num_entities )],
96+ rng .random (num_entities ).tolist (),
97+ rng .random ((num_entities , dim ), np .float32 ),
98+ rng .random ((num_entities , dim ), np .float32 ),
99+ ]
100+
101+ insert_result = hello_milvus .insert (entities )
102+
103+ insert_vector = hello_milvus .query (
104+ expr = "pk == '1'" ,
105+ output_fields = ["embeddings1" ],
106+ )[0 ]['embeddings1' ]
107+
108+ print (fmt .format ("Mean and standard deviation after normalization." ))
109+ print ("Mean: " , np .mean (insert_vector ))
110+ print ("Std: " , np .std (insert_vector ))
111+
112+ utility .drop_collection ("hello_milvus" )
0 commit comments