Introduction to Transform Messages
Why do we need to handle long contexts? The problem arises from several constraints and requirements:
-
Token limits: LLMs have token limits that restrict the amount of textual data they can process. If we exceed these limits, we may encounter errors or incur additional costs. By preprocessing the chat history, we can ensure that we stay within the acceptable token range.
-
Context relevance: As conversations progress, retaining the entire chat history may become less relevant or even counterproductive. Keeping only the most recent and pertinent messages can help the LLMs focus on the most crucial context, leading to more accurate and relevant responses.
-
Efficiency: Processing long contexts can consume more computational resources, leading to slower response times.
Transform Messages Capability
The TransformMessages
capability is designed to modify incoming messages before they are processed by the LLM agent. This can include limiting the number of messages, truncating messages to meet token limits, and more.
Exploring and Understanding Transformations
Let’s start by exploring the available transformations and understanding how they work. We will start off by importing the required modules.
Example 1: Limiting the Total Number of Messages
Consider a scenario where you want to limit the context history to only the most recent messages to maintain efficiency and relevance. You can achieve this with the MessageHistoryLimiter transformation:
By applying the MessageHistoryLimiter
, we can see that we were able to limit the context history to the 3 most recent messages. However, if the splitting point is between a “tool_calls” and “tool” pair, the complete pair will be included to obey the OpenAI API call constraints.
Example 2: Limiting the Number of Tokens
To adhere to token limitations, use the MessageTokenLimiter
transformation. This limits tokens per message and the total token count across all messages. Additionally, a min_tokens
threshold can be applied:
We can see that we were able to limit the number of tokens to 3, which is equivalent to 3 words for this instance.
In the following example we will explore the effect of the min_tokens
threshold.
We can see that no transformation was applied, because the threshold of 10 total tokens was not reached.
Apply Transformations Using Agents
So far, we have only tested the MessageHistoryLimiter
and MessageTokenLimiter
transformations individually, let’s test these transformations with AutoGen’s agents.
Setting Up the Stage
Learn more about configuring LLMs for agents here.
We first need to write the test
function that creates a very long chat history by exchanging messages between an assistant and a user proxy agent, and then attempts to initiate a new chat without clearing the history, potentially triggering an error due to token limits.
The first run will be the default implementation, where the agent does not have the TransformMessages
capability.
Running this test will result in an error due to the large number of tokens sent to OpenAI’s gpt 3.5.
Now let’s add the TransformMessages
capability to the assistant and run the same test.
The following console output shows that the agent is now able to handle the large number of tokens sent to OpenAI’s gpt 3.5.
Create Custom Transformations to Handle Sensitive Content
You can create custom transformations by implementing the MessageTransform
protocol, which provides flexibility to handle various use cases. One practical application is to create a custom transformation that redacts sensitive information, such as API keys, passwords, or personal data, from the chat history or logs. This ensures that confidential data is not inadvertently exposed, enhancing the security and privacy of your conversational AI system.
We will demonstrate this by implementing a custom transformation called MessageRedact
that detects and redacts OpenAI API keys from the conversation history. This transformation is particularly useful when you want to prevent accidental leaks of API keys, which could compromise the security of your system.