Skip to content

Commit 1b2de64

Browse files
committed
hello_epsilla_cloud add environment variables, add try expect
1 parent 21be869 commit 1b2de64

1 file changed

Lines changed: 67 additions & 42 deletions

File tree

examples/hello_epsilla_cloud.py

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,85 @@
77
# 3. get the api key with project id, run this program
88

99
import sys
10-
10+
import os
1111
from pyepsilla import cloud
1212

13+
project_id = os.getenv("epsilla_project_id")
14+
api_key = os.getenv("epsilla_api_key")
15+
db_id = os.getenv("epsilla_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+
1321
# Connect to Epsilla Cloud
1422
client = cloud.Client(
15-
project_id="7a68814c-f839-4a67-9ec6-93c027c865e6",
16-
api_key="epsilla-cloud-api-key",
23+
project_id=project_id,
24+
api_key=api_key,
1725
)
1826

1927
# Connect to Vectordb
20-
db = client.vectordb(db_id="6accafb1-476d-43b0-aa64-12ebfbf7442d")
28+
db = client.vectordb(db_id=db_id)
2129

2230

23-
# Create a table with schema
24-
status_code, response = db.create_table(
25-
table_name="MyTable",
26-
table_fields=[
27-
{"name": "ID", "dataType": "INT", "primaryKey": True},
28-
{"name": "Doc", "dataType": "STRING"},
29-
{"name": "Embedding", "dataType": "VECTOR_FLOAT", "dimensions": 4},
30-
],
31-
)
32-
print(status_code, response)
31+
try:
32+
# Create a table with schema
33+
status_code, response = db.create_table(
34+
table_name="MyTable",
35+
table_fields=[
36+
{"name": "ID", "dataType": "INT", "primaryKey": True},
37+
{"name": "Doc", "dataType": "STRING"},
38+
{"name": "Embedding", "dataType": "VECTOR_FLOAT", "dimensions": 4},
39+
],
40+
)
41+
print(status_code, response)
42+
if status_code != 200:
43+
raise Exception("Failed to create table")
3344

45+
# Insert new vector records into table
46+
status_code, response = db.insert(
47+
table_name="MyTable",
48+
records=[
49+
{"ID": 1, "Doc": "Berlin", "Embedding": [0.05, 0.61, 0.76, 0.74]},
50+
{"ID": 2, "Doc": "London", "Embedding": [0.19, 0.81, 0.75, 0.11]},
51+
{"ID": 3, "Doc": "Moscow", "Embedding": [0.36, 0.55, 0.47, 0.94]},
52+
{"ID": 4, "Doc": "San Francisco", "Embedding": [0.18, 0.01, 0.85, 0.80]},
53+
{"ID": 5, "Doc": "Shanghai", "Embedding": [0.24, 0.18, 0.22, 0.44]},
54+
],
55+
)
56+
print(status_code, response)
57+
if status_code != 200:
58+
raise Exception("Failed to insert records")
3459

35-
# Insert new vector records into table
36-
status_code, response = db.insert(
37-
table_name="MyTable",
38-
records=[
39-
{"ID": 1, "Doc": "Berlin", "Embedding": [0.05, 0.61, 0.76, 0.74]},
40-
{"ID": 2, "Doc": "London", "Embedding": [0.19, 0.81, 0.75, 0.11]},
41-
{"ID": 3, "Doc": "Moscow", "Embedding": [0.36, 0.55, 0.47, 0.94]},
42-
{"ID": 4, "Doc": "San Francisco", "Embedding": [0.18, 0.01, 0.85, 0.80]},
43-
{"ID": 5, "Doc": "Shanghai", "Embedding": [0.24, 0.18, 0.22, 0.44]},
44-
],
45-
)
46-
print(status_code, response)
60+
# Query Vectors with specific response field, otherwise it will return all fields
61+
status_code, response = db.query(
62+
table_name="MyTable",
63+
query_field="Embedding",
64+
query_vector=[0.35, 0.55, 0.47, 0.94],
65+
response_fields=["Doc"],
66+
limit=2,
67+
)
68+
print(status_code, response)
69+
if status_code != 200:
70+
raise Exception("Failed to query table")
4771

48-
# Query Vectors with specific response field, otherwise it will return all fields
49-
status_code, response = db.query(
50-
table_name="MyTable",
51-
query_field="Embedding",
52-
query_vector=[0.35, 0.55, 0.47, 0.94],
53-
response_fields=["Doc"],
54-
limit=2,
55-
)
56-
print(status_code, response)
5772

73+
# Delete specific records from table
74+
status_code, response = db.delete(table_name="MyTable", primary_keys=[4, 5])
75+
print(status_code, response)
76+
if status_code != 200:
77+
raise Exception("Failed to delete records by primary keys")
5878

59-
# Delete specific records from table
60-
status_code, response = db.delete(table_name="MyTable", primary_keys=[4, 5])
61-
status_code, response = db.delete(table_name="MyTable", filter="Doc <> 'San Francisco'")
62-
print(status_code, response)
79+
status_code, response = db.delete(table_name="MyTable", filter="Doc <> 'San Francisco'")
80+
print(status_code, response)
81+
if status_code != 200:
82+
raise Exception("Failed to delete records by filter")
6383

64-
# Drop table
65-
status_code, response = db.drop_table(table_name="MyTable")
66-
print(status_code, response)
84+
# Drop table
85+
status_code, response = db.drop_table(table_name="MyTable")
86+
print(status_code, response)
87+
if status_code != 200:
88+
raise Exception("Failed to drop table")
89+
except Exception as e:
90+
print(f"An error occurred: {e}")
91+
sys.exit(1)

0 commit comments

Comments
 (0)