TransformMessages
This capability allows you to apply a series of message transformations to a ConversableAgent’s incoming messages before they are processed for response generation. This is useful for tasks such as:
- Limiting the number of messages considered for context.
- Truncating messages to meet token limits.
- Filtering sensitive information.
- Customizing message formatting.
To use
TransformMessages: 1. Create message transformations (e.g.,
MessageHistoryLimiter, MessageTokenLimiter).2. Instantiate
TransformMessages with a list of these transformations.3. Add the
TransformMessages instance to your ConversableAgent using add_to_agent.NOTE: Order of message transformations is important. You could get different results based on the order of transformations.
Example:
```python from agentchat import ConversableAgent from agentchat.contrib.capabilities import TransformMessages, MessageHistoryLimiter, MessageTokenLimiter max_messages = MessageHistoryLimiter(max_messages=2) truncate_messages = MessageTokenLimiter(max_tokens=500) transform_messages = TransformMessages(transforms=[max_messages, truncate_messages]) agent = ConversableAgent(…) transform_messages.add_to_agent(agent)
This function performs the following modifications to the agent:
1. Registers a hook that automatically transforms all messages before they are processed for response generation. Parameters: