-
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.
Make cache store typed, and improve docs
- Loading branch information
1 parent
18e5431
commit ffc14ef
Showing
26 changed files
with
346 additions
and
89 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
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
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: |
8 changes: 8 additions & 0 deletions
8
...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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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
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
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.