-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_operations.py
More file actions
223 lines (180 loc) · 8.1 KB
/
basic_operations.py
File metadata and controls
223 lines (180 loc) · 8.1 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
"""
Scintirete SDK 基本操作示例
演示如何使用 Scintirete Python SDK 进行基本的数据库和向量操作。
"""
import random
from typing import List
from scintirete_sdk import (
ScintireteClient,
DistanceMetric,
HnswConfig,
Vector,
ScintireteError,
)
def generate_random_vector(dimension: int) -> List[float]:
"""生成随机向量"""
return [random.random() for _ in range(dimension)]
def main():
"""主函数"""
# 连接配置
SERVER_ADDRESS = "localhost:50051"
PASSWORD = None # 如果服务器需要密码,请设置
# 测试数据配置
DB_NAME = "example_db"
COLLECTION_NAME = "example_collection"
VECTOR_DIMENSION = 128
try:
# 创建客户端连接
print("🔗 连接到 Scintirete 服务器...")
with ScintireteClient(SERVER_ADDRESS, password=PASSWORD) as client:
print("✅ 连接成功!")
# 1. 数据库操作
print("\n📊 数据库操作")
print("=" * 50)
# 列出现有数据库
databases = client.list_databases()
print(f"现有数据库: {databases}")
# 删除测试数据库(如果存在)
if DB_NAME in databases:
print(f"删除现有数据库: {DB_NAME}")
success, dropped_collections = client.drop_database(DB_NAME)
print(f"删除结果: success={success}, dropped_collections={dropped_collections}")
# 创建新数据库
print(f"创建数据库: {DB_NAME}")
success = client.create_database(DB_NAME)
print(f"创建结果: {success}")
# 验证数据库创建
databases = client.list_databases()
print(f"更新后的数据库列表: {databases}")
# 2. 集合操作
print("\n📁 集合操作")
print("=" * 50)
# 创建 HNSW 配置
hnsw_config = HnswConfig(
m=16, # 每个节点最大连接数
ef_construction=200 # 构建时搜索范围
)
# 创建集合
print(f"创建集合: {COLLECTION_NAME}")
collection_info = client.create_collection(
db_name=DB_NAME,
collection_name=COLLECTION_NAME,
metric_type=DistanceMetric.COSINE,
hnsw_config=hnsw_config
)
print(f"集合信息:")
print(f" 名称: {collection_info.name}")
print(f" 维度: {collection_info.dimension}")
print(f" 向量数: {collection_info.vector_count}")
print(f" 度量类型: {collection_info.metric_type}")
print(f" HNSW配置: m={collection_info.hnsw_config.m}, ef_construction={collection_info.hnsw_config.ef_construction}")
# 列出集合
collections = client.list_collections(DB_NAME)
print(f"数据库 {DB_NAME} 中的集合: {[c.name for c in collections]}")
# 3. 向量操作
print("\n🎯 向量操作")
print("=" * 50)
# 准备测试向量
print("准备测试向量...")
vectors = []
for i in range(10):
vector = Vector(
elements=generate_random_vector(VECTOR_DIMENSION),
metadata={
"id": i,
"category": f"category_{i % 3}",
"timestamp": f"2024-01-{i+1:02d}",
"description": f"这是第 {i+1} 个测试向量"
}
)
vectors.append(vector)
print(f"生成了 {len(vectors)} 个 {VECTOR_DIMENSION} 维向量")
# 插入向量
print("插入向量...")
inserted_ids, inserted_count = client.insert_vectors(
db_name=DB_NAME,
collection_name=COLLECTION_NAME,
vectors=vectors
)
print(f"插入结果: {inserted_count} 个向量")
print(f"分配的ID: {inserted_ids}")
# 获取集合信息(更新后)
collection_info = client.get_collection_info(DB_NAME, COLLECTION_NAME)
print(f"更新后的集合信息:")
print(f" 维度: {collection_info.dimension}")
print(f" 向量数: {collection_info.vector_count}")
print(f" 内存使用: {collection_info.memory_bytes} 字节")
# 搜索向量
print("\n🔍 搜索向量")
print("-" * 30)
# 使用第一个向量作为查询向量
query_vector = vectors[0].elements
print(f"使用查询向量搜索(前5个维度: {query_vector[:5]}...)")
search_results = client.search(
db_name=DB_NAME,
collection_name=COLLECTION_NAME,
query_vector=query_vector,
top_k=5,
include_vector=False # 不返回向量数据以提高性能
)
print(f"搜索结果 (top {len(search_results)}):")
for i, result in enumerate(search_results):
print(f" {i+1}. ID: {result.id}, 距离: {result.distance:.4f}")
if result.metadata:
print(f" 元数据: {result.metadata}")
# 搜索并返回向量数据
print("\n搜索并返回向量数据:")
search_results_with_vectors = client.search(
db_name=DB_NAME,
collection_name=COLLECTION_NAME,
query_vector=query_vector,
top_k=3,
include_vector=True
)
for i, result in enumerate(search_results_with_vectors):
print(f" {i+1}. ID: {result.id}, 距离: {result.distance:.4f}")
if result.vector:
print(f" 向量维度: {len(result.vector.elements)}")
print(f" 向量前5维: {result.vector.elements[:5]}")
# 删除部分向量
print("\n🗑️ 删除向量")
print("-" * 20)
# 删除前3个向量
ids_to_delete = inserted_ids[:3]
print(f"删除向量 ID: {ids_to_delete}")
deleted_count = client.delete_vectors(
db_name=DB_NAME,
collection_name=COLLECTION_NAME,
ids=ids_to_delete
)
print(f"删除了 {deleted_count} 个向量")
# 验证删除结果
collection_info = client.get_collection_info(DB_NAME, COLLECTION_NAME)
print(f"删除后的集合信息:")
print(f" 向量数: {collection_info.vector_count}")
print(f" 删除数: {collection_info.deleted_count}")
# 4. 持久化操作
print("\n💾 持久化操作")
print("=" * 50)
# 同步保存
print("执行同步保存...")
success, message, size, duration = client.save()
print(f"保存结果: success={success}")
print(f"消息: {message}")
print(f"快照大小: {size} 字节")
print(f"耗时: {duration:.3f} 秒")
# 后台保存
print("\n执行后台保存...")
success, message, job_id = client.bg_save()
print(f"后台保存结果: success={success}")
print(f"消息: {message}")
print(f"任务ID: {job_id}")
print("\n🎉 基本操作演示完成!")
except ScintireteError as e:
print(f"❌ Scintirete 错误: {e}")
except Exception as e:
print(f"❌ 未预期错误: {e}")
raise
if __name__ == "__main__":
main()