-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
68 lines (56 loc) · 2.24 KB
/
example.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import random
from tiny_vectordb import VectorDatabase
import os
random.seed(0)
# typing
from tiny_vectordb import CollectionConfig, CompileConfig
## This will print all compilation and linking commands
# VectorDatabase.VERBOSE = True
## You may need to change these paths to your own python installation, if error occurs,
## or you may add some additional compile flags for your own environment and optimization
## or you can just set compile_config to None, or not pass it to VectorDatabase
# compile_config = {
# "cxx": "clang++",
# "additional_compile_flags": ["-IC:\\Users\\vuser\\AppData\\Local\\Programs\\Python\\Python311\\include"],
# "additional_link_flags": ["-LC:\\Users\\vuser\\AppData\\Local\\Programs\\Python\\Python311\\libs"]
# }
compile_config: CompileConfig = None # type: ignore
collection_configs: list[CollectionConfig] = [
{
"name": "hello",
"dimension": 256,
},
{
"name": "world",
"dimension": 1000,
}
]
if os.path.exists("test.db"):
if input("test.db exists, delete it? (y/n)") == "y":
os.remove("test.db")
else:
exit("This script will raise error if test.db exists, for duplicate item addition")
# create database will initialize all collections,
# for each dimension, a shared library will be compiled and loaded.
print("Compiling...")
database = VectorDatabase("test.db", collection_configs, compile_config=compile_config)
collection = database["hello"]
# 100 vectors of 256 dimension
print("Creating vectors...")
N_VECTORS = 20
vectors = [[random.random() for _ in range(256)] for _ in range(N_VECTORS)]
vector_ids = [f"vector_{i}" for i in range(N_VECTORS)]
# io operations, may encounter duplicate ids if you run this script multiple times
print("Adding vectors...")
collection.addBlock(vector_ids, vectors)
collection.deleteBlock(vector_ids[:10:2])
# set block, will have no error if id not exists
collection.setBlock(
vector_ids[10:20] + [f"addtional{i}" for i in range(3)],
[[0. for _ in range(256)] for _ in range(13)]
)
print("Current keys:", collection.keys())
search_ids, search_scores = collection.search([random.random() for _ in range(256)], k=10)
print("Top 10 search results:", search_ids, search_scores)
print("Commiting ...")
database.commit()