Files
DeepTutor/deeptutor/services/rag/preflight.py

308 lines
9.4 KiB
Python

"""Per-engine environment preflight checks.
Powers the "check whether this engine can run right now" affordance on each
engine's detail page. Every check is best-effort and never raises — a failed
import or missing config becomes a failed/optional check, not an exception.
A check is ``{key, label, ok, detail, optional}``. Overall ``ok`` is true when
every *required* (non-optional) check passes.
"""
from __future__ import annotations
from typing import Any
from .factory import (
DEFAULT_PROVIDER,
GRAPHRAG_PROVIDER,
IMA_PROVIDER,
KIWIX_PROVIDER,
LIGHTRAG_PROVIDER,
LIGHTRAG_SERVER_PROVIDER,
PAGEINDEX_OSS_PROVIDER,
PAGEINDEX_PROVIDER,
WEKNORA_PROVIDER,
normalize_provider_name,
)
def _check(key: str, label: str, ok: bool, detail: str = "", *, optional: bool = False) -> dict:
return {"key": key, "label": label, "ok": bool(ok), "detail": detail, "optional": optional}
def _active_chat_model() -> tuple[str | None, str]:
"""Return ``(model, binding)`` for the active chat LLM, or ``(None, "")``."""
try:
from deeptutor.services.config import resolve_llm_runtime_config
cfg = resolve_llm_runtime_config()
return getattr(cfg, "model", None), str(getattr(cfg, "binding", "") or "")
except Exception:
return None, ""
def _active_embedding() -> tuple[str | None, int]:
"""Return ``(model, dim)`` for the active embedding model, or ``(None, 0)``."""
try:
from deeptutor.services.embedding import get_embedding_config
cfg = get_embedding_config()
return getattr(cfg, "model", None), int(getattr(cfg, "dim", 0) or 0)
except Exception:
return None, 0
def _llamaindex_preflight() -> dict:
emb_model, emb_dim = _active_embedding()
if not emb_model:
embedding_detail = "Configure one in the model catalog."
elif emb_dim == 0:
# LlamaIndex sizes its vector store from the returned embeddings.
embedding_detail = f"{emb_model} · dimension inferred when indexing"
else:
embedding_detail = f"{emb_model} · {emb_dim}d"
checks = [
_check(
"embedding",
"Active embedding model",
bool(emb_model) and emb_dim >= 0,
embedding_detail,
)
]
try:
from .pipelines.llamaindex.retrievers import _import_bm25_retriever
bm25_ok = _import_bm25_retriever() is not None
except Exception:
bm25_ok = False
checks.append(
_check(
"bm25",
"BM25 hybrid retrieval",
bm25_ok,
"Installed." if bm25_ok else "Not installed — hybrid falls back to vector-only.",
optional=True,
)
)
return _finalize(checks)
def _pageindex_preflight() -> dict:
try:
from .pipelines.pageindex.config import get_pageindex_config
cfg = get_pageindex_config(require_key=False)
configured = bool(cfg.api_key)
except Exception:
configured = False
return _finalize(
[
_check(
"api_key",
"API key configured",
configured,
"PageIndex Cloud" if configured else "Add a PageIndex API key under Credentials.",
)
]
)
def _pageindex_oss_preflight() -> dict:
try:
from .pipelines.pageindex.client import resolve_oss_sdk_config
model, _backend = resolve_oss_sdk_config()
llm_ok, detail = bool(model), model
except Exception as exc:
llm_ok = False
detail = str(exc)
return _finalize(
[
_check("chat", "Active LLM for indexing", llm_ok, detail),
]
)
def _graphrag_preflight() -> dict:
try:
from .pipelines.graphrag.config import is_graphrag_available
installed = is_graphrag_available()
except Exception:
installed = False
emb_model, emb_dim = _active_embedding()
chat_model, _ = _active_chat_model()
return _finalize(
[
_check(
"package",
"GraphRAG package installed",
installed,
"Installed." if installed else "pip install 'deeptutor[graphrag]'",
),
_check(
"chat",
"Active chat model",
bool(chat_model),
chat_model or "Configure one in the model catalog.",
),
_check(
"embedding",
"Active embedding model",
bool(emb_model) and emb_dim > 0,
f"{emb_model} · {emb_dim}d" if emb_model else "Configure one in the model catalog.",
),
]
)
def _lightrag_preflight() -> dict:
package_detail = "pip install 'deeptutor[rag-lightrag]'"
try:
from .pipelines.lightrag.config import is_lightrag_available
from .pipelines.lightrag.engine import LIGHTRAG_VERSION, installed_version
current_version = installed_version() if is_lightrag_available() else ""
installed = current_version == LIGHTRAG_VERSION
if current_version:
package_detail = (
f"Installed {current_version}."
if installed
else f"Found {current_version}; required {LIGHTRAG_VERSION}."
)
except Exception:
installed = False
emb_model, emb_dim = _active_embedding()
chat_model, binding = _active_chat_model()
vision_ok = False
if chat_model:
try:
from deeptutor.services.llm.capabilities import supports_vision
vision_ok = supports_vision(binding, chat_model)
except Exception:
vision_ok = False
return _finalize(
[
_check(
"package",
"LightRAG package installed",
installed,
package_detail,
),
_check(
"chat",
"Active chat model",
bool(chat_model),
chat_model or "Configure one in the model catalog.",
),
_check(
"embedding",
"Active embedding model",
bool(emb_model) and emb_dim > 0,
f"{emb_model} · {emb_dim}d" if emb_model else "Configure one in the model catalog.",
),
_check(
"vision",
"Vision model for multimodal",
vision_ok,
"Active chat model supports vision."
if vision_ok
else "Active chat model has no vision — image analysis is disabled.",
optional=True,
),
]
)
def _lightrag_server_preflight() -> dict:
try:
from deeptutor.services.config import load_lightrag_server_settings
settings = load_lightrag_server_settings()
server_url = str(settings.get("server_url") or "").strip()
except Exception:
server_url = ""
return _finalize(
[
_check(
"server_url",
"Default server configured",
bool(server_url),
server_url or "Add a server URL under Connection defaults.",
)
]
)
def _ima_preflight() -> dict:
try:
from .pipelines.ima.config import get_account_credentials
credentials = get_account_credentials()
except Exception:
from .pipelines.ima.config import ImaCredentials
credentials = ImaCredentials()
return _finalize(
[
_check(
"credentials",
"IMA Client ID and API key configured",
credentials.complete,
credentials.client_id
if credentials.complete
else "Add them under Credentials, or supply a pair per knowledge base "
"when connecting one.",
)
]
)
def _connected_preflight() -> dict:
"""Connected engines are configured per knowledge base, not account-wide.
``weknora`` and ``kiwix`` are pointers: each knowledge base carries its own
server URL, credentials and remote identifier, validated by the connect
probe before the knowledge base is registered. Nothing is installed or
configured globally, so the engine reports ready. Registering them here also
keeps ``_PREFLIGHTS`` covering every ``KNOWN_PROVIDERS`` entry; a missing
entry turns the engine page's readiness check into a 500.
"""
return _finalize(
[
_check(
"connection",
"Connection details are supplied per knowledge base",
True,
"Enter the server URL and credentials when connecting a knowledge base.",
)
]
)
def _finalize(checks: list[dict]) -> dict:
ok = all(c["ok"] for c in checks if not c["optional"])
return {"ok": ok, "checks": checks}
_PREFLIGHTS = {
DEFAULT_PROVIDER: _llamaindex_preflight,
PAGEINDEX_PROVIDER: _pageindex_preflight,
PAGEINDEX_OSS_PROVIDER: _pageindex_oss_preflight,
GRAPHRAG_PROVIDER: _graphrag_preflight,
LIGHTRAG_PROVIDER: _lightrag_preflight,
LIGHTRAG_SERVER_PROVIDER: _lightrag_server_preflight,
IMA_PROVIDER: _ima_preflight,
WEKNORA_PROVIDER: _connected_preflight,
KIWIX_PROVIDER: _connected_preflight,
}
def engine_preflight(provider: str) -> dict[str, Any]:
"""Run the requirement checks for ``provider`` and return the report."""
return _PREFLIGHTS[normalize_provider_name(provider)]()
__all__ = ["engine_preflight"]