-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistent_database.py
More file actions
50 lines (36 loc) · 1.3 KB
/
persistent_database.py
File metadata and controls
50 lines (36 loc) · 1.3 KB
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
"""Persistent database example for sqlite-vec-client.
Demonstrates:
- Creating a persistent .db file
- Loading from existing .db file
- Data persistence across sessions
"""
from sqlite_vec_client import SQLiteVecClient
def create_database():
"""Create and populate a persistent database."""
print("Creating database...")
client = SQLiteVecClient(table="documents", db_path="./my_vectors.db")
client.create_table(dim=128, distance="cosine")
texts = [
"First document",
"Second document",
"Third document",
]
embeddings = [[0.1] * 128, [0.2] * 128, [0.3] * 128]
rowids = client.add(texts=texts, embeddings=embeddings)
print(f"Added {len(rowids)} documents")
print(f"Total records: {client.count()}")
client.close()
def load_database():
"""Load and query existing database."""
print("\nLoading existing database...")
client = SQLiteVecClient(table="documents", db_path="./my_vectors.db")
print(f"Total records: {client.count()}")
# Search
results = client.similarity_search(embedding=[0.15] * 128, top_k=2)
print("\nTop 2 results:")
for rowid, text, distance in results:
print(f" [{rowid}] {text} (distance: {distance:.4f})")
client.close()
if __name__ == "__main__":
create_database()
load_database()