Path: blob/main/singlestoredb/management/inference_api.py
469 views
#!/usr/bin/env python1"""SingleStoreDB Cloud Inference API."""2import os3from typing import Any4from typing import Dict5from typing import Optional67from .utils import vars_to_str8from singlestoredb.exceptions import ManagementError9from singlestoredb.management.manager import Manager101112class InferenceAPIInfo(object):13"""14Inference API definition.1516This object is not directly instantiated. It is used in results17of API calls on the :class:`InferenceAPIManager`. See :meth:`InferenceAPIManager.get`.18"""1920service_id: str21model_name: str22name: str23connection_url: str24project_id: str2526def __init__(27self,28service_id: str,29model_name: str,30name: str,31connection_url: str,32project_id: str,33):34self.service_id = service_id35self.connection_url = connection_url36self.model_name = model_name37self.name = name38self.project_id = project_id3940@classmethod41def from_dict(42cls,43obj: Dict[str, Any],44) -> 'InferenceAPIInfo':45"""46Construct a Inference API from a dictionary of values.4748Parameters49----------50obj : dict51Dictionary of values5253Returns54-------55:class:`Job`5657"""58out = cls(59service_id=obj['serviceID'],60project_id=obj['projectID'],61model_name=obj['modelName'],62name=obj['name'],63connection_url=obj['connectionURL'],64)65return out6667def __str__(self) -> str:68"""Return string representation."""69return vars_to_str(self)7071def __repr__(self) -> str:72"""Return string representation."""73return str(self)747576class InferenceAPIManager(object):77"""78SingleStoreDB Inference APIs manager.7980This class should be instantiated using :attr:`Organization.inference_apis`.8182Parameters83----------84manager : InferenceAPIManager, optional85The InferenceAPIManager the InferenceAPIManager belongs to8687See Also88--------89:attr:`InferenceAPI`90"""9192def __init__(self, manager: Optional[Manager]):93self._manager = manager94self.project_id = os.environ.get('SINGLESTOREDB_PROJECT')9596def get(self, model_name: str) -> InferenceAPIInfo:97if self._manager is None:98raise ManagementError(msg='Manager not initialized')99res = self._manager._get(f'inferenceapis/{self.project_id}/{model_name}').json()100return InferenceAPIInfo.from_dict(res)101102103