Skip to content

Commit

Permalink
ENG-1129: aixplain sdk caching functions (#324)
Browse files Browse the repository at this point in the history
* fixed corrupted file

* added languages and licenses

* made changes according to comments

* changes to constants and re-added json checker

* changes to constants and re-added json checker

* added process after save json

* Fixes in the caching function

* Move Cache folder

---------

Co-authored-by: Thiago Castro Ferreira <[email protected]>
  • Loading branch information
xainaz and thiago-aixplain authored Dec 4, 2024
1 parent 3852e2d commit 7732dad
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 45 deletions.
2 changes: 1 addition & 1 deletion aixplain/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
from .supplier import Supplier
from .sort_by import SortBy
from .sort_order import SortOrder
from .response_status import ResponseStatus
from .response_status import ResponseStatus
26 changes: 16 additions & 10 deletions aixplain/enums/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,32 @@
Function Enum
"""

import logging

from aixplain.utils import config
from aixplain.utils.request_utils import _request_with_retry
from enum import Enum
from urllib.parse import urljoin
from aixplain.utils.cache_utils import save_to_cache, load_from_cache, CACHE_FOLDER

CACHE_FILE = f"{CACHE_FOLDER}/functions.json"


def load_functions():
api_key = config.TEAM_API_KEY
backend_url = config.BACKEND_URL

url = urljoin(backend_url, "sdk/functions")
resp = load_from_cache(CACHE_FILE)
if resp is None:
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()
save_to_cache(CACHE_FILE, resp)

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"]: {
Expand All @@ -57,4 +62,5 @@ def load_functions():
}
return functions, functions_input_output


Function, FunctionInputOutput = load_functions()
37 changes: 21 additions & 16 deletions aixplain/enums/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,32 @@
Language Enum
"""

import logging

from aixplain.utils import config
from aixplain.utils.request_utils import _request_with_retry
from enum import Enum
from urllib.parse import urljoin
from aixplain.utils import config
from aixplain.utils.request_utils import _request_with_retry
from aixplain.utils.cache_utils import save_to_cache, load_from_cache, CACHE_FOLDER

CACHE_FILE = f"{CACHE_FOLDER}/languages.json"


def load_languages():
api_key = config.TEAM_API_KEY
backend_url = config.BACKEND_URL

url = urljoin(backend_url, "sdk/languages")

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'Languages 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()
resp = load_from_cache(CACHE_FILE)
if resp is None:
api_key = config.TEAM_API_KEY
backend_url = config.BACKEND_URL

url = urljoin(backend_url, "sdk/languages")

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'Languages 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()
save_to_cache(CACHE_FILE, resp)

languages = {}
for w in resp:
language = w["value"]
Expand Down
41 changes: 24 additions & 17 deletions aixplain/enums/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,36 @@
"""

import logging

from aixplain.utils import config
from aixplain.utils.request_utils import _request_with_retry
from enum import Enum
from urllib.parse import urljoin
from aixplain.utils import config
from aixplain.utils.request_utils import _request_with_retry
from aixplain.utils.cache_utils import save_to_cache, load_from_cache, CACHE_FOLDER

CACHE_FILE = f"{CACHE_FOLDER}/licenses.json"


def load_licenses():
resp = load_from_cache(CACHE_FILE)

try:
api_key = config.TEAM_API_KEY
backend_url = config.BACKEND_URL

url = urljoin(backend_url, "sdk/licenses")

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'Licenses 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()
return Enum("License", {"_".join(w["name"].split()): w["id"] for w in resp}, type=str)
except Exception as e:
if resp is None:
api_key = config.TEAM_API_KEY
backend_url = config.BACKEND_URL

url = urljoin(backend_url, "sdk/licenses")

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'Licenses 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()
save_to_cache(CACHE_FILE, resp)
licenses = {"_".join(w["name"].split()): w["id"] for w in resp}
return Enum("License", licenses, type=str)
except Exception:
logging.exception("License Loading Error")
raise Exception("License Loading Error")

Expand Down
27 changes: 27 additions & 0 deletions aixplain/utils/cache_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import json
import time
import logging

CACHE_DURATION = 24 * 60 * 60
CACHE_FOLDER = ".aixplain_cache"


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):
if os.path.exists(cache_file) is True:
with open(cache_file, "r") as f:
cache_data = json.load(f)
if time.time() - cache_data["timestamp"] < CACHE_DURATION:
return cache_data["data"]
else:
return None
return None
4 changes: 3 additions & 1 deletion aixplain/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

if AIXPLAIN_API_KEY and TEAM_API_KEY:
if AIXPLAIN_API_KEY != TEAM_API_KEY:
raise Exception("Conflicting API keys: 'AIXPLAIN_API_KEY' and 'TEAM_API_KEY' are both provided but do not match. Please provide only one API key.")
raise Exception(
"Conflicting API keys: 'AIXPLAIN_API_KEY' and 'TEAM_API_KEY' are both provided but do not match. Please provide only one API key."
)


if AIXPLAIN_API_KEY and not TEAM_API_KEY:
Expand Down

0 comments on commit 7732dad

Please sign in to comment.