Advanced AI For My Personal Retrieval-Augmented Generation — architecture & guide.
Overview
A4MP RAG AI lets you chat with your own documents. You upload PDFs, Word docs, or plain text; it splits them into semantically meaningful chunks, embeds each chunk into a 3072-dimensional vector, and stores everything in your private Postgres + pgvector database. When you ask a question, it embeds your query, retrieves the most similar chunks, and streams a grounded answer with source citations. English + తెలుగు supported.
System architecture
Three layers, all running on Lovable infrastructure:
- Frontend — React 19 + TanStack Start, cinematic Tailwind design system.
- Backend — TanStack server functions and a streaming server route (
/api/chat). Auth via Lovable Cloud (email + Google). - Data — Postgres with the
vectorextension. HNSW index over halfvec-cast 3072-dim embeddings. Row-Level Security everywhere. - AI — Lovable AI Gateway:
google/gemini-embedding-001for embeddings,google/gemini-3-flash-previewfor chat.
┌──────────────┐ sendMessage ┌────────────────────┐
│ React Chat │ ──────────────▶ │ /api/chat (stream) │
└──────────────┘ └─────────┬──────────┘
│ embed(query)
▼
┌────────────────────┐
│ Lovable AI Gateway │
└─────────┬──────────┘
│ vector
▼
┌────────────────────────────────┐
│ Postgres + pgvector (RLS) │
│ match_document_chunks(vec, k) │
└─────────┬──────────────────────┘
│ top-K chunks
▼
┌────────────────────┐
│ streamText(gemini) │──▶ tokens to client
└────────────────────┘Data flow
Ingest. File bytes → server function → text extraction (pdf-parse / mammoth / native) → paragraph-aware chunking (~1000 chars, 150 overlap) → embed each chunk → insert rows into document_chunks with vector(3072).
Query. User message → embed → match_document_chunks(query_embedding, 6) (SECURITY INVOKER, scoped to auth.uid() via RLS) → top chunks are inlined into the system prompt with numbered markers → streamText returns tokens → on finish the user + assistant messages are persisted with the source list.
API
App-internal calls use typed createServerFn RPC (they look like normal function calls in components).
uploadDocument({filename, mime, data(base64)})→{id, chunks}listDocuments()→{documents: [...]}deleteDocument({id})→{ok:true}createThread({title?}),listThreads(),getThread({id}),deleteThread({id}),renameThread({id, title})
Streaming chat uses a server route:
POST /api/chat
Authorization: Bearer <supabase access token>
{ "threadId": "<uuid>", "messages": UIMessage[] }
→ text/event-stream (AI SDK UI message stream)Database schema
documents(id, user_id, filename, mime, size_bytes, status, chunk_count, created_at)
document_chunks(id, document_id, user_id, chunk_index, content,
embedding vector(3072), metadata jsonb, created_at)
· HNSW halfvec_cosine_ops on embedding::halfvec(3072)
chat_threads(id, user_id, title, created_at, updated_at)
chat_messages(id, thread_id, user_id, role, content, sources jsonb, created_at)
match_document_chunks(query_embedding vector(3072), match_count int)
RETURNS (id, document_id, content, metadata, similarity)
· SECURITY INVOKER — RLS restricts rows to auth.uid()Security considerations
- Row-Level Security is enabled on every table; policies scope reads and writes to
auth.uid(). - The similarity SQL function runs as SECURITY INVOKER, so it inherits the caller's RLS — one user can never retrieve another user's chunks.
- The chat route requires a valid Supabase bearer token; the token is verified with
supabase.auth.getClaimsbefore any query runs. - The Lovable AI key is server-side only; the browser never sees it.
- File size is capped at 20 MB; the backend rejects unknown MIME types.
Deployment
Click Publish in the Lovable editor. The app deploys to your *.lovable.app subdomain. Custom domains are configurable in Project Settings. Lovable Cloud manages Postgres, auth, and the AI Gateway automatically — no keys or infra to babysit.
Future improvements
- Streaming citation highlights inside markdown
- Per-document scoping (chat with a single file)
- Hybrid search: pgvector +
tsvectorfull-text - Reranker pass before the LLM call
- OCR pipeline for scanned PDFs
- Voice input in Telugu & English via Lovable AI speech-to-text