All checks were successful
Deploy mindnet to llm-node / deploy (push) Successful in 3s
136 lines
4.5 KiB
Python
136 lines
4.5 KiB
Python
"""
|
||
app/main.py — mindnet API bootstrap (WP-04 Hooks)
|
||
|
||
Zweck:
|
||
Startpunkt der FastAPI-Anwendung: Router-Registrierung und Healthcheck.
|
||
Ergänzt um optionale WP-04 Router (Graph/Retriever), falls vorhanden.
|
||
Kompatibilität:
|
||
Python 3.12+; FastAPI 0.110+ (getestet lokal)
|
||
Version:
|
||
0.4.1 (WP-04 Hooks erweitert: Feature-Flags & /admin/features; bestehende Routen unverändert)
|
||
Stand:
|
||
2025-10-07
|
||
Bezug:
|
||
Handbuch & Knowledge Design (WP-03/04)
|
||
Umgebung:
|
||
Siehe app/config.py (QDRANT_URL, MINDNET_PREFIX, usw.)
|
||
Nutzung:
|
||
uvicorn app.main:app --reload --port 8000
|
||
Änderungsverlauf:
|
||
0.4.1 (2025-10-07) – Feature-Flags für dynamische WP-04-Router + /admin/features ergänzt.
|
||
0.4.0 (2025-10-06) – /admin/meta ergänzt; optionale Einbindung von graph/query/admin Routern.
|
||
0.3.x (vorher) – Basisversion laut bereitgestellter Datei.
|
||
|
||
Hinweise:
|
||
Bestehende Routen/Funktionen bleiben unverändert.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from fastapi import FastAPI
|
||
from .config import get_settings
|
||
from .routers.embed_router import router as embed_router
|
||
from .routers.qdrant_router import router as qdrant_router
|
||
|
||
|
||
def _maybe_include_wp04(app: FastAPI) -> None:
|
||
"""
|
||
WP-04 Router dynamisch einbinden, ohne harte Abhängigkeit.
|
||
Ergänzung: setzt app.state.features['wp04']-Flags, damit Admin/Monitoring
|
||
sichtbar machen kann, was tatsächlich geladen wurde.
|
||
"""
|
||
# Initialisiere Feature-Flags
|
||
if not hasattr(app.state, "features"):
|
||
app.state.features = {}
|
||
app.state.features.setdefault("wp04", {"graph": False, "query": False, "admin": False})
|
||
|
||
# Graph
|
||
try:
|
||
from .routers.graph import router as graph_router # type: ignore
|
||
app.include_router(graph_router, prefix="/graph", tags=["graph"])
|
||
app.state.features["wp04"]["graph"] = True
|
||
except Exception:
|
||
pass
|
||
|
||
# Query
|
||
try:
|
||
from .routers.query import router as query_router # type: ignore
|
||
app.include_router(query_router, prefix="/query", tags=["query"])
|
||
app.state.features["wp04"]["query"] = True
|
||
except Exception:
|
||
pass
|
||
|
||
# Admin (optional)
|
||
try:
|
||
from .routers.admin import router as admin_router # type: ignore
|
||
app.include_router(admin_router, prefix="/admin", tags=["admin"])
|
||
app.state.features["wp04"]["admin"] = True
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def create_app() -> FastAPI:
|
||
app = FastAPI(title="mindnet API", version="0.1.0")
|
||
s = get_settings()
|
||
|
||
@app.get("/healthz")
|
||
def healthz():
|
||
"""Basis-Liveness-Check (unverändert)."""
|
||
return {"status": "ok", "qdrant": s.QDRANT_URL, "prefix": s.COLLECTION_PREFIX}
|
||
|
||
# Admin-Metadaten (WP-04, unverändert)
|
||
@app.get("/admin/meta")
|
||
def admin_meta():
|
||
# Import lokal, damit fehlende Module nicht beim Start stören
|
||
meta = {}
|
||
try:
|
||
import app.main as _m
|
||
meta["main"] = getattr(_m, "__doc__", "").splitlines()[0:1]
|
||
except Exception:
|
||
pass
|
||
try:
|
||
import app.config as _c
|
||
meta["config"] = {
|
||
"QDRANT_URL": s.QDRANT_URL,
|
||
"COLLECTION_PREFIX": s.COLLECTION_PREFIX,
|
||
"VECTOR_SIZE": s.VECTOR_SIZE,
|
||
"DISTANCE": s.DISTANCE,
|
||
"MODEL_NAME": s.MODEL_NAME,
|
||
"RETRIEVER": {
|
||
"W_SEM": getattr(s, "RETRIEVER_W_SEM", 0.70),
|
||
"W_EDGE": getattr(s, "RETRIEVER_W_EDGE", 0.25),
|
||
"W_CENT": getattr(s, "RETRIEVER_W_CENT", 0.05),
|
||
"TOP_K": getattr(s, "RETRIEVER_TOP_K", 10),
|
||
"EXPAND_DEPTH": getattr(s, "RETRIEVER_EXPAND_DEPTH", 1),
|
||
},
|
||
}
|
||
except Exception:
|
||
pass
|
||
try:
|
||
import app.core.qdrant_points as _qp
|
||
meta["qdrant_points"] = getattr(_qp, "__doc__", "").splitlines()[0:1]
|
||
except Exception:
|
||
pass
|
||
return {"meta": meta}
|
||
|
||
# NEU: Features-/Readiness-Ansicht (rein ergänzend)
|
||
@app.get("/admin/features")
|
||
def admin_features():
|
||
"""
|
||
Liefert die dynamisch gesetzten Feature-Flags für WP-04 Router
|
||
(graph/query/admin), ohne bestehende Endpunkte zu beeinflussen.
|
||
"""
|
||
features = getattr(app.state, "features", {})
|
||
return {"features": features}
|
||
|
||
# Bestehende Router (unverändert)
|
||
app.include_router(embed_router)
|
||
app.include_router(qdrant_router)
|
||
|
||
# WP-04 Router ggf. dynamisch einbinden
|
||
_maybe_include_wp04(app)
|
||
return app
|
||
|
||
|
||
app = create_app()
|