Skip to content

Commit

Permalink
Merge pull request #1499 from duarteocarmo/patch-2
Browse files Browse the repository at this point in the history
Adds s3_path prefix so that we can save objects in predifined location in s3 bucket
  • Loading branch information
ishaan-jaff authored Jan 19, 2024
2 parents 8338d04 + daa399b commit 8fef1a1
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions litellm/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ def __init__(
s3_aws_secret_access_key=None,
s3_aws_session_token=None,
s3_config=None,
s3_path=None,
**kwargs,
):
import boto3

self.bucket_name = s3_bucket_name
self.key_prefix = s3_path.rstrip("/") + "/" if s3_path else ""
# Create an S3 client with custom endpoint URL
self.s3_client = boto3.client(
"s3",
Expand All @@ -155,6 +157,8 @@ def set_cache(self, key, value, **kwargs):
ttl = kwargs.get("ttl", None)
# Convert value to JSON before storing in S3
serialized_value = json.dumps(value)
key = self.key_prefix + key

if ttl is not None:
cache_control = f"immutable, max-age={ttl}, s-maxage={ttl}"
import datetime
Expand Down Expand Up @@ -193,6 +197,8 @@ def get_cache(self, key, **kwargs):
import boto3, botocore

try:
key = self.key_prefix + key

print_verbose(f"Get S3 Cache: key: {key}")
# Download the data from S3
cached_response = self.s3_client.get_object(
Expand Down

0 comments on commit 8fef1a1

Please sign in to comment.