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

MessageHistoryLimiter bug leading to incorrect order #4644

Closed
ekzhu opened this issue Dec 10, 2024 · 0 comments · Fixed by #4653
Closed

MessageHistoryLimiter bug leading to incorrect order #4644

ekzhu opened this issue Dec 10, 2024 · 0 comments · Fixed by #4653
Assignees
Labels
0.2 Issues which are related to the pre 0.4 codebase

Comments

@ekzhu
Copy link
Collaborator

ekzhu commented Dec 10, 2024

          [autogen/agentchat/contrib/capabilities/transforms.py](https://github.com/microsoft/autogen/blob/0.2/autogen/agentchat/contrib/capabilities/transforms.py)

In MessageHistoryLimiter, line 99 of the file.
It should take _keep_first_message into consider. otherwise when keep_first_message==False , the last message will be inserted in a wrong place.

        # Loop through messages in reverse
        for i in range(len(messages) - 1, 0, -1):
            if remaining_count > 1:
                truncated_messages.insert(1 if self._keep_first_message else 0, messages[i])
            if remaining_count == 1:
                # If there's only 1 slot left and it's a 'tools' message, ignore it.
                if messages[i].get("role") != "tool":
-                   truncated_messages.insert(1, messages[i])
+                   truncated_messages.insert(1 if self._keep_first_message else 0, messages[i])

for example, following code output is in wrong order

from autogen.agentchat.contrib.capabilities import transform_messages, transforms


limiter=transforms.MessageHistoryLimiter(max_messages=3, keep_first_message=False)

limiter.apply_transform([{'role':'user', 'name':'agent', 'content':'0'},
                         {'role':'user', 'name':'agent', 'content':'1'},
                         {'role':'user', 'name':'agent', 'content':'2'},
                         {'role':'user', 'name':'agent', 'content':'3'},
                         {'role':'user', 'name':'agent', 'content':'4'},
                        ])
[{'role': 'user', 'name': 'agent', 'content': '3'},
 {'role': 'user', 'name': 'agent', 'content': '2'},
 {'role': 'user', 'name': 'agent', 'content': '4'}]

Originally posted by @milkmeat in #3178 (comment)

@ekzhu ekzhu changed the title [autogen/agentchat/contrib/capabilities/transforms.py](https://github.com/microsoft/autogen/blob/0.2/autogen/agentchat/contrib/capabilities/transforms.py) MessageHistoryLimiter bug leading to incorrect order Dec 10, 2024
@ekzhu ekzhu added the 0.2 Issues which are related to the pre 0.4 codebase label Dec 10, 2024
@ekzhu ekzhu linked a pull request Dec 12, 2024 that will close this issue
2 tasks
@ekzhu ekzhu closed this as completed Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.2 Issues which are related to the pre 0.4 codebase
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants