This repo includes a real document-chat pipeline, not just a fake “upload a
PDF” button. Users can attach documents, index them, ask questions, and get
answers with citations.
First, What Is RAG?
RAG stands for retrieval-augmented generation.
In simple terms, it means:
- your app stores useful source material, like PDFs
- when the user asks a question, the app finds the most relevant pieces
- those pieces are passed into the model as context
- the model answers using that context
What The Vector Database Does
The vector database is what makes “semantic search” possible. Instead of storing only raw text, the app stores:- the original chunk of text
- metadata such as document and page information
- a vector embedding for that chunk
- Supabase PostgreSQL
- pgvector
- OpenAI embeddings
What The User Experience Looks Like
From a user’s point of view, the flow is simple:- upload a PDF in chat
- let the app index it
- ask a question in normal language
- get an answer with source citations
How The Pipeline Works
1. Upload
The PDF file is uploaded and tracked in the database.2. Text extraction
The repo extracts readable text from the PDF so the AI system has something it can work with.3. Chunking
The extracted text is split into smaller sections. This matters because models and vector search work much better on smaller chunks than one giant document.4. Embedding
Each chunk is turned into an embedding using OpenAI’s embedding model.5. Storage
Those embeddings are stored in Supabase in theembeddings table, alongside
document metadata.
6. Retrieval
When the user asks a question, the question is embedded too. The app then runs similarity search to find the closest matching chunks.7. Prompt injection
The best chunks are assembled into prompt-ready context and passed into the chat generation flow.8. Citations
The answer includes source metadata so the UI can show citations back to the user.Main Tables And Data
The exact schema is created by the Supabase migrations, but the important data concepts are:pdf_documentsfor uploaded documents and indexing statusembeddingsfor chunk vectors and metadatachat_document_linksfor linking documents to chat sessions
Where The Logic Lives
If you want to customize or debug the retrieval system, these files matter most:lib/rag/pdf-extract.tsfor PDF text extractionlib/rag/chunking.tsfor chunk creationlib/rag/embedding-ingest.tsfor embedding + persistencelib/rag/retrieve.tsfor semantic retrievallib/rag/citations.tsfor citation formattingapp/(apps)/chat/api/chat/chat-generation.tsfor injecting RAG into chat
Why OpenAI Is Still Important Here
Even if your users mostly chat with Claude, Gemini, Grok, or DeepSeek, the current document pipeline still uses OpenAI embeddings for indexing and retrieval. So if you want PDF RAG to work, you should treat OpenAI as required for this feature.What You Need To Set It Up
Supabase
Needed for PostgreSQL, pgvector, tables, and policies.
Storage
Needed for uploaded documents and file-backed workflows.
OpenAI
Needed for embeddings in the current RAG pipeline.
What Makes This Useful
This setup is useful when you want:- PDF question answering
- internal knowledge assistants
- customer support bots grounded in docs
- contract or policy lookup
- document-aware research workflows
Common Mistakes
- uploading PDFs but never indexing them
- expecting RAG to work without storage
- forgetting that embeddings currently depend on OpenAI
- assuming the model will cite sources if retrieval did not find anything useful
- treating RAG like a guarantee of truth instead of a retrieval aid

