Appearance
Python:连接与集合
创建客户端
本地 HTTP(最常用)
python
from qdrant_client import QdrantClient
client = QdrantClient(url="http://localhost:6333")API Key(云或自建鉴权)
python
client = QdrantClient(
url="https://your-cluster.example.com:6333",
api_key="your-api-key",
)内存模式(单元测试)
python
client = QdrantClient(":memory:")适合不写磁盘的快速测试,进程结束数据即失。
创建集合
python
from qdrant_client.models import Distance, VectorParams
COL = "kb_zh"
DIM = 384 # 与 sentence-transformers 等模型输出一致
if client.collection_exists(COL):
client.delete_collection(COL)
client.create_collection(
collection_name=COL,
vectors_config=VectorParams(size=DIM, distance=Distance.COSINE),
)多向量(了解)
若一个点有多个向量(如「标题向量」「正文向量」),可用:
python
from qdrant_client.models import VectorParams, Distance
client.create_collection(
collection_name="multi_vec",
vectors_config={
"title": VectorParams(size=256, distance=Distance.COSINE),
"body": VectorParams(size=768, distance=Distance.COSINE),
},
)检索时需指定向量名;新手先掌握单向量即可。
获取集合信息
python
info = client.get_collection(COL)
print(info.points_count, info.config.params.vectors)下一节:增删改查。