help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
rag-chatbot
RSK World
rag-chatbot
RAG Chatbot - Python + LangChain + ChromaDB + OpenAI API + Vector Search + Knowledge Base
rag-chatbot
  • __pycache__
  • analytics
  • conversations
  • knowledge_base
  • static
  • templates
  • vector_db
  • .env.example502 B
  • .gitignore519 B
  • ADVANCED_FEATURES.md5.2 KB
  • GITHUB_PUSH_SUMMARY.md3.6 KB
  • ISSUES_FIXED.md3.3 KB
  • LICENSE1.2 KB
  • PROJECT_INFO.md3 KB
  • QUICKSTART.md1.5 KB
  • README.md3.9 KB
  • RELEASE_NOTES.md3.7 KB
  • analytics.py6.9 KB
  • app.py8.3 KB
  • chatbot.py10.8 KB
  • config.py1.8 KB
  • conversation_manager.py5.8 KB
  • embeddings.py1.9 KB
  • hybrid_search.py4 KB
  • prepare_knowledge_base.py6.8 KB
  • requirements.txt377 B
  • setup.py2.8 KB
  • vector_store.py6.7 KB
README.mdapp.pyindex.htmlISSUES_FIXED.md
README.md
Raw Download

README.md

# RAG Chatbot

<!--
Project: RAG Chatbot
Developer: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Year: 2026
Description: Retrieval-Augmented Generation chatbot with knowledge base integration
-->

Retrieval-Augmented Generation chatbot with knowledge base integration. This chatbot uses RAG (Retrieval-Augmented Generation) architecture to provide accurate answers from a knowledge base. Perfect for building chatbots with domain-specific knowledge.

## Features

### Core Features
- Knowledge base integration with ChromaDB
- Vector similarity search
- Context retrieval from knowledge base
- Accurate responses using RAG architecture
- Domain-specific knowledge support

### Advanced Features
- **Conversation History**: Maintains context across multiple messages
- **Streaming Responses**: Real-time streaming of LLM responses
- **Hybrid Search**: Combines vector similarity with keyword matching
- **File Upload**: Upload documents directly through the web interface
- **Analytics Dashboard**: Track queries, sessions, response times, and feedback
- **Feedback System**: Thumbs up/down for responses
- **Chat Export**: Export conversations as JSON
- **Session Management**: Multiple concurrent sessions
- **Response Time Tracking**: Monitor performance metrics

## Technologies

- LangChain
- Vector DB (ChromaDB)
- Python
- OpenAI API
- Embeddings

## Installation

1. Clone the repository
2. Install dependencies:
```bash
pip install -r requirements.txt
```

3. Set up environment variables:
```bash
cp .env.example .env
# Edit .env and add your OpenAI API key
```

4. Prepare your knowledge base:
```bash
python prepare_knowledge_base.py
```

5. Run the application:
```bash
python app.py
```

## Usage

1. Start the Flask server
2. Open your browser and navigate to `http://localhost:5000`
3. Enter your questions in the chat interface
4. The chatbot will retrieve relevant context from the knowledge base and generate accurate responses

### Advanced Features Usage

- **Streaming Mode**: Toggle streaming on/off in the chat header
- **Hybrid Search**: Enable hybrid search for better results combining semantic and keyword search
- **Upload Documents**: Click the upload button to add new documents to the knowledge base
- **View Analytics**: Click the analytics button to see statistics and insights
- **Export Chat**: Click export to download your conversation as JSON
- **Feedback**: Use thumbs up/down buttons on responses to provide feedback

## Project Structure

```
rag-chatbot/
├── app.py # Flask application with advanced endpoints
├── chatbot.py # RAG chatbot implementation
├── vector_store.py # Vector database operations
├── embeddings.py # Embedding utilities
├── conversation_manager.py # Conversation history management
├── analytics.py # Analytics and statistics tracking
├── hybrid_search.py # Hybrid search implementation
├── prepare_knowledge_base.py # Knowledge base preparation
├── config.py # Configuration settings
├── setup.py # Setup script
├── templates/
│ └── index.html # Web interface with advanced UI
├── static/
│ ├── css/
│ │ └── style.css # Styles with modal and advanced UI
│ └── js/
│ └── app.js # Frontend JavaScript with all features
├── knowledge_base/ # Knowledge base documents
├── vector_db/ # Vector database storage
├── conversations/ # Conversation history storage
├── analytics/ # Analytics data storage
└── requirements.txt # Python dependencies
```

## License

© 2026 RSK World - https://rskworld.in

app.py
Raw Download
Find: Go to:
"""
RAG Chatbot - Flask Application
Project: RAG Chatbot
Developer: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Year: 2026
Description: Flask web application for the RAG chatbot
"""

