ag2 0.4.1__py3-none-any.whl → 0.4.2b1__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 (161) hide show
  1. ag2-0.4.2b1.dist-info/METADATA +19 -0
  2. ag2-0.4.2b1.dist-info/RECORD +6 -0
  3. ag2-0.4.2b1.dist-info/top_level.txt +1 -0
  4. ag2-0.4.1.dist-info/METADATA +0 -500
  5. ag2-0.4.1.dist-info/RECORD +0 -158
  6. ag2-0.4.1.dist-info/top_level.txt +0 -1
  7. autogen/__init__.py +0 -17
  8. autogen/_pydantic.py +0 -116
  9. autogen/agentchat/__init__.py +0 -42
  10. autogen/agentchat/agent.py +0 -142
  11. autogen/agentchat/assistant_agent.py +0 -85
  12. autogen/agentchat/chat.py +0 -306
  13. autogen/agentchat/contrib/__init__.py +0 -0
  14. autogen/agentchat/contrib/agent_builder.py +0 -788
  15. autogen/agentchat/contrib/agent_eval/agent_eval.py +0 -107
  16. autogen/agentchat/contrib/agent_eval/criterion.py +0 -47
  17. autogen/agentchat/contrib/agent_eval/critic_agent.py +0 -47
  18. autogen/agentchat/contrib/agent_eval/quantifier_agent.py +0 -42
  19. autogen/agentchat/contrib/agent_eval/subcritic_agent.py +0 -48
  20. autogen/agentchat/contrib/agent_eval/task.py +0 -43
  21. autogen/agentchat/contrib/agent_optimizer.py +0 -450
  22. autogen/agentchat/contrib/capabilities/__init__.py +0 -0
  23. autogen/agentchat/contrib/capabilities/agent_capability.py +0 -21
  24. autogen/agentchat/contrib/capabilities/generate_images.py +0 -297
  25. autogen/agentchat/contrib/capabilities/teachability.py +0 -406
  26. autogen/agentchat/contrib/capabilities/text_compressors.py +0 -72
  27. autogen/agentchat/contrib/capabilities/transform_messages.py +0 -92
  28. autogen/agentchat/contrib/capabilities/transforms.py +0 -565
  29. autogen/agentchat/contrib/capabilities/transforms_util.py +0 -120
  30. autogen/agentchat/contrib/capabilities/vision_capability.py +0 -217
  31. autogen/agentchat/contrib/captainagent/tools/__init__.py +0 -0
  32. autogen/agentchat/contrib/captainagent/tools/data_analysis/calculate_correlation.py +0 -41
  33. autogen/agentchat/contrib/captainagent/tools/data_analysis/calculate_skewness_and_kurtosis.py +0 -29
  34. autogen/agentchat/contrib/captainagent/tools/data_analysis/detect_outlier_iqr.py +0 -29
  35. autogen/agentchat/contrib/captainagent/tools/data_analysis/detect_outlier_zscore.py +0 -29
  36. autogen/agentchat/contrib/captainagent/tools/data_analysis/explore_csv.py +0 -22
  37. autogen/agentchat/contrib/captainagent/tools/data_analysis/shapiro_wilk_test.py +0 -31
  38. autogen/agentchat/contrib/captainagent/tools/information_retrieval/arxiv_download.py +0 -26
  39. autogen/agentchat/contrib/captainagent/tools/information_retrieval/arxiv_search.py +0 -55
  40. autogen/agentchat/contrib/captainagent/tools/information_retrieval/extract_pdf_image.py +0 -54
  41. autogen/agentchat/contrib/captainagent/tools/information_retrieval/extract_pdf_text.py +0 -39
  42. autogen/agentchat/contrib/captainagent/tools/information_retrieval/get_wikipedia_text.py +0 -22
  43. autogen/agentchat/contrib/captainagent/tools/information_retrieval/get_youtube_caption.py +0 -35
  44. autogen/agentchat/contrib/captainagent/tools/information_retrieval/image_qa.py +0 -61
  45. autogen/agentchat/contrib/captainagent/tools/information_retrieval/optical_character_recognition.py +0 -62
  46. autogen/agentchat/contrib/captainagent/tools/information_retrieval/perform_web_search.py +0 -48
  47. autogen/agentchat/contrib/captainagent/tools/information_retrieval/scrape_wikipedia_tables.py +0 -34
  48. autogen/agentchat/contrib/captainagent/tools/information_retrieval/transcribe_audio_file.py +0 -22
  49. autogen/agentchat/contrib/captainagent/tools/information_retrieval/youtube_download.py +0 -36
  50. autogen/agentchat/contrib/captainagent/tools/math/calculate_circle_area_from_diameter.py +0 -22
  51. autogen/agentchat/contrib/captainagent/tools/math/calculate_day_of_the_week.py +0 -19
  52. autogen/agentchat/contrib/captainagent/tools/math/calculate_fraction_sum.py +0 -29
  53. autogen/agentchat/contrib/captainagent/tools/math/calculate_matrix_power.py +0 -32
  54. autogen/agentchat/contrib/captainagent/tools/math/calculate_reflected_point.py +0 -17
  55. autogen/agentchat/contrib/captainagent/tools/math/complex_numbers_product.py +0 -26
  56. autogen/agentchat/contrib/captainagent/tools/math/compute_currency_conversion.py +0 -24
  57. autogen/agentchat/contrib/captainagent/tools/math/count_distinct_permutations.py +0 -28
  58. autogen/agentchat/contrib/captainagent/tools/math/evaluate_expression.py +0 -29
  59. autogen/agentchat/contrib/captainagent/tools/math/find_continuity_point.py +0 -35
  60. autogen/agentchat/contrib/captainagent/tools/math/fraction_to_mixed_numbers.py +0 -40
  61. autogen/agentchat/contrib/captainagent/tools/math/modular_inverse_sum.py +0 -23
  62. autogen/agentchat/contrib/captainagent/tools/math/simplify_mixed_numbers.py +0 -37
  63. autogen/agentchat/contrib/captainagent/tools/math/sum_of_digit_factorials.py +0 -16
  64. autogen/agentchat/contrib/captainagent/tools/math/sum_of_primes_below.py +0 -16
  65. autogen/agentchat/contrib/captainagent/tools/requirements.txt +0 -10
  66. autogen/agentchat/contrib/captainagent/tools/tool_description.tsv +0 -34
  67. autogen/agentchat/contrib/captainagent.py +0 -490
  68. autogen/agentchat/contrib/gpt_assistant_agent.py +0 -545
  69. autogen/agentchat/contrib/graph_rag/__init__.py +0 -0
  70. autogen/agentchat/contrib/graph_rag/document.py +0 -30
  71. autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +0 -111
  72. autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py +0 -81
  73. autogen/agentchat/contrib/graph_rag/graph_query_engine.py +0 -56
  74. autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +0 -64
  75. autogen/agentchat/contrib/img_utils.py +0 -390
  76. autogen/agentchat/contrib/llamaindex_conversable_agent.py +0 -123
  77. autogen/agentchat/contrib/llava_agent.py +0 -176
  78. autogen/agentchat/contrib/math_user_proxy_agent.py +0 -471
  79. autogen/agentchat/contrib/multimodal_conversable_agent.py +0 -128
  80. autogen/agentchat/contrib/qdrant_retrieve_user_proxy_agent.py +0 -325
  81. autogen/agentchat/contrib/retrieve_assistant_agent.py +0 -56
  82. autogen/agentchat/contrib/retrieve_user_proxy_agent.py +0 -705
  83. autogen/agentchat/contrib/society_of_mind_agent.py +0 -203
  84. autogen/agentchat/contrib/swarm_agent.py +0 -463
  85. autogen/agentchat/contrib/text_analyzer_agent.py +0 -76
  86. autogen/agentchat/contrib/tool_retriever.py +0 -120
  87. autogen/agentchat/contrib/vectordb/__init__.py +0 -0
  88. autogen/agentchat/contrib/vectordb/base.py +0 -243
  89. autogen/agentchat/contrib/vectordb/chromadb.py +0 -326
  90. autogen/agentchat/contrib/vectordb/mongodb.py +0 -559
  91. autogen/agentchat/contrib/vectordb/pgvectordb.py +0 -958
  92. autogen/agentchat/contrib/vectordb/qdrant.py +0 -334
  93. autogen/agentchat/contrib/vectordb/utils.py +0 -126
  94. autogen/agentchat/contrib/web_surfer.py +0 -305
  95. autogen/agentchat/conversable_agent.py +0 -2908
  96. autogen/agentchat/groupchat.py +0 -1668
  97. autogen/agentchat/user_proxy_agent.py +0 -109
  98. autogen/agentchat/utils.py +0 -207
  99. autogen/browser_utils.py +0 -291
  100. autogen/cache/__init__.py +0 -10
  101. autogen/cache/abstract_cache_base.py +0 -78
  102. autogen/cache/cache.py +0 -182
  103. autogen/cache/cache_factory.py +0 -85
  104. autogen/cache/cosmos_db_cache.py +0 -150
  105. autogen/cache/disk_cache.py +0 -109
  106. autogen/cache/in_memory_cache.py +0 -61
  107. autogen/cache/redis_cache.py +0 -128
  108. autogen/code_utils.py +0 -745
  109. autogen/coding/__init__.py +0 -22
  110. autogen/coding/base.py +0 -113
  111. autogen/coding/docker_commandline_code_executor.py +0 -262
  112. autogen/coding/factory.py +0 -45
  113. autogen/coding/func_with_reqs.py +0 -203
  114. autogen/coding/jupyter/__init__.py +0 -22
  115. autogen/coding/jupyter/base.py +0 -32
  116. autogen/coding/jupyter/docker_jupyter_server.py +0 -164
  117. autogen/coding/jupyter/embedded_ipython_code_executor.py +0 -182
  118. autogen/coding/jupyter/jupyter_client.py +0 -224
  119. autogen/coding/jupyter/jupyter_code_executor.py +0 -161
  120. autogen/coding/jupyter/local_jupyter_server.py +0 -168
  121. autogen/coding/local_commandline_code_executor.py +0 -410
  122. autogen/coding/markdown_code_extractor.py +0 -44
  123. autogen/coding/utils.py +0 -57
  124. autogen/exception_utils.py +0 -46
  125. autogen/extensions/__init__.py +0 -0
  126. autogen/formatting_utils.py +0 -76
  127. autogen/function_utils.py +0 -362
  128. autogen/graph_utils.py +0 -148
  129. autogen/io/__init__.py +0 -15
  130. autogen/io/base.py +0 -105
  131. autogen/io/console.py +0 -43
  132. autogen/io/websockets.py +0 -213
  133. autogen/logger/__init__.py +0 -11
  134. autogen/logger/base_logger.py +0 -140
  135. autogen/logger/file_logger.py +0 -287
  136. autogen/logger/logger_factory.py +0 -29
  137. autogen/logger/logger_utils.py +0 -42
  138. autogen/logger/sqlite_logger.py +0 -459
  139. autogen/math_utils.py +0 -356
  140. autogen/oai/__init__.py +0 -33
  141. autogen/oai/anthropic.py +0 -428
  142. autogen/oai/bedrock.py +0 -606
  143. autogen/oai/cerebras.py +0 -270
  144. autogen/oai/client.py +0 -1148
  145. autogen/oai/client_utils.py +0 -167
  146. autogen/oai/cohere.py +0 -453
  147. autogen/oai/completion.py +0 -1216
  148. autogen/oai/gemini.py +0 -469
  149. autogen/oai/groq.py +0 -281
  150. autogen/oai/mistral.py +0 -279
  151. autogen/oai/ollama.py +0 -582
  152. autogen/oai/openai_utils.py +0 -811
  153. autogen/oai/together.py +0 -343
  154. autogen/retrieve_utils.py +0 -487
  155. autogen/runtime_logging.py +0 -163
  156. autogen/token_count_utils.py +0 -259
  157. autogen/types.py +0 -20
  158. autogen/version.py +0 -7
  159. {ag2-0.4.1.dist-info → ag2-0.4.2b1.dist-info}/LICENSE +0 -0
  160. {ag2-0.4.1.dist-info → ag2-0.4.2b1.dist-info}/NOTICE.md +0 -0
  161. {ag2-0.4.1.dist-info → ag2-0.4.2b1.dist-info}/WHEEL +0 -0
