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
  • 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
context_manager.py
context_manager.py
Raw Download
Find: Go to:
"""
Context Management Module
Manages conversation context for multi-turn dialogues.

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

from typing import Dict, List, Optional, Any
from datetime import datetime, timedelta
from config import MAX_CONTEXT_HISTORY, CONTEXT_TIMEOUT


class ContextManager:
    """
    Manages conversation context for maintaining state across multiple turns.
    """
    
    def __init__(self, session_id: str = "default"):
        """
        Initialize context manager.
        
        Args:
            session_id: Unique identifier for the conversation session
        """
        self.session_id = session_id
        self.context: Dict[str, Any] = {
            'user_name': None,
            'entities': {},
            'previous_intents': [],
            'conversation_history': [],
            'session_start': datetime.now(),
            'last_activity': datetime.now(),
        }
        self.max_history = MAX_CONTEXT_HISTORY
        self.timeout = CONTEXT_TIMEOUT
    
    def update_context(self, user_message: str, bot_response: str, 
                      intent: Optional[str] = None, entities: Optional[Dict] = None):
        """
        Update context with new conversation turn.
        
        Args:
            user_message: User's input message
            bot_response: Bot's response
            intent: Detected intent
            entities: Extracted entities
        """
        # Update last activity time
        self.context['last_activity'] = datetime.now()
        
        # Add to conversation history
        self.context['conversation_history'].append({
            'user_message': user_message,
            'bot_response': bot_response,
            'timestamp': datetime.now().isoformat(),
            'intent': intent,
            'entities': entities
        })
        
        # Limit history size
        if len(self.context['conversation_history']) > self.max_history:
            self.context['conversation_history'] = \
                self.context['conversation_history'][-self.max_history:]
        
        # Update entities
        if entities:
            self.context['entities'].update(entities)
        
        # Track previous intents
        if intent:
            self.context['previous_intents'].append(intent)
            if len(self.context['previous_intents']) > 5:
                self.context['previous_intents'] = \
                    self.context['previous_intents'][-5:]
        
        # Extract and store user name if mentioned
        if entities and 'PERSON' in entities:
            names = entities['PERSON']
            if names:
                self.context['user_name'] = names[0]
    
    def get_context(self) -> Dict[str, Any]:
        """
        Get current context.
        
        Returns:
            Current context dictionary
        """
        return self.context.copy()
    
    def get_user_name(self) -> Optional[str]:
        """
        Get stored user name from context.
        
        Returns:
            User name if available, None otherwise
        """
        return self.context.get('user_name')
    
    def get_entities(self) -> Dict[str, Any]:
        """
        Get stored entities from context.
        
        Returns:
            Dictionary of stored entities
        """
        return self.context.get('entities', {}).copy()
    
    def get_recent_history(self, count: int = 3) -> List[Dict]:
        """
        Get recent conversation history.
        
        Args:
            count: Number of recent messages to retrieve
            
        Returns:
            List of recent conversation entries
        """
        history = self.context.get('conversation_history', [])
        return history[-count:] if history else []
    
    def get_previous_intent(self) -> Optional[str]:
        """
        Get the most recent intent.
        
        Returns:
            Most recent intent if available, None otherwise
        """
        intents = self.context.get('previous_intents', [])
        return intents[-1] if intents else None
    
    def clear_context(self):
        """Clear all context data."""
        self.context = {
            'user_name': None,
            'entities': {},
            'previous_intents': [],
            'conversation_history': [],
            'session_start': datetime.now(),
            'last_activity': datetime.now(),
        }
    
    def is_context_expired(self) -> bool:
        """
        Check if context has expired based on timeout.
        
        Returns:
            True if context has expired, False otherwise
        """
        last_activity = self.context.get('last_activity')
        if not last_activity:
            return False
        
        if isinstance(last_activity, str):
            last_activity = datetime.fromisoformat(last_activity)
        
        elapsed = (datetime.now() - last_activity).total_seconds()
        return elapsed > self.timeout
    
    def get_context_summary(self) -> str:
        """
        Get a human-readable summary of the current context.
        
        Returns:
            Context summary string
        """
        summary_parts = []
        
        if self.context.get('user_name'):
            summary_parts.append(f"User name: {self.context['user_name']}")
        
        if self.context.get('entities'):
            summary_parts.append(f"Entities: {len(self.context['entities'])} stored")
        
        history_count = len(self.context.get('conversation_history', []))
        summary_parts.append(f"Conversation turns: {history_count}")
        
        return ", ".join(summary_parts) if summary_parts else "No context available"

184 lines•5.8 KB
python

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