Your production-grade RAG chatbot has been built and is ready to use.
RAG Chatbot/
├── app.py # FastAPI application with endpoints & UI
├── ingest.py # Document ingestion pipeline
├── rag.py # RAG retrieval and answer generation
├── test_rag.py # Test script
├── requirements.txt # Python dependencies
├── README.md # Complete documentation
├── QUICKSTART.md # 5-minute quick start guide
├── .env.example # Environment variable template
├── .gitignore # Git ignore rules
└── docs/ # Sample documents folder
├── sample_product_guide.md
├── company_policies.txt
└── technical_documentation.md
- ✅ Reads PDF, TXT, and MD files from
/docsfolder - ✅ Intelligent text chunking (500-1000 characters with 100 char overlap)
- ✅ Creates embeddings using OpenAI
text-embedding-3-small - ✅ Stores in ChromaDB with metadata
- ✅ Comprehensive error handling and logging
- ✅ Batch processing for efficiency
- ✅
retrieve_and_answer(question)function - ✅ Semantic search using vector embeddings
- ✅ Top-K retrieval (default: 5 chunks)
- ✅ GPT-4o-mini for answer generation
- ✅ System prompt: "Use ONLY provided context"
- ✅ Returns answer + context used
- ✅ Proper error handling
- ✅
GET /- Beautiful HTML/JS chat interface - ✅
GET /health- Health check endpoint - ✅
POST /ask- Question answering endpoint - ✅ Request validation with Pydantic
- ✅ Proper error handling and logging
- ✅ CORS support
- ✅ OpenAPI documentation (auto-generated)
- ✅ Modern, responsive design
- ✅ Real-time chat interface
- ✅ Loading indicators
- ✅ Source attribution for answers
- ✅ Error handling with user-friendly messages
- ✅ Gradient background with smooth animations
- ✅ Automated test suite
- ✅ Tests multiple question types
- ✅ Shows answers with context
- ✅ Verifies ChromaDB connection
- ✅ Detailed output formatting
- ✅ README.md - Complete guide with installation, usage, API docs, troubleshooting
- ✅ QUICKSTART.md - 5-minute setup guide
- ✅ Inline code comments throughout
- ✅ Configuration documentation
- ✅ sample_product_guide.md - SmartHome Hub guide (IoT product)
- ✅ company_policies.txt - Corporate policies document
- ✅ technical_documentation.md - API documentation
# 1. Install dependencies
pip install -r requirements.txt
# 2. Set up API key
echo "OPENAI_API_KEY=your-key-here" > .env
# 3. Ingest documents
python ingest.py
# 4. Run the server
uvicorn app:app --reload
# 5. Open browser to http://localhost:8000See QUICKSTART.md for detailed instructions.
- Python + FastAPI + ChromaDB + OpenAI
- Single command run:
uvicorn app:app --reload - Document ingestion from
/docsfolder - Chunking (500-1000 characters)
- OpenAI embeddings (text-embedding-3-small)
- ChromaDB local storage
- Retrieval + RAG pipeline
- GPT-4o-mini for answers
- Context-only answers (system prompt enforced)
- API endpoints (GET /, POST /ask)
- Returns answer + context
- Beautiful HTML/JS chat UI
- Test script
- Comprehensive documentation
- Error handling throughout
- Logging for debugging
- Health check endpoint
- API documentation (auto-generated)
- Sample documents
- .gitignore and .env.example
- Quick start guide
- Smart chunking with sentence boundaries
- Batch embedding creation
- Proper exception handling
- Pydantic models for validation
- Rate limit awareness
- Metadata tracking
- Source attribution
- Context preview in responses
- Configurable parameters
- Environment variable management
The included sample documents cover diverse content types:
-
Product Documentation (SmartHome Hub)
- Setup instructions
- Features and specifications
- Troubleshooting
- FAQs
-
Corporate Policies (TechCorp)
- HR policies
- Benefits information
- Procedures
- Compensation details
-
Technical API Docs (CloudSync API)
- API endpoints
- Authentication
- Code examples
- Best practices
- Open http://localhost:8000
- Try the example questions in QUICKSTART.md
- Verify answers include sources
- Test error handling (ask unrelated questions)
python test_rag.py# Health check
curl http://localhost:8000/health
# Ask a question
curl -X POST http://localhost:8000/ask \
-H "Content-Type: application/json" \
-d '{"question": "What is the SmartHome Hub?"}'CHUNK_SIZE: 750 (adjustable 500-1000)CHUNK_OVERLAP: 100EMBEDDING_MODEL: text-embedding-3-small
TOP_K: 5 (number of chunks to retrieve)CHAT_MODEL: gpt-4o-miniTEMPERATURE: 0.3 (for factual responses)
Using sample documents (~122 chunks):
Initial Ingestion:
- ~$0.05 for embeddings (one-time)
Per Query:
- ~$0.001 per question (embedding + GPT-4o-mini)
For 1000 questions:
- ~$1.00 total
- Smart boundary detection (sentences, words)
- Overlap for context preservation
- Configurable size and overlap
- Handles edge cases
- Batch processing for efficiency
- Error recovery for failed batches
- Uses cost-effective text-embedding-3-small
- Semantic similarity search
- Top-K selection
- Metadata preserved for attribution
- Distance scores available
- Strict system prompt
- Context injection
- Low temperature for accuracy
- Token limit management
- Add more document types (DOCX, HTML)
- Implement document update detection
- Add conversation history
- Implement caching for common queries
- Streaming responses
- Multi-language support
- User authentication
- Query analytics
- A/B testing different prompts
- Fine-tune chunk size per document type
- Docker containerization
- Cloud deployment (AWS/GCP/Azure)
- Database for user management
- Rate limiting
- Monitoring and alerts
- Backup strategy
- Single Language: English only (easily extensible)
- Local Storage: ChromaDB stored locally (not multi-user)
- No Auth: Open API (add auth for production)
- Synchronous: Blocking operations (consider async for scale)
- ✅ API key stored in .env (not in code)
- ✅ .env in .gitignore
- ✅ Input validation with Pydantic
⚠️ No user authentication (add for production)⚠️ No rate limiting (add for production)
- ✅ Clean, readable code
- ✅ Comprehensive comments
- ✅ Type hints where appropriate
- ✅ Error handling throughout
- ✅ Logging for debugging
- ✅ No linter errors
- ✅ Follows Python best practices
- ✅ Modular architecture
You now have a complete, production-grade RAG chatbot that:
- ✅ Ingests documents automatically
- ✅ Creates semantic embeddings
- ✅ Retrieves relevant context
- ✅ Generates accurate answers
- ✅ Provides a beautiful UI
- ✅ Exposes a REST API
- ✅ Includes comprehensive docs
- ✅ Works out of the box
Total Files Created: 12
Lines of Code: ~1,500+
Time to First Answer: 5 minutes (following quick start)
# Install and run in 4 commands:
pip install -r requirements.txt
echo "OPENAI_API_KEY=your-key-here" > .env
python ingest.py
uvicorn app:app --reloadThen open http://localhost:8000 and start chatting!
Built with ❤️ - Ready for production use with proper security measures