-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
74 lines (58 loc) · 2.41 KB
/
chat.py
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
67
68
69
70
71
72
73
74
from robai.schemas import ChatMessage, AIMessage, SystemMessage
from typing import Union, List
from abc import ABC, abstractmethod
import uuid
class HistoryManager(ABC):
def __init__(self, robot_name):
self.robot_name = robot_name
self.conversation_id = str(uuid.uuid4())
self.conversation_history = []
def create_message(self, role: str, content: str):
if role == "user":
return self.create_chat_message(content)
elif role == "assistant":
return self.create_ai_message(content)
elif role == "system":
return self.create_system_message(content)
else:
raise ValueError(f"Role {role} not recognized")
def create_chat_message(self, content: str) -> ChatMessage:
return ChatMessage(role="user", robot=self.robot_name, content=content)
def create_ai_message(self, content: str) -> AIMessage:
return AIMessage(role="assistant", robot=self.robot_name, content=content)
def create_system_message(self, content: str) -> SystemMessage:
return SystemMessage(role="system", robot=self.robot_name, content=content)
@abstractmethod
async def save_message_to_history(
self, user_id: int, message: Union[ChatMessage, AIMessage, SystemMessage]
):
pass
@abstractmethod
async def get_history(self, conversation_id: str, user_id: int, limit: int = 10):
pass
# @abstractmethod
async def get_latest_conversation_id(self, user_id: int):
pass
# @abstractmethod
async def get_latest_conversation(self, user_id: int, limit: int = 10):
pass
class LocalHistoryManager(HistoryManager):
def save_message_to_history(
self, message: Union[ChatMessage, AIMessage, SystemMessage]
):
self.conversation_history.append(message)
def get_history(
self, _slice: slice
) -> List[Union[ChatMessage, AIMessage, SystemMessage]]:
history = self.conversation_history[_slice]
return history
def get_history_string(self, _slice: slice):
history = self.get_history(_slice)
return ["\n".join(message.model_dump_json()) for message in history]
def get_history_count(self, _slice: slice):
count = 0
for message in self.get_history(_slice):
count += message.token_count()
return count
def get_last_message(self):
return self.conversation_history[-1]