Path: blob/main/singlestoredb/management/inference_api.py
801 views
#!/usr/bin/env python1"""SingleStoreDB Cloud Inference API."""2import os3from typing import Any4from typing import Dict5from typing import List6from typing import Optional78from .utils import vars_to_str9from singlestoredb.exceptions import ManagementError10from singlestoredb.management.manager import Manager111213class ModelOperationResult(object):14"""15Result of a model start or stop operation.1617Attributes18----------19name : str20Name of the model21status : str22Current status of the model (e.g., 'Active', 'Initializing', 'Suspended')23hosting_platform : str24Hosting platform (e.g., 'Nova', 'Amazon', 'Azure')25"""2627def __init__(28self,29name: str,30status: str,31hosting_platform: str,32):33self.name = name34self.status = status35self.hosting_platform = hosting_platform3637@classmethod38def from_start_response(cls, response: Dict[str, Any]) -> 'ModelOperationResult':39"""40Create a ModelOperationResult from a start operation response.4142Parameters43----------44response : dict45Response from the start endpoint4647Returns48-------49ModelOperationResult5051"""52return cls(53name=response.get('modelName', ''),54status='Initializing',55hosting_platform=response.get('hostingPlatform', ''),56)5758@classmethod59def from_stop_response(cls, response: Dict[str, Any]) -> 'ModelOperationResult':60"""61Create a ModelOperationResult from a stop operation response.6263Parameters64----------65response : dict66Response from the stop endpoint6768Returns69-------70ModelOperationResult7172"""73return cls(74name=response.get('name', ''),75status=response.get('status', 'Suspended'),76hosting_platform=response.get('hostingPlatform', ''),77)7879@classmethod80def from_drop_response(cls, response: Dict[str, Any]) -> 'ModelOperationResult':81"""82Create a ModelOperationResult from a drop operation response.8384Parameters85----------86response : dict87Response from the drop endpoint8889Returns90-------91ModelOperationResult9293"""94return cls(95name=response.get('name', ''),96status=response.get('status', 'Deleted'),97hosting_platform=response.get('hostingPlatform', ''),98)99100@classmethod101def from_show_response(cls, response: Dict[str, Any]) -> 'ModelOperationResult':102"""103Create a ModelOperationResult from a show operation response.104105Parameters106----------107response : dict108Response from the show endpoint (single model info)109110Returns111-------112ModelOperationResult113114"""115return cls(116name=response.get('name', ''),117status=response.get('status', ''),118hosting_platform=response.get('hostingPlatform', ''),119)120121def get_message(self) -> str:122"""123Get a human-readable message about the operation.124125Returns126-------127str128Message describing the operation result129130"""131return f'Model is {self.status}'132133def __str__(self) -> str:134"""Return string representation."""135return vars_to_str(self)136137def __repr__(self) -> str:138"""Return string representation."""139return str(self)140141142class InferenceAPIInfo(object):143"""144Inference API definition.145146This object is not directly instantiated. It is used in results147of API calls on the :class:`InferenceAPIManager`. See :meth:`InferenceAPIManager.get`.148"""149150service_id: str151model_name: str152name: str153connection_url: str154internal_connection_url: str155project_id: str156hosting_platform: str157_manager: Optional['InferenceAPIManager']158159def __init__(160self,161service_id: str,162model_name: str,163name: str,164connection_url: str,165internal_connection_url: str,166project_id: str,167hosting_platform: str,168manager: Optional['InferenceAPIManager'] = None,169):170self.service_id = service_id171self.connection_url = connection_url172self.internal_connection_url = internal_connection_url173self.model_name = model_name174self.name = name175self.project_id = project_id176self.hosting_platform = hosting_platform177self._manager = manager178179@classmethod180def from_dict(181cls,182obj: Dict[str, Any],183) -> 'InferenceAPIInfo':184"""185Construct a Inference API from a dictionary of values.186187Parameters188----------189obj : dict190Dictionary of values191192Returns193-------194:class:`Job`195196"""197out = cls(198service_id=obj['serviceID'],199project_id=obj['projectID'],200model_name=obj['modelName'],201name=obj['name'],202connection_url=obj['connectionURL'],203internal_connection_url=obj['internalConnectionURL'],204hosting_platform=obj['hostingPlatform'],205)206return out207208def __str__(self) -> str:209"""Return string representation."""210return vars_to_str(self)211212def __repr__(self) -> str:213"""Return string representation."""214return str(self)215216def start(self) -> ModelOperationResult:217"""218Start this inference API model.219220Returns221-------222ModelOperationResult223Result object containing status information about the started model224225"""226if self._manager is None:227raise ManagementError(msg='No manager associated with this inference API')228return self._manager.start(self.name)229230def stop(self) -> ModelOperationResult:231"""232Stop this inference API model.233234Returns235-------236ModelOperationResult237Result object containing status information about the stopped model238239"""240if self._manager is None:241raise ManagementError(msg='No manager associated with this inference API')242return self._manager.stop(self.name)243244def drop(self) -> ModelOperationResult:245"""246Drop this inference API model.247248Returns249-------250ModelOperationResult251Result object containing status information about the dropped model252253"""254if self._manager is None:255raise ManagementError(msg='No manager associated with this inference API')256return self._manager.drop(self.name)257258259class InferenceAPIManager(object):260"""261SingleStoreDB Inference APIs manager.262263This class should be instantiated using :attr:`Organization.inference_apis`.264265Parameters266----------267manager : InferenceAPIManager, optional268The InferenceAPIManager the InferenceAPIManager belongs to269270See Also271--------272:attr:`InferenceAPI`273"""274275def __init__(self, manager: Optional[Manager]):276self._manager = manager277self.project_id = os.environ.get('SINGLESTOREDB_PROJECT')278279def get(self, model_name: str) -> InferenceAPIInfo:280if self._manager is None:281raise ManagementError(msg='Manager not initialized')282res = self._manager._get(f'inferenceapis/{self.project_id}/{model_name}').json()283inference_api = InferenceAPIInfo.from_dict(res)284inference_api._manager = self # Associate the manager285return inference_api286287def start(self, model_name: str) -> ModelOperationResult:288"""289Start an inference API model.290291Parameters292----------293model_name : str294Name of the model to start295296Returns297-------298ModelOperationResult299Result object containing status information about the started model300301"""302if self._manager is None:303raise ManagementError(msg='Manager not initialized')304res = self._manager._post(f'models/{model_name}/start')305return ModelOperationResult.from_start_response(res.json())306307def stop(self, model_name: str) -> ModelOperationResult:308"""309Stop an inference API model.310311Parameters312----------313model_name : str314Name of the model to stop315316Returns317-------318ModelOperationResult319Result object containing status information about the stopped model320321"""322if self._manager is None:323raise ManagementError(msg='Manager not initialized')324res = self._manager._post(f'models/{model_name}/stop')325return ModelOperationResult.from_stop_response(res.json())326327def show(self) -> List[ModelOperationResult]:328"""329Show all inference APIs in the project.330331Returns332-------333List[ModelOperationResult]334List of ModelOperationResult objects with status information335336"""337if self._manager is None:338raise ManagementError(msg='Manager not initialized')339res = self._manager._get('models').json()340return [ModelOperationResult.from_show_response(api) for api in res]341342def drop(self, model_name: str) -> ModelOperationResult:343"""344Drop an inference API model.345346Parameters347----------348model_name : str349Name of the model to drop350351Returns352-------353ModelOperationResult354Result object containing status information about the dropped model355356"""357if self._manager is None:358raise ManagementError(msg='Manager not initialized')359res = self._manager._delete(f'models/{model_name}')360return ModelOperationResult.from_drop_response(res.json())361362363