-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathsession_manager.py
More file actions
66 lines (50 loc) · 2.24 KB
/
Copy pathsession_manager.py
File metadata and controls
66 lines (50 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from dataclasses import dataclass
from typing import Dict, List, Optional
@dataclass
class Message:
"""Represents a single message in a conversation"""
role: str # "user" or "assistant"
content: str # The message content
class SessionManager:
"""Manages conversation sessions and message history"""
def __init__(self, max_history: int = 5):
self.max_history = max_history
self.sessions: Dict[str, List[Message]] = {}
self.session_counter = 0
def create_session(self) -> str:
"""Create a new conversation session"""
self.session_counter += 1
session_id = f"session_{self.session_counter}"
self.sessions[session_id] = []
return session_id
def add_message(self, session_id: str, role: str, content: str):
"""Add a message to the conversation history"""
if session_id not in self.sessions:
self.sessions[session_id] = []
message = Message(role=role, content=content)
self.sessions[session_id].append(message)
# Keep conversation history within limits
if len(self.sessions[session_id]) > self.max_history * 2:
self.sessions[session_id] = self.sessions[session_id][
-self.max_history * 2 :
]
def add_exchange(self, session_id: str, user_message: str, assistant_message: str):
"""Add a complete question-answer exchange"""
self.add_message(session_id, "user", user_message)
self.add_message(session_id, "assistant", assistant_message)
def get_conversation_history(self, session_id: Optional[str]) -> Optional[str]:
"""Get formatted conversation history for a session"""
if not session_id or session_id not in self.sessions:
return None
messages = self.sessions[session_id]
if not messages:
return None
# Format messages for context
formatted_messages = []
for msg in messages:
formatted_messages.append(f"{msg.role.title()}: {msg.content}")
return "\n".join(formatted_messages)
def clear_session(self, session_id: str):
"""Clear all messages from a session"""
if session_id in self.sessions:
self.sessions[session_id] = []