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
conversational-ai-bot
RSK World
conversational-ai-bot
Conversational AI Bot - Python + NLP + Flask + Machine Learning + Chatbot + AI
conversational-ai-bot
  • __pycache__
  • static
  • templates
  • .gitignore577 B
  • ADVANCED_FEATURES.md5.7 KB
  • CHANGELOG.md2.2 KB
  • INSTALLATION.md1.8 KB
  • LICENSE1.2 KB
  • PROJECT_INFO.md2.8 KB
  • PROJECT_STATUS.md3.4 KB
  • QUICKSTART.md2.5 KB
  • README.md4.8 KB
  • __init__.py448 B
  • api_integrations.py6 KB
  • app.py4.2 KB
  • chatbot.py14.8 KB
  • config.py1.1 KB
  • context_manager.py5.8 KB
  • conversation_analytics.py5.9 KB
  • conversation_history.json413 B
  • conversation_history.py4.9 KB
  • entity_extractor.py6.6 KB
  • example_usage.py4.3 KB
  • intent_recognizer.py6.6 KB
  • language_support.py5 KB
  • main.py4.7 KB
  • requirements.txt311 B
  • response_templates.py7.2 KB
  • sentiment_analyzer.py5.6 KB
  • setup.py1.6 KB
  • test_chatbot.py5 KB
  • validate_project.py4.1 KB
.gitignorechatbot.pyapp.pylogging.pyPROJECT_STATUS.md
.gitignore
Raw Download
Find: Go to:
# Conversational AI Bot - Git Ignore
# Developer: RSK World (https://rskworld.in)
# Email: help@rskworld.in
# Phone: +91 93305 39277
# Year: 2026

# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Virtual Environment
venv/
env/
ENV/
.venv

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# Conversation History
conversation_history.json
*.log

# OS
.DS_Store
Thumbs.db

# Project specific
*.zip
*.png

54 lines•577 B
text
chatbot.py
Raw Download
Find: Go to:
"""
Conversational AI Bot - Main Chatbot Class
Advanced conversational chatbot with context management and multi-turn dialogue support.

Developer: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Year: 2026
"""

from typing import Optional, Dict
import re
from datetime import datetime
import uuid

from context_manager import ContextManager
from intent_recognizer import IntentRecognizer
from entity_extractor import EntityExtractor
from conversation_history import ConversationHistory
from sentiment_analyzer import SentimentAnalyzer
from language_support import LanguageSupport
from api_integrations import APIIntegrations
from conversation_analytics import ConversationAnalytics
from response_templates import ResponseTemplates
from config import (
    BOT_NAME, DEFAULT_RESPONSE, INTENT_CONFIDENCE_THRESHOLD,
    DEVELOPER_NAME, DEVELOPER_WEBSITE, DEVELOPER_EMAIL, DEVELOPER_PHONE, YEAR
)


