1010from singlestoredb .management .manager import Manager
1111
1212
13+ class ModelOperationResult (object ):
14+ """
15+ Result of a model start or stop operation.
16+
17+ Attributes
18+ ----------
19+ name : str
20+ Name of the model
21+ status : str
22+ Current status of the model (e.g., 'Active', 'Initializing', 'Suspended')
23+ hosting_platform : str
24+ Hosting platform (e.g., 'Nova', 'Amazon', 'Azure')
25+ """
26+
27+ def __init__ (
28+ self ,
29+ name : str ,
30+ status : str ,
31+ hosting_platform : str ,
32+ ):
33+ self .name = name
34+ self .status = status
35+ self .hosting_platform = hosting_platform
36+
37+ @classmethod
38+ def from_start_response (cls , response : Dict [str , Any ]) -> 'ModelOperationResult' :
39+ """
40+ Create a ModelOperationResult from a start operation response.
41+
42+ Parameters
43+ ----------
44+ response : dict
45+ Response from the start endpoint
46+
47+ Returns
48+ -------
49+ ModelOperationResult
50+
51+ """
52+ return cls (
53+ name = response .get ('modelName' , '' ),
54+ status = 'Initializing' ,
55+ hosting_platform = response .get ('hostingPlatform' , '' ),
56+ )
57+
58+ @classmethod
59+ def from_stop_response (cls , response : Dict [str , Any ]) -> 'ModelOperationResult' :
60+ """
61+ Create a ModelOperationResult from a stop operation response.
62+
63+ Parameters
64+ ----------
65+ response : dict
66+ Response from the stop endpoint
67+
68+ Returns
69+ -------
70+ ModelOperationResult
71+
72+ """
73+ return cls (
74+ name = response .get ('name' , '' ),
75+ status = response .get ('status' , 'Suspended' ),
76+ hosting_platform = response .get ('hostingPlatform' , '' ),
77+ )
78+
79+ def get_message (self ) -> str :
80+ """
81+ Get a human-readable message about the operation.
82+
83+ Returns
84+ -------
85+ str
86+ Message describing the operation result
87+
88+ """
89+ return f'Model is { self .status } '
90+
91+ def __str__ (self ) -> str :
92+ """Return string representation."""
93+ return vars_to_str (self )
94+
95+ def __repr__ (self ) -> str :
96+ """Return string representation."""
97+ return str (self )
98+
99+
13100class InferenceAPIInfo (object ):
14101 """
15102 Inference API definition.
@@ -24,6 +111,7 @@ class InferenceAPIInfo(object):
24111 connection_url : str
25112 project_id : str
26113 hosting_platform : str
114+ _manager : Optional ['InferenceAPIManager' ]
27115
28116 def __init__ (
29117 self ,
@@ -33,13 +121,15 @@ def __init__(
33121 connection_url : str ,
34122 project_id : str ,
35123 hosting_platform : str ,
124+ manager : Optional ['InferenceAPIManager' ] = None ,
36125 ):
37126 self .service_id = service_id
38127 self .connection_url = connection_url
39128 self .model_name = model_name
40129 self .name = name
41130 self .project_id = project_id
42131 self .hosting_platform = hosting_platform
132+ self ._manager = manager
43133
44134 @classmethod
45135 def from_dict (
@@ -77,6 +167,34 @@ def __repr__(self) -> str:
77167 """Return string representation."""
78168 return str (self )
79169
170+ def start (self ) -> ModelOperationResult :
171+ """
172+ Start this inference API model.
173+
174+ Returns
175+ -------
176+ ModelOperationResult
177+ Result object containing status information about the started model
178+
179+ """
180+ if self ._manager is None :
181+ raise ManagementError (msg = 'No manager associated with this inference API' )
182+ return self ._manager .start (self .name )
183+
184+ def stop (self ) -> ModelOperationResult :
185+ """
186+ Stop this inference API model.
187+
188+ Returns
189+ -------
190+ ModelOperationResult
191+ Result object containing status information about the stopped model
192+
193+ """
194+ if self ._manager is None :
195+ raise ManagementError (msg = 'No manager associated with this inference API' )
196+ return self ._manager .stop (self .name )
197+
80198
81199class InferenceAPIManager (object ):
82200 """
@@ -102,4 +220,46 @@ def get(self, model_name: str) -> InferenceAPIInfo:
102220 if self ._manager is None :
103221 raise ManagementError (msg = 'Manager not initialized' )
104222 res = self ._manager ._get (f'inferenceapis/{ self .project_id } /{ model_name } ' ).json ()
105- return InferenceAPIInfo .from_dict (res )
223+ inference_api = InferenceAPIInfo .from_dict (res )
224+ inference_api ._manager = self # Associate the manager
225+ return inference_api
226+
227+ def start (self , model_name : str ) -> ModelOperationResult :
228+ """
229+ Start an inference API model.
230+
231+ Parameters
232+ ----------
233+ model_name : str
234+ Name of the model to start
235+
236+ Returns
237+ -------
238+ ModelOperationResult
239+ Result object containing status information about the started model
240+
241+ """
242+ if self ._manager is None :
243+ raise ManagementError (msg = 'Manager not initialized' )
244+ res = self ._manager ._post (f'models/{ model_name } /start' )
245+ return ModelOperationResult .from_start_response (res .json ())
246+
247+ def stop (self , model_name : str ) -> ModelOperationResult :
248+ """
249+ Stop an inference API model.
250+
251+ Parameters
252+ ----------
253+ model_name : str
254+ Name of the model to stop
255+
256+ Returns
257+ -------
258+ ModelOperationResult
259+ Result object containing status information about the stopped model
260+
261+ """
262+ if self ._manager is None :
263+ raise ManagementError (msg = 'Manager not initialized' )
264+ res = self ._manager ._post (f'models/{ model_name } /stop' )
265+ return ModelOperationResult .from_stop_response (res .json ())
0 commit comments