66from dataclasses import dataclass
77from enum import Enum
88from typing import List , Union
9+ from datetime import datetime , timedelta
10+ from threading import Lock
11+ from cachetools import TTLCache
912
1013import oci
11-
1214from ads .aqua import logger
1315from ads .aqua .base import AquaApp
1416from ads .aqua .exception import AquaRuntimeError
@@ -80,13 +82,20 @@ class AquaModelApp(AquaApp):
8082 Retrieves details of an Aqua model by its unique identifier.
8183 list(compartment_id: str = None, project_id: str = None, **kwargs) -> List[AquaModelSummary]:
8284 Lists all Aqua models within a specified compartment and/or project.
85+ clear_model_list_cache()
86+ Allows clear list model cache items from the service models compartment.
8387
8488 Note:
8589 This class is designed to work within the Oracle Cloud Infrastructure
8690 and requires proper configuration and authentication set up to interact
8791 with OCI services.
8892 """
8993
94+ _service_models_cache = TTLCache (
95+ maxsize = 10 , ttl = timedelta (hours = 5 ), timer = datetime .now
96+ )
97+ _cache_lock = Lock ()
98+
9099 def create (
91100 self , model_id : str , project_id : str , compartment_id : str = None , ** kwargs
92101 ) -> DataScienceModel :
@@ -134,10 +143,7 @@ def create(
134143 .with_defined_metadata_list (service_model .defined_metadata_list )
135144 .with_provenance_metadata (service_model .provenance_metadata )
136145 # TODO: decide what kwargs will be needed.
137- .create (
138- model_by_reference = True ,
139- ** kwargs
140- )
146+ .create (model_by_reference = True , ** kwargs )
141147 )
142148 logger .debug (
143149 f"Aqua Model { custom_model .id } created with the service model { model_id } "
@@ -177,8 +183,9 @@ def list(
177183 ) -> List ["AquaModelSummary" ]:
178184 """Lists all Aqua models within a specified compartment and/or project.
179185 If `compartment_id` is not specified, the method defaults to returning
180- the service models within the pre-configured default compartment.
181-
186+ the service models within the pre-configured default compartment. By default, the list
187+ of models in the service compartment are cached. Use clear_model_list_cache() to invalidate
188+ the cache.
182189
183190 Parameters
184191 ----------
@@ -199,6 +206,11 @@ def list(
199206 logger .info (f"Fetching custom models from compartment_id={ compartment_id } ." )
200207 models = self ._rqs (compartment_id )
201208 else :
209+ if ODSC_MODEL_COMPARTMENT_OCID in self ._service_models_cache .keys ():
210+ logger .info (
211+ f"Returning service models list in { ODSC_MODEL_COMPARTMENT_OCID } from cache."
212+ )
213+ return self ._service_models_cache .get (ODSC_MODEL_COMPARTMENT_OCID )
202214 logger .info (
203215 f"Fetching service models from compartment_id={ ODSC_MODEL_COMPARTMENT_OCID } "
204216 )
@@ -213,7 +225,6 @@ def list(
213225 )
214226
215227 aqua_models = []
216- # TODO: build index.json for service model as caching if needed.
217228
218229 for model in models :
219230 aqua_models .append (
@@ -223,8 +234,35 @@ def list(
223234 )
224235 )
225236
237+ if not compartment_id :
238+ self ._service_models_cache .__setitem__ (
239+ key = ODSC_MODEL_COMPARTMENT_OCID , value = aqua_models
240+ )
241+
226242 return aqua_models
227243
244+ def clear_model_list_cache (
245+ self ,
246+ ):
247+ """
248+ Allows user to clear list model cache items from the service models compartment.
249+ Returns
250+ -------
251+ dict with the key used, and True if cache has the key that needs to be deleted.
252+ """
253+ res = {}
254+ logger .info (f"Clearing _service_models_cache" )
255+ with self ._cache_lock :
256+ if ODSC_MODEL_COMPARTMENT_OCID in self ._service_models_cache .keys ():
257+ self ._service_models_cache .pop (key = ODSC_MODEL_COMPARTMENT_OCID )
258+ res = {
259+ "key" : {
260+ "compartment_id" : ODSC_MODEL_COMPARTMENT_OCID ,
261+ },
262+ "cache_deleted" : True ,
263+ }
264+ return res
265+
228266 def _process_model (
229267 self , model : Union ["ModelSummary" , "Model" , "ResourceSummary" ], region : str
230268 ) -> dict :
0 commit comments