Skip to content

Commit

Permalink
Ensure first chat message is retained to prevent "Could not determine…
Browse files Browse the repository at this point in the history
… chat role" error (#633)
  • Loading branch information
igrmk committed Oct 12, 2024
1 parent aac2f54 commit 49e6f61
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sgpt/handlers/chat_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 49e6f61

Please sign in to comment.