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

View file

@ -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:]