|
| 1 | +#!/usr/bin/env python |
| 2 | +"""SingleStoreDB Cloud Inference API.""" |
| 3 | +import os |
| 4 | +from typing import Any |
| 5 | +from typing import Dict |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +from .utils import vars_to_str |
| 9 | +from singlestoredb.exceptions import ManagementError |
| 10 | +from singlestoredb.management.manager import Manager |
| 11 | + |
| 12 | + |
| 13 | +class InferenceAPIInfo(object): |
| 14 | + """ |
| 15 | + Inference API definition. |
| 16 | +
|
| 17 | + This object is not directly instantiated. It is used in results |
| 18 | + of API calls on the :class:`InferenceAPIManager`. See :meth:`InferenceAPIManager.get`. |
| 19 | + """ |
| 20 | + |
| 21 | + service_id: str |
| 22 | + model_name: str |
| 23 | + name: str |
| 24 | + connection_url: str |
| 25 | + project_id: str |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + service_id: str, |
| 30 | + model_name: str, |
| 31 | + name: str, |
| 32 | + connection_url: str, |
| 33 | + project_id: str, |
| 34 | + ): |
| 35 | + self.service_id = service_id |
| 36 | + self.connection_url = connection_url |
| 37 | + self.model_name = model_name |
| 38 | + self.name = name |
| 39 | + self.project_id = project_id |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def from_dict( |
| 43 | + cls, |
| 44 | + obj: Dict[str, Any], |
| 45 | + ) -> 'InferenceAPIInfo': |
| 46 | + """ |
| 47 | + Construct a Inference API from a dictionary of values. |
| 48 | +
|
| 49 | + Parameters |
| 50 | + ---------- |
| 51 | + obj : dict |
| 52 | + Dictionary of values |
| 53 | +
|
| 54 | + Returns |
| 55 | + ------- |
| 56 | + :class:`Job` |
| 57 | +
|
| 58 | + """ |
| 59 | + out = cls( |
| 60 | + service_id=obj['serviceID'], |
| 61 | + project_id=obj['projectID'], |
| 62 | + model_name=obj['modelName'], |
| 63 | + name=obj['name'], |
| 64 | + connection_url=obj['connectionURL'], |
| 65 | + ) |
| 66 | + return out |
| 67 | + |
| 68 | + def __str__(self) -> str: |
| 69 | + """Return string representation.""" |
| 70 | + return vars_to_str(self) |
| 71 | + |
| 72 | + def __repr__(self) -> str: |
| 73 | + """Return string representation.""" |
| 74 | + return str(self) |
| 75 | + |
| 76 | + |
| 77 | +class InferenceAPIManager(object): |
| 78 | + """ |
| 79 | + SingleStoreDB Inference APIs manager. |
| 80 | +
|
| 81 | + This class should be instantiated using :attr:`Organization.inference_apis`. |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + manager : InferenceAPIManager, optional |
| 86 | + The InferenceAPIManager the InferenceAPIManager belongs to |
| 87 | +
|
| 88 | + See Also |
| 89 | + -------- |
| 90 | + :attr:`InferenceAPI` |
| 91 | + """ |
| 92 | + |
| 93 | + def __init__(self, manager: Optional[Manager]): |
| 94 | + self._manager = manager |
| 95 | + self.project_id = os.environ.get('SINGLESTOREDB_PROJECT') |
| 96 | + |
| 97 | + def get(self, model_name: str) -> InferenceAPIInfo: |
| 98 | + if self._manager is None: |
| 99 | + raise ManagementError(msg='Manager not initialized') |
| 100 | + res = self._manager._get(f'inferenceapis/{self.project_id}/{model_name}').json() |
| 101 | + return InferenceAPIInfo.from_dict(res) |
0 commit comments