-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENG-1129: aixplain sdk caching functions #324
Merged
+92
−45
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e315105
fixed corrupted file
xainaz 68124d9
added languages and licenses
xainaz c172647
made changes according to comments
xainaz aa03071
changes to constants and re-added json checker
xainaz 10f6b89
changes to constants and re-added json checker
xainaz 63790d3
added process after save json
xainaz 3556d18
Fixes in the caching function
thiago-aixplain 318cd00
Merge changes
thiago-aixplain f735507
Move Cache folder
thiago-aixplain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
import json | ||
import time | ||
import logging | ||
|
||
CACHE_DURATION = 24 * 60 * 60 | ||
|
||
def save_to_cache(cache_file, data): | ||
try: | ||
os.makedirs(os.path.dirname(cache_file), exist_ok=True) | ||
with open(cache_file, "w") as f: | ||
json.dump({"timestamp": time.time(), "data": data}, f) | ||
except Exception as e: | ||
logging.error(f"Failed to save cache to {cache_file}: {e}") | ||
|
||
def load_from_cache(cache_file): | ||
try: | ||
with open(cache_file, "r") as f: | ||
cache_data = json.load(f) | ||
if time.time() - cache_data["timestamp"] < CACHE_DURATION: | ||
logging.info(f"Loaded valid cache from {cache_file}.") | ||
return cache_data["data"] | ||
else: | ||
logging.info(f"Cache expired for {cache_file}.") | ||
return None | ||
except FileNotFoundError: | ||
return None | ||
except json.JSONDecodeError: | ||
return None |
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 |
---|---|---|
|
@@ -27,34 +27,51 @@ | |
from aixplain.utils.request_utils import _request_with_retry | ||
from enum import Enum | ||
from urllib.parse import urljoin | ||
import logging | ||
from .cache_utils import save_to_cache, load_from_cache | ||
|
||
CACHE_FILE = ".aixplain_cache/functions.json" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add these constants into aixplain.utils.config There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to add the CACHE_FILE into the right constant place. |
||
|
||
def load_functions(): | ||
api_key = config.TEAM_API_KEY | ||
backend_url = config.BACKEND_URL | ||
|
||
url = urljoin(backend_url, "sdk/functions") | ||
|
||
headers = {"x-api-key": api_key, "Content-Type": "application/json"} | ||
r = _request_with_retry("get", url, headers=headers) | ||
if not 200 <= r.status_code < 300: | ||
raise Exception( | ||
f'Functions could not be loaded, probably due to the set API key (e.g. "{api_key}") is not valid. For help, please refer to the documentation (https://github.com/aixplain/aixplain#api-key-setup)' | ||
) | ||
resp = r.json() | ||
functions = Enum("Function", {w["id"].upper().replace("-", "_"): w["id"] for w in resp["items"]}, type=str) | ||
functions_input_output = { | ||
function["id"]: { | ||
"input": { | ||
input_data_object["dataType"] | ||
for input_data_object in function["params"] | ||
if input_data_object["required"] is True | ||
}, | ||
"output": {output_data_object["dataType"] for output_data_object in function["output"]}, | ||
"spec": function, | ||
cached_data = load_from_cache(CACHE_FILE) | ||
if cached_data: | ||
return Enum("Function", cached_data["enum"], type=str), cached_data["input_output"] | ||
|
||
|
||
try: | ||
api_key = config.TEAM_API_KEY | ||
backend_url = config.BACKEND_URL | ||
url = urljoin(backend_url, "sdk/functions") | ||
headers = {"x-api-key": api_key, "Content-Type": "application/json"} | ||
|
||
r = _request_with_retry("get", url, headers=headers) | ||
if not 200 <= r.status_code < 300: | ||
raise Exception("Functions could not be loaded. Invalid API key or server issue.") | ||
|
||
resp = r.json() | ||
functions_enum = { | ||
w["id"].upper().replace("-", "_"): w["id"] for w in resp["items"] | ||
} | ||
for function in resp["items"] | ||
} | ||
return functions, functions_input_output | ||
functions_input_output = { | ||
function["id"]: { | ||
"input": { | ||
input_data_object["dataType"] | ||
for input_data_object in function["params"] | ||
if input_data_object["required"] is True | ||
}, | ||
"output": {output_data_object["dataType"] for output_data_object in function["output"]}, | ||
"spec": function, | ||
} | ||
for function in resp["items"] | ||
} | ||
|
||
save_to_cache(CACHE_FILE, {"enum": functions_enum, "input_output": functions_input_output}) | ||
|
||
return Enum("Function", functions_enum, type=str), functions_input_output | ||
|
||
except Exception as e: | ||
logging.error(f"Failed to load functions from API: {e}") | ||
raise Exception("Unable to load functions from cache or API.") | ||
|
||
|
||
Function, FunctionInputOutput = load_functions() |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add this file in this folder: https://github.com/aixplain/aiXplain/tree/main/aixplain/utils