mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 02:34:29 +00:00
(feat) add docstring for caching
This commit is contained in:
parent
882ac46727
commit
69065e9864
1 changed files with 45 additions and 0 deletions
|
@ -90,6 +90,21 @@ class Cache():
|
|||
port = None,
|
||||
password = None
|
||||
):
|
||||
"""
|
||||
Initializes the cache based on the given type.
|
||||
|
||||
Args:
|
||||
type (str, optional): The type of cache to initialize. Defaults to "local".
|
||||
host (str, optional): The host address for the Redis cache. Required if type is "redis".
|
||||
port (int, optional): The port number for the Redis cache. Required if type is "redis".
|
||||
password (str, optional): The password for the Redis cache. Required if type is "redis".
|
||||
|
||||
Raises:
|
||||
ValueError: If an invalid cache type is provided.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
if type == "redis":
|
||||
self.cache = RedisCache(host, port, password)
|
||||
if type == "local":
|
||||
|
@ -102,6 +117,16 @@ class Cache():
|
|||
litellm.success_callback.append("cache")
|
||||
|
||||
def get_cache_key(self, *args, **kwargs):
|
||||
"""
|
||||
Get the cache key for the given arguments.
|
||||
|
||||
Args:
|
||||
*args: args to litellm.completion() or embedding()
|
||||
**kwargs: kwargs to litellm.completion() or embedding()
|
||||
|
||||
Returns:
|
||||
str: The cache key generated from the arguments, or None if no cache key could be generated.
|
||||
"""
|
||||
prompt = get_prompt(*args, **kwargs)
|
||||
if prompt is not None:
|
||||
cache_key = prompt
|
||||
|
@ -122,6 +147,16 @@ class Cache():
|
|||
time.sleep(0.02)
|
||||
|
||||
def get_cache(self, *args, **kwargs):
|
||||
"""
|
||||
Retrieves the cached result for the given arguments.
|
||||
|
||||
Args:
|
||||
*args: args to litellm.completion() or embedding()
|
||||
**kwargs: kwargs to litellm.completion() or embedding()
|
||||
|
||||
Returns:
|
||||
The cached result if it exists, otherwise None.
|
||||
"""
|
||||
try: # never block execution
|
||||
cache_key = self.get_cache_key(*args, **kwargs)
|
||||
if cache_key is not None:
|
||||
|
@ -136,6 +171,16 @@ class Cache():
|
|||
return None
|
||||
|
||||
def add_cache(self, result, *args, **kwargs):
|
||||
"""
|
||||
Adds a result to the cache.
|
||||
|
||||
Args:
|
||||
*args: args to litellm.completion() or embedding()
|
||||
**kwargs: kwargs to litellm.completion() or embedding()
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
try:
|
||||
cache_key = self.get_cache_key(*args, **kwargs)
|
||||
# print("adding to cache", cache_key, result)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue