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
| from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct
# 連接
client = QdrantClient("localhost", port=6333)
# 建立 Collection
client.create_collection(
collection_name="security_docs",
vectors_config=VectorParams(size=384, distance=Distance.COSINE)
)
# 新增向量
client.upsert(
collection_name="security_docs",
points=[
PointStruct(
id=1,
vector=[0.1, 0.2, ...], # 384 維向量
payload={"title": "SQL Injection", "severity": "high"}
),
PointStruct(
id=2,
vector=[0.3, 0.4, ...],
payload={"title": "XSS Attack", "severity": "medium"}
)
]
)
# 搜尋
results = client.search(
collection_name="security_docs",
query_vector=[0.15, 0.25, ...],
limit=5
)
|