File size: 776 Bytes
8fb51c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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])