class ConversationalAIBot:
    """
    Main conversational AI bot class with context awareness and multi-turn dialogue support.
    """
    
    def __init__(self, session_id: Optional[str] = None):
        """
        Initialize the conversational AI bot.
        
        Args:
            session_id: Optional session identifier for conversation tracking
        """
        self.session_id = session_id or str(uuid.uuid4())
        self.context_manager = ContextManager(self.session_id)
        self.intent_recognizer = IntentRecognizer()
        self.entity_extractor = EntityExtractor()
        self.conversation_history = ConversationHistory(self.session_id)
        
        # Advanced features
        self.sentiment_analyzer = SentimentAnalyzer()
        self.language_support = LanguageSupport()
        self.api_integrations = APIIntegrations()
        self.analytics = ConversationAnalytics()
        self.response_templates = ResponseTemplates()
        
        # Initialize analytics
        self.analytics.start_session(self.session_id)
        
        # Initialize bot with greeting
        self._initialize_bot()
    
    def _initialize_bot(self):
        """Initialize the bot with default settings."""
        print(f"Initializing {BOT_NAME}...")
        print(f"Session ID: {self.session_id}")
        print(f"Developer: {DEVELOPER_NAME} ({DEVELOPER_WEBSITE})")
        print("-" * 50)
    
    def chat(self, user_message: str) -> str:
        """
        Process user message and generate response.
        
        Args:
            user_message: User's input message
            
        Returns:
            Bot's response
        """
        if not user_message or not user_message.strip():
            return "I didn't receive any message. Please try again."
        
        # Check if context has expired
        if self.context_manager.is_context_expired():
            self.context_manager.clear_context()
            self.conversation_history.clear_history()
        
        # Detect language
        detected_language = self.language_support.detect_language(user_message)
        self.language_support.set_language(detected_language)
        
        # Analyze sentiment
        sentiment_analysis = self.sentiment_analyzer.analyze(user_message)
        sentiment = sentiment_analysis['sentiment']
        
        # Recognize intent
        intent, confidence = self.intent_recognizer.recognize(user_message)
        
        # Extract entities
        entities = self.entity_extractor.extract(user_message)
        
        # Generate response based on intent and context
        response = self._generate_response(user_message, intent, confidence, entities, sentiment_analysis)
        
        # Update context
        self.context_manager.update_context(user_message, response, intent, entities)
        
        # Save to conversation history
        self.conversation_history.add_message(user_message, response, intent, entities)
        
        # Track analytics
        self.analytics.track_message(
            self.session_id, intent, entities, sentiment, detected_language
        )
        
        return response
    
    def _generate_response(self, user_message: str, intent: str, 
                          confidence: float, entities: Dict, sentiment_analysis: Dict = None) -> str:
        """
        Generate response based on intent, context, and entities.
        
        Args:
            user_message: User's message
            intent: Detected intent
            confidence: Intent confidence score
            entities: Extracted entities
            
        Returns:
            Generated response
        """
        user_message_lower = user_message.lower()
        
        # Handle low confidence intents
        if confidence < INTENT_CONFIDENCE_THRESHOLD:
            return self._handle_unknown_intent(user_message)
        
        # Check for API-related queries
        if 'joke' in user_message_lower or 'tell me a joke' in user_message_lower:
            joke_result = self.api_integrations.get_joke()
            if joke_result.get('success'):
                joke = joke_result.get('joke', {})
                return f"{joke.get('setup', '')}\n{joke.get('punchline', '')}"
        
        if 'quote' in user_message_lower or 'inspiration' in user_message_lower:
            quote_result = self.api_integrations.get_quote()
            if quote_result.get('success'):
                quote = quote_result.get('quote', {})
                return f'"{quote.get("text", "")}" - {quote.get("author", "")}'
        
        # Check for calculation requests
        if any(op in user_message for op in ['+', '-', '*', '/', '=']):
            calc_match = self._extract_calculation(user_message)
            if calc_match:
                calc_result = self.api_integrations.calculate(calc_match)
                if calc_result.get('success'):
                    return f"The answer is {calc_result.get('result')}"
        
        # Handle specific intents
        if intent == 'greeting':
            return self._handle_greeting(entities)
        
        elif intent == 'goodbye':
            return self._handle_goodbye()
        
        elif intent == 'name_introduction':
            return self._handle_name_introduction(entities)
        
        elif intent == 'name_query':
            return self._handle_name_query()
        
        elif intent == 'question':
            return self._handle_question(user_message, entities)
        
        elif intent == 'help':
            return self._handle_help()
        
        elif intent == 'weather':
            return self._handle_weather(entities)
        
        elif intent == 'time':
            return self._handle_time()
        
        elif intent == 'date':
            return self._handle_date()
        
        elif intent == 'compliment':
            return self._handle_compliment()
        
        else:
            return self._handle_unknown_intent(user_message, sentiment_analysis)
    
    def _handle_greeting(self, entities: Dict) -> str:
        """Handle greeting intent."""
        user_name = self.context_manager.get_user_name()
        
        if user_name:
            return self.response_templates.get_response('greeting_with_name', name=user_name)
        else:
            return self.response_templates.get_response('greeting')
    
    def _handle_goodbye(self) -> str:
        """Handle goodbye intent."""
        user_name = self.context_manager.get_user_name()
        
        if user_name:
            return self.response_templates.get_response('goodbye_with_name', name=user_name)
        else:
            return self.response_templates.get_response('goodbye')
    
    def _handle_name_introduction(self, entities: Dict) -> str:
        """Handle name introduction intent."""
        if 'PERSON' in entities and entities['PERSON']:
            name = entities['PERSON'][0]
            return self.response_templates.get_response('name_introduction', name=name)
        else:
            return self.response_templates.get_response('name_not_found')
    
    def _handle_name_query(self) -> str:
        """Handle name query intent."""
        user_name = self.context_manager.get_user_name()
        
        if user_name:
            return self.response_templates.get_response('name_query', name=user_name)
        else:
            return self.response_templates.get_response('name_not_found')
    
    def _handle_question(self, user_message: str, entities: Dict) -> str:
        """Handle question intent."""
        user_message_lower = user_message.lower()
        
        # Check for specific question types
        if 'what can you do' in user_message_lower or 'what do you do' in user_message_lower:
            return self._handle_help()
        
        if 'how are you' in user_message_lower:
            return "I'm doing well, thank you for asking! I'm here to help you with conversations. How can I assist you?"
        
        if 'who are you' in user_message_lower or 'what are you' in user_message_lower:
            return f"I'm {BOT_NAME}, an advanced conversational AI bot. I can help you with various tasks and have natural conversations. I'm developed by {DEVELOPER_NAME}."
        
        # Generic question response
        return "That's an interesting question! I'm still learning, but I'll do my best to help. Could you provide more details?"
    
    def _handle_help(self) -> str:
        """Handle help intent."""
        return self.response_templates.get_response('help')
    
    def _handle_weather(self, entities: Dict) -> str:
        """Handle weather intent."""
        location = None
        if 'LOCATION' in entities and entities['LOCATION']:
            location = entities['LOCATION'][0]
        
        if location:
            weather_data = self.api_integrations.get_weather(location)
            if weather_data.get('success'):
                return f"Weather for {location}: {weather_data.get('temperature')}, {weather_data.get('condition')}"
            return self.response_templates.get_response('weather', location=location)
        else:
            return "I'd be happy to help with weather information! Could you tell me which location you're interested in?"
    
    def _handle_time(self) -> str:
        """Handle time query intent."""
        current_time = datetime.now().strftime("%I:%M %p")
        return self.response_templates.get_response('time', time=current_time)
    
    def _handle_date(self) -> str:
        """Handle date query intent."""
        current_date = datetime.now().strftime("%B %d, %Y")
        return self.response_templates.get_response('date', date=current_date)
    
    def _handle_compliment(self) -> str:
        """Handle compliment intent."""
        return self.response_templates.get_response('compliment')
    
    def _handle_unknown_intent(self, user_message: str, sentiment_analysis: Dict = None) -> str:
        """Handle unknown or low-confidence intents."""
        # Try to use context to provide a better response
        recent_history = self.context_manager.get_recent_history(1)
        
        # Adjust response based on sentiment
        if sentiment_analysis and sentiment_analysis.get('sentiment') == 'negative':
            return "I sense you might be frustrated. I'm here to help! Could you rephrase your question or tell me what you need?"
        
        if recent_history:
            # Reference previous conversation
            return self.response_templates.get_response('unknown')
        else:
            return self.response_templates.get_response('unknown')
    
    def _extract_calculation(self, text: str) -> Optional[str]:
        """Extract mathematical expression from text."""
        # Simple pattern for basic calculations
        pattern = r'(\d+(?:\.\d+)?)\s*([+\-*/])\s*(\d+(?:\.\d+)?)'
        match = re.search(pattern, text)
        if match:
            return f"{match.group(1)}{match.group(2)}{match.group(3)}"
        return None
    
    def get_context_summary(self) -> str:
        """
        Get a summary of the current conversation context.
        
        Returns:
            Context summary string
        """
        return self.context_manager.get_context_summary()
    
    def get_conversation_history(self, limit: Optional[int] = None):
        """
        Get conversation history.
        
        Args:
            limit: Maximum number of entries to return
            
        Returns:
            List of conversation entries
        """
        return self.conversation_history.get_history(limit)
    
    def clear_session(self):
        """Clear current session context and history."""
        self.context_manager.clear_context()
        self.conversation_history.clear_history()
        self.analytics.end_session(self.session_id)
        print("Session cleared. Starting fresh conversation.")
    
    def get_analytics(self) -> Dict:
        """
        Get conversation analytics.
        
        Returns:
            Dictionary with analytics data
        """
        return self.analytics.get_metrics()
    
    def get_analytics_summary(self) -> str:
        """
        Get human-readable analytics summary.
        
        Returns:
            Formatted analytics summary
        """
        return self.analytics.get_summary()
    
    def get_sentiment_analysis(self, text: str) -> Dict:
        """
        Analyze sentiment of text.
        
        Args:
            text: Text to analyze
            
        Returns:
            Sentiment analysis results
        """
        return self.sentiment_analyzer.analyze(text)
    
    def set_language(self, language_code: str) -> bool:
        """
        Set conversation language.
        
        Args:
            language_code: Language code (e.g., 'en', 'es', 'hi')
            
        Returns:
            True if language is supported, False otherwise
        """
        return self.language_support.set_language(language_code)
    
    def get_current_language(self) -> str:
        """Get current language code."""
        return self.language_support.get_language()


