A sophisticated Retrieval-Augmented Generation (RAG) chatbot system with nested conversation threading for enhanced context awareness and topic management. This project combines modern AI/ML technologies with an intuitive user interface to create an intelligent document Q&A system.
This system enables users to:
- π Upload PDF Documents: Upload and process PDF files for AI-powered Q&A
- π¬ Interactive Chat: Ask questions about uploaded documents with context-aware responses
- π³ Nested Conversations: Create branching conversations from selected text snippets for deeper topic exploration
- π Smart Context Retrieval: Automatically finds relevant document sections to answer questions
- β‘ Real-time Streaming: Get AI responses in real-time with streaming technology
- π₯ User Management: Secure authentication and personalized chat history
- π± Responsive UI: Modern, mobile-friendly interface with shadcn/ui components
| Technology | Purpose | Version |
|---|---|---|
| FastAPI | Web framework | Latest |
| PostgreSQL | Database | 16 |
| SQLAlchemy | ORM | 2.0+ |
| Alembic | Database migrations | Latest |
| LangChain | RAG framework | 0.3+ |
| ChromaDB | Vector database | 1.0+ |
| HuggingFace | Embeddings & LLM | Latest |
| Sentence Transformers | Text embeddings | 5.0+ |
| PyPDF | PDF processing | Latest |
| Passlib + BCrypt | Authentication | Latest |
| PyJWT | JWT tokens | Latest |
| Docker | Containerization | Latest |
| Technology | Purpose | Version |
|---|---|---|
| Next.js | React framework | 15.2+ |
| TypeScript | Type safety | Latest |
| TailwindCSS | Styling | Latest |
| shadcn/ui | UI components | Latest |
| Radix UI | Primitive components | Latest |
| Lucide React | Icons | Latest |
| class-variance-authority | Component variants | Latest |
| Component | Technology | Model/Service |
|---|---|---|
| Embeddings | HuggingFace | sentence-transformers/all-MiniLM-L6-v2 |
| LLM | HuggingFace API | deepseek/deepseek-v3-0324 |
| Vector Store | ChromaDB | In-memory + persistent |
| Text Splitting | LangChain | RecursiveCharacterTextSplitter |
| PDF Processing | PyPDF | Document loading |
prompt = f"""You are a helpful assistant. Use the following context to answer the question:
{context}
Question: {question}"""Engineering Features:
- Context Injection: Relevant document chunks are dynamically inserted
- Clear Role Definition: Assistant role with specific instruction to use provided context
- Structured Format: Clean separation between context and question
- Document-Specific Answers: Responses are grounded in uploaded document content
- Relevance Filtering: Only retrieves most relevant document sections
- Chunk Optimization: Uses 1000 character chunks with 100 character overlap for optimal context
- Chat History Integration: Maintains conversation context across multiple turns
- Nested Context Handling: Preserves context when creating sub-conversations from selected text
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Frontend β β Backend β β AI Services β
β (Next.js) ββββββ (FastAPI) ββββββ (HuggingFace) β
β β β β β β
β β’ Chat UI β β β’ RAG API β β β’ Embeddings β
β β’ PDF Upload β β β’ Auth System β β β’ LLM API β
β β’ Nested Chats β β β’ Vector Store β β β’ Text Models β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β β
β ββββββββββββββββββββ β
ββββββββββββββββ PostgreSQL βββββββββββββββ
β Database β
β β
β β’ Users β
β β’ Chat Messages β
β β’ Sessions β
ββββββββββββββββββββ
- PDF Upload β Text Extraction β Chunking β Embedding β Vector Storage
- Smart Chunking: Optimized chunk sizes for better context retrieval
- Semantic Search: Vector similarity search for relevant document sections
- Nested Conversations: Create sub-chats from selected text snippets
- Context Preservation: Maintains conversation history and document context
- Real-time Streaming: Token-by-token response streaming
- LaTeX Support: Preserves mathematical expressions in responses
- JWT Authentication: Secure token-based auth
- Password Hashing: BCrypt encryption
- User Sessions: Persistent login state
- API Protection: All endpoints secured with auth middleware
- Responsive Design: Works on desktop, tablet, and mobile
- Dark/Light Mode: Theme support with next-themes
- Smooth Animations: Framer Motion-powered transitions
- Accessibility: ARIA-compliant components
A-Rag-System-to-Read-Books/
βββ π chatbot_backend_FastAPI/
β βββ π backend/
β βββ π app/
β β βββ π api/routes/ # API endpoints
β β βββ π core/ # Configuration
β β βββ π db/ # Database setup
β β βββ π models/ # SQLAlchemy models
β β βββ π schemas/ # Pydantic schemas
β βββ π alembic/ # Database migrations
β βββ π³ docker-compose.yml # PostgreSQL container
β βββ π requirements.txt # Python dependencies
βββ π frontend-nested-chat-interface/
β βββ π app/ # Next.js app router
β βββ π components/ # React components
β βββ π hooks/ # Custom React hooks
β βββ π lib/ # Utility functions
β βββ π package.json # Node.js dependencies
βββ π .env # Environment variables
βββ π .env.example # Environment template
βββ π ENVIRONMENT_SETUP.md # Setup guide
βββ π README.md # This file
- π Python 3.11+
- π’ Node.js 18+
- π³ Docker & Docker Compose
- π HuggingFace API Key
- π LangChain API Key
# Clone the repository
git clone https://github.com/moonmehedi/A-Rag-System-to-Read-Books.git
cd A-Rag-System-to-Read-Books
# Copy and configure environment variables
cp .env.example .env
# Edit .env with your API keyscd chatbot_backend_FastAPI/backend
# Start PostgreSQL database
docker-compose up -d
# Create virtual environment
python -m venv venv
source venv/bin/activate # Linux/Mac
# venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Run database migrations
alembic upgrade head
# Start FastAPI server
uvicorn app.main:app --reloadcd frontend-nested-chat-interface
# Install dependencies
npm install
# Start development server
npm run dev- π Frontend: http://localhost:3000
- π§ Backend API: http://localhost:8000
- π API Docs: http://localhost:8000/docs
| Variable | Description | Required |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | β |
HUGGINGFACE_TOKEN |
HuggingFace API token | β |
LANGCHAIN_API_KEY |
LangChain API key | β |
JWT_SECRET_KEY |
JWT signing secret | β |
GITHUB_TOKEN |
GitHub API token (optional) | β |
- π Register/Login: Create an account or sign in
- π Upload Document: Upload a PDF file for processing
- π¬ Start Chatting: Ask questions about your document
- π³ Create Sub-chats: Select text to create focused conversations
- π Explore Context: Navigate through nested conversation threads
- Text Selection: Highlight any text in responses to create sub-conversations
- Context Inheritance: Sub-chats maintain parent conversation context
- Visual Navigation: Tree-like interface for exploring conversation branches
- Semantic Chunking: Intelligent document splitting for optimal retrieval
- Multi-document Support: Handle multiple PDFs simultaneously
- Context Ranking: Advanced relevance scoring for document chunks
- Streaming Responses: See AI responses as they're generated
- Live Document Processing: Real-time PDF upload and indexing
- Dynamic Context Updates: Context awareness updates in real-time
- 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
This project is licensed under the MIT License - see the LICENSE file for details.
- LangChain: For the powerful RAG framework
- HuggingFace: For state-of-the-art AI models
- shadcn/ui: For beautiful, accessible UI components
- FastAPI: For the lightning-fast backend framework
- Next.js: For the modern React framework
- π Homepage: GitHub Repository
- π Documentation: Setup Guide
- π Issues: Report Bugs
- π‘ Features: Request Features
Made with β€οΈ by moonmehedi
A research project for improving chatbot context awareness and topic management through nested conversation threading.