Technology-机器学习-向量数据库 Zvec

阿里开源的轻量级向量数据库 Zvec.

安装

1
pip install zvec

案例

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
import zvec

# Define collection schema
schema = zvec.CollectionSchema(
name="example",
vectors=zvec.VectorSchema("embedding", zvec.DataType.VECTOR_FP32, 4),
)

# Create collection
collection = zvec.create_and_open(path="./zvec_example", schema=schema)

# Insert documents
collection.insert([
zvec.Doc(id="doc_1", vectors={"embedding": [0.1, 0.2, 0.3, 0.4]}),
zvec.Doc(id="doc_2", vectors={"embedding": [0.2, 0.3, 0.4, 0.1]}),
])

# Search by vector similarity
results = collection.query(
zvec.Query(field_name="embedding", vector=[0.4, 0.3, 0.3, 0.1]),
topk=10
)

# Results: list of {'id': str, 'score': float, ...}, sorted by relevance
print(results)

测试

用了 3 千多条向量数据,每条数据长度为 1028 位的 INT8 长度,性能测试结果为:10 - 30ms 之间,速度还行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Search time: 0.01824737548828125 seconds


[{
"id": "4488e47a4f2011ea8b746b720afafec6",
"score": 0.9999998807907104,
"fields": {},
"vectors": {}
}, {
"id": "f6b642064c0411eaac9ef1ec564ea6d8",
"score": 0.4397549033164978,
"fields": {},
"vectors": {}
}, {
"id": "aeb2daa04f1f11ea8b746b720afafec6",
"score": 0.36075806617736816,
"fields": {},
"vectors": {}
}]