from flask import Flask, render_template, request, jsonify, Response
from chatbot import RAGChatbot
from analytics import Analytics
from conversation_manager import ConversationManager
import os
import json
import uuid
from werkzeug.utils import secure_filename
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Initialize Flask app
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB max file size
app.config['UPLOAD_FOLDER'] = './knowledge_base'

# Initialize chatbot
try:
    chatbot = RAGChatbot()
    chatbot_initialized = True
except Exception as e:
    print(f"Error initializing chatbot: {str(e)}")
    chatbot = None
    chatbot_initialized = False

# Initialize analytics
analytics = Analytics()

# Allowed file extensions
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'md', 'docx'}


@app.route('/')
def index():
    """
    Render the main chat interface.
    """
    return render_template('index.html')


def allowed_file(filename):
    """Check if file extension is allowed."""
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/api/chat', methods=['POST'])
def chat():
    """
    Handle chat requests with conversation history.
    """
    if not chatbot_initialized:
        return jsonify({
            "answer": "Chatbot is not initialized. Please check your configuration.",
            "sources": [],
            "success": False
        }), 500
    
    try:
        data = request.get_json()
        question = data.get('question', '').strip()
        session_id = data.get('session_id', 'default')
        use_hybrid = data.get('use_hybrid', False)
        include_history = data.get('include_history', True)
        
        if not question:
            return jsonify({
                "answer": "Please provide a question.",
                "sources": [],
                "success": False
            }), 400
        
        # Record session
        if session_id == 'default':
            session_id = str(uuid.uuid4())
            analytics.record_session()
        
        # Get response from chatbot
        response = chatbot.chat(question, session_id, use_hybrid, include_history)
        
        # Record analytics
        analytics.record_query(question, response.get('response_time'), response.get('sources', []))
        
        response['session_id'] = session_id
        return jsonify(response), 200
        
    except Exception as e:
        analytics.record_error()
        return jsonify({
            "answer": f"An error occurred: {str(e)}",
            "sources": [],
            "success": False
        }), 500


@app.route('/api/chat/stream', methods=['POST'])
def chat_stream():
    """
    Handle streaming chat requests.
    """
    if not chatbot_initialized:
        return jsonify({"error": "Chatbot not initialized"}), 500
    
    try:
        data = request.get_json()
        question = data.get('question', '').strip()
        session_id = data.get('session_id', 'default')
        
        if not question:
            return jsonify({"error": "Please provide a question"}), 400
        
        def generate():
            for chunk in chatbot.stream_chat(question, session_id):
                yield f"data: {json.dumps({'chunk': chunk})}\n\n"
            yield "data: [DONE]\n\n"
        
        return Response(generate(), mimetype='text/event-stream')
        
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@app.route('/api/health', methods=['GET'])
def health():
    """
    Health check endpoint.
    """
    return jsonify({
        "status": "healthy" if chatbot_initialized else "unhealthy",
        "initialized": chatbot_initialized
    }), 200