if __name__ == "__main__":
    # Example usage
    bot = ConversationalAIBot()
    
    print("\n" + "="*50)
    print("Conversational AI Bot - Interactive Mode")
    print("Type 'quit' or 'exit' to end the conversation")
    print("="*50 + "\n")
    
    while True:
        user_input = input("You: ").strip()
        
        if user_input.lower() in ['quit', 'exit', 'bye']:
            print(f"\nBot: {bot.chat(user_input)}")
            break
        
        if user_input:
            response = bot.chat(user_input)
            print(f"Bot: {response}\n")

396 lines•14.8 KB
python
app.py
Raw Download
Find: Go to:
"""
Flask Web Interface for Conversational AI Bot
Provides a web-based interface for the chatbot.

Developer: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Year: 2026
"""

from flask import Flask, render_template, request, jsonify, session
from chatbot import ConversationalAIBot
import uuid
import os

app = Flask(__name__)
app.secret_key = os.urandom(24)

# Store bot instances per session
bot_instances = {}


def get_bot():
    """Get or create bot instance for current session."""
    if 'session_id' not in session:
        session['session_id'] = str(uuid.uuid4())
    
    session_id = session['session_id']
    
    if session_id not in bot_instances:
        bot_instances[session_id] = ConversationalAIBot(session_id)
    
    return bot_instances[session_id]


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


