mindnet/app/core/qdrant.py

121 lines
3.9 KiB
Python
Raw Normal View History

2025-09-04 08:37:10 +02:00
# app/core/qdrant.py
2025-09-03 08:04:02 +02:00
from __future__ import annotations
2025-09-04 08:37:10 +02:00
2025-09-03 08:04:02 +02:00
import os
2025-09-04 08:37:10 +02:00
from dataclasses import dataclass
from typing import Optional
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
# -------------------------------
# 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 (bevorzugt) oder Host/Port
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
# Collection-Prefix und Vektor-Dimension
prefix = os.getenv("COLLECTION_PREFIX", "mindnet")
dim = int(os.getenv("VECTOR_DIM", "384")) # MiniLM-384 by default
return QdrantConfig(url=url, api_key=api_key, prefix=prefix, dim=dim)
# -------------------------------
# Client / Setup
# -------------------------------
2025-09-03 08:04:02 +02:00
def get_client(cfg: QdrantConfig) -> QdrantClient:
2025-09-03 08:12:31 +02:00
"""
Erstellt einen QdrantClient basierend auf der Config.
"""
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-04 08:37:10 +02:00
def ensure_collections(client: QdrantClient, prefix: str, dim: int) -> None:
2025-09-03 08:04:02 +02:00
"""
2025-09-04 08:37:10 +02:00
Stellt sicher, dass die drei Collections existieren:
- {prefix}_notes : Vektor-Dim = dim (COSINE)
- {prefix}_chunks : Vektor-Dim = dim (COSINE)
- {prefix}_edges : Vektor-Dim = 1 (DOT) <-- Dummy-Vektor, damit der Python-Client kein 'vector' zwingt
Falls {prefix}_edges bereits vektorlos existiert, wird sie gelöscht und mit 1D neu erstellt.
2025-09-03 08:04:02 +02:00
"""
2025-09-04 08:37:10 +02:00
notes = f"{prefix}_notes"
chunks = f"{prefix}_chunks"
edges = f"{prefix}_edges"
2025-09-03 08:04:02 +02:00
2025-09-04 08:37:10 +02:00
# Notes
if not client.collection_exists(notes):
2025-09-03 08:04:02 +02:00
client.create_collection(
2025-09-04 08:37:10 +02:00
collection_name=notes,
vectors_config=rest.VectorParams(size=dim, distance=rest.Distance.COSINE),
2025-09-03 08:04:02 +02:00
)
2025-09-04 08:37:10 +02:00
# Chunks
if not client.collection_exists(chunks):
client.create_collection(
collection_name=chunks,
vectors_config=rest.VectorParams(size=dim, distance=rest.Distance.COSINE),
)
2025-09-03 08:04:02 +02:00
2025-09-04 08:37:10 +02:00
# Edges: 1D Dummy-Vektor (Workaround)
recreate_edges = False
if client.collection_exists(edges):
2025-09-03 08:04:02 +02:00
try:
2025-09-04 08:37:10 +02:00
info = client.get_collection(edges)
# Prüfen, ob Vektor-Konfig existiert
vectors_cfg = getattr(getattr(info.result, "config", None), "params", None)
# Neuere Clients: info.result.config.params.vectors kann VectorParams oder dict sein
has_vectors = getattr(vectors_cfg, "vectors", None) is not None
if not has_vectors:
recreate_edges = True
2025-09-03 08:04:02 +02:00
except Exception:
2025-09-04 08:37:10 +02:00
# Wenn Metadaten nicht lesbar → sicherheitshalber neu anlegen
recreate_edges = True
else:
# Existiert noch nicht → wird gleich erstellt
pass
2025-09-03 08:04:02 +02:00
2025-09-04 08:37:10 +02:00
if recreate_edges and client.collection_exists(edges):
client.delete_collection(edges)
2025-09-03 08:04:02 +02:00
2025-09-04 08:37:10 +02:00
if not client.collection_exists(edges):
client.create_collection(
collection_name=edges,
vectors_config=rest.VectorParams(size=1, distance=rest.Distance.DOT),
)
2025-09-03 08:04:02 +02:00
2025-09-04 08:37:10 +02:00
# -------------------------------
# (Optionale) Utility-Funktionen
# -------------------------------
def collection_names(prefix: str) -> tuple[str, str, str]:
"""Hilfsfunktion, falls du die Namen an einer Stelle brauchst."""
return (f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges")
def wipe_collections(client: QdrantClient, prefix: str) -> None:
"""
Löscht alle drei Collections (nur nutzen, wenn du bewusst neu aufsetzen willst).
"""
for name in collection_names(prefix):
if client.collection_exists(name):
client.delete_collection(name)