import streamlit as st import random from datetime import datetime # Mock ChatAgent class to replace the backend dependency class MockChatAgent: def __init__(self): # Sample order data self.orders = { "ORD123": {"status": "Shipped", "date": "2023-11-15", "items": ["Laptop", "Mouse"], "total": "$1200"}, "ORD456": {"status": "Processing", "date": "2023-11-20", "items": ["Keyboard", "Headphones"], "total": "$150"}, "ORD789": {"status": "Delivered", "date": "2023-11-10", "items": ["Monitor", "USB Cable"], "total": "$350"}, } # Predefined responses for common queries self.responses = [ "I'm checking your order information now...", "Let me look that up for you...", "Based on our records...", "According to the system...", "I can help you with that...", ] def query(self, prompt): # Simple keyword-based response system prompt_lower = prompt.lower() # Check for order number pattern for order_id, order_info in self.orders.items(): if order_id.lower() in prompt_lower: response = f"Order {order_id}: Status is {order_info['status']}. " response += f"Ordered on {order_info['date']}. Items: {', '.join(order_info['items'])}. " response += f"Total: {order_info['total']}." return response # Check for status queries if "status" in prompt_lower or "track" in prompt_lower: return "To check your order status, please provide your order number (e.g., ORD123)." # Check for delivery queries if "delivery" in prompt_lower or "deliver" in prompt_lower: return "Delivery times vary based on your location. Most orders arrive within 3-5 business days after shipping." # Check for return queries if "return" in prompt_lower: return "You can return items within 30 days of delivery. Please visit our returns page to initiate a return." # Default response return f"{random.choice(self.responses)} I'm a mock agent, so I can only provide limited information about orders. Try asking about order status, delivery, or returns, or provide an order number like ORD123." # Initialize session state if 'messages' not in st.session_state: st.session_state.messages = [] if 'agent' not in st.session_state: st.session_state.agent = MockChatAgent() # Page config st.set_page_config(page_title="AI Agent Chatbot", page_icon="🤖") # Title st.title("🤖 AI Agent Chatbot") st.markdown("Ask me about your orders! (This is a demo with mock data)") # Display chat messages for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) # Chat input if prompt := st.chat_input("Ask about your orders..."): # Add user message st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt) # Show loading state with st.chat_message("assistant"): thinking_placeholder = st.empty() thinking_placeholder.markdown("Thinking...") try: # Get response from agent response = st.session_state.agent.query(prompt) # Replace loading with response thinking_placeholder.markdown(response) # Update session state st.session_state.messages.append({"role": "assistant", "content": response}) except Exception as e: # Show error thinking_placeholder.markdown(f"❌ Sorry, I encountered an error: {str(e)}") st.session_state.messages.append({"role": "assistant", "content": f"❌ Error: {str(e)}"}) # Sidebar for clearing chat with st.sidebar: st.title("Settings") if st.button("Clear Chat"): st.session_state.messages = [] st.session_state.agent = MockChatAgent() # Reinitialize agent with clean memory st.rerun() st.markdown("---") st.markdown(f"**Messages:** {len(st.session_state.messages)}") st.markdown(f"**Agent Status:** Ready") st.markdown("---") st.markdown("**Sample Order Numbers:**") st.markdown("- ORD123") st.markdown("- ORD456") st.markdown("- ORD789")