RAG AI Agent Backend
July 20, 2026 ยท View on GitHub
A minimal, production-ready FastAPI backend demonstrating Retrieval-Augmented Generation (RAG) with vector similarity search. Built for educational purposes and easy frontend integration.
๐น Full YouTube Guide: Youtube link
๐ X Post: X link
๐ก Try the RAG AI Agent: App link
โ๏ธ Buy me a coffee: Cafe Latte
๐ค๏ธ Discord: Invite link
๐ฏ Features
- FastAPI backend with automatic API documentation
- Supabase integration with pgvector for vector similarity search
- Multi-AI Provider support (OpenAI & Anthropic)
- Vector embeddings with semantic search
- Citation-based answers with source tracking
- Frontend-ready architecture for NextJS integration
- Docker containerization for easy deployment
๐๏ธ Architecture
yt-rag/
โโโ app/
โ โโโ core/ # Infrastructure (config, database)
โ โโโ models/ # Pydantic data models
โ โโโ services/ # Business logic (RAG, embeddings)
โ โโโ main.py # FastAPI application
โโโ sql/
โ โโโ init_supabase.sql # Database initialization script
โโโ requirements.txt
๐ Quick Start Guide
Complete setup from clone to asking questions in ~10 minutes
Prerequisites
- Python 3.11+
- Supabase account
- OpenAI API key
- Anthropic API key (optional, for Claude)
Step 1: Clone and Install Dependencies
# Clone the repository
git clone https://github.com/ShenSeanChen/yt-rag.git
cd yt-rag
# Create virtual environment
python3.11 -m venv venv_yt_rag
source venv_yt_rag/bin/activate # On Windows: venv_yt_rag\Scripts\activate
# Install dependencies
pip install -r requirements.txt
Step 2: Get API Keys (5 minutes)
Supabase Setup:
- Go to supabase.com and create a new project
- Wait for project to be ready (~2 minutes)
- Go to Settings โ API and copy:
- Project URL (e.g.,
https://abc123.supabase.co) - Anon public key (starts with
eyJ...) - Service role secret key (starts with
eyJ...)
- Project URL (e.g.,
OpenAI Setup:
- Go to platform.openai.com
- Create account/sign in โ API Keys โ Create new key
- Copy the key (starts with
sk-...)
If you meet issues accessing overseas services due to the lack of a Visa or Mastercard, please check out the alternative version of this repository. That version uses Chinese APIs to ensure the project runs smoothly (Supabase is still required, but the free tier fully covers this project).
Step 3: Configure Environment
# Copy environment template
cp .env.example .env
# Edit with your real API keys
nano .env # or use your preferred editor
Update .env with your values:
# Supabase Configuration
SUPABASE_URL=https://your-project-ref.supabase.co
SUPABASE_ANON_KEY=your_anon_key_here
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key_here
# OpenAI Configuration (using latest models)
OPENAI_API_KEY=sk-your_openai_key_here
OPENAI_EMBED_MODEL=text-embedding-3-large
OPENAI_CHAT_MODEL=gpt-4o
# AI Provider
AI_PROVIDER=openai
# Optional: Anthropic
ANTHROPIC_API_KEY=your_anthropic_key_here
ANTHROPIC_CHAT_MODEL=claude-3-5-sonnet-20241022
Step 4: Initialize Database (2 minutes)
- Open Supabase Dashboard โ SQL Editor
- Click "New query"
- Copy entire contents of
sql/init_supabase.sql - Paste and click "Run"
โ This creates everything needed:
- pgvector extension
rag_chunkstable with VECTOR(3072) for latest embeddings- Performance indexes
- Vector search functions
- RLS policies for future auth
Step 5: Test Setup (Optional)
# Test your complete setup
python test_setup.py
This verifies:
- โ Dependencies installed
- โ API keys configured
- โ Database connected
- โ Schema initialized
- โ RAG pipeline working
Step 6: Start the Server
uvicorn main:app --reload --port 8000
The API will be available at:
- API: http://localhost:8000
- Interactive Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
๐ API Usage
Health Check
curl http://localhost:8000/healthz
Seed Knowledge Base
# Seed with default documents
curl -X POST http://localhost:8000/seed
# Or seed with custom documents
curl -X POST http://localhost:8000/seed \
-H "Content-Type: application/json" \
-d '{
"docs": [
{
"chunk_id": "policy_returns_v1#window",
"source": "https://help.example.com/returns",
"text": "You can return unworn items within 30 days of purchase..."
}
]
}'
Ask Questions (RAG)
curl -X POST http://localhost:8000/answer \
-H "Content-Type: application/json" \
-d '{
"query": "Can I return shoes after 30 days?",
"top_k": 6
}'
Example Response:
{
"text": "Based on our return policy, you can return unworn shoes within 30 days of purchase [policy_returns_v1#window]. Items must be in original condition...",
"citations": ["policy_returns_v1#window", "policy_returns_v1#conditions"],
"debug": {
"top_doc_ids": ["policy_returns_v1#window", "policy_returns_v1#conditions"],
"latency_ms": 1250
}
}
๐ง Configuration Options
AI Providers
OpenAI (Recommended)
AI_PROVIDER=openai
OPENAI_API_KEY=your_key
OPENAI_EMBED_MODEL=text-embedding-3-small # 1536 dimensions
OPENAI_CHAT_MODEL=gpt-4o-mini
Anthropic Claude
AI_PROVIDER=anthropic
ANTHROPIC_API_KEY=your_key
ANTHROPIC_CHAT_MODEL=claude-3-haiku-20240307
# Note: Still need OpenAI key for embeddings
OPENAI_API_KEY=your_openai_key
RAG Parameters
Adjust in app/core/config.py:
chunk_size: Token limit per chunk (default: 400)chunk_overlap: Overlap between chunks (default: 60 tokens)default_top_k: Number of chunks to retrieve (default: 6)temperature: LLM creativity (default: 0.1)
๐ณ Docker Deployment
# Build image
docker build -t yt-rag .
# Run container
docker run -p 8080:8080 --env-file .env yt-rag
๐ฎ NextJS Frontend Integration
This backend is designed for seamless frontend integration:
Frontend Setup (NextJS)
// lib/supabase.js
import { createClient } from '@supabase/supabase-js'
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
)
// API calls to your backend
const response = await fetch('http://localhost:8000/answer', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: 'user question' })
})
Future Auth Integration When adding Google authentication:
- Enable Google Auth in Supabase
- The RLS policies are already configured
- Frontend and backend will share the same Supabase client
- No backend changes needed!
๐ Project Structure
yt-rag/
โโโ app/
โ โโโ core/
โ โ โโโ config.py # Environment & settings
โ โ โโโ database.py # Supabase client & operations
โ โโโ models/
โ โ โโโ requests.py # API request schemas
โ โ โโโ responses.py # API response schemas
โ โ โโโ entities.py # Database entities
โ โโโ services/
โ โ โโโ embedding.py # AI provider abstraction
โ โ โโโ rag.py # RAG pipeline logic
โ โ โโโ chunker.py # Text processing utilities
โ โโโ main.py # FastAPI app & routes
โโโ sql/
โ โโโ init_supabase.sql # Database setup script
โโโ .env.example # Environment template
โโโ requirements.txt # Python dependencies
โโโ Dockerfile # Container configuration
โโโ README.md # This file
๐ ๏ธ Development
Running Tests
# Install dev dependencies
pip install pytest pytest-asyncio httpx
# Run tests (coming soon)
pytest
Code Quality
# Format code
black app/
isort app/
# Lint code
flake8 app/
๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐โโ๏ธ Support
- ๐ Documentation: Check the
/docsendpoint when running - ๐ Issues: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
Built with โค๏ธ for the developer community
This project demonstrates modern RAG architecture patterns and is perfect for learning, prototyping, or building production applications.
๐ Support / Sponsors
If my open-source work saves you time, consider becoming a sponsor. It keeps these repos maintained, documented, and free.