Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoGen cache using Azure Cosmos DB #2327

Merged
merged 170 commits into from
Apr 26, 2024
Merged
Changes from 7 commits
Commits
Show all changes
170 commits
Select commit Hold shift + click to select a range
5cf7cba
Create cosmos_db_cache.py
wmwxwa Apr 8, 2024
05bc607
Update cosmos_db_cache.py
wmwxwa Apr 9, 2024
d44123c
Update cosmos_db_cache.py
wmwxwa Apr 9, 2024
e220217
Update cosmos_db_cache.py
wmwxwa Apr 9, 2024
0abe66f
Merge branch 'main' into patch-1
wmwxwa Apr 9, 2024
99514a0
Update cosmos_db_cache.py
wmwxwa Apr 9, 2024
b014425
Merge branch 'main' into patch-1
wmwxwa Apr 10, 2024
f6174c9
Update cosmos_db_cache.py
wmwxwa Apr 11, 2024
ddd7a64
Update cosmos_db_cache.py
wmwxwa Apr 11, 2024
3f06613
Merge branch 'main' into patch-1
wmwxwa Apr 11, 2024
6ea087b
Create test_cosmos_db_cache.py
wmwxwa Apr 11, 2024
332d270
Update cosmos_db_cache.py
wmwxwa Apr 12, 2024
e3aca04
Update test_cosmos_db_cache.py
wmwxwa Apr 12, 2024
bd6ec85
Update cosmos_db_cache.py
wmwxwa Apr 12, 2024
ca2dca2
Update test_cosmos_db_cache.py
wmwxwa Apr 12, 2024
cd145d7
Update cosmos_db_cache.py
wmwxwa Apr 12, 2024
e780f08
Update test_cosmos_db_cache.py
wmwxwa Apr 12, 2024
70489f9
Update cosmos_db_cache.py
wmwxwa Apr 12, 2024
b7ba3dd
Update cosmos_db_cache.py
wmwxwa Apr 12, 2024
e35bdf8
Update test_cosmos_db_cache.py
wmwxwa Apr 12, 2024
c27fb7e
Update cosmos_db_cache.py
wmwxwa Apr 12, 2024
1658fdd
Update cosmos_db_cache.py
wmwxwa Apr 12, 2024
83ee9d7
Update cache.py
wmwxwa Apr 12, 2024
825555a
Update cache_factory.py
wmwxwa Apr 12, 2024
f38d0a8
Update cache.py
wmwxwa Apr 12, 2024
3c2d66a
Update cache_factory.py
wmwxwa Apr 12, 2024
816a672
Update test_cache.py
wmwxwa Apr 12, 2024
10e8c6e
Update test_cache.py
wmwxwa Apr 12, 2024
100a76e
Merge branch 'main' into patch-1
wmwxwa Apr 12, 2024
1de08ae
Merge branch 'main' into patch-1
wmwxwa Apr 15, 2024
f71ee81
Merge branch 'main' into patch-1
wmwxwa Apr 16, 2024
7dd7680
Update cache.py
wmwxwa Apr 16, 2024
79f53f7
Update llm-caching.md
wmwxwa Apr 16, 2024
ae09c71
Update cache.py
wmwxwa Apr 16, 2024
e7c916d
Update cache.py
wmwxwa Apr 16, 2024
06a7486
Update cache.py
wmwxwa Apr 16, 2024
bdb274e
Update cache_factory.py
wmwxwa Apr 16, 2024
64e0afb
Update cosmos_db_cache.py
wmwxwa Apr 16, 2024
88b408f
Update cache.py
wmwxwa Apr 16, 2024
04d1927
Update cosmos_db_cache.py
wmwxwa Apr 16, 2024
2dad10e
Update cosmos_db_cache.py
wmwxwa Apr 16, 2024
18c3caa
Merge branch 'main' into patch-1
wmwxwa Apr 17, 2024
a1865f0
Merge branch 'main' into patch-1
wmwxwa Apr 17, 2024
e115e6f
Update cosmos_db_cache.py
wmwxwa Apr 17, 2024
1e9e6e7
Update cosmos_db_cache.py
wmwxwa Apr 17, 2024
de57ad5
Merge branch 'main' into patch-1
wmwxwa Apr 18, 2024
5a7a2b0
Merge branch 'main' into patch-1
wmwxwa Apr 19, 2024
a2d727e
Update build.yml
wmwxwa Apr 19, 2024
32bdb61
Update build.yml
wmwxwa Apr 19, 2024
3f0779e
Update test_cosmos_db_cache.py
wmwxwa Apr 19, 2024
56a8a96
Update test_cosmos_db_cache.py
wmwxwa Apr 19, 2024
f2c914e
Update test_cosmos_db_cache.py
wmwxwa Apr 19, 2024
ea635cf
Update test_cosmos_db_cache.py
wmwxwa Apr 19, 2024
14805b0
Update test_cosmos_db_cache.py
wmwxwa Apr 19, 2024
c8b963d
Update autogen/cache/cache_factory.py
wmwxwa Apr 19, 2024
e4dd065
Update cache_factory.py
wmwxwa Apr 19, 2024
7653d06
Update cosmos_db_cache.py
wmwxwa Apr 19, 2024
f569999
Update cache.py
wmwxwa Apr 19, 2024
4c1c64b
Update cache_factory.py
wmwxwa Apr 19, 2024
a3bd190
Update cosmos_db_cache.py
wmwxwa Apr 19, 2024
7aeaa47
Update .github/workflows/build.yml
wmwxwa Apr 22, 2024
533833d
Merge branch 'main' into patch-1
wmwxwa Apr 22, 2024
0890c97
Update cache.py
wmwxwa Apr 22, 2024
a18fd0c
Update cache.py
wmwxwa Apr 22, 2024
96d7c6f
Update cache.py
wmwxwa Apr 22, 2024
045c136
Update cache_factory.py
wmwxwa Apr 22, 2024
4084506
Update cosmos_db_cache.py
wmwxwa Apr 22, 2024
0551474
Update cache.py
wmwxwa Apr 22, 2024
c5e5be9
Update cache_factory.py
wmwxwa Apr 22, 2024
ee82458
Update cosmos_db_cache.py
wmwxwa Apr 22, 2024
959e0a0
Update cache.py
wmwxwa Apr 22, 2024
e08d8cd
Update cache_factory.py
wmwxwa Apr 22, 2024
2f9b1b0
Update cache_factory.py
wmwxwa Apr 22, 2024
acbc6e3
Update cache.py
wmwxwa Apr 22, 2024
e7f6fad
Update cosmos_db_cache.py
wmwxwa Apr 22, 2024
1dac41c
Update cache.py
wmwxwa Apr 22, 2024
9316cb0
Update cache.py
wmwxwa Apr 22, 2024
fd60c6b
Update cache_factory.py
wmwxwa Apr 22, 2024
f62c75f
Update cache.py
wmwxwa Apr 22, 2024
399d76f
Update cache_factory.py
wmwxwa Apr 22, 2024
44fbcf4
Update cosmos_db_cache.py
wmwxwa Apr 22, 2024
1408af4
Update cache.py
wmwxwa Apr 22, 2024
42432f7
Update cache_factory.py
wmwxwa Apr 22, 2024
6a6e5c8
Update cosmos_db_cache.py
wmwxwa Apr 22, 2024
6395028
Update cache.py
wmwxwa Apr 22, 2024
1459e75
Update cache_factory.py
wmwxwa Apr 22, 2024
9ff1512
Update cosmos_db_cache.py
wmwxwa Apr 22, 2024
5d79166
Update test_cache.py
wmwxwa Apr 22, 2024
da24523
Update test_cache.py
wmwxwa Apr 22, 2024
1f1d9cf
Update test_cache.py
wmwxwa Apr 22, 2024
44c3a90
Update cache.py
wmwxwa Apr 22, 2024
fa79a98
Update cache.py
wmwxwa Apr 22, 2024
531d4f0
Update cache_factory.py
wmwxwa Apr 22, 2024
64293ce
Update cache.py
wmwxwa Apr 22, 2024
208b14d
Update cache_factory.py
wmwxwa Apr 22, 2024
95bfdb2
Update test_cache.py
wmwxwa Apr 22, 2024
23b3a5d
Update test_cache.py
wmwxwa Apr 22, 2024
55743f5
Update cache.py
wmwxwa Apr 22, 2024
d538847
Update cache.py
wmwxwa Apr 22, 2024
562f46e
Update test_cache.py
wmwxwa Apr 22, 2024
35d3d74
Update cache.py
wmwxwa Apr 22, 2024
f719446
Update cache.py
wmwxwa Apr 22, 2024
e2da161
Update cache_factory.py
wmwxwa Apr 22, 2024
534188f
Update cache_factory.py
wmwxwa Apr 22, 2024
934e025
Update cache_factory.py
wmwxwa Apr 22, 2024
2b4fb18
Update cache_factory.py
wmwxwa Apr 22, 2024
75d8dc7
Update cache_factory.py
wmwxwa Apr 22, 2024
087e540
Merge branch 'main' into patch-1
wmwxwa Apr 23, 2024
33c5538
Merge branch 'main' into patch-1
wmwxwa Apr 23, 2024
6a1c1e6
Update build.yml
wmwxwa Apr 23, 2024
5fddc54
Update test_cache.py
wmwxwa Apr 23, 2024
b5d5fc1
Update test_cosmos_db_cache.py
wmwxwa Apr 23, 2024
da6d4c5
Update test_cache.py
wmwxwa Apr 23, 2024
1b6201b
Merge branch 'main' into patch-1
wmwxwa Apr 23, 2024
383e738
Update cache.py
wmwxwa Apr 23, 2024
b4eea01
Update cache_factory.py
wmwxwa Apr 23, 2024
dd6b1ac
Update cosmos_db_cache.py
wmwxwa Apr 23, 2024
3439b15
Update test_cache.py
wmwxwa Apr 24, 2024
9a7aa68
Update test_cosmos_db_cache.py
wmwxwa Apr 24, 2024
0ee17d8
Update test_cache.py
wmwxwa Apr 24, 2024
228ff73
Update test_cosmos_db_cache.py
wmwxwa Apr 24, 2024
9877222
Update build.yml
wmwxwa Apr 24, 2024
6447fdb
Update build.yml
wmwxwa Apr 24, 2024
f4a0bbc
Update build.yml
wmwxwa Apr 24, 2024
8f38d77
Update build.yml
wmwxwa Apr 24, 2024
2a0654d
Merge branch 'main' into patch-1
wmwxwa Apr 24, 2024
f590ff8
Update cache_factory.py
wmwxwa Apr 24, 2024
f97dccd
Update cache.py
wmwxwa Apr 24, 2024
8d32447
Update cosmos_db_cache.py
wmwxwa Apr 24, 2024
9feb3c9
Update cache.py
wmwxwa Apr 24, 2024
657abf6
Update build.yml
wmwxwa Apr 24, 2024
28d6c6b
Update test_cache.py
wmwxwa Apr 24, 2024
84d54e5
Update test_cache.py
wmwxwa Apr 24, 2024
5c5d661
Update test_cache.py
wmwxwa Apr 24, 2024
4257682
Update test_cache.py
wmwxwa Apr 24, 2024
bf8a2e8
Update cache_factory.py
wmwxwa Apr 24, 2024
ca30023
Update cosmos_db_cache.py
wmwxwa Apr 24, 2024
f0a5d4f
Update test_cache.py
wmwxwa Apr 24, 2024
8defeec
Update test_cache.py
wmwxwa Apr 24, 2024
4e97d9a
Update test_cache.py
wmwxwa Apr 24, 2024
74224dc
Update test_cache.py
wmwxwa Apr 24, 2024
343c3a1
Update test_cosmos_db_cache.py
wmwxwa Apr 24, 2024
173c801
Update cosmos_db_cache.py
wmwxwa Apr 24, 2024
5794d2f
Update test_cosmos_db_cache.py
wmwxwa Apr 24, 2024
1a3c1ce
Update cosmos_db_cache.py
wmwxwa Apr 24, 2024
b74e949
Update cosmos_db_cache.py
wmwxwa Apr 24, 2024
0a39f49
Update test_cache.py
wmwxwa Apr 24, 2024
2123bbf
Merge branch 'main' into patch-1
wmwxwa Apr 25, 2024
f674e88
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
8bdd0da
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
df4dd70
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
8ce3f47
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
6fb4f73
Merge branch 'main' into patch-1
wmwxwa Apr 25, 2024
f2cb949
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
04dcc95
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
27b76a6
Update cosmos_db_cache.py
wmwxwa Apr 25, 2024
c6e20fa
Update test_cache.py
wmwxwa Apr 25, 2024
518d974
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
3c9cbc3
Update cache.py
wmwxwa Apr 25, 2024
00e844f
Update cache.py
wmwxwa Apr 25, 2024
a3b6794
Update cache.py
wmwxwa Apr 25, 2024
0ff53db
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
9ee1e8f
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
01cdc19
Update cache.py
wmwxwa Apr 25, 2024
50e1fdc
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
fd82eff
Merge branch 'main' into patch-1
wmwxwa Apr 25, 2024
65365e5
Update cosmos_db_cache.py
wmwxwa Apr 25, 2024
53a071b
Update cache.py
wmwxwa Apr 25, 2024
c153330
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
37ec58c
Update test_cosmos_db_cache.py
wmwxwa Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions autogen/cache/cosmos_db_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#Install Azure Cosmos DB SDK if not already
# pip install azure-cosmos

