-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into lpinheiro/chore/update-ci-tests-and-coverage
- Loading branch information
Showing
23 changed files
with
803 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...es/autogen-core/docs/src/reference/python/autogen_ext.cache_store.diskcache.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
autogen\_ext.cache_store.diskcache | ||
================================== | ||
|
||
|
||
.. automodule:: autogen_ext.cache_store.diskcache | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
8 changes: 8 additions & 0 deletions
8
...ckages/autogen-core/docs/src/reference/python/autogen_ext.cache_store.redis.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
autogen\_ext.cache_store.redis | ||
============================== | ||
|
||
|
||
.. automodule:: autogen_ext.cache_store.redis | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
8 changes: 8 additions & 0 deletions
8
...on/packages/autogen-core/docs/src/reference/python/autogen_ext.models.cache.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
autogen\_ext.models.cache | ||
========================= | ||
|
||
|
||
.. automodule:: autogen_ext.models.cache | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
16 changes: 8 additions & 8 deletions
16
...n/packages/autogen-core/docs/src/reference/python/autogen_ext.models.replay.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
autogen\_ext.models.replay | ||
========================== | ||
|
||
|
||
.. automodule:: autogen_ext.models.replay | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
autogen\_ext.models.replay | ||
========================== | ||
|
||
|
||
.. automodule:: autogen_ext.models.replay | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,8 @@ dev = [ | |
"autogen_ext==0.4.3", | ||
|
||
# Documentation tooling | ||
"diskcache", | ||
"redis", | ||
"sphinx-autobuild", | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
python/packages/autogen-core/src/autogen_core/_cache_store.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Dict, Generic, Optional, Protocol, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class CacheStore(Protocol, Generic[T]): | ||
""" | ||
This protocol defines the basic interface for store/cache operations. | ||
Sub-classes should handle the lifecycle of underlying storage. | ||
""" | ||
|
||
def get(self, key: str, default: Optional[T] = None) -> Optional[T]: | ||
""" | ||
Retrieve an item from the store. | ||
Args: | ||
key: The key identifying the item in the store. | ||
default (optional): The default value to return if the key is not found. | ||
Defaults to None. | ||
Returns: | ||
The value associated with the key if found, else the default value. | ||
""" | ||
... | ||
|
||
def set(self, key: str, value: T) -> None: | ||
""" | ||
Set an item in the store. | ||
Args: | ||
key: The key under which the item is to be stored. | ||
value: The value to be stored in the store. | ||
""" | ||
... | ||
|
||
|
||
class InMemoryStore(CacheStore[T]): | ||
def __init__(self) -> None: | ||
self.store: Dict[str, T] = {} | ||
|
||
def get(self, key: str, default: Optional[T] = None) -> Optional[T]: | ||
return self.store.get(key, default) | ||
|
||
def set(self, key: str, value: T) -> None: | ||
self.store[key] = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from unittest.mock import Mock | ||
|
||
from autogen_core import CacheStore, InMemoryStore | ||
|
||
|
||
def test_set_and_get_object_key_value() -> None: | ||
mock_store = Mock(spec=CacheStore) | ||
test_key = "test_key" | ||
test_value = object() | ||
mock_store.set(test_key, test_value) | ||
mock_store.get.return_value = test_value | ||
mock_store.set.assert_called_with(test_key, test_value) | ||
assert mock_store.get(test_key) == test_value | ||
|
||
|
||
def test_get_non_existent_key() -> None: | ||
mock_store = Mock(spec=CacheStore) | ||
key = "non_existent_key" | ||
mock_store.get.return_value = None | ||
assert mock_store.get(key) is None | ||
|
||
|
||
def test_set_overwrite_existing_key() -> None: | ||
mock_store = Mock(spec=CacheStore) | ||
key = "test_key" | ||
initial_value = "initial_value" | ||
new_value = "new_value" | ||
mock_store.set(key, initial_value) | ||
mock_store.set(key, new_value) | ||
mock_store.get.return_value = new_value | ||
mock_store.set.assert_called_with(key, new_value) | ||
assert mock_store.get(key) == new_value | ||
|
||
|
||
def test_inmemory_store() -> None: | ||
store = InMemoryStore[int]() | ||
test_key = "test_key" | ||
test_value = 42 | ||
store.set(test_key, test_value) | ||
assert store.get(test_key) == test_value | ||
|
||
new_value = 2 | ||
store.set(test_key, new_value) | ||
assert store.get(test_key) == new_value | ||
|
||
key = "non_existent_key" | ||
default_value = 99 | ||
assert store.get(key, default_value) == default_value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
26 changes: 26 additions & 0 deletions
26
python/packages/autogen-ext/src/autogen_ext/cache_store/diskcache.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Any, Optional, TypeVar, cast | ||
|
||
import diskcache | ||
from autogen_core import CacheStore | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class DiskCacheStore(CacheStore[T]): | ||
""" | ||
A typed CacheStore implementation that uses diskcache as the underlying storage. | ||
See :class:`~autogen_ext.models.cache.ChatCompletionCache` for an example of usage. | ||
Args: | ||
cache_instance: An instance of diskcache.Cache. | ||
The user is responsible for managing the DiskCache instance's lifetime. | ||
""" | ||
|
||
def __init__(self, cache_instance: diskcache.Cache): # type: ignore[no-any-unimported] | ||
self.cache = cache_instance | ||
|
||
def get(self, key: str, default: Optional[T] = None) -> Optional[T]: | ||
return cast(Optional[T], self.cache.get(key, default)) # type: ignore[reportUnknownMemberType] | ||
|
||
def set(self, key: str, value: T) -> None: | ||
self.cache.set(key, cast(Any, value)) # type: ignore[reportUnknownMemberType] |
29 changes: 29 additions & 0 deletions
29
python/packages/autogen-ext/src/autogen_ext/cache_store/redis.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Any, Optional, TypeVar, cast | ||
|
||
import redis | ||
from autogen_core import CacheStore | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class RedisStore(CacheStore[T]): | ||
""" | ||
A typed CacheStore implementation that uses redis as the underlying storage. | ||
See :class:`~autogen_ext.models.cache.ChatCompletionCache` for an example of usage. | ||
Args: | ||
cache_instance: An instance of `redis.Redis`. | ||
The user is responsible for managing the Redis instance's lifetime. | ||
""" | ||
|
||
def __init__(self, redis_instance: redis.Redis): | ||
self.cache = redis_instance | ||
|
||
def get(self, key: str, default: Optional[T] = None) -> Optional[T]: | ||
value = cast(Optional[T], self.cache.get(key)) | ||
if value is None: | ||
return default | ||
return value | ||
|
||
def set(self, key: str, value: T) -> None: | ||
self.cache.set(key, cast(Any, value)) |
6 changes: 6 additions & 0 deletions
6
python/packages/autogen-ext/src/autogen_ext/models/cache/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from ._chat_completion_cache import CHAT_CACHE_VALUE_TYPE, ChatCompletionCache | ||
|
||
__all__ = [ | ||
"CHAT_CACHE_VALUE_TYPE", | ||
"ChatCompletionCache", | ||
] |
Oops, something went wrong.