|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | + |
| 4 | +# Try this simple example |
| 5 | +# 1. docker run --pull=always -d -p 8888:8888 epsilla/vectordb |
| 6 | +# 2. pip3 install --upgrade pyepsilla |
| 7 | +# 3. python3 simple_example.py |
| 8 | +# |
| 9 | + |
| 10 | +from pyepsilla import vectordb |
| 11 | + |
| 12 | +# Connect to Epsilla VectorDB |
| 13 | +client = vectordb.Client(protocol="http", host="127.0.0.1", port="8888") |
| 14 | + |
| 15 | +# You can also use Epsilla Cloud |
| 16 | +# client = vectordb.Client(protocol='https', host='demo.epsilla.com', port='443') |
| 17 | + |
| 18 | +# Load DB with path |
| 19 | +## pay attention to change db_path to persistent volume for production environment |
| 20 | +status_code, response = client.load_db(db_name="MyDB", db_path="/data/epsilla_demo") |
| 21 | +print(response) |
| 22 | + |
| 23 | +# Set DB to current DB |
| 24 | +client.use_db(db_name="MyDB") |
| 25 | + |
| 26 | +# Create a table with schema in current DB |
| 27 | +status_code, response = client.create_table( |
| 28 | + table_name="MyTable", |
| 29 | + table_fields=[ |
| 30 | + {"name": "ID", "dataType": "INT", "primaryKey": True}, |
| 31 | + {"name": "Doc", "dataType": "STRING"}, |
| 32 | + ], |
| 33 | + indices=[ |
| 34 | + {"name": "Index", "field": "Doc"}, |
| 35 | + ] |
| 36 | +) |
| 37 | +print(response) |
| 38 | + |
| 39 | +# Get a list of table names in current DB |
| 40 | +status_code, response = client.list_tables() |
| 41 | +print(response) |
| 42 | + |
| 43 | +# Insert new vector records into table |
| 44 | +status_code, response = client.insert( |
| 45 | + table_name="MyTable", |
| 46 | + records=[ |
| 47 | + {"ID": 1, "Doc": "The garden was blooming with vibrant flowers, attracting butterflies and bees with their sweet nectar."}, |
| 48 | + {"ID": 2, "Doc": "In the busy city streets, people rushed to and fro, hardly noticing the beauty of the day."}, |
| 49 | + {"ID": 3, "Doc": "The library was a quiet haven, filled with the scent of old books and the soft rustling of pages."}, |
| 50 | + {"ID": 4, "Doc": "High in the mountains, the air was crisp and clear, revealing breathtaking views of the valley below."}, |
| 51 | + {"ID": 5, "Doc": "At the beach, children played joyfully in the sand, building castles and chasing the waves."}, |
| 52 | + {"ID": 6, "Doc": "Deep in the forest, a deer cautiously stepped out, its ears alert for any signs of danger."}, |
| 53 | + {"ID": 7, "Doc": "The old town's historical architecture spoke volumes about its rich cultural past."}, |
| 54 | + {"ID": 8, "Doc": "Night fell, and the sky was a canvas of stars, shining brightly in the moon's soft glow."}, |
| 55 | + {"ID": 9, "Doc": "A cozy cafe on the corner served the best hot chocolate, warming the hands and hearts of its visitors."}, |
| 56 | + {"ID": 10, "Doc": "The artist's studio was cluttered but inspiring, filled with unfinished canvases and vibrant paints."}, |
| 57 | + ], |
| 58 | +) |
| 59 | +print(response) |
| 60 | + |
| 61 | +# Query Vectors with specific response field |
| 62 | +status_code, response = client.query( |
| 63 | + table_name="MyTable", |
| 64 | + query_text="Where can I find a serene environment, ideal for relaxation and introspection?", |
| 65 | + response_fields=["Doc"], |
| 66 | + with_distance=True, |
| 67 | + limit=3 |
| 68 | +) |
| 69 | +print(response) |
| 70 | + |
| 71 | +# Query Vectors without specific response field, then it will return all fields |
| 72 | +status_code, response = client.query( |
| 73 | + table_name="MyTable", |
| 74 | + query_text="Where can I find a serene environment, ideal for relaxation and introspection?", |
| 75 | +) |
| 76 | +print(response) |
| 77 | + |
| 78 | +# Get Vectors |
| 79 | +status_code, response = client.get(table_name="MyTable", limit=2) |
| 80 | +print(response) |
| 81 | + |
| 82 | +# status_code, response = client.delete(table_name="MyTable", ids=[3]) |
| 83 | +status_code, response = client.delete(table_name="MyTable", primary_keys=[1, 3, 4]) |
| 84 | +# status_code, response = client.delete(table_name="MyTable", filter="Doc <> 'San Francisco'") |
| 85 | +print(response) |
| 86 | + |
| 87 | + |
| 88 | +# Drop table |
| 89 | +status_code, response = client.drop_table("MyTable") |
| 90 | +print(response) |
| 91 | + |
| 92 | +# Unload db |
| 93 | +status_code, response = client.unload_db("MyDB") |
| 94 | +print(response) |
0 commit comments