import pickle
from typing import Any, Optional, Union
from azure.cosmos import PartitionKey, CosmosClient, exceptions
from autogen.cache.abstract_cache_base import AbstractCache

class CosmosDBCache(AbstractCache):
"""
Synchronous implementation of AbstractCache using Azure Cosmos DB NoSQL API.

This class provides a concrete implementation of the AbstractCache
interface using Azure Cosmos DB for caching data, with synchronous operations.

Attributes:
seed (Union[str, int]): A seed or namespace used as a partition key.
client (CosmosClient): The Cosmos DB client used for caching.
container: The container instance used for caching.
"""
def __init__(self, seed: Union[str, int], connection_string: str, database_id: str, container_id: str, client: Optional[CosmosClient] = None):
"""
Initialize the CosmosDBCache instance.

Args:
seed (Union[str, int]): A seed or namespace for the cache, used as a partition key.
connection_string (str): The connection string for the Cosmos DB account.
database_id (str): The database ID to be used.
container_id (str): The container ID to be used for caching.
client (Optional[CosmosClient]): An existing CosmosClient instance to be used for caching.
"""
self.seed = seed
self.client = client if client else CosmosClient.from_connection_string(connection_string)
wmwxwa marked this conversation as resolved.
Show resolved Hide resolved
self.database = self.client.get_database_client(database_id)
self.container = self.database.get_container_client(container_id)
self.container = database.get_container_client(container_id)

