A programming framework for agentic AI
Transform Messages Capability (#1923)
Wael Karkoub8cb5c14c82f6
diff --git a/.github/workflows/contrib-tests.yml b/.github/workflows/contrib-tests.ymlindex ced35dc11..942c36ef5 100644--- a/.github/workflows/contrib-tests.yml+++ b/.github/workflows/contrib-tests.yml@@ -291,3 +291,40 @@ jobs:with:file: ./coverage.xmlflags: unittests++ TransformMessages:+ runs-on: ${{ matrix.os }}+ strategy:+ fail-fast: false+ matrix:+ os: [ubuntu-latest, macos-latest, windows-2019]+ python-version: ["3.11"]+ steps:+ - uses: actions/checkout@v3+ - name: Set up Python ${{ matrix.python-version }}+ uses: actions/setup-python@v4+ with:+ python-version: ${{ matrix.python-version }}+ - name: Install packages and dependencies for all tests+ run: |+ python -m pip install --upgrade pip wheel+ pip install pytest+ - name: Install packages and dependencies for Transform Messages+ run: |+ pip install -e .+ - name: Set AUTOGEN_USE_DOCKER based on OS+ shell: bash+ run: |+ if [[ ${{ matrix.os }} != ubuntu-latest ]]; then+ echo "AUTOGEN_USE_DOCKER=False" >> $GITHUB_ENV+ fi+ - name: Coverage+ run: |+ pip install coverage>=5.3+ coverage run -a -m pytest test/agentchat/contrib/capabilities/test_transform_messages.py --skip-openai+ coverage xml+ - name: Upload coverage to Codecov+ uses: codecov/codecov-action@v3+ with:+ file: ./coverage.xml+ flags: unittestdiff --git a/autogen/agentchat/contrib/capabilities/context_handling.py b/autogen/agentchat/contrib/capabilities/context_handling.pyindex 1510ae5fc..dcdf30758 100644--- a/autogen/agentchat/contrib/capabilities/context_handling.py+++ b/autogen/agentchat/contrib/capabilities/context_handling.py@@ -1,9 +1,18 @@import sys-from termcolor import colored-from typing import Dict, Optional, List-from autogen import ConversableAgent-from autogen import token_count_utils+from typing import Dict, List, Optional+from warnings import warn+import tiktoken+from termcolor import colored++from autogen import ConversableAgent, token_count_utils++warn(+ "Context handling with TransformChatHistory is deprecated. "+ "Please use TransformMessages from autogen/agentchat/contrib/capabilities/transform_messages.py instead.",+ DeprecationWarning,+ stacklevel=2,+)class TransformChatHistory:diff --git a/autogen/agentchat/contrib/capabilities/transform_messages.py b/autogen/agentchat/contrib/capabilities/transform_messages.pynew file mode 100644index 000000000..db12fe688--- /dev/null+++ b/autogen/agentchat/contrib/capabilities/transform_messages.py@@ -0,0 +1,90 @@+import copy+from typing import Dict, List++from termcolor import colored++from autogen import ConversableAgent+from .transforms import MessageTransform+++class TransformMessages:+ """Agent capability for transforming messages before reply generation.++ 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)+ ```+ """++ def __init__(self, *, transforms: List[MessageTransform] = []):+ """+ Args:+ transforms: A list of message transformations to apply.+ """+ self._transforms = transforms++ def add_to_agent(self, agent: ConversableAgent):+ """Adds the message transformations capability to the specified ConversableAgent.++ 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.+ """+ agent.register_hook(hookable_method="process_all_messages_before_reply", hook=self._transform_messages)++ def _transform_messages(self, messages: List[Dict]) -> List[Dict]:+ temp_messages = copy.deepcopy(messages)+ system_message = None++ if messages[0]["role"] == "system":+ system_message = copy.deepcopy(messages[0])+ temp_messages.pop(0)++ for transform in self._transforms:+ temp_messages = transform.apply_transform(temp_messages)++ if system_message:+ temp_messages.insert(0, system_message)++ self._print_stats(messages, temp_messages)++ return temp_messages++ def _print_stats(self, pre_transform_messages: List[Dict], post_transform_messages: List[Dict]):+ pre_transform_messages_len = len(pre_transform_messages)+ post_transform_messages_len = len(post_transform_messages)++ if pre_transform_messages_len < post_transform_messages_len:+ print(+ colored(+ f"Number of messages reduced from {pre_transform_messages_len} to {post_transform_messages_len}.",+ "yellow",+ )+ )diff --git a/autogen/agentchat/contrib/capabilities/transforms.py b/autogen/agentchat/contrib/capabilities/transforms.pynew file mode 100644index 000000000..d87963ec8--- /dev/null+++ b/autogen/agentchat/contrib/capabilities/transforms.py@@ -0,0 +1,210 @@+import sys+from typing import Any, Dict, List, Optional, Protocol, Union++import tiktoken+from termcolor import colored++from autogen import token_count_utils+++class MessageTransform(Protocol):+ """Defines a contract for message transformation.++ Classes implementing this protocol should provide an `apply_transform` method+ that takes a list of messages and returns the transformed list.+ """++ def apply_transform(self, messages: List[Dict]) -> List[Dict]:+ """Applies a transformation to a list of messages.++ Args:+ messages: A list of dictionaries representing messages.++ Returns:+ A new list of dictionaries containing the transformed messages.+ """+ ...+++class MessageHistoryLimiter:+ """Limits the number of messages considered by an agent for response generation.++ This transform keeps only the most recent messages up to the specified maximum number of messages (max_messages).+ It trims the conversation history by removing older messages, retaining only the most recent messages.+ """++ def __init__(self, max_messages: Optional[int] = None):+ """+ Args:+ max_messages (None or int): Maximum number of messages to keep in the context.+ Must be greater than 0 if not None.+ """+ self._validate_max_messages(max_messages)+ self._max_messages = max_messages++ def apply_transform(self, messages: List[Dict]) -> List[Dict]:+ """Truncates the conversation history to the specified maximum number of messages.++ This method returns a new list containing the most recent messages up to the specified+ maximum number of messages (max_messages). If max_messages is None, it returns the+ original list of messages unmodified.++ Args:+ messages (List[Dict]): The list of messages representing the conversation history.++ Returns:+ List[Dict]: A new list containing the most recent messages up to the specified maximum.+ """+ if self._max_messages is None:+ return messages++ return messages[-self._max_messages :]++ def _validate_max_messages(self, max_messages: Optional[int]):+ if max_messages is not None and max_messages < 1:+ raise ValueError("max_messages must be None or greater than 1")+++class MessageTokenLimiter:+ """Truncates messages to meet token limits for efficient processing and response generation.++ This transformation applies two levels of truncation to the conversation history:++ 1. Truncates each individual message to the maximum number of tokens specified by max_tokens_per_message.+ 2. Truncates the overall conversation history to the maximum number of tokens specified by max_tokens.++ NOTE: Tokens are counted using the encoder for the specified model. Different models may yield different token+ counts for the same text.++ NOTE: For multimodal LLMs, the token count may be inaccurate as it does not account for the non-text input+ (e.g images).++ The truncation process follows these steps in order:++ 1. Messages are processed in reverse order (newest to oldest).+ 2. Individual messages are truncated based on max_tokens_per_message. For multimodal messages containing both text+ and other types of content, only the text content is truncated.+ 3. The overall conversation history is truncated based on the max_tokens limit. Once the accumulated token count+ exceeds this limit, the current message being processed as well as any remaining messages are discarded.+ 4. The truncated conversation history is reconstructed by prepending the messages to a new list to preserve the+ original message order.+ """++ def __init__(+ self,+ max_tokens_per_message: Optional[int] = None,+ max_tokens: Optional[int] = None,+ model: str = "gpt-3.5-turbo-0613",+ ):+ """+ Args:+ max_tokens_per_message (None or int): Maximum number of tokens to keep in each message.+ Must be greater than or equal to 0 if not None.+ max_tokens (Optional[int]): Maximum number of tokens to keep in the chat history.+ Must be greater than or equal to 0 if not None.+ model (str): The target OpenAI model for tokenization alignment.+ """+ self._model = model+ self._max_tokens_per_message = self._validate_max_tokens(max_tokens_per_message)+ self._max_tokens = self._validate_max_tokens(max_tokens)++ def apply_transform(self, messages: List[Dict]) -> List[Dict]:+ """Applies token truncation to the conversation history.++ Args:+ messages (List[Dict]): The list of messages representing the conversation history.++ Returns:+ List[Dict]: A new list containing the truncated messages up to the specified token limits.+ """+ assert self._max_tokens_per_message is not None+ assert self._max_tokens is not None++ temp_messages = messages.copy()+ processed_messages = []+ processed_messages_tokens = 0++ # calculate tokens for all messages+ total_tokens = sum(_count_tokens(msg["content"]) for msg in temp_messages)++ for msg in reversed(temp_messages):+ msg["content"] = self._truncate_str_to_tokens(msg["content"])+ msg_tokens = _count_tokens(msg["content"])++ # If adding this message would exceed the token limit, discard it and all remaining messages+ if processed_messages_tokens + msg_tokens > self._max_tokens:+ break++ # prepend the message to the list to preserve order+ processed_messages_tokens += msg_tokens+ processed_messages.insert(0, msg)++ if total_tokens > processed_messages_tokens:+ print(+ colored(+ f"Truncated {total_tokens - processed_messages_tokens} tokens. Tokens reduced from {total_tokens} to {processed_messages_tokens}",+ "yellow",+ )+ )++ return processed_messages++ def _truncate_str_to_tokens(self, contents: Union[str, List]) -> Union[str, List]:+ if isinstance(contents, str):+ return self._truncate_tokens(contents)+ elif isinstance(contents, list):+ return self._truncate_multimodal_text(contents)+ else:+ raise ValueError(f"Contents must be a string or a list of dictionaries. Received type: {type(contents)}")++ def _truncate_multimodal_text(self, contents: List[Dict[str, Any]]) -> List[Dict[str, Any]]:+ """Truncates text content within a list of multimodal elements, preserving the overall structure."""+ tmp_contents = []+ for content in contents:+ if content["type"] == "text":+ truncated_text = self._truncate_tokens(content["text"])+ tmp_contents.append({"type": "text", "text": truncated_text})+ else:+ tmp_contents.append(content)+ return tmp_contents++ def _truncate_tokens(self, text: str) -> str:+ encoding = tiktoken.encoding_for_model(self._model) # Get the appropriate tokenizer++ encoded_tokens = encoding.encode(text)+ truncated_tokens = encoded_tokens[: self._max_tokens_per_message]+ truncated_text = encoding.decode(truncated_tokens) # Decode back to text++ return truncated_text++ def _validate_max_tokens(self, max_tokens: Optional[int] = None) -> Optional[int]:+ if max_tokens is not None and max_tokens < 0:+ raise ValueError("max_tokens and max_tokens_per_message must be None or greater than or equal to 0")++ try:+ allowed_tokens = token_count_utils.get_max_token_limit(self._model)+ except Exception:+ print(colored(f"Model {self._model} not found in token_count_utils.", "yellow"))+ allowed_tokens = None++ if max_tokens is not None and allowed_tokens is not None:+ if max_tokens > allowed_tokens:+ print(+ colored(+ f"Max token was set to {max_tokens}, but {self._model} can only accept {allowed_tokens} tokens. Capping it to {allowed_tokens}.",+ "yellow",+ )+ )+ return allowed_tokens++ return max_tokens if max_tokens is not None else sys.maxsize+++def _count_tokens(content: Union[str, List[Dict[str, Any]]]) -> int:+ token_count = 0+ if isinstance(content, str):+ token_count = token_count_utils.count_token(content)+ elif isinstance(content, list):+ for item in content:+ token_count += _count_tokens(item.get("text", ""))+ return token_countdiff --git a/notebook/agentchat_capability_long_context_handling.ipynb b/notebook/agentchat_capability_long_context_handling.ipynbindex 7d2ef12a5..4848990b1 100644--- a/notebook/agentchat_capability_long_context_handling.ipynb+++ b/notebook/agentchat_capability_long_context_handling.ipynb@@ -6,6 +6,10 @@"source": ["# Handling A Long Context via `TransformChatHistory`\n","\n",+ "<div class=\"alert alert-warning\" role=\"alert\">\n",+ " <strong>Deprecation Notice:</strong> <code>TransformChatHistory</code> is no longer supported. Please use <code>TransformMessages</code> as the new standard method. For the latest examples, visit the notebook at <a href=\"https://github.com/microsoft/autogen/blob/main/notebook/agentchat_transform_messages.ipynb\" target=\"_blank\">notebook/agentchat_transform_messages.ipynb</a>.\n",+ "</div>\n",+ "\n","This notebook illustrates how you can use the `TransformChatHistory` capability to give any `Conversable` agent an ability to handle a long context. \n","\n","````{=mdx}\n",diff --git a/notebook/agentchat_transform_messages.ipynb b/notebook/agentchat_transform_messages.ipynbnew file mode 100644index 000000000..37b58b427--- /dev/null+++ b/notebook/agentchat_transform_messages.ipynb@@ -0,0 +1,502 @@+{+ "cells": [+ {+ "cell_type": "markdown",+ "id": "b7549a27-bc4a-4609-bb25-cc7d95cf8c23",+ "metadata": {},+ "source": [+ "# Preprocessing Chat History with `TransformMessages`\n",+ "\n",+ "## Introduction\n",+ "This notebook illustrates how to use `TransforMessages` give any `ConversableAgent` the ability to handle long contexts, sensitive data, and more.\n",+ "\n",+ "````{=mdx}\n",+ ":::info Requirements\n",+ "Install `pyautogen`:\n",+ "```bash\n",+ "pip install pyautogen\n",+ "```\n",+ "\n",+ "For more information, please refer to the [installation guide](/docs/installation/).\n",+ ":::\n",+ "````"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 2,+ "id": "47773f79-c0fd-4993-bc6e-3d1a57690118",+ "metadata": {},+ "outputs": [],+ "source": [+ "import os\n",+ "import pprint\n",+ "import copy\n",+ "import re\n",+ "\n",+ "import autogen\n",+ "from autogen.agentchat.contrib.capabilities import transform_messages, transforms\n",+ "from typing import Dict, List"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 5,+ "id": "9f09246b-a7d0-4238-b62c-1e72c7d815b3",+ "metadata": {},+ "outputs": [],+ "source": [+ "config_list = autogen.config_list_from_json(\n",+ " env_or_file=\"OAI_CONFIG_LIST\",\n",+ ")\n",+ "# Define your llm config\n",+ "llm_config = {\"config_list\": config_list}"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "ea68962a-048d-42e9-9fca-cd944c56184d",+ "metadata": {},+ "source": [+ "````{=mdx}\n",+ ":::tip\n",+ "Learn more about configuring LLMs for agents [here](/docs/topics/llm_configuration).\n",+ ":::\n",+ "````"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 3,+ "id": "84d0e5ad-8b35-4b30-847e-4723e9c76f7c",+ "metadata": {},+ "outputs": [],+ "source": [+ "# Define your agent; the user proxy and an assistant\n",+ "assistant = autogen.AssistantAgent(\n",+ " \"assistant\",\n",+ " llm_config=llm_config,\n",+ ")\n",+ "user_proxy = autogen.UserProxyAgent(\n",+ " \"user_proxy\",\n",+ " human_input_mode=\"NEVER\",\n",+ " is_termination_msg=lambda x: \"TERMINATE\" in x.get(\"content\", \"\"),\n",+ " max_consecutive_auto_reply=10,\n",+ ")"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "180aa953-45be-469a-a94f-0ed0b4ef5ddf",+ "metadata": {},+ "source": [+ "## Handling Long Contexts\n",+ "\n",+ "Imagine a scenario where the LLM generates an extensive amount of text, surpassing the token limit imposed by your API provider. To address this issue, you can leverage `TransformMessages` along with its constituent transformations, `MessageHistoryLimiter` and `MessageTokenLimiter`.\n",+ "\n",+ "- `MessageHistoryLimiter`: You can restrict the total number of messages considered as context history. This transform is particularly useful when you want to limit the conversational context to a specific number of recent messages, ensuring efficient processing and response generation.\n",+ "- `MessageTokenLimiter`: Enables you to cap the total number of tokens, either on a per-message basis or across the entire context history (or both). This transformation is invaluable when you need to adhere to strict token limits imposed by your API provider, preventing unnecessary costs or errors caused by exceeding the allowed token count."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 4,+ "id": "34b943a2-ec58-41bc-a449-d9118c4bbdea",+ "metadata": {},+ "outputs": [],+ "source": [+ "# Limit the message history to the 3 most recent messages\n",+ "max_msg_transfrom = transforms.MessageHistoryLimiter(max_messages=3)\n",+ "\n",+ "# Limit the token limit per message to 10 tokens\n",+ "token_limit_transform = transforms.MessageTokenLimiter(max_tokens_per_message=3)"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "679c1026-4e1b-4c07-85cc-86594cc0b87b",+ "metadata": {},+ "source": [+ "## Example 1: Limiting number of messages\n",+ "Let's take a look at how these transformations will effect the messages. Below we see that by applying the `MessageHistoryLimiter`, we can see that we limited the context history to the 3 most recent messages."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 5,+ "id": "61a2ead4-5f8b-4108-b1f0-3b51b41e2231",+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "[{'content': 'how', 'role': 'user'},\n",+ " {'content': [{'text': 'are you doing?', 'type': 'text'}], 'role': 'assistant'},\n",+ " {'content': 'very very very very very very long string', 'role': 'user'}]\n"+ ]+ }+ ],+ "source": [+ "messages = [\n",+ " {\"role\": \"user\", \"content\": \"hello\"},\n",+ " {\"role\": \"assistant\", \"content\": [{\"type\": \"text\", \"text\": \"there\"}]},\n",+ " {\"role\": \"user\", \"content\": \"how\"},\n",+ " {\"role\": \"assistant\", \"content\": [{\"type\": \"text\", \"text\": \"are you doing?\"}]},\n",+ " {\"role\": \"user\", \"content\": \"very very very very very very long string\"},\n",+ "]\n",+ "\n",+ "processed_messages = max_msg_transfrom.apply_transform(copy.deepcopy(messages))\n",+ "pprint.pprint(processed_messages)"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "610739af-b812-404e-82d2-b3ed796b8b6c",+ "metadata": {},+ "source": [+ "## Example 2: Limiting number of tokens\n",+ "\n",+ "Now let's test limiting the number of tokens in messages. We can see that we can limit the number of tokens to 3, which is equivalent to 3 words in this instance."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 6,+ "id": "739dd260-fa95-4e5d-ae84-9cb7f40de975",+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "\u001b[33mTruncated 6 tokens. Tokens reduced from 15 to 9\u001b[0m\n",+ "[{'content': 'hello', 'role': 'user'},\n",+ " {'content': [{'text': 'there', 'type': 'text'}], 'role': 'assistant'},\n",+ " {'content': 'how', 'role': 'user'},\n",+ " {'content': [{'text': 'are you doing', 'type': 'text'}], 'role': 'assistant'},\n",+ " {'content': 'very very very', 'role': 'user'}]\n"+ ]+ }+ ],+ "source": [+ "processed_messages = token_limit_transform.apply_transform(copy.deepcopy(messages))\n",+ "\n",+ "pprint.pprint(processed_messages)"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "35fa2844-bd83-42ac-8275-959f093b7bc7",+ "metadata": {},+ "source": [+ "## Example 3: Combining transformations\n",+ "\n",+ "Let's test these transforms with agents (the upcoming test is replicated from the agentchat_capability_long_context_handling notebook). We will see that the agent without the capability to handle long context will result in an error, while the agent with that capability will have no issues."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 7,+ "id": "80e53623-2830-41b7-8ae2-bf3668071657",+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "\u001b[33muser_proxy\u001b[0m (to assistant):\n",+ "\n",+ "plot and save a graph of x^2 from -10 to 10\n",+ "\n",+ "--------------------------------------------------------------------------------\n",+ "Encountered an error with the base assistant\n",+ "Error code: 429 - {'error': {'message': 'Request too large for gpt-3.5-turbo in organization org-U58JZBsXUVAJPlx2MtPYmdx1 on tokens per min (TPM): Limit 60000, Requested 1252546. The input or output tokens must be reduced in order to run successfully. Visit https://platform.openai.com/account/rate-limits to learn more.', 'type': 'tokens', 'param': None, 'code': 'rate_limit_exceeded'}}\n",+ "\n",+ "\n",+ "\n",+ "\u001b[33muser_proxy\u001b[0m (to assistant):\n",+ "\n",+ "plot and save a graph of x^2 from -10 to 10\n",+ "\n",+ "--------------------------------------------------------------------------------\n",+ "\u001b[33mTruncated 3804 tokens. Tokens reduced from 4019 to 215\u001b[0m\n",+ "\u001b[33massistant\u001b[0m (to user_proxy):\n",+ "\n",+ "To plot the graph of \\( x^2 \\) from -10 to 10 and save it, we can use Python with the matplotlib library. Here is the code to achieve this:\n",+ "\n",+ "```python\n",+ "# filename: plot_graph.py\n",+ "import matplotlib.pyplot as plt\n",+ "import numpy as np\n",+ "\n",+ "x = np.linspace(-10, 10, 100)\n",+ "y = x**2\n",+ "\n",+ "plt.plot(x, y)\n",+ "plt.xlabel('x')\n",+ "plt.ylabel('x^2')\n",+ "plt.title('Graph of x^2')\n",+ "plt.grid(True)\n",+ "plt.savefig('x_squared_graph.png')\n",+ "plt.show()\n",+ "```\n",+ "\n",+ "After executing this code, you should see the graph of \\( x^2 \\) displayed and saved as `x_squared_graph.png`.\n",+ "\n",+ "Please make sure you have matplotlib installed. If not, you can install it using pip:\n",+ "\n",+ "```sh\n",+ "pip install matplotlib\n",+ "```\n",+ "\n",+ "Go ahead and execute the Python script provided above to plot and save the graph of \\( x^2 \\). Let me know if you encounter any issues.\n",+ "\n",+ "--------------------------------------------------------------------------------\n",+ "\u001b[31m\n",+ ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",+ "\u001b[31m\n",+ ">>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is sh)...\u001b[0m\n",+ "\u001b[33muser_proxy\u001b[0m (to assistant):\n",+ "\n",+ "exitcode: 0 (execution succeeded)\n",+ "Code output: \n",+ "Figure(640x480)\n",+ "\n",+ "Requirement already satisfied: matplotlib in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (3.8.2)\n",+ "Requirement already satisfied: contourpy>=1.0.1 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (1.2.0)\n",+ "Requirement already satisfied: cycler>=0.10 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (0.12.1)\n",+ "Requirement already satisfied: fonttools>=4.22.0 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (4.48.1)\n",+ "Requirement already satisfied: kiwisolver>=1.3.1 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (1.4.5)\n",+ "Requirement already satisfied: numpy<2,>=1.21 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (1.26.4)\n",+ "Requirement already satisfied: packaging>=20.0 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (23.2)\n",+ "Requirement already satisfied: pillow>=8 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (10.2.0)\n",+ "Requirement already satisfied: pyparsing>=2.3.1 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (3.1.1)\n",+ "Requirement already satisfied: python-dateutil>=2.7 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from matplotlib) (2.8.2)\n",+ "Requirement already satisfied: six>=1.5 in /home/wael/workspaces/autogen/.venv/lib/python3.11/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n",+ "\n",+ "\n",+ "--------------------------------------------------------------------------------\n",+ "\u001b[33mTruncated 3435 tokens. Tokens reduced from 3700 to 265\u001b[0m\n",+ "\u001b[33massistant\u001b[0m (to user_proxy):\n",+ "\n",+ "The graph has been successfully created and saved. You can find the graph as a file named \"x_squared_plot.png\" in the directory where you ran the script. You can open and view this file to see the plotted graph of \\(x^2\\) from -10 to 10.\n",+ "\n",+ "TERMINATE\n",+ "\n",+ "--------------------------------------------------------------------------------\n"+ ]+ }+ ],+ "source": [+ "llm_config = {\n",+ " \"config_list\": [{\"model\": \"gpt-3.5-turbo\", \"api_key\": os.environ.get(\"OPENAI_API_KEY\")}],\n",+ "}\n",+ "\n",+ "assistant_base = autogen.AssistantAgent(\n",+ " \"assistant\",\n",+ " llm_config=llm_config,\n",+ ")\n",+ "\n",+ "assistant_with_context_handling = autogen.AssistantAgent(\n",+ " \"assistant\",\n",+ " llm_config=llm_config,\n",+ ")\n",+ "# suppose this capability is not available\n",+ "context_handling = transform_messages.TransformMessages(\n",+ " transforms=[\n",+ " transforms.MessageHistoryLimiter(max_messages=10),\n",+ " transforms.MessageTokenLimiter(max_tokens=1000, max_tokens_per_message=50),\n",+ " ]\n",+ ")\n",+ "\n",+ "context_handling.add_to_agent(assistant_with_context_handling)\n",+ "\n",+ "user_proxy = autogen.UserProxyAgent(\n",+ " \"user_proxy\",\n",+ " human_input_mode=\"NEVER\",\n",+ " is_termination_msg=lambda x: \"TERMINATE\" in x.get(\"content\", \"\"),\n",+ " code_execution_config={\n",+ " \"work_dir\": \"coding\",\n",+ " \"use_docker\": False,\n",+ " },\n",+ " max_consecutive_auto_reply=2,\n",+ ")\n",+ "\n",+ "# suppose the chat history is large\n",+ "# Create a very long chat history that is bound to cause a crash\n",+ "# for gpt 3.5\n",+ "for i in range(1000):\n",+ " # define a fake, very long messages\n",+ " assitant_msg = {\"role\": \"assistant\", \"content\": \"test \" * 1000}\n",+ " user_msg = {\"role\": \"user\", \"content\": \"\"}\n",+ "\n",+ " assistant_base.send(assitant_msg, user_proxy, request_reply=False, silent=True)\n",+ " assistant_with_context_handling.send(assitant_msg, user_proxy, request_reply=False, silent=True)\n",+ " user_proxy.send(user_msg, assistant_base, request_reply=False, silent=True)\n",+ " user_proxy.send(user_msg, assistant_with_context_handling, request_reply=False, silent=True)\n",+ "\n",+ "try:\n",+ " user_proxy.initiate_chat(assistant_base, message=\"plot and save a graph of x^2 from -10 to 10\", clear_history=False)\n",+ "except Exception as e:\n",+ " print(\"Encountered an error with the base assistant\")\n",+ " print(e)\n",+ " print(\"\\n\\n\")\n",+ "\n",+ "try:\n",+ " user_proxy.initiate_chat(\n",+ " assistant_with_context_handling, message=\"plot and save a graph of x^2 from -10 to 10\", clear_history=False\n",+ " )\n",+ "except Exception as e:\n",+ " print(e)"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "5e380678-a923-43cb-91b1-f9c9e8deede2",+ "metadata": {},+ "source": [+ "## Handling Sensitive Data\n",+ "\n",+ "You can use the `MessageTransform` protocol to create custom message transformations that redact sensitive data from the chat history. This is particularly useful when you want to ensure that sensitive information, such as API keys, passwords, or personal data, is not exposed in the chat history or logs.\n",+ "\n",+ "Now, we will create a custom message transform to detect any OpenAI API key and redact it."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 8,+ "id": "74429344-3c0a-4057-aba3-27358fbf059c",+ "metadata": {},+ "outputs": [],+ "source": [+ "# The transform must adhere to transform_messages.MessageTransform protocol.\n",+ "class MessageRedact:\n",+ " def __init__(self):\n",+ " self._openai_key_pattern = r\"sk-([a-zA-Z0-9]{48})\"\n",+ " self._replacement_string = \"REDACTED\"\n",+ "\n",+ " def apply_transform(self, messages: List[Dict]) -> List[Dict]:\n",+ " temp_messages = copy.deepcopy(messages)\n",+ "\n",+ " for message in temp_messages:\n",+ " if isinstance(message[\"content\"], str):\n",+ " message[\"content\"] = re.sub(self._openai_key_pattern, self._replacement_string, message[\"content\"])\n",+ " elif isinstance(message[\"content\"], list):\n",+ " for item in message[\"content\"]:\n",+ " if item[\"type\"] == \"text\":\n",+ " item[\"text\"] = re.sub(self._openai_key_pattern, self._replacement_string, item[\"text\"])\n",+ " return temp_messages"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 9,+ "id": "8a79c0b4-5ff8-49c5-b8a6-c54ca4c7cca2",+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "\u001b[33muser_proxy\u001b[0m (to assistant):\n",+ "\n",+ "What are the two API keys that I just provided\n",+ "\n",+ "--------------------------------------------------------------------------------\n",+ "\u001b[33massistant\u001b[0m (to user_proxy):\n",+ "\n",+ "To retrieve the two API keys you provided, I will display them individually in the output. \n",+ "\n",+ "Here is the first API key:\n",+ "```python\n",+ "# Display the first API key\n",+ "print(\"API key 1 =\", \"REDACTED\")\n",+ "```\n",+ "\n",+ "Here is the second API key:\n",+ "```python\n",+ "# Display the second API key\n",+ "print(\"API key 2 =\", \"REDACTED\")\n",+ "```\n",+ "\n",+ "Please run the code snippets to see the API keys. After that, I will mark this task as complete.\n",+ "\n",+ "--------------------------------------------------------------------------------\n",+ "\u001b[31m\n",+ ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",+ "\u001b[31m\n",+ ">>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is python)...\u001b[0m\n",+ "\u001b[33muser_proxy\u001b[0m (to assistant):\n",+ "\n",+ "exitcode: 0 (execution succeeded)\n",+ "Code output: \n",+ "API key 1 = REDACTED\n",+ "\n",+ "API key 2 = REDACTED\n",+ "\n",+ "\n",+ "--------------------------------------------------------------------------------\n"+ ]+ }+ ],+ "source": [+ "assistant_with_redact = autogen.AssistantAgent(\n",+ " \"assistant\",\n",+ " llm_config=llm_config,\n",+ " max_consecutive_auto_reply=1,\n",+ ")\n",+ "# suppose this capability is not available\n",+ "redact_handling = transform_messages.TransformMessages(transforms=[MessageRedact()])\n",+ "\n",+ "redact_handling.add_to_agent(assistant_with_redact)\n",+ "\n",+ "user_proxy = autogen.UserProxyAgent(\n",+ " \"user_proxy\",\n",+ " human_input_mode=\"NEVER\",\n",+ " max_consecutive_auto_reply=1,\n",+ ")\n",+ "\n",+ "messages = [\n",+ " {\"content\": \"api key 1 = sk-7nwt00xv6fuegfu3gnwmhrgxvuc1cyrhxcq1quur9zvf05fy\"}, # Don't worry, randomly generated\n",+ " {\"content\": [{\"type\": \"text\", \"text\": \"API key 2 = sk-9wi0gf1j2rz6utaqd3ww3o6c1h1n28wviypk7bd81wlj95an\"}]},\n",+ "]\n",+ "\n",+ "for message in messages:\n",+ " user_proxy.send(message, assistant_with_redact, request_reply=False, silent=True)\n",+ "\n",+ "result = user_proxy.initiate_chat(\n",+ " assistant_with_redact, message=\"What are the two API keys that I just provided\", clear_history=False\n",+ ")"+ ]+ }+ ],+ "metadata": {+ "front_matter": {+ "description": "Preprocessing chat history with `TransformMessages`",+ "tags": [+ "long context handling",+ "capability"+ ]+ },+ "kernelspec": {+ "display_name": "Python 3 (ipykernel)",+ "language": "python",+ "name": "python3"+ },+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3",+ "version": "3.11.8"+ }+ },+ "nbformat": 4,+ "nbformat_minor": 5+}diff --git a/test/agentchat/contrib/capabilities/test_transform_messages.py b/test/agentchat/contrib/capabilities/test_transform_messages.pynew file mode 100644index 000000000..c5b7c1dcf--- /dev/null+++ b/test/agentchat/contrib/capabilities/test_transform_messages.py@@ -0,0 +1,154 @@+import copy+import os+import sys+import tempfile+from typing import Any, Dict, List, Union++import pytest++import autogen+from autogen import token_count_utils+from autogen.agentchat.contrib.capabilities.transform_messages import TransformMessages+from autogen.agentchat.contrib.capabilities.transforms import MessageHistoryLimiter, MessageTokenLimiter++sys.path.append(os.path.join(os.path.dirname(__file__), "../../.."))+from conftest import skip_openai # noqa: E402++sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))+from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402+++def _count_tokens(content: Union[str, List[Dict[str, Any]]]) -> int:+ token_count = 0+ if isinstance(content, str):+ token_count = token_count_utils.count_token(content)+ elif isinstance(content, list):+ for item in content:+ token_count += _count_tokens(item.get("text", ""))+ return token_count+++def test_limit_token_transform():+ """+ Test the TokenLimitTransform capability.+ """++ messages = [+ {"role": "user", "content": "short string"},+ {+ "role": "assistant",+ "content": [{"type": "text", "text": "very very very very very very very very long string"}],+ },+ ]++ # check if token limit per message is not exceeded.+ max_tokens_per_message = 5+ token_limit_transform = MessageTokenLimiter(max_tokens_per_message=max_tokens_per_message)+ transformed_messages = token_limit_transform.apply_transform(copy.deepcopy(messages))++ for message in transformed_messages:+ assert _count_tokens(message["content"]) <= max_tokens_per_message++ # check if total token limit is not exceeded.+ max_tokens = 10+ token_limit_transform = MessageTokenLimiter(max_tokens=max_tokens)+ transformed_messages = token_limit_transform.apply_transform(copy.deepcopy(messages))++ token_count = 0+ for message in transformed_messages:+ token_count += _count_tokens(message["content"])++ assert token_count <= max_tokens+ assert len(transformed_messages) <= len(messages)++ # check if token limit per message works nicely with total token limit.+ token_limit_transform = MessageTokenLimiter(max_tokens=max_tokens, max_tokens_per_message=max_tokens_per_message)++ transformed_messages = token_limit_transform.apply_transform(copy.deepcopy(messages))++ token_count = 0+ for message in transformed_messages:+ token_count_local = _count_tokens(message["content"])+ token_count += token_count_local+ assert token_count_local <= max_tokens_per_message++ assert token_count <= max_tokens+ assert len(transformed_messages) <= len(messages)+++def test_max_message_history_length_transform():+ """+ Test the MessageHistoryLimiter capability to limit the number of messages.+ """+ messages = [+ {"role": "user", "content": "hello"},+ {"role": "assistant", "content": [{"type": "text", "text": "there"}]},+ {"role": "user", "content": "how"},+ {"role": "assistant", "content": [{"type": "text", "text": "are you doing?"}]},+ ]++ max_messages = 2+ messages_limiter = MessageHistoryLimiter(max_messages=max_messages)+ transformed_messages = messages_limiter.apply_transform(copy.deepcopy(messages))++ assert len(transformed_messages) == max_messages+ assert transformed_messages == messages[max_messages:]++[email protected](skip_openai, reason="Requested to skip openai test.")+def test_transform_messages_capability():+ """Test the TransformMessages capability to handle long contexts.++ This test is a replica of test_transform_chat_history_with_agents in test_context_handling.py+ """+ with tempfile.TemporaryDirectory() as temp_dir:+ config_list = autogen.config_list_from_json(+ OAI_CONFIG_LIST,+ KEY_LOC,+ filter_dict={+ "model": "gpt-3.5-turbo",+ },+ )++ assistant = autogen.AssistantAgent(+ "assistant", llm_config={"config_list": config_list}, max_consecutive_auto_reply=1+ )++ context_handling = TransformMessages(+ transforms=[+ MessageHistoryLimiter(max_messages=10),+ MessageTokenLimiter(max_tokens=10, max_tokens_per_message=5),+ ]+ )+ context_handling.add_to_agent(assistant)+ user = autogen.UserProxyAgent(+ "user",+ code_execution_config={"work_dir": temp_dir},+ human_input_mode="NEVER",+ is_termination_msg=lambda x: "TERMINATE" in x.get("content", ""),+ max_consecutive_auto_reply=1,+ )++ # Create a very long chat history that is bound to cause a crash+ # for gpt 3.5+ for i in range(1000):+ assitant_msg = {"role": "assistant", "content": "test " * 1000}+ user_msg = {"role": "user", "content": ""}++ assistant.send(assitant_msg, user, request_reply=False)+ user.send(user_msg, assistant, request_reply=False)++ try:+ user.initiate_chat(+ assistant,+ message="Plot a chart of nvidia and tesla stock prices for the last 5 years",+ clear_history=False,+ )+ except Exception as e:+ assert False, f"Chat initiation failed with error {str(e)}"+++if __name__ == "__main__":+ test_limit_token_transform()+ test_max_message_history_length_transform()+ test_transform_messages_capability()