-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
13 changed files
with
492 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import json | ||
from enum import Enum | ||
from json import JSONDecodeError | ||
from typing import Optional | ||
|
||
from extensions.ext_redis import redis_client | ||
|
||
|
||
class ToolParameterCacheType(Enum): | ||
PARAMETER = "tool_parameter" | ||
|
||
class ToolParameterCache: | ||
def __init__(self, | ||
tenant_id: str, | ||
provider: str, | ||
tool_name: str, | ||
cache_type: ToolParameterCacheType | ||
): | ||
self.cache_key = f"{cache_type.value}_secret:tenant_id:{tenant_id}:provider:{provider}:tool_name:{tool_name}" | ||
|
||
def get(self) -> Optional[dict]: | ||
""" | ||
Get cached model provider credentials. | ||
:return: | ||
""" | ||
cached_tool_parameter = redis_client.get(self.cache_key) | ||
if cached_tool_parameter: | ||
try: | ||
cached_tool_parameter = cached_tool_parameter.decode('utf-8') | ||
cached_tool_parameter = json.loads(cached_tool_parameter) | ||
except JSONDecodeError: | ||
return None | ||
|
||
return cached_tool_parameter | ||
else: | ||
return None | ||
|
||
def set(self, parameters: dict) -> None: | ||
""" | ||
Cache model provider credentials. | ||
:param credentials: provider credentials | ||
:return: | ||
""" | ||
redis_client.setex(self.cache_key, 86400, json.dumps(parameters)) | ||
|
||
def delete(self) -> None: | ||
""" | ||
Delete cached model provider credentials. | ||
:return: | ||
""" | ||
redis_client.delete(self.cache_key) |
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
Oops, something went wrong.