** Before you start, please make sure Docker is installed and running on your system.
docker pull qdrant/qdrant
docker run -p 6333:6333 -p 6334:6334 \
-v "$(pwd)/qdrant_storage:/qdrant/storage:z" \
qdrant/qdrant
pip install qdrant-client
# Before running this script, start the Qdrant server using Docker:
# docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams
from qdrant_client.models import PointStruct
from qdrant_client.http.exceptions import UnexpectedResponse
from qdrant_client.models import Filter, FieldCondition, MatchValue
# Initialize the client
client = QdrantClient(url="http://localhost:6333")
# Create a collection
try:
client.create_collection(
collection_name="test_collection",
vectors_config=VectorParams(size=4, distance=Distance.DOT),
)
print("Collection created successfully")
except UnexpectedResponse as e:
if "already exists" in str(e):
print("Collection already exists, proceeding with upsert")
else:
raise e
# Add vectors
operation_info = client.upsert(
collection_name="test_collection",
wait=True,
points=[
PointStruct(id=1, vector=[0.05, 0.61, 0.76, 0.74], payload={"city": "Berlin"}),
PointStruct(id=2, vector=[0.19, 0.81, 0.75, 0.11], payload={"city": "London"}),
PointStruct(id=3, vector=[0.36, 0.55, 0.47, 0.94], payload={"city": "Moscow"}),
PointStruct(id=4, vector=[0.18, 0.01, 0.85, 0.80], payload={"city": "New York"}),
PointStruct(id=5, vector=[0.24, 0.18, 0.22, 0.44], payload={"city": "Beijing"}),
PointStruct(id=6, vector=[0.35, 0.08, 0.11, 0.44], payload={"city": "Mumbai"}),
],
)
print("Upsert operation result:", operation_info)
# Run a query
search_result = client.query_points(
collection_name="test_collection",
query=[0.2, 0.1, 0.9, 0.7],
with_payload=False,
limit=3
).points
print(search_result)
# Add a filter
search_result = client.query_points(
collection_name="test_collection",
query=[0.2, 0.1, 0.9, 0.7],
query_filter=Filter(
must=[FieldCondition(key="city", match=MatchValue(value="London"))]
),
with_payload=True,
limit=3,
).points
print(search_result)
from qdrant_client import QdrantClient
from sentence_transformers import SentenceTransformer
# qdrant 연결
client = QdrantClient(host="localhost", port=6333)
# 임베딩 모델 로딩
model = SentenceTransformer("all-MiniLM-L6-v2")
# 예제 문장 리스트
documents = [
{"id":1, "text":"Hello, how are you?", "category":"greeting"},
{"id":2, "text":"What is the weather like in Tokyo?", "category":"weather"},
{"id":3, "text":"I'm looking for a good restaurant in Paris.", "category":"food"},
{"id":4, "text":"How do I get to the Eiffel Tower?", "category":"transport"},
{"id":5, "text":"What is the best way to learn Python?", "category":"education"}
]
# 벡터화 및 저장
vectors = [model.encode(doc["text"]).tolist() for doc in documents]
# 컬렉션 존재 여부 확인 후 생성
if not client.collection_exists("demo_news"):
client.create_collection(
collection_name="demo_news",
vectors_config={"size": len(vectors[0]), "distance": "Cosine"},
)
client.upsert(
collection_name="demo_news",
points = [
{
"id": doc["id"],
"vector": vectors[i],
"payload": {"category": doc["category"], "text": doc["text"]}
}
for i, doc in enumerate(documents)
]
)
# 검색 쿼리
query = "What is the best way to learn Python?"
query_vector = model.encode(query).tolist()
# 검색 결과 출력
result = client.query_points(
collection_name="demo_news",
query=query_vector,
with_payload=True,
limit=3
)
print("\n검색 결과:")
for point in result.points:
print(f"ID: {point.id}")
print(f"Text: {point.payload['text']}")
print(f"Category: {point.payload['category']}")
print()
참고자료-Python Qdrant Client: https://github.com/qdrant/qdrant-client
참고자료: https://qdrant.tech/documentation/quickstart/
참고자료-Database Tutorials: https://qdrant.tech/documentation/database-tutorials/
참고자료-Examples: https://qdrant.tech/documentation/examples/