## Summary - Closes Phases 1–3 of [#62](#62) (Phase 4 eval harness left for a follow-up). - **Accuracy:** Retrieval is decided every turn (`GroundingDecider`, fails open). `FAST` no longer skips search — it only selects `OLLAMA_MODEL_FAST`. Search failures surface an explicit error instead of hallucinating from parametric memory. - **Search:** Pluggable `services/search/` with **SearxNG primary** + DDGS failover, ranking/dedupe/rumour filtering, numbered dated source blocks, citations persisted on `Prompt.citations` and emitted as `{"v":1,"type":"citations",...}` after stream end. - **Models:** Role-scoped `OLLAMA_MODEL_THINKING` / `_FAST` / `_UTILITY` / `OLLAMA_EMBED_MODEL=nomic-embed-text`, configurable `num_ctx`, real model name on `PromptMetric`, `reindex_embeddings` management command + loud embedding-dimension mismatch. ## SearxNG (ops) See README **SearxNG** section. Short version: run `searxng/searxng` on the GPU host, enable `json` in `settings.yml`, set `SEARXNG_BASE_URL=http://10.0.0.128:8080` in prod/beta secrets, open `:8080` on the LAN firewall like Ollama. ## Test plan - [x] `SKIP_RAG_INIT=1 python manage.py test chat_backend.tests` — 442 OK (6 skipped) - [ ] Deploy beta with updated secrets (`OLLAMA_MODEL_*`, `OLLAMA_EMBED_MODEL=nomic-embed-text`, `SEARXNG_BASE_URL`) - [ ] After embed change: `python manage.py reindex_embeddings` - [ ] Verify `did Taylor Swift get married` in FAST and THINKING returns grounded answer with citations frame - [ ] Kill SearxNG and confirm factual turns return search_unavailable (not Joe Alwyn hallucination); non-factual chat still worksReviewed-on: #65
61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
from django.apps import AppConfig
|
|
from django.conf import settings
|
|
from django.db import OperationalError, ProgrammingError
|
|
import os
|
|
import sys
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ChatBackendConfig(AppConfig):
|
|
default_auto_field = "django.db.models.BigAutoField"
|
|
name = "chat_backend"
|
|
|
|
def ready(self):
|
|
import chat_backend.signals
|
|
|
|
# Skip heavy Ollama/Chroma init during migrate/collectstatic/test/CI.
|
|
management_cmds = {
|
|
"migrate",
|
|
"makemigrations",
|
|
"collectstatic",
|
|
"test",
|
|
"shell",
|
|
"check",
|
|
"reindex_embeddings",
|
|
}
|
|
if any(cmd in sys.argv for cmd in management_cmds):
|
|
return
|
|
if os.environ.get("SKIP_RAG_INIT", "").lower() in {"1", "true", "yes"}:
|
|
return
|
|
|
|
FORCE_RELOAD = False
|
|
|
|
try:
|
|
from .services.rag_services import (
|
|
AsyncRAGService,
|
|
EmbeddingDimensionMismatch,
|
|
)
|
|
from chat_backend.models import Document
|
|
|
|
if Document.objects.exists():
|
|
rag_service = AsyncRAGService()
|
|
|
|
if rag_service.vector_store._collection.count() == 0:
|
|
print("Initializing ChromaDB with existing documents...")
|
|
rag_service.ingest_documents()
|
|
if FORCE_RELOAD:
|
|
print("Force Reload ChromaDB with existing documents...")
|
|
rag_service.clear_vector_store()
|
|
except EmbeddingDimensionMismatch as exc:
|
|
# Loud failure — do not silently serve with the wrong embed model.
|
|
logger.error("RAG embedding dimension mismatch: %s", exc)
|
|
raise
|
|
except (OperationalError, ProgrammingError):
|
|
# Database tables might not exist yet during migration
|
|
pass
|
|
except Exception as exc:
|
|
# Ollama/Chroma unreachable must not block process start.
|
|
print(f"Skipping RAG init at startup: {exc}")
|