@app.route('/api/chat', methods=['POST'])
def chat():
    """Handle chat API requests."""
    try:
        data = request.get_json()
        user_message = data.get('message', '').strip()
        
        if not user_message:
            return jsonify({
                'success': False,
                'error': 'Message is required'
            }), 400
        
        bot = get_bot()
        response = bot.chat(user_message)
        
        # Get additional metadata
        sentiment = bot.get_sentiment_analysis(user_message)
        context = bot.get_context_summary()
        
        return jsonify({
            'success': True,
            'response': response,
            'sentiment': sentiment.get('sentiment'),
            'sentiment_score': sentiment.get('score'),
            'context': context
        })
    
    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


@app.route('/api/history', methods=['GET'])
def get_history():
    """Get conversation history."""
    try:
        bot = get_bot()
        limit = request.args.get('limit', 10, type=int)
        history = bot.get_conversation_history(limit=limit)
        
        return jsonify({
            'success': True,
            'history': history
        })
    
    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


@app.route('/api/analytics', methods=['GET'])
def get_analytics():
    """Get conversation analytics."""
    try:
        bot = get_bot()
        analytics = bot.get_analytics()
        summary = bot.get_analytics_summary()
        
        return jsonify({
            'success': True,
            'analytics': analytics,
            'summary': summary
        })
    
    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


@app.route('/api/clear', methods=['POST'])
def clear_session():
    """Clear conversation session."""
    try:
        bot = get_bot()
        bot.clear_session()
        
        return jsonify({
            'success': True,
            'message': 'Session cleared successfully'
        })
    
    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


