ag2 0.3.2b2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ag2 might be problematic. Click here for more details.

Files changed (112) hide show
  1. ag2-0.3.2b2.dist-info/LICENSE +201 -0
  2. ag2-0.3.2b2.dist-info/METADATA +490 -0
  3. ag2-0.3.2b2.dist-info/NOTICE.md +19 -0
  4. ag2-0.3.2b2.dist-info/RECORD +112 -0
  5. ag2-0.3.2b2.dist-info/WHEEL +5 -0
  6. ag2-0.3.2b2.dist-info/top_level.txt +1 -0
  7. autogen/__init__.py +17 -0
  8. autogen/_pydantic.py +116 -0
  9. autogen/agentchat/__init__.py +26 -0
  10. autogen/agentchat/agent.py +142 -0
  11. autogen/agentchat/assistant_agent.py +85 -0
  12. autogen/agentchat/chat.py +306 -0
  13. autogen/agentchat/contrib/__init__.py +0 -0
  14. autogen/agentchat/contrib/agent_builder.py +785 -0
  15. autogen/agentchat/contrib/agent_optimizer.py +450 -0
  16. autogen/agentchat/contrib/capabilities/__init__.py +0 -0
  17. autogen/agentchat/contrib/capabilities/agent_capability.py +21 -0
  18. autogen/agentchat/contrib/capabilities/generate_images.py +297 -0
  19. autogen/agentchat/contrib/capabilities/teachability.py +406 -0
  20. autogen/agentchat/contrib/capabilities/text_compressors.py +72 -0
  21. autogen/agentchat/contrib/capabilities/transform_messages.py +92 -0
  22. autogen/agentchat/contrib/capabilities/transforms.py +565 -0
  23. autogen/agentchat/contrib/capabilities/transforms_util.py +120 -0
  24. autogen/agentchat/contrib/capabilities/vision_capability.py +217 -0
  25. autogen/agentchat/contrib/gpt_assistant_agent.py +545 -0
  26. autogen/agentchat/contrib/graph_rag/__init__.py +0 -0
  27. autogen/agentchat/contrib/graph_rag/document.py +24 -0
  28. autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +76 -0
  29. autogen/agentchat/contrib/graph_rag/graph_query_engine.py +50 -0
  30. autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +56 -0
  31. autogen/agentchat/contrib/img_utils.py +390 -0
  32. autogen/agentchat/contrib/llamaindex_conversable_agent.py +114 -0
  33. autogen/agentchat/contrib/llava_agent.py +176 -0
  34. autogen/agentchat/contrib/math_user_proxy_agent.py +471 -0
  35. autogen/agentchat/contrib/multimodal_conversable_agent.py +128 -0
  36. autogen/agentchat/contrib/qdrant_retrieve_user_proxy_agent.py +325 -0
  37. autogen/agentchat/contrib/retrieve_assistant_agent.py +56 -0
  38. autogen/agentchat/contrib/retrieve_user_proxy_agent.py +701 -0
  39. autogen/agentchat/contrib/society_of_mind_agent.py +203 -0
  40. autogen/agentchat/contrib/text_analyzer_agent.py +76 -0
  41. autogen/agentchat/contrib/vectordb/__init__.py +0 -0
  42. autogen/agentchat/contrib/vectordb/base.py +243 -0
  43. autogen/agentchat/contrib/vectordb/chromadb.py +326 -0
  44. autogen/agentchat/contrib/vectordb/mongodb.py +559 -0
  45. autogen/agentchat/contrib/vectordb/pgvectordb.py +958 -0
  46. autogen/agentchat/contrib/vectordb/qdrant.py +334 -0
  47. autogen/agentchat/contrib/vectordb/utils.py +126 -0
  48. autogen/agentchat/contrib/web_surfer.py +305 -0
  49. autogen/agentchat/conversable_agent.py +2904 -0
  50. autogen/agentchat/groupchat.py +1666 -0
  51. autogen/agentchat/user_proxy_agent.py +109 -0
  52. autogen/agentchat/utils.py +207 -0
  53. autogen/browser_utils.py +291 -0
  54. autogen/cache/__init__.py +10 -0
  55. autogen/cache/abstract_cache_base.py +78 -0
  56. autogen/cache/cache.py +182 -0
  57. autogen/cache/cache_factory.py +85 -0
  58. autogen/cache/cosmos_db_cache.py +150 -0
  59. autogen/cache/disk_cache.py +109 -0
  60. autogen/cache/in_memory_cache.py +61 -0
  61. autogen/cache/redis_cache.py +128 -0
  62. autogen/code_utils.py +745 -0
  63. autogen/coding/__init__.py +22 -0
  64. autogen/coding/base.py +113 -0
  65. autogen/coding/docker_commandline_code_executor.py +262 -0
  66. autogen/coding/factory.py +45 -0
  67. autogen/coding/func_with_reqs.py +203 -0
  68. autogen/coding/jupyter/__init__.py +22 -0
  69. autogen/coding/jupyter/base.py +32 -0
  70. autogen/coding/jupyter/docker_jupyter_server.py +164 -0
  71. autogen/coding/jupyter/embedded_ipython_code_executor.py +182 -0
  72. autogen/coding/jupyter/jupyter_client.py +224 -0
  73. autogen/coding/jupyter/jupyter_code_executor.py +161 -0
  74. autogen/coding/jupyter/local_jupyter_server.py +168 -0
  75. autogen/coding/local_commandline_code_executor.py +410 -0
  76. autogen/coding/markdown_code_extractor.py +44 -0
  77. autogen/coding/utils.py +57 -0
  78. autogen/exception_utils.py +46 -0
  79. autogen/extensions/__init__.py +0 -0
  80. autogen/formatting_utils.py +76 -0
  81. autogen/function_utils.py +362 -0
  82. autogen/graph_utils.py +148 -0
  83. autogen/io/__init__.py +15 -0
  84. autogen/io/base.py +105 -0
  85. autogen/io/console.py +43 -0
  86. autogen/io/websockets.py +213 -0
  87. autogen/logger/__init__.py +11 -0
  88. autogen/logger/base_logger.py +140 -0
  89. autogen/logger/file_logger.py +287 -0
  90. autogen/logger/logger_factory.py +29 -0
  91. autogen/logger/logger_utils.py +42 -0
  92. autogen/logger/sqlite_logger.py +459 -0
  93. autogen/math_utils.py +356 -0
  94. autogen/oai/__init__.py +33 -0
  95. autogen/oai/anthropic.py +428 -0
  96. autogen/oai/bedrock.py +600 -0
  97. autogen/oai/cerebras.py +264 -0
  98. autogen/oai/client.py +1148 -0
  99. autogen/oai/client_utils.py +167 -0
  100. autogen/oai/cohere.py +453 -0
  101. autogen/oai/completion.py +1216 -0
  102. autogen/oai/gemini.py +469 -0
  103. autogen/oai/groq.py +281 -0
  104. autogen/oai/mistral.py +279 -0
  105. autogen/oai/ollama.py +576 -0
  106. autogen/oai/openai_utils.py +810 -0
  107. autogen/oai/together.py +343 -0
  108. autogen/retrieve_utils.py +487 -0
  109. autogen/runtime_logging.py +163 -0
  110. autogen/token_count_utils.py +257 -0
  111. autogen/types.py +20 -0
  112. autogen/version.py +7 -0
