{/* Trang này được tạo tự động từ SKILL.md của kỹ năng bởi website/scripts/generate-skill-docs.py. Chỉnh sửa nguồn SKILL.md, không phải trang này. */}
Tìm kiếm vectơ Qdrant
Công cụ tìm kiếm tương tự vectơ hiệu suất cao cho RAG và tìm kiếm ngữ nghĩa. Sử dụng khi xây dựng hệ thống RAG sản xuất yêu cầu tìm kiếm lân cận gần nhất nhanh chóng, tìm kiếm kết hợp với tính năng lọc hoặc lưu trữ vectơ có thể mở rộng với hiệu suất được hỗ trợ bởi Rust.
Siêu dữ liệu kỹ năng
| Nguồn | Tùy chọn — cài đặt với |
| `Hermes skills install official/mlops/Qdrant | |
| ` | |
| Đường dẫn |
optional-skills/mlops/Qdrant ` | | Phiên bản |
1.0.0 ` | | Tác giả | Nghiên cứu dàn nhạc | | Giấy phép | MIT | | Phụ thuộc |
Qdrant-CLIent>=1.12.0 ` | | Nền tảng | Linux, macOS, Windows | | Thẻ |
RAG
, `Vector Search
, `Qdrant
, `Semantic Search
, `Embeddings
, `Similarity Search
, `HNSW
, `Production
,
Distributed |
Tham khảo: đầy đủ SKILL.md
Sau đây là định nghĩa kỹ năng đầy đủ mà Hermes tải khi kỹ năng này được kích hoạt. Đây là những gì tác nhân coi là hướng dẫn khi kỹ năng được kích hoạt.
Qdrant - Công cụ tìm kiếm tương tự vectơ
Cơ sở dữ liệu vectơ hiệu suất cao được viết bằng Rust để sản xuất RAG và tìm kiếm ngữ nghĩa.
Khi nào nên sử dụng Qdrant`Sử dụng Qdrant khi:
- Xây dựng hệ thống RAG sản xuất yêu cầu độ trễ thấp
- Cần tìm kiếm kết hợp (vectơ + lọc siêu dữ liệu)
- Yêu cầu chia tỷ lệ theo chiều ngang với sharding/sao chép
- Muốn triển khai tại chỗ với toàn quyền kiểm soát dữ liệu
- Cần lưu trữ nhiều vectơ cho mỗi bản ghi (dày đặc + thưa thớt)
- Xây dựng hệ thống khuyến nghị thời gian thực`Các tính năng chính:
- Chạy bằng nguồn điện: An toàn bộ nhớ, hiệu năng cao
- Lọc phong phú: Lọc theo bất kỳ trường tải trọng nào trong quá trình tìm kiếm
- Nhiều vectơ: Dày đặc, thưa thớt, đa mật độ trên mỗi điểm
- Lượng tử hóa: Vô hướng, tích, nhị phân để tăng hiệu quả bộ nhớ
- Phân phối: Raft đồng thuận, sharding, nhân rộng
- REST + gRPC: Cả hai API đều có đầy đủ tính năng tương đương`Sử dụng các lựa chọn thay thế thay thế:
- Chroma: Thiết lập đơn giản hơn, các trường hợp sử dụng được nhúng
- FAISS: Tốc độ thô tối đa, nghiên cứu/xử lý hàng loạt
- Pinecone: Được quản lý hoàn toàn, ưu tiên không cần thao tác
- Weaviate: Tùy chọn GraphQL, bộ vector hóa tích hợp
Bắt đầu nhanh
Cài đặt
# Python CLIent
pip install Qdrant-CLIent
# Docker (recommended for development)
Docker run -p 6333:6333 -p 6334:6334 Qdrant/Qdrant
# Docker with persistent storage
Docker run -p 6333:6333 -p 6334:6334 \
-v $(pwd)/Qdrant_storage:/Qdrant/storage \
Qdrant/Qdrant
`
``###Cách sử dụng cơ bản
``` python
from Qdrant_CLIent import QdrantCLIent
from Qdrant_CLIent.models import Distance, VectorParams, PointStruct
# Connect to Qdrant
CLIent = QdrantCLIent(host="localhost", port=6333)
# Create collection
CLIent.create_collection(
collection_name="documents",
vectors_config=VectorParams(size=384, distance=Distance.COSINE)
)
# Insert vectors with payload
CLIent.upsert(
collection_name="documents",
points=[
PointStruct(
id=1,
vector=[0.1, 0.2, ...], # 384-dim vector
payload=\{"title": "Doc 1", "category": "tech"}
),
PointStruct(
id=2,
vector=[0.3, 0.4, ...],
payload=\{"title": "Doc 2", "category": "science"}
)
]
)
# Search with filtering
results = CLIent.search(
collection_name="documents",
query_vector=[0.15, 0.25, ...],
query_filter={
"must": [\{"key": "category", "match": \{"value": "tech"}}]
},
limit=10
)
for point in results:
print(f"ID: \{point.id}, Score: \{point.score}, Payload: \{point.payload}")
`
## Khái niệm cốt lõi
### Điểm - Đơn vị dữ liệu cơ bản
`Python
from Qdrant_CLIent.models import PointStruct
# Point = ID + Vector(s) + Payload
point = PointStruct(
id=123, # Integer or UUID string
vector=[0.1, 0.2, 0.3, ...], # Dense vector
payload={ # Arbitrary JSON metadata
"title": "Document title",
"category": "tech",
"timestamp": 1699900000,
"tags": ["Python", "ml"]
}
)
# Batch upsert (recommended)
CLIent.upsert(
collection_name="documents",
points=[point1, point2, point3],
wait=True # Wait for indexing
)
`
### Bộ sưu tập - Vùng chứa vectơ
`Python
from Qdrant_CLIent.models import VectorParams, Distance, HnswConfigDiff
# Create with HNSW configuration
CLIent.create_collection(
collection_name="documents",
vectors_config=VectorParams(
size=384, # Vector dimensions
distance=Distance.COSINE # COSINE, EUCLID, DOT, MANHATTAN
),
hnsw_config=HnswConfigDiff(
m=16, # Connections per node (default 16)
ef_construct=100, # Build-time accuracy (default 100)
full_scan_threshold=10000 # Switch to brute force below this
),
on_disk_payload=True # Store payload on disk
)
# Collection info
info = CLIent.get_collection("documents")
print(f"Points: \{info.points_count}, Vectors: \{info.vectors_count}")
`
### Số liệu khoảng cách
| Số liệu | Trường hợp sử dụng | Phạm vi |
|--------|----------|-------|
|
`COSINE
` | Nhúng văn bản, vectơ chuẩn hóa | 0 đến 2 |
|
`EUCLID
` | Dữ liệu không gian, đặc điểm hình ảnh | 0 đến ∞ |
|
`DOT
` | Khuyến nghị, không chuẩn hóa | -∞ đến ∞ |
|
`MANHATTAN
` | Tính năng thưa thớt, dữ liệu rời rạc | 0 đến ∞ |
## Hoạt động tìm kiếm
### Tìm kiếm cơ bản
`Python
# Simple nearest neighbor search
results = CLIent.search(
collection_name="documents",
query_vector=[0.1, 0.2, ...],
limit=10,
with_payload=True,
with_vectors=False # Don't return vectors (faster)
)
`
### Tìm kiếm có lọc
``` python
from Qdrant_CLIent.models import Filter, FieldCondition, MatchValue, Range
# Complex filtering
results = CLIent.search(
collection_name="documents",
query_vector=query_embedding,
query_filter=Filter(
must=[
FieldCondition(key="category", match=MatchValue(value="tech")),
FieldCondition(key="timestamp", range=Range(gte=1699000000))
],
must_not=[
FieldCondition(key="status", match=MatchValue(value="archived"))
]
),
limit=10
)
# Shorthand filter syntax
results = CLIent.search(
collection_name="documents",
query_vector=query_embedding,
query_filter={
"must": [
\{"key": "category", "match": \{"value": "tech"}},
\{"key": "price", "range": \{"gte": 10, "lte": 100}}
]
},
limit=10
)
`
### Tìm kiếm hàng loạt
`Python
from Qdrant_CLIent.models import SearchRequest
# Multiple queries in one request
results = CLIent.search_batch(
collection_name="documents",
requests=[
SearchRequest(vector=[0.1, ...], limit=5),
SearchRequest(vector=[0.2, ...], limit=5, filter=\{"must": [...]}),
SearchRequest(vector=[0.3, ...], limit=10)
]
)
`
## Tích hợp RAG
### Với bộ chuyển đổi câu
`Python
from sentence_transformers import SentenceTransformer
from Qdrant_CLIent import QdrantCLIent
from Qdrant_CLIent.models import VectorParams, Distance, PointStruct
# Initialize
encoder = SentenceTransformer("all-MiniLM-L6-v2")
CLIent = QdrantCLIent(host="localhost", port=6333)
# Create collection
CLIent.create_collection(
collection_name="knowledge_base",
vectors_config=VectorParams(size=384, distance=Distance.COSINE)
)
# Index documents
documents = [
\{"id": 1, "text": "Python is a programming language", "source": "wiki"},
\{"id": 2, "text": "Machine learning uses algorithms", "source": "textbook"},
]
points = [
PointStruct(
id=doc["id"],
vector=encoder.encode(doc["text"]).tolist(),
payload=\{"text": doc["text"], "source": doc["source"]}
)
for doc in documents
]
CLIent.upsert(collection_name="knowledge_base", points=points)
# RAG retrieval
def retrieve(query: str, top_k: int = 5) -> list[dict]:
query_vector = encoder.encode(query).tolist()
results = CLIent.search(
collection_name="knowledge_base",
query_vector=query_vector,
limit=top_k
)
return [\{"text": r.payload["text"], "score": r.score} for r in results]
# Use in RAG pipeline
context = retrieve("What is Python?")
prompt = f"Context: \{context}\n\nQuestion: What is Python?"
`
### Với LangChain
`Python
from langchain_community.vectorstores import Qdrant
from langchain_community.embeddings import HuggingFaceEmbeddings`embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = Qdrant.from_documents(documents, embeddings, url="http://localhost:6333", collection_name="docs")
retriever = vectorstore.as_retriever(search_kwargs=\{"k": 5})
`
### Với LlamaIndex
`Python
from Llama_index.vector_stores.Qdrant import QdrantVectorStore
from Llama_index.core import VectorStoreIndex, StorageContext`vector_store = QdrantVectorStore(CLIent=CLIent, collection_name="Llama_docs")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
query_engine = index.as_query_engine()
`
## Hỗ trợ đa vector
### Các vectơ được đặt tên (các mô hình nhúng khác nhau)
`Python
from Qdrant_CLIent.models import VectorParams, Distance
# Collection with multiple vector types
CLIent.create_collection(
collection_name="hybrid_search",
vectors_config={
"dense": VectorParams(size=384, distance=Distance.COSINE),
"sparse": VectorParams(size=30000, distance=Distance.DOT)
}
)
# Insert with named vectors
CLIent.upsert(
collection_name="hybrid_search",
points=[
PointStruct(
id=1,
vector={
"dense": dense_embedding,
"sparse": sparse_embedding
},
payload=\{"text": "document text"}
)
]
)
# Search specific vector
results = CLIent.search(
collection_name="hybrid_search",
query_vector=("dense", query_dense), # Specify which vector
limit=10
)
`
### Vectơ thưa thớt (BM25, SPLADE)
`Python
from Qdrant_CLIent.models import SparseVectorParams, SparseIndexParams, SparseVector
# Collection with sparse vectors
CLIent.create_collection(
collection_name="sparse_search",
vectors_config=\{},
sparse_vectors_config=\{"text": SparseVectorParams(index=SparseIndexParams(on_disk=False))}
)
# Insert sparse vector
CLIent.upsert(
collection_name="sparse_search",
points=[PointStruct(id=1, vector=\{"text": SparseVector(indices=[1, 5, 100], values=[0.5, 0.8, 0.2])}, payload=\{"text": "document"})]
)
`
## Lượng tử hóa (tối ưu hóa bộ nhớ)
`Python
from Qdrant_CLIent.models import ScalarQuantization, ScalarQuantizationConfig, ScalarType
# Scalar quantization (4x memory reduction)
CLIent.create_collection(
collection_name="quantiZed",
vectors_config=VectorParams(size=384, distance=Distance.COSINE),
quantization_config=ScalarQuantization(
scalar=ScalarQuantizationConfig(
type=ScalarType.INT8,
quantile=0.99, # CLIp outliers
always_ram=True # Keep quantiZed in RAM
)
)
)
# Search with rescoring
results = CLIent.search(
collection_name="quantiZed",
query_vector=query,
search_params=\{"quantization": \{"rescore": True}}, # Rescore top results
limit=10
)
`
## Lập chỉ mục tải trọng
`Python
from Qdrant_CLIent.models import PayloadSchemaType
# Create payload index for faster filtering
CLIent.create_payload_index(
collection_name="documents",
field_name="category",
field_schema=PayloadSchemaType.KEYWORD
)
CLIent.create_payload_index(
collection_name="documents",
field_name="timestamp",
field_schema=PayloadSchemaType.INTEGER
)
# Index types: KEYWORD, INTEGER, FLOAT, GEO, TEXT (full-text), BOOL
`
## Triển khai sản xuất
### Đám mây Qdrant
`Python
from Qdrant_CLIent import QdrantCLIent
# Connect to Qdrant Cloud
CLIent = QdrantCLIent(
url="https://your-cluster.cloud.Qdrant.io",
API_key="your-API-key"
)
`
### Điều chỉnh hiệu suất
`Python
# Optimize for search speed (higher recall)
CLIent.update_collection(
collection_name="documents",
hnsw_config=HnswConfigDiff(ef_construct=200, m=32)
)
# Optimize for indexing speed (bulk loads)
CLIent.update_collection(
collection_name="documents",
optimizer_config=\{"indexing_threshold": 20000}
)
`
## Các phương pháp hay nhất
1. **Thao tác hàng loạt** - Sử dụng tính năng cập nhật hàng loạt/tìm kiếm hiệu quả
2. **Lập chỉ mục tải trọng** - Các trường chỉ mục được sử dụng trong bộ lọc
3. **Lượng tử hóa** - Kích hoạt cho các bộ sưu tập lớn (>1M vectơ)
4. **Sharding** - Sử dụng cho các bộ sưu tập >10M vectơ
5. **Bộ lưu trữ trên đĩa** - Kích hoạt
`on_disk_payload
` cho tải trọng lớn
6. **Kết nối tổng hợp** - Tái sử dụng các phiên bản máy khách
## Các vấn đề thường gặp`**Tìm kiếm chậm với các bộ lọc:**
`
``` python
# Create payload index for filtered fields
CLIent.create_payload_index(
collection_name="docs",
field_name="category",
field_schema=PayloadSchemaType.KEYWORD
)
`
``**Hết bộ nhớ:**
`
``` python
# Enable quantization and on-disk storage
CLIent.create_collection(
collection_name="large_collection",
vectors_config=VectorParams(size=384, distance=Distance.COSINE),
quantization_config=ScalarQuantization(...),
on_disk_payload=True
)
`
``**Vấn đề kết nối:**
`
``` python
# Use timeout and retry
CLIent = QdrantCLIent(
host="localhost",
port=6333,
timeout=30,
prefer_grpc=True # gRPC for better performance
)
`
## Tài liệu tham khảo
- **[Advanced Usage](https://GitHub.com/NousResearch/Hermes-agent/blob/main/optional-skills/mlops/Qdrant/references/advanced-usage.md)** - Chế độ phân phối, tìm kiếm kết hợp, đề xuất
- **[Troubleshooting](https://GitHub.com/NousResearch/Hermes-agent/blob/main/optional-skills/mlops/Qdrant/references/troubleshooting.md)** - Các sự cố thường gặp, gỡ lỗi, điều chỉnh hiệu suất
## Tài nguyên
- **GitHub**: https://GitHub.com/Qdrant/Qdrant (22k+ sao)
- **Tài liệu**: https://Qdrant.tech/documentation/
- **Máy khách Python**: https://GitHub.com/Qdrant/Qdrant-CLIent
- **Đám mây**: https://cloud.Qdrant.io
- **Phiên bản**: 1.12.0+
- **Giấy phép**: Apache 2.0