Path: blob/main/singlestoredb/fusion/handlers/models.py
801 views
#!/usr/bin/env python31import os2from typing import Any3from typing import Dict4from typing import Optional56from .. import result7from ..handler import SQLHandler8from ..result import FusionSQLResult9from .files import ShowFilesHandler10from .utils import get_file_space11from .utils import get_inference_api12from .utils import get_inference_api_manager131415class ShowCustomModelsHandler(ShowFilesHandler):16"""17SHOW CUSTOM MODELS18[ at_path ] [ <like> ]19[ <order-by> ]20[ <limit> ] [ recursive ] [ extended ];2122# File path to list23at_path = AT '<path>'2425# Should the listing be recursive?26recursive = RECURSIVE2728# Should extended attributes be shown?29extended = EXTENDED3031Description32-----------33Displays the list of models in models space.3435Arguments36---------37* ``<path>``: A path in the models space.38* ``<pattern>``: A pattern similar to SQL LIKE clause.39Uses ``%`` as the wildcard character.4041Remarks42-------43* Use the ``LIKE`` clause to specify a pattern and return only the44files that match the specified pattern.45* The ``LIMIT`` clause limits the number of results to the46specified number.47* Use the ``ORDER BY`` clause to sort the results by the specified48key. By default, the results are sorted in the ascending order.49* The ``AT`` clause specifies the path in the models50space to list the files from.51* To return more information about the files, use the ``EXTENDED``52clause.5354Examples55--------56The following command lists the models::5758SHOW CUSTOM MODELS;5960The following command lists the models with additional information::6162SHOW CUSTOM MODELS EXTENDED;6364See Also65--------66* ``UPLOAD CUSTOM MODEL model_name FROM path``67* ``DOWNLOAD CUSTOM MODEL model_name``686970""" # noqa: E5017172def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:73params['file_location'] = 'MODELS'7475return super().run(params)767778ShowCustomModelsHandler.register(overwrite=True)798081class UploadCustomModelHandler(SQLHandler):82"""83UPLOAD CUSTOM MODEL model_name84FROM local_path [ overwrite ];8586# Model Name87model_name = '<model-name>'8889# Path to local file or directory90local_path = '<local-path>'9192# Should an existing file be overwritten?93overwrite = OVERWRITE9495Description96-----------97Uploads a file or folder to models space.9899Arguments100---------101* ``<model-name>``: Model name.102* ``<local-path>``: The path to the file or folder to upload in the local103directory.104105Remarks106-------107* If the ``OVERWRITE`` clause is specified, any existing file at the108specified path in the models space is overwritten.109110Examples111--------112The following command uploads a file to models space and overwrite any113existing files at the specified path::114115UPLOAD CUSTOM MODEL model_name116FROM 'llama3/' OVERWRITE;117118See Also119--------120* ``DOWNLOAD CUSTOM MODEL model_name``121122""" # noqa: E501123124def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:125params['file_location'] = 'MODELS'126127model_name = params['model_name']128local_path = params['local_path']129130file_space = get_file_space(params)131132if os.path.isdir(local_path):133file_space.upload_folder(134local_path=local_path,135path=os.path.join(model_name, ''),136overwrite=params['overwrite'],137)138else:139file_space.upload_file(140local_path=local_path,141path=os.path.join(model_name, local_path),142overwrite=params['overwrite'],143)144145return None146147148UploadCustomModelHandler.register(overwrite=True)149150151class DownloadCustomModelHandler(SQLHandler):152"""153DOWNLOAD CUSTOM MODEL model_name154[ local_path ]155[ overwrite ];156157# Model Name158model_name = '<model-name>'159160# Path to local directory161local_path = TO '<local-path>'162163# Should an existing directory be overwritten?164overwrite = OVERWRITE165166Description167-----------168Download a model from models space.169170Arguments171---------172* ``<model-name>``: Model name to download in models space.173* ``<local-path>``: Specifies the path in the local directory174where the model is downloaded.175176Remarks177-------178* If the ``OVERWRITE`` clause is specified, any existing file or folder at179the download location is overwritten.180* If ``<local-path>`` is not specified, the model is downloaded to the current location.181182Examples183--------184The following command displays the contents of the file on the185standard output::186187DOWNLOAD CUSTOM MODEL llama3;188189The following command downloads a model to a specific location and190overwrites any existing models folder with the name ``local_llama3`` on the local storage::191192DOWNLOAD CUSTOM MODEL llama3193TO 'local_llama3' OVERWRITE;194195See Also196--------197* ``UPLOAD CUSTOM MODEL model_name FROM local_path``198199""" # noqa: E501200201def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:202params['file_location'] = 'MODELS'203204file_space = get_file_space(params)205206model_name = params['model_name']207file_space.download_folder(208path=os.path.join(model_name, ''),209local_path=params['local_path'] or model_name,210overwrite=params['overwrite'],211)212213return None214215216DownloadCustomModelHandler.register(overwrite=True)217218219class DropCustomModelHandler(SQLHandler):220"""221DROP CUSTOM MODEL model_name;222223# Model Name224model_name = '<model-name>'225226Description227-----------228Deletes a model from models space.229230Arguments231---------232* ``<model-name>``: Model name to delete in models space.233234Example235--------236The following commands deletes a model from a model space::237238DROP CUSTOM MODEL llama3;239240""" # noqa: E501241242def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:243params['file_location'] = 'MODELS'244path = os.path.join(params['model_name'], '')245246file_space = get_file_space(params)247file_space.removedirs(path=path)248249return None250251252DropCustomModelHandler.register(overwrite=True)253254255class StartModelHandler(SQLHandler):256"""257START MODEL model_name ;258259# Model Name260model_name = '<model-name>'261262Description263-----------264Starts an inference API model.265266Arguments267---------268* ``<model-name>``: Name of the model to start.269270Example271--------272The following command starts a model::273274START MODEL my_model;275276See Also277--------278* ``STOP MODEL model_name``279* ``SHOW MODELS``280281""" # noqa: E501282283def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:284inference_api = get_inference_api(params)285operation_result = inference_api.start()286287res = FusionSQLResult()288res.add_field('Status', result.STRING)289res.add_field('Message', result.STRING)290res.set_rows([291(292operation_result.status,293operation_result.get_message(),294),295])296297return res298299300StartModelHandler.register(overwrite=True)301302303class StopModelHandler(SQLHandler):304"""305STOP MODEL model_name ;306307# Model Name308model_name = '<model-name>'309310Description311-----------312Stops an inference API model.313314Arguments315---------316* ``<model-name>``: Name of the model to stop.317318Example319--------320The following command stops a model::321322STOP MODEL my_model;323324See Also325--------326* ``START MODEL model_name``327* ``SHOW MODELS``328329""" # noqa: E501330331def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:332inference_api = get_inference_api(params)333operation_result = inference_api.stop()334335res = FusionSQLResult()336res.add_field('Status', result.STRING)337res.add_field('Message', result.STRING)338res.set_rows([339(340operation_result.status,341operation_result.get_message(),342),343])344345return res346347348StopModelHandler.register(overwrite=True)349350351class ShowModelsHandler(SQLHandler):352"""353SHOW MODELS ;354355Description356-----------357Displays the list of inference APIs in the current project.358359Example360--------361The following command lists all inference APIs::362363SHOW MODELS;364365See Also366--------367* ``START MODEL model_name``368* ``STOP MODEL model_name``369* ``DROP MODEL model_name``370371""" # noqa: E501372373def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:374inference_api_manager = get_inference_api_manager()375models = inference_api_manager.show()376377res = FusionSQLResult()378res.add_field('Model Name', result.STRING)379res.add_field('Status', result.STRING)380381rows = []382for model in models:383rows.append((384model.name,385model.status,386))387388res.set_rows(rows)389return res390391392ShowModelsHandler.register(overwrite=True)393394395class DropModelHandler(SQLHandler):396"""397DROP MODEL model_name ;398399# Model Name400model_name = '<model-name>'401402Description403-----------404Drops (deletes) an inference API model.405406Arguments407---------408* ``<model-name>``: Name of the model to drop.409410Example411--------412The following command drops an inference API::413414DROP MODEL my_model;415416See Also417--------418* ``START MODEL model_name``419* ``STOP MODEL model_name``420* ``SHOW MODELS``421422""" # noqa: E501423424def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:425inference_api = get_inference_api(params)426operation_result = inference_api.drop()427428res = FusionSQLResult()429res.add_field('Model Name', result.STRING)430res.add_field('Status', result.STRING)431res.add_field('Message', result.STRING)432res.set_rows([433(434operation_result.name,435operation_result.status,436operation_result.get_message(),437),438])439440return res441442443DropModelHandler.register(overwrite=True)444445446