@@ -0,0 +1,565 @@
1
+ # Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+ #
5
+ # Portions derived from https://github.com/microsoft/autogen are under the MIT License.
6
+ # SPDX-License-Identifier: MIT
7
+ import copy
8
+ import sys
9
+ from typing import Any, Dict, List, Optional, Protocol, Tuple, Union
10
+
11
+ import tiktoken
12
+ from termcolor import colored
13
+
14
+ from autogen import token_count_utils
15
+ from autogen.cache import AbstractCache, Cache
16
+ from autogen.types import MessageContentType
17
+
18
+ from . import transforms_util
19
+ from .text_compressors import LLMLingua, TextCompressor
20
+
21
+
22
+ class MessageTransform(Protocol):
23
+ """Defines a contract for message transformation.
24
+
25
+ Classes implementing this protocol should provide an `apply_transform` method
26
+ that takes a list of messages and returns the transformed list.
27
+ """
28
+
29
+ def apply_transform(self, messages: List[Dict]) -> List[Dict]:
30
+ """Applies a transformation to a list of messages.
31
+
32
+ Args:
33
+ messages: A list of dictionaries representing messages.
34
+
35
+ Returns:
36
+ A new list of dictionaries containing the transformed messages.
37
+ """
38
+ ...
39
+
40
+ def get_logs(self, pre_transform_messages: List[Dict], post_transform_messages: List[Dict]) -> Tuple[str, bool]:
41
+ """Creates the string including the logs of the transformation
42
+
43
+ Alongside the string, it returns a boolean indicating whether the transformation had an effect or not.
44
+
45
+ Args:
46
+ pre_transform_messages: A list of dictionaries representing messages before the transformation.
47
+ post_transform_messages: A list of dictionaries representig messages after the transformation.
48
+
49
+ Returns:
50
+ A tuple with a string with the logs and a flag indicating whether the transformation had an effect or not.
51
+ """
52
+ ...
53
+
54
+
55
+ class MessageHistoryLimiter:
56
+ """Limits the number of messages considered by an agent for response generation.
57
+
58
+ This transform keeps only the most recent messages up to the specified maximum number of messages (max_messages).
59
+ It trims the conversation history by removing older messages, retaining only the most recent messages.
60
+ """
61
+
62
+ def __init__(self, max_messages: Optional[int] = None, keep_first_message: bool = False):
63
+ """
64
+ Args:
65
+ max_messages Optional[int]: Maximum number of messages to keep in the context. Must be greater than 0 if not None.
66
+ keep_first_message bool: Whether to keep the original first message in the conversation history.
67
+ Defaults to False.
68
+ """
69
+ self._validate_max_messages(max_messages)
70
+ self._max_messages = max_messages
71
+ self._keep_first_message = keep_first_message
72
+
73
+ def apply_transform(self, messages: List[Dict]) -> List[Dict]:
74
+ """Truncates the conversation history to the specified maximum number of messages.
75
+
76
+ This method returns a new list containing the most recent messages up to the specified
77
+ maximum number of messages (max_messages). If max_messages is None, it returns the
78
+ original list of messages unmodified.
79
+
80
+ Args:
81
+ messages (List[Dict]): The list of messages representing the conversation history.
82
+
83
+ Returns:
84
+ List[Dict]: A new list containing the most recent messages up to the specified maximum.
85
+ """
86
+
87
+ if self._max_messages is None or len(messages) <= self._max_messages:
88
+ return messages
89
+
90
+ truncated_messages = []
91
+ remaining_count = self._max_messages
92
+
93
+ # Start with the first message if we need to keep it
94
+ if self._keep_first_message:
95
+ truncated_messages = [messages[0]]
96
+ remaining_count -= 1
97
+
98
+ # Loop through messages in reverse
99
+ for i in range(len(messages) - 1, 0, -1):
100
+ if remaining_count > 1:
101
+ truncated_messages.insert(1 if self._keep_first_message else 0, messages[i])
102
+ if remaining_count == 1:
103
+ # If there's only 1 slot left and it's a 'tools' message, ignore it.
104
+ if messages[i].get("role") != "tool":
105
+ truncated_messages.insert(1, messages[i])
106
+
107
+ remaining_count -= 1
108
+ if remaining_count == 0:
109
+ break
110
+
111
+ return truncated_messages
112
+
113
+ def get_logs(self, pre_transform_messages: List[Dict], post_transform_messages: List[Dict]) -> Tuple[str, bool]:
114
+ pre_transform_messages_len = len(pre_transform_messages)
115
+ post_transform_messages_len = len(post_transform_messages)
116
+
117
+ if post_transform_messages_len < pre_transform_messages_len:
118
+ logs_str = (
119
+ f"Removed {pre_transform_messages_len - post_transform_messages_len} messages. "
120
+ f"Number of messages reduced from {pre_transform_messages_len} to {post_transform_messages_len}."
121
+ )
122
+ return logs_str, True
123
+ return "No messages were removed.", False
124
+
125
+ def _validate_max_messages(self, max_messages: Optional[int]):
126
+ if max_messages is not None and max_messages < 1:
127
+ raise ValueError("max_messages must be None or greater than 1")
128
+
129
+
130
+ class MessageTokenLimiter:
131
+ """Truncates messages to meet token limits for efficient processing and response generation.
132
+
133
+ This transformation applies two levels of truncation to the conversation history:
134
+
135
+ 1. Truncates each individual message to the maximum number of tokens specified by max_tokens_per_message.
136
+ 2. Truncates the overall conversation history to the maximum number of tokens specified by max_tokens.
137
+
138
+ NOTE: Tokens are counted using the encoder for the specified model. Different models may yield different token
139
+ counts for the same text.
140
+
141
+ NOTE: For multimodal LLMs, the token count may be inaccurate as it does not account for the non-text input
142
+ (e.g images).
143
+
144
+ The truncation process follows these steps in order:
145
+
146
+ 1. The minimum tokens threshold (`min_tokens`) is checked (0 by default). If the total number of tokens in messages
147
+ are less than this threshold, then the messages are returned as is. In other case, the following process is applied.
148
+ 2. Messages are processed in reverse order (newest to oldest).
149
+ 3. Individual messages are truncated based on max_tokens_per_message. For multimodal messages containing both text
150
+ and other types of content, only the text content is truncated.
151
+ 4. The overall conversation history is truncated based on the max_tokens limit. Once the accumulated token count
152
+ exceeds this limit, the current message being processed get truncated to meet the total token count and any
153
+ remaining messages get discarded.
154
+ 5. The truncated conversation history is reconstructed by prepending the messages to a new list to preserve the
155
+ original message order.
156
+ """
157
+
158
+ def __init__(
159
+ self,
160
+ max_tokens_per_message: Optional[int] = None,
161
+ max_tokens: Optional[int] = None,
162
+ min_tokens: Optional[int] = None,
163
+ model: str = "gpt-3.5-turbo-0613",
164
+ filter_dict: Optional[Dict] = None,
165
+ exclude_filter: bool = True,
166
+ ):
167
+ """
168
+ Args:
169
+ max_tokens_per_message (None or int): Maximum number of tokens to keep in each message.
170
+ Must be greater than or equal to 0 if not None.
171
+ max_tokens (Optional[int]): Maximum number of tokens to keep in the chat history.
172
+ Must be greater than or equal to 0 if not None.
173
+ min_tokens (Optional[int]): Minimum number of tokens in messages to apply the transformation.
174
+ Must be greater than or equal to 0 if not None.
175
+ model (str): The target OpenAI model for tokenization alignment.
176
+ filter_dict (None or dict): A dictionary to filter out messages that you want/don't want to compress.
177
+ If None, no filters will be applied.
178
+ exclude_filter (bool): If exclude filter is True (the default value), messages that match the filter will be
179
+ excluded from token truncation. If False, messages that match the filter will be truncated.
180
+ """
181
+ self._model = model
182
+ self._max_tokens_per_message = self._validate_max_tokens(max_tokens_per_message)
183
+ self._max_tokens = self._validate_max_tokens(max_tokens)
184
+ self._min_tokens = self._validate_min_tokens(min_tokens, max_tokens)
185
+ self._filter_dict = filter_dict
186
+ self._exclude_filter = exclude_filter
187
+
188
+ def apply_transform(self, messages: List[Dict]) -> List[Dict]:
189
+ """Applies token truncation to the conversation history.
190
+
191
+ Args:
192
+ messages (List[Dict]): The list of messages representing the conversation history.
193
+
194
+ Returns:
195
+ List[Dict]: A new list containing the truncated messages up to the specified token limits.
196
+ """
197
+ assert self._max_tokens_per_message is not None
198
+ assert self._max_tokens is not None
199
+ assert self._min_tokens is not None
200
+
201
+ # if the total number of tokens in the messages is less than the min_tokens, return the messages as is
202
+ if not transforms_util.min_tokens_reached(messages, self._min_tokens):
203
+ return messages
204
+
205
+ temp_messages = copy.deepcopy(messages)
206
+ processed_messages = []
207
+ processed_messages_tokens = 0
208
+
209
+ for msg in reversed(temp_messages):
210
+ # Some messages may not have content.
211
+ if not transforms_util.is_content_right_type(msg.get("content")):
212
+ processed_messages.insert(0, msg)
213
+ continue
214
+
215
+ if not transforms_util.should_transform_message(msg, self._filter_dict, self._exclude_filter):
216
+ processed_messages.insert(0, msg)
217
+ processed_messages_tokens += transforms_util.count_text_tokens(msg["content"])
218
+ continue
219
+
220
+ expected_tokens_remained = self._max_tokens - processed_messages_tokens - self._max_tokens_per_message
221
+
222
+ # If adding this message would exceed the token limit, truncate the last message to meet the total token
223
+ # limit and discard all remaining messages
224
+ if expected_tokens_remained < 0:
225
+ msg["content"] = self._truncate_str_to_tokens(
226
+ msg["content"], self._max_tokens - processed_messages_tokens
227
+ )
228
+ processed_messages.insert(0, msg)
229
+ break
230
+
231
+ msg["content"] = self._truncate_str_to_tokens(msg["content"], self._max_tokens_per_message)
232
+ msg_tokens = transforms_util.count_text_tokens(msg["content"])
233
+
234
+ # prepend the message to the list to preserve order
235
+ processed_messages_tokens += msg_tokens
236
+ processed_messages.insert(0, msg)
237
+
238
+ return processed_messages
239
+
240
+ def get_logs(self, pre_transform_messages: List[Dict], post_transform_messages: List[Dict]) -> Tuple[str, bool]:
241
+ pre_transform_messages_tokens = sum(
242
+ transforms_util.count_text_tokens(msg["content"]) for msg in pre_transform_messages if "content" in msg
243
+ )
244
+ post_transform_messages_tokens = sum(
245
+ transforms_util.count_text_tokens(msg["content"]) for msg in post_transform_messages if "content" in msg
246
+ )
247
+
248
+ if post_transform_messages_tokens < pre_transform_messages_tokens:
249
+ logs_str = (
250
+ f"Truncated {pre_transform_messages_tokens - post_transform_messages_tokens} tokens. "
251
+ f"Number of tokens reduced from {pre_transform_messages_tokens} to {post_transform_messages_tokens}"
252
+ )
253
+ return logs_str, True
254
+ return "No tokens were truncated.", False
255
+
256
+ def _truncate_str_to_tokens(self, contents: Union[str, List], n_tokens: int) -> Union[str, List]:
257
+ if isinstance(contents, str):
258
+ return self._truncate_tokens(contents, n_tokens)
259
+ elif isinstance(contents, list):
260
+ return self._truncate_multimodal_text(contents, n_tokens)
261
+ else:
262
+ raise ValueError(f"Contents must be a string or a list of dictionaries. Received type: {type(contents)}")
263
+
264
+ def _truncate_multimodal_text(self, contents: List[Dict[str, Any]], n_tokens: int) -> List[Dict[str, Any]]:
265
+ """Truncates text content within a list of multimodal elements, preserving the overall structure."""
266
+ tmp_contents = []
267
+ for content in contents:
268
+ if content["type"] == "text":
269
+ truncated_text = self._truncate_tokens(content["text"], n_tokens)
270
+ tmp_contents.append({"type": "text", "text": truncated_text})
271
+ else:
272
+ tmp_contents.append(content)
273
+ return tmp_contents
274
+
275
+ def _truncate_tokens(self, text: str, n_tokens: int) -> str:
276
+ encoding = tiktoken.encoding_for_model(self._model) # Get the appropriate tokenizer
277
+
278
+ encoded_tokens = encoding.encode(text)
279
+ truncated_tokens = encoded_tokens[:n_tokens]
280
+ truncated_text = encoding.decode(truncated_tokens) # Decode back to text
281
+
282
+ return truncated_text
283
+
284
+ def _validate_max_tokens(self, max_tokens: Optional[int] = None) -> Optional[int]:
285
+ if max_tokens is not None and max_tokens < 0:
286
+ raise ValueError("max_tokens and max_tokens_per_message must be None or greater than or equal to 0")
287
+
288
+ try:
289
+ allowed_tokens = token_count_utils.get_max_token_limit(self._model)
290
+ except Exception:
291
+ print(colored(f"Model {self._model} not found in token_count_utils.", "yellow"))
292
+ allowed_tokens = None
293
+
294
+ if max_tokens is not None and allowed_tokens is not None:
295
+ if max_tokens > allowed_tokens:
296
+ print(
297
+ colored(
298
+ f"Max token was set to {max_tokens}, but {self._model} can only accept {allowed_tokens} tokens. Capping it to {allowed_tokens}.",
299
+ "yellow",
300
+ )
301
+ )
302
+ return allowed_tokens
303
+
304
+ return max_tokens if max_tokens is not None else sys.maxsize
305
+
306
+ def _validate_min_tokens(self, min_tokens: Optional[int], max_tokens: Optional[int]) -> int:
307
+ if min_tokens is None:
308
+ return 0
309
+ if min_tokens < 0:
310
+ raise ValueError("min_tokens must be None or greater than or equal to 0.")
311
+ if max_tokens is not None and min_tokens > max_tokens:
312
+ raise ValueError("min_tokens must not be more than max_tokens.")
313
+ return min_tokens
314
+
315
+
316
+ class TextMessageCompressor:
317
+ """A transform for compressing text messages in a conversation history.
318
+
319
+ It uses a specified text compression method to reduce the token count of messages, which can lead to more efficient
320
+ processing and response generation by downstream models.
321
+ """
322
+
323
+ def __init__(
324
+ self,
325
+ text_compressor: Optional[TextCompressor] = None,
326
+ min_tokens: Optional[int] = None,
327
+ compression_params: Dict = dict(),
328
+ cache: Optional[AbstractCache] = None,
329
+ filter_dict: Optional[Dict] = None,
330
+ exclude_filter: bool = True,
331
+ ):
332
+ """
333
+ Args:
334
+ text_compressor (TextCompressor or None): An instance of a class that implements the TextCompressor
335
+ protocol. If None, it defaults to LLMLingua.
336
+ min_tokens (int or None): Minimum number of tokens in messages to apply the transformation. Must be greater
337
+ than or equal to 0 if not None. If None, no threshold-based compression is applied.
338
+ compression_args (dict): A dictionary of arguments for the compression method. Defaults to an empty
339
+ dictionary.
340
+ cache (None or AbstractCache): The cache client to use to store and retrieve previously compressed messages.
341
+ If None, no caching will be used.
342
+ filter_dict (None or dict): A dictionary to filter out messages that you want/don't want to compress.
343
+ If None, no filters will be applied.
344
+ exclude_filter (bool): If exclude filter is True (the default value), messages that match the filter will be
345
+ excluded from compression. If False, messages that match the filter will be compressed.
346
+ """
347
+
348
+ if text_compressor is None:
349
+ text_compressor = LLMLingua()
350
+
351
+ self._validate_min_tokens(min_tokens)
352
+
353
+ self._text_compressor = text_compressor
354
+ self._min_tokens = min_tokens
355
+ self._compression_args = compression_params
356
+ self._filter_dict = filter_dict
357
+ self._exclude_filter = exclude_filter
358
+
359
+ if cache is None:
360
+ self._cache = Cache.disk()
361
+ else:
362
+ self._cache = cache
363
+
364
+ # Optimizing savings calculations to optimize log generation
365
+ self._recent_tokens_savings = 0
366
+
367
+ def apply_transform(self, messages: List[Dict]) -> List[Dict]:
368
+ """Applies compression to messages in a conversation history based on the specified configuration.
369
+
370
+ The function processes each message according to the `compression_args` and `min_tokens` settings, applying
371
+ the specified compression configuration and returning a new list of messages with reduced token counts
372
+ where possible.
373
+
374
+ Args:
375
+ messages (List[Dict]): A list of message dictionaries to be compressed.
376
+
377
+ Returns:
378
+ List[Dict]: A list of dictionaries with the message content compressed according to the configured
379
+ method and scope.
380
+ """
381
+ # Make sure there is at least one message
382
+ if not messages:
383
+ return messages
384
+
385
+ # if the total number of tokens in the messages is less than the min_tokens, return the messages as is
386
+ if not transforms_util.min_tokens_reached(messages, self._min_tokens):
387
+ return messages
388
+
389
+ total_savings = 0
390
+ processed_messages = messages.copy()
391
+ for message in processed_messages:
392
+ # Some messages may not have content.
393
+ if not transforms_util.is_content_right_type(message.get("content")):
394
+ continue
395
+
396
+ if not transforms_util.should_transform_message(message, self._filter_dict, self._exclude_filter):
397
+ continue
398
+
399
+ if transforms_util.is_content_text_empty(message["content"]):
400
+ continue
401
+
402
+ cache_key = transforms_util.cache_key(message["content"], self._min_tokens)
403
+ cached_content = transforms_util.cache_content_get(self._cache, cache_key)
404
+ if cached_content is not None:
405
+ message["content"], savings = cached_content
406
+ else:
407
+ message["content"], savings = self._compress(message["content"])
408
+
409
+ transforms_util.cache_content_set(self._cache, cache_key, message["content"], savings)
410
+
411
+ assert isinstance(savings, int)
412
+ total_savings += savings
413
+
414
+ self._recent_tokens_savings = total_savings
415
+ return processed_messages
416
+
417
+ def get_logs(self, pre_transform_messages: List[Dict], post_transform_messages: List[Dict]) -> Tuple[str, bool]:
418
+ if self._recent_tokens_savings > 0:
419
+ return f"{self._recent_tokens_savings} tokens saved with text compression.", True
420
+ else:
421
+ return "No tokens saved with text compression.", False
422
+
423
+ def _compress(self, content: MessageContentType) -> Tuple[MessageContentType, int]:
424
+ """Compresses the given text or multimodal content using the specified compression method."""
425
+ if isinstance(content, str):
426
+ return self._compress_text(content)
427
+ elif isinstance(content, list):
428
+ return self._compress_multimodal(content)
429
+ else:
430
+ return content, 0
431
+
432
+ def _compress_multimodal(self, content: MessageContentType) -> Tuple[MessageContentType, int]:
433
+ tokens_saved = 0
434
+ for item in content:
435
+ if isinstance(item, dict) and "text" in item:
436
+ item["text"], savings = self._compress_text(item["text"])
437
+ tokens_saved += savings
438
+
439
+ elif isinstance(item, str):
440
+ item, savings = self._compress_text(item)
441
+ tokens_saved += savings
442
+
443
+ return content, tokens_saved
444
+
445
+ def _compress_text(self, text: str) -> Tuple[str, int]:
446
+ """Compresses the given text using the specified compression method."""
447
+ compressed_text = self._text_compressor.compress_text(text, **self._compression_args)
448
+
449
+ savings = 0
450
+ if "origin_tokens" in compressed_text and "compressed_tokens" in compressed_text:
451
+ savings = compressed_text["origin_tokens"] - compressed_text["compressed_tokens"]
452
+
453
+ return compressed_text["compressed_prompt"], savings
454
+
455
+ def _validate_min_tokens(self, min_tokens: Optional[int]):
456
+ if min_tokens is not None and min_tokens <= 0:
457
+ raise ValueError("min_tokens must be greater than 0 or None")
458
+
459
+
460
+ class TextMessageContentName:
461
+ """A transform for including the agent's name in the content of a message.
462
+
463
+ How to create and apply the transform:
464
+ # Imports
465
+ from autogen.agentchat.contrib.capabilities import transform_messages, transforms
466
+
467
+ # Create Transform
468
+ name_transform = transforms.TextMessageContentName(position="start", format_string="'{name}' said:\n")
469
+
470
+ # Create the TransformMessages
471
+ context_handling = transform_messages.TransformMessages(
472
+ transforms=[
473
+ name_transform
474
+ ]
475
+ )
476
+
477
+ # Add it to an agent so when they run inference it will apply to the messages
478
+ context_handling.add_to_agent(my_agent)
479
+ """
480
+
481
+ def __init__(
482
+ self,
483
+ position: str = "start",
484
+ format_string: str = "{name}:\n",
485
+ deduplicate: bool = True,
486
+ filter_dict: Optional[Dict] = None,
487
+ exclude_filter: bool = True,
488
+ ):
489
+ """
490
+ Args:
491
+ position (str): The position to add the name to the content. The possible options are 'start' or 'end'. Defaults to 'start'.
492
+ format_string (str): The f-string to format the message name with. Use '{name}' as a placeholder for the agent's name. Defaults to '{name}:\n' and must contain '{name}'.
493
+ deduplicate (bool): Whether to deduplicate the formatted string so it doesn't appear twice (sometimes the LLM will add it to new messages itself). Defaults to True.
494
+ filter_dict (None or dict): A dictionary to filter out messages that you want/don't want to compress.
495
+ If None, no filters will be applied.
496
+ exclude_filter (bool): If exclude filter is True (the default value), messages that match the filter will be
497
+ excluded from compression. If False, messages that match the filter will be compressed.
498
+ """
499
+
500
+ assert isinstance(position, str) and position in ["start", "end"]
501
+ assert isinstance(format_string, str) and "{name}" in format_string
502
+ assert isinstance(deduplicate, bool) and deduplicate is not None
503
+
504
+ self._position = position
505
+ self._format_string = format_string
506
+ self._deduplicate = deduplicate
507
+ self._filter_dict = filter_dict
508
+ self._exclude_filter = exclude_filter
509
+
510
+ # Track the number of messages changed for logging
511
+ self._messages_changed = 0
512
+
513
+ def apply_transform(self, messages: List[Dict]) -> List[Dict]:
514
+ """Applies the name change to the message based on the position and format string.
515
+
516
+ Args:
517
+ messages (List[Dict]): A list of message dictionaries.
518
+
519
+ Returns:
520
+ List[Dict]: A list of dictionaries with the message content updated with names.
521
+ """
522
+ # Make sure there is at least one message
523
+ if not messages:
524
+ return messages
525
+
526
+ messages_changed = 0
527
+ processed_messages = copy.deepcopy(messages)
528
+ for message in processed_messages:
529
+ # Some messages may not have content.
530
+ if not transforms_util.is_content_right_type(
531
+ message.get("content")
532
+ ) or not transforms_util.is_content_right_type(message.get("name")):
533
+ continue
534
+
535
+ if not transforms_util.should_transform_message(message, self._filter_dict, self._exclude_filter):
536
+ continue
537
+
538
+ if transforms_util.is_content_text_empty(message["content"]) or transforms_util.is_content_text_empty(
539
+ message["name"]
540
+ ):
541
+ continue
542
+
543
+ # Get and format the name in the content
544
+ content = message["content"]
545
+ formatted_name = self._format_string.format(name=message["name"])
546
+
547
+ if self._position == "start":
548
+ if not self._deduplicate or not content.startswith(formatted_name):
549
+ message["content"] = f"{formatted_name}{content}"
550
+
551
+ messages_changed += 1
552
+ else:
553
+ if not self._deduplicate or not content.endswith(formatted_name):
554
+ message["content"] = f"{content}{formatted_name}"
555
+
556
+ messages_changed += 1
557
+
558
+ self._messages_changed = messages_changed
559
+ return processed_messages
560
+
561
+ def get_logs(self, pre_transform_messages: List[Dict], post_transform_messages: List[Dict]) -> Tuple[str, bool]:
562
+ if self._messages_changed > 0:
563
+ return f"{self._messages_changed} message(s) changed to incorporate name.", True
564
+ else:
565
+ return "No messages changed to incorporate name.", False
@@ -0,0 +1,120 @@
1
+ # Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+ #
5
+ # Portions derived from https://github.com/microsoft/autogen are under the MIT License.
6
+ # SPDX-License-Identifier: MIT
7
+ from typing import Any, Dict, Hashable, List, Optional, Tuple
8
+
9
+ from autogen import token_count_utils
10
+ from autogen.cache.abstract_cache_base import AbstractCache
11
+ from autogen.oai.openai_utils import filter_config
12
+ from autogen.types import MessageContentType
13
+
14
+
15
+ def cache_key(content: MessageContentType, *args: Hashable) -> str:
16
+ """Calculates the cache key for the given message content and any other hashable args.
17
+
18
+ Args:
19
+ content (MessageContentType): The message content to calculate the cache key for.
20
+ *args: Any additional hashable args to include in the cache key.
21
+ """
22
+ str_keys = [str(key) for key in (content, *args)]
23
+ return "".join(str_keys)
24
+
25
+
26
+ def cache_content_get(cache: Optional[AbstractCache], key: str) -> Optional[Tuple[MessageContentType, ...]]:
27
+ """Retrieves cachedd content from the cache.
28
+
29
+ Args:
30
+ cache (None or AbstractCache): The cache to retrieve the content from. If None, the cache is ignored.
31
+ key (str): The key to retrieve the content from.
32
+ """
33
+ if cache:
34
+ cached_value = cache.get(key)
35
+ if cached_value:
36
+ return cached_value
37
+
38
+
39
+ def cache_content_set(cache: Optional[AbstractCache], key: str, content: MessageContentType, *extra_values):
40
+ """Sets content into the cache.
41
+
42
+ Args:
43
+ cache (None or AbstractCache): The cache to set the content into. If None, the cache is ignored.
44
+ key (str): The key to set the content into.
45
+ content (MessageContentType): The message content to set into the cache.
46
+ *extra_values: Additional values to be passed to the cache.
47
+ """
48
+ if cache:
49
+ cache_value = (content, *extra_values)
50
+ cache.set(key, cache_value)
51
+
52
+
53
+ def min_tokens_reached(messages: List[Dict], min_tokens: Optional[int]) -> bool:
54
+ """Returns True if the total number of tokens in the messages is greater than or equal to the specified value.
55
+
56
+ Args:
57
+ messages (List[Dict]): A list of messages to check.
58
+ """
59
+ if not min_tokens:
60
+ return True
61
+
62
+ messages_tokens = sum(count_text_tokens(msg["content"]) for msg in messages if "content" in msg)
63
+ return messages_tokens >= min_tokens
64
+
65
+
66
+ def count_text_tokens(content: MessageContentType) -> int:
67
+ """Calculates the number of text tokens in the given message content.
68
+
69
+ Args:
70
+ content (MessageContentType): The message content to calculate the number of text tokens for.
71
+ """
72
+ token_count = 0
73
+ if isinstance(content, str):
74
+ token_count = token_count_utils.count_token(content)
75
+ elif isinstance(content, list):
76
+ for item in content:
77
+ if isinstance(item, str):
78
+ token_count += token_count_utils.count_token(item)
79
+ else:
80
+ token_count += count_text_tokens(item.get("text", ""))
81
+ return token_count
82
+
83
+
84
+ def is_content_right_type(content: Any) -> bool:
85
+ """A helper function to check if the passed in content is of the right type."""
86
+ return isinstance(content, (str, list))
87
+
88
+
89
+ def is_content_text_empty(content: MessageContentType) -> bool:
90
+ """Checks if the content of the message does not contain any text.
91
+
92
+ Args:
93
+ content (MessageContentType): The message content to check.
94
+ """
95
+ if isinstance(content, str):
96
+ return content == ""
97
+ elif isinstance(content, list):
98
+ texts = []
99
+ for item in content:
100
+ if isinstance(item, str):
101
+ texts.append(item)
102
+ elif isinstance(item, dict):
103
+ texts.append(item.get("text", ""))
104
+ return not any(texts)
105
+ else:
106
+ return True
107
+
108
+
109
+ def should_transform_message(message: Dict[str, Any], filter_dict: Optional[Dict[str, Any]], exclude: bool) -> bool:
110
+ """Validates whether the transform should be applied according to the filter dictionary.
111
+
112
+ Args:
113
+ message (Dict[str, Any]): The message to validate.
114
+ filter_dict (None or Dict[str, Any]): The filter dictionary to validate against. If None, the transform is always applied.
115
+ exclude (bool): Whether to exclude messages that match the filter dictionary.
116
+ """
117
+ if not filter_dict:
118
+ return True
119
+
120
+ return len(filter_config([message], filter_dict, exclude)) > 0