@app.route('/api/conversation/<session_id>', methods=['GET'])
def get_conversation(session_id):
    """
    Get conversation history for a session.
    """
    try:
        history = chatbot.conversation_manager.get_conversation_history(session_id)
        return jsonify({"history": history}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@app.route('/api/conversation/<session_id>', methods=['DELETE'])
def clear_conversation(session_id):
    """
    Clear conversation history for a session.
    """
    try:
        chatbot.conversation_manager.clear_conversation(session_id)
        return jsonify({"success": True}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@app.route('/api/conversation/<session_id>/export', methods=['GET'])
def export_conversation(session_id):
    """
    Export conversation as JSON.
    """
    try:
        conversation_data = chatbot.conversation_manager.export_conversation(session_id)
        return jsonify(conversation_data), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@app.route('/api/upload', methods=['POST'])
def upload_document():
    """
    Upload and process a document to add to knowledge base.
    """
    if not chatbot_initialized:
        return jsonify({"error": "Chatbot not initialized"}), 500
    
    try:
        if 'file' not in request.files:
            return jsonify({"error": "No file provided"}), 400
        
        file = request.files['file']
        if file.filename == '':
            return jsonify({"error": "No file selected"}), 400
        
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            file.save(filepath)
            
            # Process and add to knowledge base
            # Load the uploaded file
            try:
                from langchain_community.document_loaders import PyPDFLoader, TextLoader
            except ImportError:
                # Fallback for older LangChain versions
                from langchain.document_loaders import PyPDFLoader, TextLoader
            
            if filename.endswith('.pdf'):
                loader = PyPDFLoader(filepath)
            else:
                loader = TextLoader(filepath)
            
            documents = loader.load()
            chatbot.add_knowledge(documents)
            
            return jsonify({
                "success": True,
                "message": f"Document '{filename}' added to knowledge base",
                "chunks": len(documents)
            }), 200
        else:
            return jsonify({"error": "File type not allowed"}), 400
            
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@app.route('/api/analytics', methods=['GET'])
def get_analytics():
    """
    Get analytics and statistics.
    """
    try:
        days = request.args.get('days', 30, type=int)
        stats = analytics.get_stats(days)
        return jsonify(stats), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@app.route('/api/feedback', methods=['POST'])
def submit_feedback():
    """
    Submit feedback for a response.
    """
    try:
        data = request.get_json()
        positive = data.get('positive', True)
        analytics.record_feedback(positive)
        return jsonify({"success": True}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


@app.route('/api/knowledge-base/stats', methods=['GET'])
def knowledge_base_stats():
    """
    Get knowledge base statistics.
    """
    if not chatbot_initialized:
        return jsonify({"error": "Chatbot not initialized"}), 500
    
    try:
        count = chatbot.vector_store_manager.get_collection_count()
        return jsonify({
            "document_count": count,
            "vector_db_path": chatbot.vector_store_manager.persist_directory
        }), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


if __name__ == '__main__':
    port = int(os.getenv('PORT', 5000))
    debug = os.getenv('DEBUG', 'False').lower() == 'true'
    app.run(host='0.0.0.0', port=port, debug=debug)

281 lines•8.3 KB
python
ISSUES_FIXED.md
Raw Download

ISSUES_FIXED.md

# Issues Found and Fixed

<!--
Project: RAG Chatbot
Developer: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Year: 2026
-->

## Issues Identified and Resolved

### 1. Unused Imports ✅ FIXED
- **File**: `chatbot.py`
- **Issue**: Unused import `ConversationBufferMemory` from langchain.memory
- **Fix**: Removed the unused import

### 2. Unused Imports ✅ FIXED
- **File**: `app.py`
- **Issue**: Unused imports `send_file` and `Path`
- **Fix**: Removed unused imports

### 3. Vector Store Collection Count ✅ FIXED
- **File**: `vector_store.py`
- **Issue**: `get_collection_count()` method used private attribute `_collection` which might not work in all LangChain versions
- **Fix**: Added multiple fallback methods to get collection count safely:
- Try `_collection.count()`
- Try `collection.count()`
- Fallback to similarity search with empty query

### 4. Streaming Implementation ✅ FIXED
- **File**: `chatbot.py`
- **Issue**: Streaming might fail if chat_history is empty string
- **Fix**: Added fallback value "No previous conversation." when chat_history is empty

### 5. File Upload Import Handling ✅ FIXED
- **File**: `app.py`
- **Issue**: Import statements for document loaders not properly handled with fallback
- **Fix**: Added proper try/except for langchain_community imports with fallback to langchain

### 6. Missing Directories ✅ FIXED
- **Issue**: Required directories for conversations, analytics, and vector_db might not exist
- **Fix**: Created directories:
- `conversations/` - for conversation history storage
- `analytics/` - for analytics data storage
- `vector_db/` - for vector database storage

### 7. Git Ignore Updates ✅ FIXED
- **File**: `.gitignore`
- **Issue**: Missing entries for conversations and analytics directories
- **Fix**: Added `conversations/` and `analytics/` to .gitignore

## Verification

### Syntax Check ✅
- All Python files compiled successfully with `py_compile`
- No syntax errors found

### Linter Check ✅
- All files passed linter checks
- No linting errors

### Import Structure ✅
- All imports are properly structured
- Fallback imports added where needed for compatibility

## Remaining Notes

1. **Dependencies**: The project requires dependencies to be installed via `pip install -r requirements.txt`
2. **Environment Variables**: Make sure `.env` file is created with `OPENAI_API_KEY`
3. **Knowledge Base**: Run `python prepare_knowledge_base.py` to initialize the knowledge base

## Files Verified

✅ `app.py` - Flask application
✅ `chatbot.py` - RAG chatbot implementation
✅ `vector_store.py` - Vector database operations
✅ `embeddings.py` - Embedding utilities
✅ `conversation_manager.py` - Conversation history
✅ `analytics.py` - Analytics tracking
✅ `hybrid_search.py` - Hybrid search
✅ `prepare_knowledge_base.py` - Knowledge base preparation
✅ `config.py` - Configuration
✅ `setup.py` - Setup script
✅ `templates/index.html` - Web interface
✅ `static/css/style.css` - Styles
✅ `static/js/app.js` - Frontend JavaScript

## All Issues Resolved ✅

The project is now ready for use. All identified issues have been fixed and the codebase is clean and functional.

© 2026 RSK World - https://rskworld.in

🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer