class ConversationMemory: def __init__(self): self.sessions = {} def add_to_history(self, session_id: str, user_input: str, response: str): if session_id not in self.sessions: self.sessions[session_id] = [] self.sessions[session_id].append({"user": user_input, "bot": response}) def get_history(self, session_id: str, last_n=5): history = self.sessions.get(session_id, []) return history[-last_n:] def clear_history(self, session_id: str): if session_id in self.sessions: self.sessions[session_id] = [] def summarize_history(self, session_id: str): history = self.get_history(session_id) return "\n".join([f"User: {h['user']}\nBot: {h['bot']}" for h in history])