-
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.
[Core] implement redis cache mode (#1222)
* implement redis cache mode, if redis_url is set in the llm_config then it will try to use this. also adds a test to validate both the existing and the redis cache behavior. * PR feedback, add unit tests * more PR feedback, move the new style cache to a context manager * Update agent_chat.md * more PR feedback, remove tests from contrib and have them run with the normal jobs * doc * updated * Update website/docs/Use-Cases/agent_chat.md Co-authored-by: Chi Wang <wang.chi@microsoft.com> * update docs * update docs; let openaiwrapper to use cache object * typo * Update website/docs/Use-Cases/enhanced_inference.md Co-authored-by: Chi Wang <wang.chi@microsoft.com> * save previous client cache and reset it after send/a_send * a_run_chat --------- Co-authored-by: Vijay Ramesh <vijay@regrello.com> Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com>
- Loading branch information
1 parent
e97b639
commit ee6ad8d
Showing
21 changed files
with
1,149 additions
and
17 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
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.
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,90 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class AbstractCache(ABC): | ||
""" | ||
Abstract base class for cache implementations. | ||
This class defines the basic interface for cache operations. | ||
Implementing classes should provide concrete implementations for | ||
these methods to handle caching mechanisms. | ||
""" | ||
|
||
@abstractmethod | ||
def get(self, key, default=None): | ||
""" | ||
Retrieve an item from the cache. | ||
Abstract method that must be implemented by subclasses to | ||
retrieve an item from the 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. | ||
Defaults to None. | ||
Returns: | ||
The value associated with the key if found, else the default value. | ||
Raises: | ||
NotImplementedError: If the subclass does not implement this method. | ||
""" | ||
|
||
@abstractmethod | ||
def set(self, key, value): | ||
""" | ||
Set an item in the cache. | ||
Abstract method that must be implemented by subclasses to | ||
store an item in the cache. | ||
Args: | ||
key (str): The key under which the item is to be stored. | ||
value: The value to be stored in the cache. | ||
Raises: | ||
NotImplementedError: If the subclass does not implement this method. | ||
""" | ||
|
||
@abstractmethod | ||
def close(self): | ||
""" | ||
Close the cache. | ||
Abstract method that should be implemented by subclasses to | ||
perform any necessary cleanup, such as closing network connections or | ||
releasing resources. | ||
Raises: | ||
NotImplementedError: If the subclass does not implement this method. | ||
""" | ||
|
||
@abstractmethod | ||
def __enter__(self): | ||
""" | ||
Enter the runtime context related to this object. | ||
The with statement will bind this method’s return value to the target(s) | ||
specified in the as clause of the statement, if any. | ||
Raises: | ||
NotImplementedError: If the subclass does not implement this method. | ||
""" | ||
|
||
@abstractmethod | ||
def __exit__(self, exc_type, exc_value, traceback): | ||
""" | ||
Exit the runtime context and close the cache. | ||
Abstract method that should be implemented by subclasses to handle | ||
the exit from a with statement. It is responsible for resource | ||
release and cleanup. | ||
Args: | ||
exc_type: The exception type if an exception was raised in the context. | ||
exc_value: The exception value if an exception was raised in the context. | ||
traceback: The traceback if an exception was raised in the context. | ||
Raises: | ||
NotImplementedError: If the subclass does not implement this method. | ||
""" |
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,137 @@ | ||
import os | ||
from typing import Dict, Any | ||
|
||
from autogen.cache.cache_factory import CacheFactory | ||
|
||
|
||
class Cache: | ||
""" | ||
A wrapper class for managing cache configuration and instances. | ||
This class provides a unified interface for creating and interacting with | ||
different types of cache (e.g., Redis, Disk). It abstracts the underlying | ||
cache implementation details, providing methods for cache operations. | ||
Attributes: | ||
config (Dict[str, Any]): A dictionary containing cache configuration. | ||
cache: The cache instance created based on the provided configuration. | ||
Methods: | ||
redis(cache_seed=42, redis_url="redis://localhost:6379/0"): Static method to create a Redis cache instance. | ||
disk(cache_seed=42, cache_path_root=".cache"): Static method to create a Disk cache instance. | ||
__init__(self, config): Initializes the Cache with the given configuration. | ||
__enter__(self): Context management entry, returning the cache instance. | ||
__exit__(self, exc_type, exc_value, traceback): Context management exit. | ||
get(self, key, default=None): Retrieves an item from the cache. | ||
set(self, key, value): Sets an item in the cache. | ||
close(self): Closes the cache. | ||
""" | ||
|
||
ALLOWED_CONFIG_KEYS = ["cache_seed", "redis_url", "cache_path_root"] | ||
|
||
@staticmethod | ||
def redis(cache_seed=42, redis_url="redis://localhost:6379/0"): | ||
""" | ||
Create a Redis cache instance. | ||
Args: | ||
cache_seed (int, optional): A seed for the cache. Defaults to 42. | ||
redis_url (str, optional): The URL for the Redis server. Defaults to "redis://localhost:6379/0". | ||
Returns: | ||
Cache: A Cache instance configured for Redis. | ||
""" | ||
return Cache({"cache_seed": cache_seed, "redis_url": redis_url}) | ||
|
||
@staticmethod | ||
def disk(cache_seed=42, cache_path_root=".cache"): | ||
""" | ||
Create a Disk cache instance. | ||
Args: | ||
cache_seed (int, optional): A seed for the cache. Defaults to 42. | ||
cache_path_root (str, optional): The root path for the disk cache. Defaults to ".cache". | ||
Returns: | ||
Cache: A Cache instance configured for Disk caching. | ||
""" | ||
return Cache({"cache_seed": cache_seed, "cache_path_root": cache_path_root}) | ||
|
||
def __init__(self, config: Dict[str, Any]): | ||
""" | ||
Initialize the Cache with the given configuration. | ||
Validates the configuration keys and creates the cache instance. | ||
Args: | ||
config (Dict[str, Any]): A dictionary containing the cache configuration. | ||
Raises: | ||
ValueError: If an invalid configuration key is provided. | ||
""" | ||
self.config = config | ||
# validate config | ||
for key in self.config.keys(): | ||
if key not in self.ALLOWED_CONFIG_KEYS: | ||
raise ValueError(f"Invalid config key: {key}") | ||
# create cache instance | ||
self.cache = CacheFactory.cache_factory( | ||
self.config.get("cache_seed", "42"), | ||
self.config.get("redis_url", None), | ||
self.config.get("cache_path_root", None), | ||
) | ||
|
||
def __enter__(self): | ||
""" | ||
Enter the runtime context related to the cache object. | ||
Returns: | ||
The cache instance for use within a context block. | ||
""" | ||
return self.cache.__enter__() | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
""" | ||
Exit the runtime context related to the cache object. | ||
Cleans up the cache instance and handles any exceptions that occurred | ||
within the context. | ||
Args: | ||
exc_type: The exception type if an exception was raised in the context. | ||
exc_value: The exception value if an exception was raised in the context. | ||
traceback: The traceback if an exception was raised in the context. | ||
""" | ||
return self.cache.__exit__(exc_type, exc_value, traceback) | ||
|
||
def get(self, key, default=None): | ||
""" | ||
Retrieve an item from the 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. | ||
Defaults to None. | ||
Returns: | ||
The value associated with the key if found, else the default value. | ||
""" | ||
return self.cache.get(key, default) | ||
|
||
def set(self, key, value): | ||
""" | ||
Set an item in the cache. | ||
Args: | ||
key (str): The key under which the item is to be stored. | ||
value: The value to be stored in the cache. | ||
""" | ||
self.cache.set(key, value) | ||
|
||
def close(self): | ||
""" | ||
Close the cache. | ||
Perform any necessary cleanup, such as closing connections or releasing resources. | ||
""" | ||
self.cache.close() |
Oops, something went wrong.