A production-grade Retrieval-Augmented Generation (RAG) assistant for students and engineers studying technical textbooks, syllabi, and research papers.
- Hybrid Search: Combines Dense Vector Embeddings (pgvector) with Sparse Full-Text Search (tsvector) via Reciprocal Rank Fusion (RRF)
- PDF Citation Highlighting: Click any citation to jump directly to the referenced page in the embedded PDF viewer
- Streaming Responses: Real-time LLM output with Server-Sent Events (SSE)
- Math Support: LaTeX rendering via KaTeX for technical documents
- Multi-Provider: Supports OpenAI and Anthropic LLMs
- Frontend: Next.js 14, TypeScript, Tailwind CSS, shadcn/ui, react-pdf
- Backend: FastAPI, SQLAlchemy Async, Pydantic v2, PyMuPDF
- Database: PostgreSQL 16 + pgvector extension
- AI: OpenAI/Anthropic LLMs, text-embedding-3-small
- Docker & Docker Compose
- OpenAI API key
git clone <repo-url>
cd rag-assistantCreate a .env file in the project root:
OPENAI_API_KEY=sk-your-openai-keydocker-compose up --buildThis starts:
- PostgreSQL with pgvector on port 5432
- FastAPI backend on port 8000
- Next.js frontend on port 3000
- Frontend: http://localhost:3000
- API Docs: http://localhost:8000/docs
cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# Start PostgreSQL with pgvector
docker run -d --name pgvector -e POSTGRES_PASSWORD=postgres -p 5432:5432 ankane/pgvector:latest
# Configure environment
cp .env.example .env
# Edit .env with your OPENAI_API_KEY
# Run server
uvicorn app.main:app --reloadcd frontend
npm install
npm run dev- Register/Login at http://localhost:3000/login
- Upload PDFs on the Documents page
- Chat with your documents - ask questions, request summaries, or explore topics
- Click citations to jump to referenced pages in the PDF viewer
User Query
|
v
[Embedding] ---> [Dense Search (pgvector HNSW)] ---+
| |
v v
[tsvector Query] ---> [Sparse Search (Full-Text)] +---> [RRF Fusion] ---> [Top-K Chunks]
|
v
[LLM Prompt with Context]
|
v
[Streaming Response + Citations]
users- Authenticationdocuments- PDF metadatadocument_chunks- Text chunks with embeddings and tsvectorchat_sessions- Conversation threadschat_messages- Messages with citation tracking
| Endpoint | Method | Description |
|---|---|---|
/api/v1/auth/register |
POST | Create account |
/api/v1/auth/login |
POST | Get access token |
/api/v1/documents/upload |
POST | Upload PDF |
/api/v1/documents/ |
GET | List documents |
/api/v1/chat/completions |
POST | Chat with streaming |
/api/v1/search/ |
POST | Hybrid search |
MIT