if not self.container.exists():
self.database.create_container(id=container_id, partition_key=PartitionKey(path='/partitionKey'))

def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
"""
Retrieve an item from the Cosmos DB cache.

Args:
key (str): The key identifying the item in the cache.
default (optional): The default value to return if the key is not found.

Returns:
The deserialized value associated with the key if found, else the default value.
"""
try:
response = self.container.read_item(item=key, partition_key=str(self.seed))
return pickle.loads(response['data'])
except CosmosResourceNotFoundError:
return default
except Exception as e:
# Log the exception or rethrow after logging if needed
# Consider logging or handling the error appropriately here
raise e

def set(self, key: str, value: Any) -> None:
"""
Set an item in the Cosmos DB cache.

Args:
key (str): The key under which the item is to be stored.
value: The value to be stored in the cache.

Notes:
The value is serialized using pickle before being stored.
"""
serialized_value = pickle.dumps(value)
item = {'id': key, 'partitionKey': str(self.seed), 'data': serialized_value}
self.container.upsert_item(item)

def close(self) -> None:
"""
Close the Cosmos DB client.

Perform any necessary cleanup, such as closing network connections.
"""
# CosmosClient doesn't require explicit close in the current SDK
# If you created the client inside this class, you should close it if necessary
wmwxwa marked this conversation as resolved.
Show resolved Hide resolved
pass

async def __enter__(self):
wmwxwa marked this conversation as resolved.
Show resolved Hide resolved
"""
Context management entry.

Returns:
self: The instance itself.
"""
return self

def __exit__(self, exc_type: Optional[type], exc_value: Optional[Exception], traceback: Optional[Any]) -> None:
"""
Context management exit.

Perform cleanup actions such as closing the Cosmos DB client.
"""
self.close()
Loading