YouTube RAG System
A production-style Retrieval-Augmented Generation (RAG) system that enables semantic question-answering over YouTube videos using vector search and LLMs.
This project demonstrates clean separation of ingestion and serving, official API integration, vector database usage (pgvector), production deployment practices, and infrastructure debugging.
Interactive Demo UI: https://youtube-rag-backend-1roe.onrender.com/ui
Deployed API: https://youtube-rag-backend-1roe.onrender.com/docs
Pre-indexed demo video: https://www.youtube.com/watch?v=osKyvYJ3PRM
Given a YouTube video and a question, the system:
- Retrieves relevant transcript chunks using vector similarity search
- Constructs a grounded context
- Generates an answer strictly based on retrieved content
- Returns structured metadata and timestamp links
Local Ingestion Layer (ETL) ↓ Supabase (PostgreSQL + pgvector) ↓ Render (Stateless Query API) ↓ OpenAI (Answer Generation)
Separation of Responsibilities
Runs locally to avoid YouTube cloud IP restrictions.
Flow:
- Extract video_id from URL
- Validate video using YouTube Data API v3
- Fetch official metadata
- Fetch transcript locally
- Chunk transcript with token overlap
- Generate embeddings
- Store metadata + vectors in Supabase
Stateless API that:
- Accepts user question
- Generates query embedding
- Performs pgvector similarity search
- Retrieves top relevant transcript chunks
- Constructs grounded prompt
- Calls LLM for final answer
- Returns structured response
- Production does not perform ingestion.
1️⃣ Ingestion Disabled in Production
- Prevents abuse
- Avoids cost spikes
- Avoids YouTube cloud IP blocking
- Keeps production stateless
2️⃣ Transaction Pooler for Supabase
- Render requires IPv4-compatible DB connections.
- Using Supabase transaction pooler ensures reliable connectivity.
3️⃣ Vector Search via pgvector
- Cosine similarity (<=>)
- HNSW indexing for performance
- Thresholding to avoid irrelevant LLM calls
4️⃣ Rate Limiting (10 requests/minute)
Prevents:
- API abuse
- OpenAI cost explosions
- Accidental load testing
5️⃣ Retrieval Thresholding
- Prevents calling the LLM if similarity is too low.
- This reduces hallucinations and improves grounding.
videos table Column Description id YouTube video ID (Primary Key) title Video title description Video description channel Channel name published_at Publish timestamp views View count
video_chunks_with_indx table Column Description video_id Foreign key → videos.id chunk_index Order within transcript content Transcript text chunk token_count Token size start_time Chunk start timestamp end_time Chunk end timestamp embedding pgvector embedding
{ "answer": "...", "confidence": "medium", "similarity_score": 0.71, "video": { "video_id": "osKyvYJ3PRM", "title": "Everything You Need To Know About Large Language Models (LLMs)", "channel": "Matthew Berman" }, "top_match": { "start": 0.16, "end": 260.639, "youtube_link": "https://www.youtube.com/watch?v=osKyvYJ3PRM&t=0s" } }
Debug mode optionally returns retrieved chunks.
- Python
- FastAPI
- Supabase (PostgreSQL + pgvector)
- OpenAI Embeddings (text-embedding-3-small)
- YouTube Data API v3
- youtube-transcript-api (local only)
- Render (deployment)
- SlowAPI (rate limiting)
1️ Install dependencies pip install -r backend/requirements.txt
2️ Environment variables OPENAI_API_KEY=your_key YOUTUBE_API_KEY=your_key DATABASE_URL=your_supabase_url ENVIRONMENT=local
3 Run ingestion POST /ingest
4 Start API uvicorn backend.app:app --reload
- Hosted on Render
- Supabase used as managed vector database
- Ingestion disabled in production (ENVIRONMENT=production)
- Rate limiting enabled
- Stateless query-only API
- Environment-based route control
- Rate limiting
- Retrieval relevance threshold
- Structured response format
- Cloud IPv6 issue resolved via transaction pooler
- Foreign key integrity
- Clean error handling
- Authentication for ingestion
- Hybrid search (vector + keyword)
- Multi-video querying
- Caching layer
- Frontend UI
- Background ingestion queue
This project demonstrates:
- Real-world RAG architecture
- Vector database integration
- LLM grounding strategies
- Cloud deployment debugging
- API hardening
- Infrastructure-level problem solving (IPv4/IPv6 networking issue)
- Clean separation of ETL and serving layers