diff --git a/sgpt/handlers/chat_handler.py b/sgpt/handlers/chat_handler.py index 6ba0a18d..e24559cd 100644 --- a/sgpt/handlers/chat_handler.py +++ b/sgpt/handlers/chat_handler.py @@ -71,7 +71,9 @@ def _read(self, chat_id: str) -> List[Dict[str, str]]: def _write(self, messages: List[Dict[str, str]], chat_id: str) -> None: file_path = self.storage_path / chat_id - 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