Backend Service.mdc
Patterns for service modules (ingest, rag, storage, vectorstore)
Loading actions...
Skill content
Main instructions and any bundled files for this skill.
Backend Service Patterns
-
No classes — services are plain Python modules with top-level functions. No service class wrappers.
-
Dual cloud/local — read env var at module top, expose
_is_cloud() -> bool, branch inside every public function:QDRANT_URL = os.getenv("QDRANT_URL") def _is_cloud() -> bool: return bool(QDRANT_URL) -
Lazy client imports — import heavy clients (supabase, qdrant_client, chromadb, langchain_*) inside the function body, not at module top. They are slow to initialise and not needed in every path.
-
No DB access in services — services never import
engineorget_session. DB writes happen in the router or background task that calls the service. -
Vectorstore — always
delete_document_vectors(slug, filename)beforeupsert_chunks()to prevent duplicates on re-upload. -
Constants at module top:
LLM_MODEL = "gpt-4o-mini" EMBED_MODEL = "text-embedding-3-small" CHUNK_SIZE = 800 CHUNK_OVERLAP = 100 -
Async RAG —
stream_answer_questionis anasyncgenerator. Useawait loop.run_in_executor(None, retriever.invoke, question)for the sync retriever call inside async context. -
Exception handling in services — let exceptions propagate; callers (background tasks / routers) catch and record
doc.status = "error". -
File path helper — use
Path(os.getenv("DATA_DIR", "./data")) / ...for all local paths; call.mkdir(parents=True, exist_ok=True)before writing.
Related Skills
Frontend Typescript Linting.mdc
TypeScript and ESLint rules that MUST be followed when creating, modifying, or reviewing any file under apps/frontend/, including .ts, .tsx, .js, and .jsx files. Also apply when discussing frontend li...
2. Apply Deepthink Protocol (reason about dependencies
risks