Retain system role message in cache file

This commit is contained in:
Igor Mikushkin
2025-04-11 09:40:09 +01:00
committed by GitHub
parent 005d4fc8fb
commit b7cad0bd85
+3 -7
View File
@@ -71,13 +71,9 @@ class ChatSession:
def _write(self, messages: List[Dict[str, str]], chat_id: str) -> None:
file_path = self.storage_path / chat_id
if len(messages) > self.length:
message_role_item = messages[0]
json.dump(
[message_role_item] + messages[-self.length :], file_path.open("w")
)
return
json.dump(messages[-self.length :], file_path.open("w"))
# Retain the first message since it defines the role
truncated_messages = messages[:1] + messages[1 + max(0, len(messages) - self.length):]
json.dump(truncated_messages, file_path.open("w"))
def invalidate(self, chat_id: str) -> None:
file_path = self.storage_path / chat_id