2025-09-05 09:22:15 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
"""
|
|
|
|
|
|
Name: app/core/qdrant.py
|
2025-09-05 09:27:28 +02:00
|
|
|
|
Version: v1.3.1 (2025-09-05)
|
2025-09-05 09:22:15 +02:00
|
|
|
|
|
|
|
|
|
|
Kurzbeschreibung:
|
|
|
|
|
|
Qdrant-Client & Collection-Setup für mindnet.
|
2025-09-05 09:27:28 +02:00
|
|
|
|
- Stellt sicher, dass {prefix}_notes / {prefix}_chunks / {prefix}_edges existieren.
|
2025-09-05 09:22:15 +02:00
|
|
|
|
- Edges-Collection nutzt 1D Dummy-Vektor (Workaround für Python-Client).
|
2025-09-05 09:27:28 +02:00
|
|
|
|
- **Nicht-destruktiv per Default**: ensure_collections(..., destructive=False).
|
|
|
|
|
|
- **Abwärtskompatibel**: collection_names(prefix) wieder verfügbar.
|
2025-09-05 09:22:15 +02:00
|
|
|
|
|
2025-09-05 09:27:28 +02:00
|
|
|
|
Aufruf/Verwendung:
|
|
|
|
|
|
from app.core.qdrant import QdrantConfig, get_client, ensure_collections, collection_names
|
|
|
|
|
|
|
|
|
|
|
|
Umgebungsvariablen (optional):
|
|
|
|
|
|
QDRANT_URL | QDRANT_HOST/QDRANT_PORT, QDRANT_API_KEY,
|
|
|
|
|
|
COLLECTION_PREFIX (Default "mindnet"), VECTOR_DIM (Default 384)
|
2025-09-05 09:22:15 +02:00
|
|
|
|
|
|
|
|
|
|
Änderungen:
|
2025-09-05 09:27:28 +02:00
|
|
|
|
v1.3.1: Helper collection_names(prefix) wiederhergestellt (für reset_qdrant usw.).
|
|
|
|
|
|
v1.3.0: ensure_collections(..., destructive=False) – keine stillen Drops im Dry-Run.
|
|
|
|
|
|
Edges-Collection nur bei explicit destructive=True neu anlegen.
|
|
|
|
|
|
≤v1.2.x: Konnte {prefix}_edges bei fehlender VectorConfig automatisch neu erstellen (riskant).
|
2025-09-05 09:22:15 +02:00
|
|
|
|
|
2025-09-05 09:27:28 +02:00
|
|
|
|
Bezug/Quelle:
|
|
|
|
|
|
Alte Core-Variante enthielt collection_names(prefix); diverse Scripts nutzen das weiterhin. :contentReference[oaicite:1]{index=1}
|
2025-09-05 09:22:15 +02:00
|
|
|
|
"""
|
2025-09-03 08:04:02 +02:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import os
|
2025-09-04 08:37:10 +02:00
|
|
|
|
from dataclasses import dataclass
|
2025-09-05 09:27:28 +02:00
|
|
|
|
from typing import Optional, Tuple
|
2025-09-03 08:04:02 +02:00
|
|
|
|
|
|
|
|
|
|
from qdrant_client import QdrantClient
|
|
|
|
|
|
from qdrant_client.http import models as rest
|
|
|
|
|
|
|
2025-09-04 08:37:10 +02:00
|
|
|
|
|
2025-09-05 09:27:28 +02:00
|
|
|
|
# -------------------------------
|
|
|
|
|
|
# Konfiguration
|
|
|
|
|
|
# -------------------------------
|
|
|
|
|
|
|
2025-09-03 08:04:02 +02:00
|
|
|
|
@dataclass
|
|
|
|
|
|
class QdrantConfig:
|
|
|
|
|
|
url: str
|
2025-09-04 08:37:10 +02:00
|
|
|
|
api_key: Optional[str]
|
|
|
|
|
|
prefix: str
|
|
|
|
|
|
dim: int
|
2025-09-03 08:04:02 +02:00
|
|
|
|
|
2025-09-04 08:37:10 +02:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def from_env() -> "QdrantConfig":
|
|
|
|
|
|
url = os.getenv("QDRANT_URL")
|
|
|
|
|
|
if not url:
|
|
|
|
|
|
host = os.getenv("QDRANT_HOST", "127.0.0.1")
|
|
|
|
|
|
port = int(os.getenv("QDRANT_PORT", "6333"))
|
|
|
|
|
|
url = f"http://{host}:{port}"
|
|
|
|
|
|
api_key = os.getenv("QDRANT_API_KEY") or None
|
|
|
|
|
|
prefix = os.getenv("COLLECTION_PREFIX", "mindnet")
|
2025-09-05 09:22:15 +02:00
|
|
|
|
dim = int(os.getenv("VECTOR_DIM", "384"))
|
2025-09-04 08:37:10 +02:00
|
|
|
|
return QdrantConfig(url=url, api_key=api_key, prefix=prefix, dim=dim)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-05 09:27:28 +02:00
|
|
|
|
# -------------------------------
|
|
|
|
|
|
# Client
|
|
|
|
|
|
# -------------------------------
|
|
|
|
|
|
|
2025-09-03 08:04:02 +02:00
|
|
|
|
def get_client(cfg: QdrantConfig) -> QdrantClient:
|
2025-09-04 08:37:10 +02:00
|
|
|
|
return QdrantClient(url=cfg.url, api_key=cfg.api_key)
|
|
|
|
|
|
|
2025-09-03 08:04:02 +02:00
|
|
|
|
|
2025-09-05 09:27:28 +02:00
|
|
|
|
# -------------------------------
|
|
|
|
|
|
# Collection-Erzeuger (Hilfsfunktionen)
|
|
|
|
|
|
# -------------------------------
|
|
|
|
|
|
|
2025-09-05 09:22:15 +02:00
|
|
|
|
def _create_notes(client: QdrantClient, name: str, dim: int) -> None:
|
|
|
|
|
|
if not client.collection_exists(name):
|
2025-09-03 08:04:02 +02:00
|
|
|
|
client.create_collection(
|
2025-09-05 09:22:15 +02:00
|
|
|
|
collection_name=name,
|
2025-09-04 08:37:10 +02:00
|
|
|
|
vectors_config=rest.VectorParams(size=dim, distance=rest.Distance.COSINE),
|
2025-09-03 08:04:02 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-09-05 09:22:15 +02:00
|
|
|
|
def _create_chunks(client: QdrantClient, name: str, dim: int) -> None:
|
|
|
|
|
|
if not client.collection_exists(name):
|
2025-09-04 08:37:10 +02:00
|
|
|
|
client.create_collection(
|
2025-09-05 09:22:15 +02:00
|
|
|
|
collection_name=name,
|
2025-09-04 08:37:10 +02:00
|
|
|
|
vectors_config=rest.VectorParams(size=dim, distance=rest.Distance.COSINE),
|
|
|
|
|
|
)
|
2025-09-03 08:04:02 +02:00
|
|
|
|
|
2025-09-05 09:22:15 +02:00
|
|
|
|
def _create_edges(client: QdrantClient, name: str) -> None:
|
|
|
|
|
|
if not client.collection_exists(name):
|
2025-09-04 08:37:10 +02:00
|
|
|
|
client.create_collection(
|
2025-09-05 09:22:15 +02:00
|
|
|
|
collection_name=name,
|
|
|
|
|
|
vectors_config=rest.VectorParams(size=1, distance=rest.Distance.DOT), # 1D-Dummy
|
2025-09-04 08:37:10 +02:00
|
|
|
|
)
|
2025-09-03 08:04:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
2025-09-05 09:27:28 +02:00
|
|
|
|
# -------------------------------
|
|
|
|
|
|
# Public API
|
|
|
|
|
|
# -------------------------------
|
|
|
|
|
|
|
2025-09-05 09:22:15 +02:00
|
|
|
|
def ensure_collections(client: QdrantClient, prefix: str, dim: int, destructive: bool = False) -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Stellt sicher, dass die drei Collections existieren.
|
|
|
|
|
|
- Default **nicht destruktiv**: vorhandene Collections bleiben unangetastet.
|
|
|
|
|
|
- Nur wenn 'destructive=True', wird eine ungeeignete Edges-Collection gelöscht und neu angelegt.
|
|
|
|
|
|
"""
|
|
|
|
|
|
notes = f"{prefix}_notes"
|
|
|
|
|
|
chunks = f"{prefix}_chunks"
|
|
|
|
|
|
edges = f"{prefix}_edges"
|
2025-09-04 08:37:10 +02:00
|
|
|
|
|
2025-09-05 09:22:15 +02:00
|
|
|
|
_create_notes(client, notes, dim)
|
|
|
|
|
|
_create_chunks(client, chunks, dim)
|
2025-09-04 08:37:10 +02:00
|
|
|
|
|
2025-09-05 09:22:15 +02:00
|
|
|
|
if client.collection_exists(edges):
|
|
|
|
|
|
# Prüfen, ob die Edges-Collection bereits eine Vektorkonfig hat
|
|
|
|
|
|
try:
|
|
|
|
|
|
info = client.get_collection(edges)
|
|
|
|
|
|
vectors_cfg = getattr(getattr(info.result, "config", None), "params", None)
|
|
|
|
|
|
has_vectors = getattr(vectors_cfg, "vectors", None) is not None
|
|
|
|
|
|
except Exception:
|
2025-09-05 09:27:28 +02:00
|
|
|
|
# konservativ: nichts anfassen, um Datenverlust zu vermeiden
|
|
|
|
|
|
has_vectors = True
|
2025-09-05 09:22:15 +02:00
|
|
|
|
|
|
|
|
|
|
if not has_vectors:
|
|
|
|
|
|
if destructive:
|
|
|
|
|
|
client.delete_collection(edges)
|
|
|
|
|
|
_create_edges(client, edges)
|
|
|
|
|
|
else:
|
2025-09-05 09:27:28 +02:00
|
|
|
|
print(
|
|
|
|
|
|
f"[ensure_collections] WARN: '{edges}' ohne VectorConfig gefunden; "
|
|
|
|
|
|
f"keine destruktive Änderung (destructive=False).",
|
|
|
|
|
|
flush=True,
|
|
|
|
|
|
)
|
2025-09-05 09:22:15 +02:00
|
|
|
|
else:
|
|
|
|
|
|
_create_edges(client, edges)
|
2025-09-05 09:27:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def collection_names(prefix: str) -> Tuple[str, str, str]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Abwärtskompatibler Helper für Scripts:
|
|
|
|
|
|
returns (f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges")
|
|
|
|
|
|
"""
|
|
|
|
|
|
return (f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wipe_collections(client: QdrantClient, prefix: str) -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Löscht alle drei Collections – nur verwenden, wenn bewusst ein Clean-Rebuild gewünscht ist.
|
|
|
|
|
|
"""
|
|
|
|
|
|
for name in collection_names(prefix):
|
|
|
|
|
|
if client.collection_exists(name):
|
|
|
|
|
|
client.delete_collection(name)
|