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,78 @@
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 ADDED
@@ -0,0 +1,182 @@
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()
@@ -0,0 +1,85 @@
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))
@@ -0,0 +1,150 @@
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()
@@ -0,0 +1,109 @@
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()
@@ -0,0 +1,61 @@
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()