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.
This commit is contained in:
Paul Gauthier 2023-07-24 19:08:13 -03:00
parent 0db9d210d7
commit 270ea21c05

View file

@ -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)