From 270ea21c05eb42cf659f107f843417a0049840ce Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 24 Jul 2023 19:08:13 -0300 Subject: [PATCH] aider: Change the `summarize` method in the `ChatSummary` class to split the messages so that the tail holds as close as possible but less than `max_tokens`//2 tokens and the head holds the rest. # Aider chat conversation: USER: instead of splitting the history in half based on the msg number, split it in half based on the token count ASSISTANT: To implement this change, we need to modify the `summarize` method in the `ChatSummary` class in the `aider/history.py` file. The current implementation splits the messages in half based on the number of messages. We need to change this to split the messages based on the token count. Here are the steps to implement this change: 1. Calculate the total token count of all messages. 2. Iterate over the messages and keep a running total of the token count. 3. Once the running total exceeds half of the total token count, split the messages at that point. Here is the *edit block* for this change: ```python aider/history.py <<<<<<< ORIGINAL def summarize(self, messages): num = len(messages) dump(num) if num < 2: return messages sized = self.tokenize(messages) total = sum(tokens for tokens, _msg in sized) if total <= self.max_tokens: return messages num = num // 2 # we want the head to end with an assistant msg while messages[num]["role"] == "assistant" and num < len(messages) - 1: num += 1 head = messages[:num] tail = messages[num:] ======= def summarize(self, messages): ^C KeyboardInterrupt USER: instead of splitting the history in half based on the msg number, split it so that the tail holds as close as possible but less than `max_tokens`//2 tokens and head holds the rest ASSISTANT: To implement this change, we need to modify the `summarize` method in the `ChatSummary` class in the `aider/history.py` file. The current implementation splits the messages in half based on the number of messages. We need to change this to split the messages so that the tail holds as close as possible but less than `max_tokens`//2 tokens and head holds the rest. Here are the steps to implement this change: 1. Iterate over the messages in reverse order and keep a running total of the token count. 2. Once the running total exceeds `max_tokens`//2, split the messages at that point. Here is the *edit block* for this change: ```python aider/history.py <<<<<<< ORIGINAL def summarize(self, messages): num = len(messages) dump(num) if num < 2: return messages sized = self.tokenize(messages) total = sum(tokens for tokens, _msg in sized) if total <= self.max_tokens: return messages num = num // 2 # we want the head to end with an assistant msg while messages[num]["role"] == "assistant" and num < len(messages) - 1: num += 1 head = messages[:num] tail = messages[num:] ======= 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:] >>>>>>> UPDATED ``` This change ensures that the tail of the messages holds as close as possible but less than `max_tokens`//2 tokens and the head holds the rest. --- aider/history.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/aider/history.py b/aider/history.py index d342530a7..4a1e2ca3d 100644 --- a/aider/history.py +++ b/aider/history.py @@ -27,9 +27,7 @@ class ChatSummary: return sized def summarize(self, messages): - num = len(messages) - dump(num) - if num < 2: + if len(messages) < 2: return messages sized = self.tokenize(messages) @@ -37,14 +35,21 @@ class ChatSummary: if total <= self.max_tokens: return messages - num = num // 2 + tail_tokens = 0 + split_index = len(messages) + half_max_tokens = self.max_tokens // 2 - # we want the head to end with an assistant msg - while messages[num]["role"] == "assistant" and num < len(messages) - 1: - num += 1 + # 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[:num] - tail = messages[num:] + head = messages[:split_index] + tail = messages[split_index:] summary = self.summarize_all(head)