44# Try this simple example for epsilla cloud
55# 1. create project and db on epsilla cloud
66# 2. create a table with schema in db
7- # 3. get the api key with project id, run this program
7+ # 3. get the epsilla cloud api key with project id, run this program
88
9+ import sys
10+ import os
911from pyepsilla import cloud
1012
13+ project_id = os .getenv ("EPSILLA_PROJECT_ID" , "Your-Project-ID" )
14+ epsilla_api_key = os .getenv ("EPSILLA_API_KEY" , "Your-Epsilla-API-Key" )
15+ db_id = os .getenv ("EPSILLA_DB_ID" , "Your-DB-ID" )
16+
17+ if not project_id or not api_key or not db_id :
18+ print ("Please set the environment variables: EPSILLA_PROJECT_ID, EPSILLA_API_KEY, EPSILLA_DB_ID" )
19+ sys .exit (1 )
20+
1121# Connect to Epsilla Cloud
12- client = cloud .Client (
13- project_id = "7a68814c-f839-4a67-9ec6-93c027c865e0" ,
14- api_key = "epsilla-cloud-api-key" ,
22+ cloud_client = cloud .Client (
23+ project_id = project_id ,
24+ api_key = epsilla_api_key ,
1525)
1626
1727# Connect to Vectordb
18- db = client .vectordb (db_id = "6accafb1-476d-43b0-aa64-12ebfbf7441d" )
28+ db_cloud = cloud_client .vectordb (db_id = db_id )
1929
2030
21- # Create a table with schema
22- status_code , response = db .create_table (
23- table_name = "MyTable" ,
24- table_fields = [
25- {"name" : "ID" , "dataType" : "INT" , "primaryKey" : True },
26- {"name" : "Doc" , "dataType" : "STRING" },
27- {"name" : "Embedding" , "dataType" : "VECTOR_FLOAT" , "dimensions" : 4 },
28- ],
29- )
30- print (status_code , response )
3131
32+ try :
33+ # Create a table with schema
34+ status_code , response = db .create_table (
35+ table_name = "MyTable" ,
36+ table_fields = [
37+ {"name" : "ID" , "dataType" : "INT" , "primaryKey" : True },
38+ {"name" : "Doc" , "dataType" : "STRING" },
39+ {"name" : "Embedding" , "dataType" : "VECTOR_FLOAT" , "dimensions" : 4 },
40+ ],
41+ )
42+ print (status_code , response )
43+ if status_code != 200 :
44+ raise Exception ("Failed to create table" )
3245
33- # Insert new vector records into table
34- status_code , response = db .insert (
35- table_name = "MyTable" ,
36- records = [
37- {"ID" : 1 , "Doc" : "Berlin" , "Embedding" : [0.05 , 0.61 , 0.76 , 0.74 ]},
38- {"ID" : 2 , "Doc" : "London" , "Embedding" : [0.19 , 0.81 , 0.75 , 0.11 ]},
39- {"ID" : 3 , "Doc" : "Moscow" , "Embedding" : [0.36 , 0.55 , 0.47 , 0.94 ]},
40- {"ID" : 4 , "Doc" : "San Francisco" , "Embedding" : [0.18 , 0.01 , 0.85 , 0.80 ]},
41- {"ID" : 5 , "Doc" : "Shanghai" , "Embedding" : [0.24 , 0.18 , 0.22 , 0.44 ]},
42- ],
43- )
44- print (status_code , response )
46+ # Insert new vector records into table
47+ status_code , response = db .insert (
48+ table_name = "MyTable" ,
49+ records = [
50+ {"ID" : 1 , "Doc" : "Berlin" , "Embedding" : [0.05 , 0.61 , 0.76 , 0.74 ]},
51+ {"ID" : 2 , "Doc" : "London" , "Embedding" : [0.19 , 0.81 , 0.75 , 0.11 ]},
52+ {"ID" : 3 , "Doc" : "Moscow" , "Embedding" : [0.36 , 0.55 , 0.47 , 0.94 ]},
53+ {"ID" : 4 , "Doc" : "San Francisco" , "Embedding" : [0.18 , 0.01 , 0.85 , 0.80 ]},
54+ {"ID" : 5 , "Doc" : "Shanghai" , "Embedding" : [0.24 , 0.18 , 0.22 , 0.44 ]},
55+ ],
56+ )
57+ print (status_code , response )
58+ if status_code != 200 :
59+ raise Exception ("Failed to insert records" )
60+
61+ # Query Vectors with specific response field, otherwise it will return all fields
62+ status_code , response = db .query (
63+ table_name = "MyTable" ,
64+ query_field = "Embedding" ,
65+ query_vector = [0.35 , 0.55 , 0.47 , 0.94 ],
66+ response_fields = ["Doc" ],
67+ limit = 2 ,
68+ )
69+ print (status_code , response )
70+ if status_code != 200 :
71+ raise Exception ("Failed to query table" )
4572
46- # Query Vectors with specific response field, otherwise it will return all fields
47- status_code , response = db .query (
48- table_name = "MyTable" ,
49- query_field = "Embedding" ,
50- query_vector = [0.35 , 0.55 , 0.47 , 0.94 ],
51- response_fields = ["Doc" ],
52- limit = 2 ,
53- )
54- print (status_code , response )
5573
74+ # Delete specific records from table
75+ status_code , response = db .delete (table_name = "MyTable" , primary_keys = [4 , 5 ])
76+ print (status_code , response )
77+ if status_code != 200 :
78+ raise Exception ("Failed to delete records by primary keys" )
5679
57- # Delete specific records from table
58- status_code , response = db . delete ( table_name = "MyTable" , primary_keys = [ 4 , 5 ] )
59- status_code , response = db . delete ( table_name = "MyTable" , filter = "Doc <> 'San Francisco'" )
60- print ( status_code , response )
80+ status_code , response = db . delete ( table_name = "MyTable" , filter = "Doc <> 'San Francisco'" )
81+ print ( status_code , response )
82+ if status_code != 200 :
83+ raise Exception ( "Failed to delete records by filter" )
6184
62- # Drop table
63- status_code , response = db .drop_table (table_name = "MyTable" )
64- print (status_code , response )
85+ # Drop table
86+ status_code , response = db .drop_table (table_name = "MyTable" )
87+ print (status_code , response )
88+ if status_code != 200 :
89+ raise Exception ("Failed to drop table" )
90+ except Exception as e :
91+ print (f"An error occurred: { e } " )
92+ sys .exit (1 )
0 commit comments