Skip to content

Commit 890d822

Browse files
fusion commands to start and stop model
1 parent f10eef4 commit 890d822

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

singlestoredb/fusion/handlers/models.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ..result import FusionSQLResult
99
from .files import ShowFilesHandler
1010
from .utils import get_file_space
11+
from .utils import get_inference_api
1112

1213

1314
class ShowModelsHandler(ShowFilesHandler):
@@ -248,3 +249,77 @@ def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
248249

249250

250251
DropModelsHandler.register(overwrite=True)
252+
253+
254+
class StartModelHandler(SQLHandler):
255+
"""
256+
START MODEL model_name ;
257+
258+
# Model Name
259+
model_name = '<model-name>'
260+
261+
Description
262+
-----------
263+
Starts an inference API model.
264+
265+
Arguments
266+
---------
267+
* ``<model-name>``: Name of the model to start.
268+
269+
Example
270+
--------
271+
The following command starts a model::
272+
273+
START MODEL my_model;
274+
275+
See Also
276+
--------
277+
* ``STOP MODEL model_name``
278+
* ``SHOW MODELS``
279+
280+
""" # noqa: E501
281+
282+
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
283+
inference_api = get_inference_api(params)
284+
inference_api.start()
285+
return None
286+
287+
288+
StartModelHandler.register(overwrite=True)
289+
290+
291+
class StopModelHandler(SQLHandler):
292+
"""
293+
STOP MODEL model_name ;
294+
295+
# Model Name
296+
model_name = '<model-name>'
297+
298+
Description
299+
-----------
300+
Stops an inference API model.
301+
302+
Arguments
303+
---------
304+
* ``<model-name>``: Name of the model to stop.
305+
306+
Example
307+
--------
308+
The following command stops a model::
309+
310+
STOP MODEL my_model;
311+
312+
See Also
313+
--------
314+
* ``START MODEL model_name``
315+
* ``SHOW MODELS``
316+
317+
""" # noqa: E501
318+
319+
def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
320+
inference_api = get_inference_api(params)
321+
inference_api.stop()
322+
return None
323+
324+
325+
StopModelHandler.register(overwrite=True)

singlestoredb/fusion/handlers/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from ...management.workspace import Workspace
1717
from ...management.workspace import WorkspaceGroup
1818
from ...management.workspace import WorkspaceManager
19+
from ...management.inference_api import InferenceAPIInfo
20+
from ...management.inference_api import InferenceAPIManager
1921

2022

2123
def get_workspace_manager() -> WorkspaceManager:
@@ -322,3 +324,16 @@ def get_file_space(params: Dict[str, Any]) -> FileSpace:
322324
raise ValueError(f'invalid file location: {file_location}')
323325

324326
raise KeyError('no file space was specified')
327+
328+
329+
def get_inference_api_manager() -> InferenceAPIManager:
330+
"""Return the inference API manager for the current project."""
331+
wm = get_workspace_manager()
332+
return wm.organization.inference_apis
333+
334+
335+
def get_inference_api(params: Dict[str, Any]) -> InferenceAPIInfo:
336+
"""Return an inference API based on model name in params."""
337+
inference_apis = get_inference_api_manager()
338+
model_name = params['model_name']
339+
return inference_apis.get(model_name)

singlestoredb/management/inference_api.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class InferenceAPIInfo(object):
2424
connection_url: str
2525
project_id: str
2626
hosting_platform: str
27+
_manager: Optional['InferenceAPIManager']
2728

2829
def __init__(
2930
self,
@@ -33,13 +34,15 @@ def __init__(
3334
connection_url: str,
3435
project_id: str,
3536
hosting_platform: str,
37+
manager: Optional['InferenceAPIManager'] = None,
3638
):
3739
self.service_id = service_id
3840
self.connection_url = connection_url
3941
self.model_name = model_name
4042
self.name = name
4143
self.project_id = project_id
4244
self.hosting_platform = hosting_platform
45+
self._manager = manager
4346

4447
@classmethod
4548
def from_dict(
@@ -77,6 +80,34 @@ def __repr__(self) -> str:
7780
"""Return string representation."""
7881
return str(self)
7982

83+
def start(self) -> Dict[str, Any]:
84+
"""
85+
Start this inference API model.
86+
87+
Returns
88+
-------
89+
dict
90+
Response from the start operation
91+
92+
"""
93+
if self._manager is None:
94+
raise ManagementError(msg='No manager associated with this inference API')
95+
return self._manager.start(self.name)
96+
97+
def stop(self) -> Dict[str, Any]:
98+
"""
99+
Stop this inference API model.
100+
101+
Returns
102+
-------
103+
dict
104+
Response from the stop operation
105+
106+
"""
107+
if self._manager is None:
108+
raise ManagementError(msg='No manager associated with this inference API')
109+
return self._manager.stop(self.name)
110+
80111

81112
class InferenceAPIManager(object):
82113
"""
@@ -102,4 +133,46 @@ def get(self, model_name: str) -> InferenceAPIInfo:
102133
if self._manager is None:
103134
raise ManagementError(msg='Manager not initialized')
104135
res = self._manager._get(f'inferenceapis/{self.project_id}/{model_name}').json()
105-
return InferenceAPIInfo.from_dict(res)
136+
inference_api = InferenceAPIInfo.from_dict(res)
137+
inference_api._manager = self # Associate the manager
138+
return inference_api
139+
140+
def start(self, model_name: str) -> Dict[str, Any]:
141+
"""
142+
Start an inference API model.
143+
144+
Parameters
145+
----------
146+
model_name : str
147+
Name of the model to start
148+
149+
Returns
150+
-------
151+
dict
152+
Response from the start operation
153+
154+
"""
155+
if self._manager is None:
156+
raise ManagementError(msg='Manager not initialized')
157+
res = self._manager._post(f'inferenceapis/{self.project_id}/{model_name}/start')
158+
return res.json()
159+
160+
def stop(self, model_name: str) -> Dict[str, Any]:
161+
"""
162+
Stop an inference API model.
163+
164+
Parameters
165+
----------
166+
model_name : str
167+
Name of the model to stop
168+
169+
Returns
170+
-------
171+
dict
172+
Response from the stop operation
173+
174+
"""
175+
if self._manager is None:
176+
raise ManagementError(msg='Manager not initialized')
177+
res = self._manager._post(f'inferenceapis/{self.project_id}/{model_name}/stop')
178+
return res.json()

0 commit comments

Comments
 (0)