@@ -1,78 +0,0 @@
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 sys
8
- from types import TracebackType
9
- from typing import Any, Optional, Protocol, Type
10
-
11
- if sys.version_info >= (3, 11):
12
- from typing import Self
13
- else:
14
- from typing_extensions import Self
15
-
16
-
17
- class AbstractCache(Protocol):
18
- """
19
- This protocol defines the basic interface for cache operations.
20
- Implementing classes should provide concrete implementations for
21
- these methods to handle caching mechanisms.
22
- """
23
-
24
- def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
25
- """
26
- Retrieve an item from the cache.
27
-
28
- Args:
29
- key (str): The key identifying the item in the cache.
30
- default (optional): The default value to return if the key is not found.
31
- Defaults to None.
32
-
33
- Returns:
34
- The value associated with the key if found, else the default value.
35
- """
36
- ...
37
-
38
- def set(self, key: str, value: Any) -> None:
39
- """
40
- Set an item in the cache.
41
-
42
- Args:
43
- key (str): The key under which the item is to be stored.
44
- value: The value to be stored in the cache.
45
- """
46
- ...
47
-
48
- def close(self) -> None:
49
- """
50
- Close the cache. Perform any necessary cleanup, such as closing network connections or
51
- releasing resources.
52
- """
53
- ...
54
-
55
- def __enter__(self) -> Self:
56
- """
57
- Enter the runtime context related to this object.
58
-
59
- The with statement will bind this method's return value to the target(s)
60
- specified in the as clause of the statement, if any.
61
- """
62
- ...
63
-
64
- def __exit__(
65
- self,
66
- exc_type: Optional[Type[BaseException]],
67
- exc_value: Optional[BaseException],
68
- traceback: Optional[TracebackType],
69
- ) -> None:
70
- """
71
- Exit the runtime context and close the cache.
72
-
73
- Args:
74
- exc_type: The exception type if an exception was raised in the context.
75
- exc_value: The exception value if an exception was raised in the context.
76
- traceback: The traceback if an exception was raised in the context.
77
- """
78
- ...
autogen/cache/cache.py DELETED
@@ -1,182 +0,0 @@
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 __future__ import annotations
8
-
9
- import sys
10
- from types import TracebackType
11
- from typing import Any, Dict, Optional, Type, TypedDict, Union
12
-
13
- from .abstract_cache_base import AbstractCache
14
- from .cache_factory import CacheFactory
15
-
16
- if sys.version_info >= (3, 11):
17
- from typing import Self
18
- else:
19
- from typing_extensions import Self
20
-
21
-
22
- class Cache(AbstractCache):
23
- """
24
- A wrapper class for managing cache configuration and instances.
25
-
26
- This class provides a unified interface for creating and interacting with
27
- different types of cache (e.g., Redis, Disk). It abstracts the underlying
28
- cache implementation details, providing methods for cache operations.
29
-
30
- Attributes:
31
- config (Dict[str, Any]): A dictionary containing cache configuration.
32
- cache: The cache instance created based on the provided configuration.
33
- """
34
-
35
- ALLOWED_CONFIG_KEYS = [
36
- "cache_seed",
37
- "redis_url",
38
- "cache_path_root",
39
- "cosmos_db_config",
40
- ]
41
-
42
- @staticmethod
43
- def redis(cache_seed: Union[str, int] = 42, redis_url: str = "redis://localhost:6379/0") -> "Cache":
44
- """
45
- Create a Redis cache instance.
46
-
47
- Args:
48
- cache_seed (Union[str, int], optional): A seed for the cache. Defaults to 42.
49
- redis_url (str, optional): The URL for the Redis server. Defaults to "redis://localhost:6379/0".
50
-
51
- Returns:
52
- Cache: A Cache instance configured for Redis.
53
- """
54
- return Cache({"cache_seed": cache_seed, "redis_url": redis_url})
55
-
56
- @staticmethod
57
- def disk(cache_seed: Union[str, int] = 42, cache_path_root: str = ".cache") -> "Cache":
58
- """
59
- Create a Disk cache instance.
60
-
61
- Args:
62
- cache_seed (Union[str, int], optional): A seed for the cache. Defaults to 42.
63
- cache_path_root (str, optional): The root path for the disk cache. Defaults to ".cache".
64
-
65
- Returns:
66
- Cache: A Cache instance configured for Disk caching.
67
- """
68
- return Cache({"cache_seed": cache_seed, "cache_path_root": cache_path_root})
69
-
70
- @staticmethod
71
- def cosmos_db(
72
- connection_string: Optional[str] = None,
73
- container_id: Optional[str] = None,
74
- cache_seed: Union[str, int] = 42,
75
- client: Optional[any] = None,
76
- ) -> "Cache":
77
- """
78
- Create a Cosmos DB cache instance with 'autogen_cache' as database ID.
79
-
80
- Args:
81
- connection_string (str, optional): Connection string to the Cosmos DB account.
82
- container_id (str, optional): The container ID for the Cosmos DB account.
83
- cache_seed (Union[str, int], optional): A seed for the cache.
84
- client: Optional[CosmosClient]: Pass an existing Cosmos DB client.
85
- Returns:
86
- Cache: A Cache instance configured for Cosmos DB.
87
- """
88
- cosmos_db_config = {
89
- "connection_string": connection_string,
90
- "database_id": "autogen_cache",
91
- "container_id": container_id,
92
- "client": client,
93
- }
94
- return Cache({"cache_seed": str(cache_seed), "cosmos_db_config": cosmos_db_config})
95
-
96
- def __init__(self, config: Dict[str, Any]):
97
- """
98
- Initialize the Cache with the given configuration.
99
-
100
- Validates the configuration keys and creates the cache instance.
101
-
102
- Args:
103
- config (Dict[str, Any]): A dictionary containing the cache configuration.
104
-
105
- Raises:
106
- ValueError: If an invalid configuration key is provided.
107
- """
108
- self.config = config
109
- # Ensure that the seed is always treated as a string before being passed to any cache factory or stored.
110
- self.config["cache_seed"] = str(self.config.get("cache_seed", 42))
111
-
112
- # validate config
113
- for key in self.config.keys():
114
- if key not in self.ALLOWED_CONFIG_KEYS:
115
- raise ValueError(f"Invalid config key: {key}")
116
- # create cache instance
117
- self.cache = CacheFactory.cache_factory(
118
- seed=self.config["cache_seed"],
119
- redis_url=self.config.get("redis_url"),
120
- cache_path_root=self.config.get("cache_path_root"),
121
- cosmosdb_config=self.config.get("cosmos_db_config"),
122
- )
123
-
124
- def __enter__(self) -> "Cache":
125
- """
126
- Enter the runtime context related to the cache object.
127
-
128
- Returns:
129
- The cache instance for use within a context block.
130
- """
131
- return self.cache.__enter__()
132
-
133
- def __exit__(
134
- self,
135
- exc_type: Optional[Type[BaseException]],
136
- exc_value: Optional[BaseException],
137
- traceback: Optional[TracebackType],
138
- ) -> None:
139
- """
140
- Exit the runtime context related to the cache object.
141
-
142
- Cleans up the cache instance and handles any exceptions that occurred
143
- within the context.
144
-
145
- Args:
146
- exc_type: The exception type if an exception was raised in the context.
147
- exc_value: The exception value if an exception was raised in the context.
148
- traceback: The traceback if an exception was raised in the context.
149
- """
150
- return self.cache.__exit__(exc_type, exc_value, traceback)
151
-
152
- def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
153
- """
154
- Retrieve an item from the cache.
155
-
156
- Args:
157
- key (str): The key identifying the item in the cache.
158
- default (optional): The default value to return if the key is not found.
159
- Defaults to None.
160
-
161
- Returns:
162
- The value associated with the key if found, else the default value.
163
- """
164
- return self.cache.get(key, default)
165
-
166
- def set(self, key: str, value: Any) -> None:
167
- """
168
- Set an item in the cache.
169
-
170
- Args:
171
- key (str): The key under which the item is to be stored.
172
- value: The value to be stored in the cache.
173
- """
174
- self.cache.set(key, value)
175
-
176
- def close(self) -> None:
177
- """
178
- Close the cache.
179
-
180
- Perform any necessary cleanup, such as closing connections or releasing resources.
181
- """
182
- self.cache.close()
@@ -1,85 +0,0 @@
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 logging
8
- import os
9
- from typing import Any, Dict, Optional, Union
10
-
11
- from .abstract_cache_base import AbstractCache
12
- from .disk_cache import DiskCache
13
-
14
-
15
- class CacheFactory:
16
- @staticmethod
17
- def cache_factory(
18
- seed: Union[str, int],
19
- redis_url: Optional[str] = None,
20
- cache_path_root: str = ".cache",
21
- cosmosdb_config: Optional[Dict[str, Any]] = None,
22
- ) -> AbstractCache:
23
- """
24
- Factory function for creating cache instances.
25
-
26
- This function decides whether to create a RedisCache, DiskCache, or CosmosDBCache instance
27
- based on the provided parameters. If RedisCache is available and a redis_url is provided,
28
- a RedisCache instance is created. If connection_string, database_id, and container_id
29
- are provided, a CosmosDBCache is created. Otherwise, a DiskCache instance is used.
30
-
31
- Args:
32
- seed (Union[str, int]): Used as a seed or namespace for the cache.
33
- redis_url (Optional[str]): URL for the Redis server.
34
- cache_path_root (str): Root path for the disk cache.
35
- cosmosdb_config (Optional[Dict[str, str]]): Dictionary containing 'connection_string',
36
- 'database_id', and 'container_id' for Cosmos DB cache.
37
-
38
- Returns:
39
- An instance of RedisCache, DiskCache, or CosmosDBCache.
40
-
41
- Examples:
42
-
43
- Creating a Redis cache
44
-
45
- ```python
46
- redis_cache = cache_factory("myseed", "redis://localhost:6379/0")
47
- ```
48
- Creating a Disk cache
49
-
50
- ```python
51
- disk_cache = cache_factory("myseed", None)
52
- ```
53
-
54
- Creating a Cosmos DB cache:
55
- ```python
56
- cosmos_cache = cache_factory("myseed", cosmosdb_config={
57
- "connection_string": "your_connection_string",
58
- "database_id": "your_database_id",
59
- "container_id": "your_container_id"}
60
- )
61
- ```
62
-
63
- """
64
- if redis_url:
65
- try:
66
- from .redis_cache import RedisCache
67
-
68
- return RedisCache(seed, redis_url)
69
- except ImportError:
70
- logging.warning(
71
- "RedisCache is not available. Checking other cache options. The last fallback is DiskCache."
72
- )
73
-
74
- if cosmosdb_config:
75
- try:
76
- from .cosmos_db_cache import CosmosDBCache
77
-
78
- return CosmosDBCache.create_cache(seed, cosmosdb_config)
79
-
80
- except ImportError:
81
- logging.warning("CosmosDBCache is not available. Fallback to DiskCache.")
82
-
83
- # Default to DiskCache if neither Redis nor Cosmos DB configurations are provided
84
- path = os.path.join(cache_path_root, str(seed))
85
- return DiskCache(os.path.join(".", path))
@@ -1,150 +0,0 @@
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
- # Install Azure Cosmos DB SDK if not already
8
-
9
- import pickle
10
- from typing import Any, Optional, TypedDict, Union
11
-
12
- from azure.cosmos import CosmosClient, PartitionKey, exceptions
13
- from azure.cosmos.exceptions import CosmosResourceNotFoundError
14
-
15
- from autogen.cache.abstract_cache_base import AbstractCache
16
-
17
-
18
- class CosmosDBConfig(TypedDict, total=False):
19
- connection_string: str
20
- database_id: str
21
- container_id: str
22
- cache_seed: Optional[Union[str, int]]
23
- client: Optional[CosmosClient]
24
-
25
-
26
- class CosmosDBCache(AbstractCache):
27
- """
28
- Synchronous implementation of AbstractCache using Azure Cosmos DB NoSQL API.
29
-
30
- This class provides a concrete implementation of the AbstractCache
31
- interface using Azure Cosmos DB for caching data, with synchronous operations.
32
-
33
- Attributes:
34
- seed (Union[str, int]): A seed or namespace used as a partition key.
35
- client (CosmosClient): The Cosmos DB client used for caching.
36
- container: The container instance used for caching.
37
- """
38
-
39
- def __init__(self, seed: Union[str, int], cosmosdb_config: CosmosDBConfig):
40
- """
41
- Initialize the CosmosDBCache instance.
42
-
43
- Args:
44
- seed (Union[str, int]): A seed or namespace for the cache, used as a partition key.
45
- connection_string (str): The connection string for the Cosmos DB account.
46
- container_id (str): The container ID to be used for caching.
47
- client (Optional[CosmosClient]): An existing CosmosClient instance to be used for caching.
48
- """
49
- self.seed = str(seed)
50
- self.client = cosmosdb_config.get("client") or CosmosClient.from_connection_string(
51
- cosmosdb_config["connection_string"]
52
- )
53
- database_id = cosmosdb_config.get("database_id", "autogen_cache")
54
- self.database = self.client.get_database_client(database_id)
55
- container_id = cosmosdb_config.get("container_id")
56
- self.container = self.database.create_container_if_not_exists(
57
- id=container_id, partition_key=PartitionKey(path="/partitionKey")
58
- )
59
-
60
- @classmethod
61
- def create_cache(cls, seed: Union[str, int], cosmosdb_config: CosmosDBConfig):
62
- """
63
- Factory method to create a CosmosDBCache instance based on the provided configuration.
64
- This method decides whether to use an existing CosmosClient or create a new one.
65
- """
66
- if "client" in cosmosdb_config and isinstance(cosmosdb_config["client"], CosmosClient):
67
- return cls.from_existing_client(seed, **cosmosdb_config)
68
- else:
69
- return cls.from_config(seed, cosmosdb_config)
70
-
71
- @classmethod
72
- def from_config(cls, seed: Union[str, int], cosmosdb_config: CosmosDBConfig):
73
- return cls(str(seed), cosmosdb_config)
74
-
75
- @classmethod
76
- def from_connection_string(cls, seed: Union[str, int], connection_string: str, database_id: str, container_id: str):
77
- config = {"connection_string": connection_string, "database_id": database_id, "container_id": container_id}
78
- return cls(str(seed), config)
79
-
80
- @classmethod
81
- def from_existing_client(cls, seed: Union[str, int], client: CosmosClient, database_id: str, container_id: str):
82
- config = {"client": client, "database_id": database_id, "container_id": container_id}
83
- return cls(str(seed), config)
84
-
85
- def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
86
- """
87
- Retrieve an item from the Cosmos DB cache.
88
-
89
- Args:
90
- key (str): The key identifying the item in the cache.
91
- default (optional): The default value to return if the key is not found.
92
-
93
- Returns:
94
- The deserialized value associated with the key if found, else the default value.
95
- """
96
- try:
97
- response = self.container.read_item(item=key, partition_key=str(self.seed))
98
- return pickle.loads(response["data"])
99
- except CosmosResourceNotFoundError:
100
- return default
101
- except Exception as e:
102
- # Log the exception or rethrow after logging if needed
103
- # Consider logging or handling the error appropriately here
104
- raise e
105
-
106
- def set(self, key: str, value: Any) -> None:
107
- """
108
- Set an item in the Cosmos DB cache.
109
-
110
- Args:
111
- key (str): The key under which the item is to be stored.
112
- value: The value to be stored in the cache.
113
-
114
- Notes:
115
- The value is serialized using pickle before being stored.
116
- """
117
- try:
118
- serialized_value = pickle.dumps(value)
119
- item = {"id": key, "partitionKey": str(self.seed), "data": serialized_value}
120
- self.container.upsert_item(item)
121
- except Exception as e:
122
- # Log or handle exception
123
- raise e
124
-
125
- def close(self) -> None:
126
- """
127
- Close the Cosmos DB client.
128
-
129
- Perform any necessary cleanup, such as closing network connections.
130
- """
131
- # CosmosClient doesn"t require explicit close in the current SDK
132
- # If you created the client inside this class, you should close it if necessary
133
- pass
134
-
135
- def __enter__(self):
136
- """
137
- Context management entry.
138
-
139
- Returns:
140
- self: The instance itself.
141
- """
142
- return self
143
-
144
- def __exit__(self, exc_type: Optional[type], exc_value: Optional[Exception], traceback: Optional[Any]) -> None:
145
- """
146
- Context management exit.
147
-
148
- Perform cleanup actions such as closing the Cosmos DB client.
149
- """
150
- self.close()
@@ -1,109 +0,0 @@
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 sys
8
- from types import TracebackType
9
- from typing import Any, Optional, Type, Union
10
-
11
- import diskcache
12
-
13
- from .abstract_cache_base import AbstractCache
14
-
15
- if sys.version_info >= (3, 11):
16
- from typing import Self
17
- else:
18
- from typing_extensions import Self
19
-
20
-
21
- class DiskCache(AbstractCache):
22
- """
23
- Implementation of AbstractCache using the DiskCache library.
24
-
25
- This class provides a concrete implementation of the AbstractCache
26
- interface using the diskcache library for caching data on disk.
27
-
28
- Attributes:
29
- cache (diskcache.Cache): The DiskCache instance used for caching.
30
-
31
- Methods:
32
- __init__(self, seed): Initializes the DiskCache with the given seed.
33
- get(self, key, default=None): Retrieves an item from the cache.
34
- set(self, key, value): Sets an item in the cache.
35
- close(self): Closes the cache.
36
- __enter__(self): Context management entry.
37
- __exit__(self, exc_type, exc_value, traceback): Context management exit.
38
- """
39
-
40
- def __init__(self, seed: Union[str, int]):
41
- """
42
- Initialize the DiskCache instance.
43
-
44
- Args:
45
- seed (Union[str, int]): A seed or namespace for the cache. This is used to create
46
- a unique storage location for the cache data.
47
-
48
- """
49
- self.cache = diskcache.Cache(seed)
50
-
51
- def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
52
- """
53
- Retrieve an item from the cache.
54
-
55
- Args:
56
- key (str): The key identifying the item in the cache.
57
- default (optional): The default value to return if the key is not found.
58
- Defaults to None.
59
-
60
- Returns:
61
- The value associated with the key if found, else the default value.
62
- """
63
- return self.cache.get(key, default)
64
-
65
- def set(self, key: str, value: Any) -> None:
66
- """
67
- Set an item in the cache.
68
-
69
- Args:
70
- key (str): The key under which the item is to be stored.
71
- value: The value to be stored in the cache.
72
- """
73
- self.cache.set(key, value)
74
-
75
- def close(self) -> None:
76
- """
77
- Close the cache.
78
-
79
- Perform any necessary cleanup, such as closing file handles or
80
- releasing resources.
81
- """
82
- self.cache.close()
83
-
84
- def __enter__(self) -> Self:
85
- """
86
- Enter the runtime context related to the object.
87
-
88
- Returns:
89
- self: The instance itself.
90
- """
91
- return self
92
-
93
- def __exit__(
94
- self,
95
- exc_type: Optional[Type[BaseException]],
96
- exc_value: Optional[BaseException],
97
- traceback: Optional[TracebackType],
98
- ) -> None:
99
- """
100
- Exit the runtime context related to the object.
101
-
102
- Perform cleanup actions such as closing the cache.
103
-
104
- Args:
105
- exc_type: The exception type if an exception was raised in the context.
106
- exc_value: The exception value if an exception was raised in the context.
107
- traceback: The traceback if an exception was raised in the context.
108
- """
109
- self.close()
@@ -1,61 +0,0 @@
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 sys
8
- from types import TracebackType
9
- from typing import Any, Dict, Optional, Type, Union
10
-
11
- from .abstract_cache_base import AbstractCache
12
-
13
- if sys.version_info >= (3, 11):
14
- from typing import Self
15
- else:
16
- from typing_extensions import Self
17
-
18
-
19
- class InMemoryCache(AbstractCache):
20
-
21
- def __init__(self, seed: Union[str, int] = ""):
22
- self._seed = str(seed)
23
- self._cache: Dict[str, Any] = {}
24
-
25
- def _prefixed_key(self, key: str) -> str:
26
- separator = "_" if self._seed else ""
27
- return f"{self._seed}{separator}{key}"
28
-
29
- def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
30
- result = self._cache.get(self._prefixed_key(key))
31
- if result is None:
32
- return default
33
- return result
34
-
35
- def set(self, key: str, value: Any) -> None:
36
- self._cache[self._prefixed_key(key)] = value
37
-
38
- def close(self) -> None:
39
- pass
40
-
41
- def __enter__(self) -> Self:
42
- """
43
- Enter the runtime context related to the object.
44
-
45
- Returns:
46
- self: The instance itself.
47
- """
48
- return self
49
-
50
- def __exit__(
51
- self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
52
- ) -> None:
53
- """
54
- Exit the runtime context related to the object.
55
-
56
- Args:
57
- exc_type: The exception type if an exception was raised in the context.
58
- exc_value: The exception value if an exception was raised in the context.
59
- traceback: The traceback if an exception was raised in the context.
60
- """
61
- self.close()