From 3c656fe3be575f83c3bffbe2d90f96bb480ebd58 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 24 Jul 2023 19:08:51 -0300 Subject: [PATCH] aider: Ensure the head ends with an assistant message. # Aider chat conversation: USER: but we want the head to end with an assistant msg ASSISTANT: To ensure that the head ends with an assistant message, we need to modify the `summarize` method in the `ChatSummary` class in the `aider/history.py` file. After splitting the messages based on the token count, we need to check if the last message in the head is from the assistant. If not, we need to move messages from the head to the tail until we find an assistant message. Here is the *edit block* for this change: ```python aider/history.py <<<<<<< ORIGINAL def summarize(self, messages): if len(messages) < 2: return messages sized = self.tokenize(messages) total = sum(tokens for tokens, _msg in sized) if total <= self.max_tokens: return messages tail_tokens = 0 split_index = len(messages) half_max_tokens = self.max_tokens // 2 # Iterate over the messages in reverse order for i in range(len(sized)-1, -1, -1): tokens, _msg = sized[i] if tail_tokens + tokens < half_max_tokens: tail_tokens += tokens split_index = i else: break head = messages[:split_index] tail = messages[split_index:] ======= def summarize(self, messages): if len(messages) < 2: return messages sized = self.tokenize(messages) total = sum(tokens for tokens, _msg in sized) if total <= self.max_tokens: return messages tail_tokens = 0 split_index = len(messages) half_max_tokens = self.max_tokens // 2 # Iterate over the messages in reverse order for i in range(len(sized)-1, -1, -1): tokens, _msg = sized[i] if tail_tokens + tokens < half_max_tokens: tail_tokens += tokens split_index = i else: break # Ensure the head ends with an assistant message while messages[split_index - 1]["role"] != "assistant" and split_index < len(messages): split_index += 1 head = messages[:split_index] tail = messages[split_index:] >>>>>>> UPDATED ``` This change ensures that the head of the messages ends with an assistant message. --- aider/history.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aider/history.py b/aider/history.py index 4a1e2ca3d..283ca34ed 100644 --- a/aider/history.py +++ b/aider/history.py @@ -48,6 +48,10 @@ class ChatSummary: else: break + # Ensure the head ends with an assistant message + while messages[split_index - 1]["role"] != "assistant" and split_index < len(messages): + split_index += 1 + head = messages[:split_index] tail = messages[split_index:]