@app.route('/api/language', methods=['POST'])
def set_language():
    """Set conversation language."""
    try:
        data = request.get_json()
        language_code = data.get('language', 'en')
        
        bot = get_bot()
        success = bot.set_language(language_code)
        
        return jsonify({
            'success': success,
            'language': bot.get_current_language(),
            'message': f'Language set to {language_code}' if success else 'Invalid language code'
        })
    
    except Exception as e:
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


if __name__ == '__main__':
    # Create templates directory if it doesn't exist
    os.makedirs('templates', exist_ok=True)
    os.makedirs('static', exist_ok=True)
    
    app.run(debug=True, host='0.0.0.0', port=5000)

168 lines•4.2 KB
python
PROJECT_STATUS.md
Raw Download

PROJECT_STATUS.md

# Project Status Report

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

## Project Validation Status

✅ **ALL CHECKS PASSED**

### File Structure
- ✅ All required Python modules present
- ✅ All documentation files present
- ✅ Configuration files present
- ✅ Required directories (templates/, static/) present

### Code Quality
- ✅ All Python files have valid syntax
- ✅ All modules can be imported successfully
- ✅ No linter errors detected
- ✅ All imports are working correctly

### Features Status
- ✅ Core chatbot functionality
- ✅ Context management
- ✅ Intent recognition
- ✅ Entity extraction
- ✅ Conversation history
- ✅ Sentiment analysis
- ✅ Multi-language support
- ✅ API integrations
- ✅ Conversation analytics
- ✅ Response templates
- ✅ Web interface (Flask)
- ✅ CLI interface

### Documentation
- ✅ README.md - Main documentation
- ✅ QUICKSTART.md - Quick start guide
- ✅ INSTALLATION.md - Installation instructions
- ✅ ADVANCED_FEATURES.md - Advanced features documentation
- ✅ PROJECT_INFO.md - Project information
- ✅ CHANGELOG.md - Version history
- ✅ LICENSE - MIT License

### Developer Information
- ✅ All files include developer information
- ✅ Contact details in all files
- ✅ Website reference included

## Project Files Summary

### Core Modules (6 files)
1. `chatbot.py` - Main chatbot class
2. `context_manager.py` - Context management
3. `intent_recognizer.py` - Intent recognition
4. `entity_extractor.py` - Entity extraction
5. `conversation_history.py` - History management
6. `config.py` - Configuration

### Advanced Modules (5 files)
1. `sentiment_analyzer.py` - Sentiment analysis
2. `language_support.py` - Multi-language support
3. `api_integrations.py` - API integrations
4. `conversation_analytics.py` - Analytics
5. `response_templates.py` - Response templates

### Interface Files (3 files)
1. `main.py` - CLI interface
2. `app.py` - Flask web interface
3. `templates/index.html` - Web UI

### Documentation (7 files)
1. `README.md`
2. `QUICKSTART.md`
3. `INSTALLATION.md`
4. `ADVANCED_FEATURES.md`
5. `PROJECT_INFO.md`
6. `CHANGELOG.md`
7. `LICENSE`

### Configuration & Setup (4 files)
1. `requirements.txt` - Dependencies
2. `setup.py` - Installation script
3. `.gitignore` - Git ignore rules
4. `__init__.py` - Package initialization

### Utilities (3 files)
1. `example_usage.py` - Usage examples
2. `test_chatbot.py` - Test suite
3. `validate_project.py` - Validation script

### Directories
- `templates/` - Web templates
- `static/` - Static files

## Total Files: 28+ files

## Testing

Run validation:
```bash
python validate_project.py
```

Run tests:
```bash
python test_chatbot.py
```

## Usage

### CLI Mode
```bash
python main.py
```

### Web Mode
```bash
python app.py
```
Then open: http://localhost:5000

## Dependencies

All dependencies listed in `requirements.txt`:
- numpy>=1.21.0
- scikit-learn>=1.0.0
- nltk>=3.6
- spacy>=3.4.0
- python-dateutil>=2.8.2
- colorama>=0.4.4
- setuptools>=65.0.0
- flask>=2.3.0
- requests>=2.28.0

## Conclusion

✅ **Project is complete and ready for use!**

All files are validated, no errors found, and all features are implemented.

---

© 2026 RSK World. All rights reserved.

🚀 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