A modern, web-based Retrieval-Augmented Generation (RAG) chatbot that provides answers with source attribution. Built with Next.js frontend and FastAPI backend, this application allows users to upload documents and ask questions with transparent source references.
- 🤖 Intelligent Q&A: Ask questions and get accurate answers from your documents
- 📚 Source Attribution: View the exact sources used to generate each answer
- 📤 Document Upload: Support for PDF and TXT files
- 💬 Modern Chat Interface: Clean, responsive UI with real-time interactions
- 📊 Statistics Dashboard: Track indexed documents and collection stats
- 🎨 Responsive Design: Works seamlessly on desktop, tablet, and mobile devices
- RAG stats view
- RAG upload view
- RAG chat view
- FastAPI: High-performance Python web framework
- LangChain: Framework for LLM applications
- ChromaDB: Vector database for document embeddings
- Google Gemini: Language model for answer generation
- HuggingFace Embeddings: Sentence transformers for document vectorization
- Next.js 14: React framework with App Router
- TypeScript: Type-safe development
- Tailwind CSS: Modern, responsive styling
- Lucide React: Beautiful icon library
- Axios: HTTP client for API communication
- Python 3.8 or higher
- Node.js 18 or higher
- Google AI (Gemini) API key
- Git
git clone <repository-url>
cd RAG# Navigate to backend directory
cd backend
# Create a virtual environment
python -m venv venv
# Activate virtual environment
# On Linux/Mac:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Create .env file from example
cp .env.example .env
# Edit .env and add your Google AI (Gemini) API key
# GOOGLE_API_KEY=your_api_key_here# Navigate to frontend directory (from project root)
cd frontend
# Install dependencies
npm install
# Environment variables are already set in .env.local# From backend directory with activated virtual environment
cd backend
source venv/bin/activate # or venv\Scripts\activate on Windows
python main.pyThe backend will start at http://localhost:8000
# From frontend directory in a new terminal
cd frontend
npm run devThe frontend will start at http://localhost:3000
- Navigate to the Upload tab
- Click "Choose a file" or drag and drop a PDF or TXT file
- Click "Upload Document"
- Wait for the document to be processed and indexed
- Navigate to the Chat tab
- Type your question in the input field
- Press Enter or click "Send"
- View the answer along with the sources used
- Sources are automatically displayed in the right panel after each query
- Each source shows:
- Document name and page number (if applicable)
- Relevance score
- Text excerpt used for the answer
- Navigate to the Stats tab
- View total indexed documents
- See collection information
- Clear all documents if needed (with confirmation)
Query the RAG system with a question.
Request Body:
{
"question": "What is RAG?",
"k": 4
}Response:
{
"answer": "RAG stands for...",
"sources": [
{
"id": 1,
"content": "...",
"metadata": {"source": "doc.pdf", "page": 1},
"relevance_score": 0.95
}
],
"success": true
}Upload and index a document.
Request: Multipart form data with file
Response:
{
"success": true,
"message": "Successfully indexed 10 chunks",
"filename": "document.pdf",
"chunks": 10
}Get collection statistics.
Response:
{
"total_documents": 25,
"collection_name": "rag_collection",
"embedding_model": "sentence-transformers/all-MiniLM-L6-v2"
}Clear all documents from the collection.
Response:
{
"success": true,
"message": "Collection cleared successfully"
}Health check endpoint.
Response:
{
"status": "healthy",
"service": "RAG Chatbot API"
}# Gemini is the only supported provider
LLM_PROVIDER=google
# Supply your Google AI (Gemini) API key
GOOGLE_API_KEY=your_google_gemini_api_key_here
EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
# Optional override. Default: gemini-pro
LLM_MODEL=gemini-2.5-pro
CHROMA_DB_PATH=./chroma_db# Use /api when running via the built-in Next.js proxy (Codespaces, localhost)
NEXT_PUBLIC_API_URL=/api
# Or point directly at the FastAPI instance if proxying is disabled
# NEXT_PUBLIC_API_URL=http://localhost:8000RAG/
├── backend/
│ ├── main.py # FastAPI application
│ ├── rag_service.py # RAG logic and vector store
│ ├── requirements.txt # Python dependencies
│ ├── .env.example # Environment variables template
│ └── .gitignore
│
├── frontend/
│ ├── app/
│ │ ├── layout.tsx # Root layout
│ │ ├── page.tsx # Main page with tabs
│ │ └── globals.css # Global styles
│ ├── components/
│ │ ├── ChatInterface.tsx # Chat UI component
│ │ ├── MessageBubble.tsx # Message display
│ │ ├── SourceCard.tsx # Source display
│ │ ├── DocumentUpload.tsx # Upload interface
│ │ └── StatsPanel.tsx # Statistics display
│ ├── lib/
│ │ └── api.ts # API client
│ ├── package.json
│ ├── tsconfig.json
│ ├── tailwind.config.js
│ └── next.config.js
│
└── README.md
Error: Module not found
# Make sure virtual environment is activated and dependencies installed
pip install -r requirements.txtError: Gemini API key not found
# Check .env file exists and contains GOOGLE_API_KEY
cat backend/.envError: ChromaDB initialization failed
# Delete existing database and restart
rm -rf backend/chroma_db
python backend/main.pyError: Cannot connect to backend
- Ensure backend is running on port 8000
- Check CORS settings in backend/main.py
- Verify NEXT_PUBLIC_API_URL in frontend/.env.local
Error: Module not found
# Reinstall dependencies
cd frontend
rm -rf node_modules package-lock.json
npm install- Backend: Add endpoints in
main.pyand logic inrag_service.py - Frontend: Create components in
components/and add API calls inlib/api.ts
# Backend (from backend directory)
python -m pytest tests/
# Frontend (from frontend directory)
npm run test# Backend - use production ASGI server
pip install gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker
# Frontend
npm run build
npm run start- Chunking: Adjust
chunk_sizeandchunk_overlapinrag_service.py - Retrieval: Modify
kparameter for number of retrieved documents - Embeddings: Use different embedding models for better accuracy
- Caching: Implement Redis for query caching
- Store API keys securely (never commit .env files)
- Implement authentication for production deployments
- Validate and sanitize file uploads
- Rate limit API endpoints
- Use HTTPS in production
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
MIT License - feel free to use this project for personal or commercial purposes.
For issues and questions:
- Open an issue on GitHub
- Check existing documentation
- Review API documentation
- Google for Gemini models
- LangChain for the RAG framework
- Vercel for Next.js
- ChromaDB for vector storage
Built with ❤️ using Next.js, FastAPI, and LangChain