Skip to content

moonmehedi/A-Rag-System-to-Read-Books

Repository files navigation

πŸ€– A-RAG-System-to-Read-Books

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.

🎯 What It Does

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 Stack

Backend (FastAPI + Python)

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

Frontend (Next.js + TypeScript)

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

AI/ML Components

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 Engineering Applied

1. RAG System Prompt

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

2. Context-Aware Responses

  • 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

3. Conversation Memory

  • Chat History Integration: Maintains conversation context across multiple turns
  • Nested Context Handling: Preserves context when creating sub-conversations from selected text

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   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       β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Key Features

πŸ“„ Document Processing Pipeline

  1. PDF Upload β†’ Text Extraction β†’ Chunking β†’ Embedding β†’ Vector Storage
  2. Smart Chunking: Optimized chunk sizes for better context retrieval
  3. Semantic Search: Vector similarity search for relevant document sections

πŸ’¬ Advanced Chat System

  • 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

πŸ” Security & Authentication

  • JWT Authentication: Secure token-based auth
  • Password Hashing: BCrypt encryption
  • User Sessions: Persistent login state
  • API Protection: All endpoints secured with auth middleware

🎨 Modern UI/UX

  • 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

πŸ“ Project Structure

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

βš™οΈ Installation & Setup

Prerequisites

  • 🐍 Python 3.11+
  • 🟒 Node.js 18+
  • 🐳 Docker & Docker Compose
  • πŸ”‘ HuggingFace API Key
  • πŸ”— LangChain API Key

1. Environment Setup

# 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 keys

2. Backend Setup

cd 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 --reload

3. Frontend Setup

cd frontend-nested-chat-interface

# Install dependencies
npm install

# Start development server
npm run dev

4. Access the Application

πŸ”§ Environment Variables

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) ❌

πŸ“ˆ Usage

  1. πŸ” Register/Login: Create an account or sign in
  2. πŸ“„ Upload Document: Upload a PDF file for processing
  3. πŸ’¬ Start Chatting: Ask questions about your document
  4. 🌳 Create Sub-chats: Select text to create focused conversations
  5. πŸ“š Explore Context: Navigate through nested conversation threads

πŸ§ͺ Advanced Features

Nested Conversation Threading

  • 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

RAG Enhancement

  • Semantic Chunking: Intelligent document splitting for optimal retrieval
  • Multi-document Support: Handle multiple PDFs simultaneously
  • Context Ranking: Advanced relevance scoring for document chunks

Real-time Features

  • 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

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • 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

πŸ”— Links


Made with ❀️ by moonmehedi

A research project for improving chatbot context awareness and topic management through nested conversation threading.

Releases

No releases published

Packages

 
 
 

Contributors