Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure first chat message is retained to prevent "Could not determine chat role" error (#633) #634

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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