-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
121 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ repos: | |
args: | ||
- --no-warn-no-return | ||
- --ignore-missing-imports | ||
additional_dependencies: [redis==5.0.1] |
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,22 @@ | ||
from typing import Generic, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class CacheManager(Generic[T]): | ||
""" | ||
Abstract base class for a cache manager to handle caching operations. | ||
Specific cache backends should extend this class and implement its methods. | ||
""" | ||
|
||
async def get(self, key: str) -> T | None: | ||
raise NotImplementedError() | ||
|
||
async def set(self, key: str, value: T) -> None: | ||
raise NotImplementedError() | ||
|
||
async def delete(self, key: str) -> None: | ||
raise NotImplementedError() | ||
|
||
async def close(self) -> None: | ||
raise NotImplementedError() |
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,40 @@ | ||
import asyncio | ||
import pickle | ||
|
||
from agency_swarm import Agency | ||
from redis import asyncio as aioredis | ||
|
||
from nalgonda.caching.cache_manager import CacheManager | ||
from nalgonda.settings import settings | ||
|
||
|
||
class RedisCacheManager(CacheManager): | ||
"""Redis cache manager | ||
This class implements the CacheManager interface using Redis as the cache backend. | ||
""" | ||
|
||
def __init__(self): | ||
"""Initializes the Redis cache manager""" | ||
self.redis = aioredis.from_url(str(settings.redis_dsn), decode_responses=True) | ||
|
||
def __del__(self): | ||
"""Wait for the Redis connection to close""" | ||
asyncio.run(self.close()) | ||
|
||
async def get(self, key: str) -> Agency | None: | ||
"""Gets the value for the given key from the cache""" | ||
serialized_data = await self.redis.get(key) | ||
return pickle.loads(serialized_data) if serialized_data else None | ||
|
||
async def set(self, key: str, value: Agency) -> None: | ||
"""Sets the value for the given key in the cache""" | ||
serialized_data = pickle.dumps(value) | ||
await self.redis.set(key, serialized_data) | ||
|
||
async def delete(self, key: str) -> None: | ||
"""Deletes the value for the given key from the cache""" | ||
await self.redis.delete(key) | ||
|
||
async def close(self) -> None: | ||
"""Closes the Redis connection""" | ||
await self.redis.close() |
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,15 +1,19 @@ | ||
from pydantic import Field | ||
from pydantic import AliasChoices, Field, RedisDsn | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
LATEST_GPT_MODEL = "gpt-4-1106-preview" | ||
|
||
|
||
class Settings(BaseSettings): | ||
openai_api_key: str | None = Field(default=None, validation_alias="OPENAI_API_KEY") | ||
gpt_model: str = Field(default=LATEST_GPT_MODEL, validation_alias="GPT_MODEL") | ||
google_credentials: str | None = Field(default=None, validation_alias="GOOGLE_CREDENTIALS") | ||
openai_api_key: str | None = Field(default=None) | ||
gpt_model: str = Field(default=LATEST_GPT_MODEL) | ||
google_credentials: str | None = Field(default=None) | ||
redis_dsn: RedisDsn = Field( | ||
"redis://localhost:6379/1", | ||
validation_alias=AliasChoices("service_redis_dsn", "redis_url"), | ||
) | ||
|
||
model_config = SettingsConfigDict(env_file=".env", case_sensitive=True) | ||
model_config = SettingsConfigDict(env_file=".env") | ||
|
||
|
||
settings = Settings() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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