How to use Python API?
New Release 1.4.11
On Nob 28, 2023, version 1.4.11 of the Python client library was released on PyPi:
https://pypi.org/project/onesaitplatform-client-services/#history
Â
This tutorial shows the installation of the Python REST client for OnesaitPlatform, as well as some use cases.
Introduction
This Platform client library contains several classes that implement the functionalities of different platform clients:
Iot-Broker Client: Queries and insertions of ontology data.
Api-Manager client: Management of REST APIs and calls.
File-Manager Client: Upload and download of binary files.
MQTT Client: Queries and insertions of ontology data using MQTT protocol.
BaseModelService: Management of Machine/Deep Learning models
With these clients, a simple communication with the platform from any Python environment is established.
Library installation from pypi:
The easiest way to install the library is by using the pypi repository and using pip. To install it, just run the command:
>> pip install onesaitplatform-client-services
It is recommended to use the upgrade flag to update the dependencies in case they are already installed (recommended for all installations).
>> pip install --upgrade onesaitplatform-client-services
Client use examples
Sample notebooks can be found in the /examples/*.ipynb library installation folder.
NOTE: Very important: For every IoTBrokerClient, always do a join() at the beginning to open the connection and leave () at the end to close it.
Digital Client
Create Client
To create a Digital Client, a connection configuration is required. We will next explain how to properly set up the parameters.
If the client connects from within the Platform's network (from platform notebooks or internal microservices), then the configuration to connect to the Digital Broker (Iot Broker) is as follows:
%python
from onesaitplatform.iotbroker import DigitalClient
HOST = "iotbrokerservice"
PORT = 19000
IOT_CLIENT = <digital client name>
IOT_CLIENT_TOKEN = <digital client token>
PROTOCOL = 'http'
AVOID_SSL_CERTIFICATE = False
client = DigitalClient(host=HOST, port=PORT, iot_client=IOT_CLIENT, iot_client_token=IOT_CLIENT_TOKEN)
client.protocol = PROTOCOL
client.avoid_ssl_certificate = AVOID_SSL_CERTIFICATE
However, if the client is used from an external platform network (local PC and other network locations ), then the configuration is as follows:
The parameter is automatically redirected so this is not necessary, but you can set PORT = 443 (default https port).
The PROTOCOL parameter is set to "https" by default, but you can be set it up to PROTOCOL = "https".
The AVOID_SSL_CERTIFICATE parameter is set to True by default, but it can be set to AVOID_SSL_CERTIFICATE = True.
import json
from onesaitplatform.iotbroker import DigitalClient
HOST = "lab.onesaitplatform.com"
PORT = 443
IOT_CLIENT = "RestaurantTestClient"
IOT_CLIENT_TOKEN = "f633128219f54a37b23409e7f4173100"
PROTOCOL = 'https'
AVOID_SSL_CERTIFICATE = True
client = DigitalClient(host=HOST, port=PORT, iot_client=IOT_CLIENT, iot_client_token=IOT_CLIENT_TOKEN)
client.protocol = PROTOCOL
client.avoid_ssl_certificate = AVOID_SSL_CERTIFICATE
Join / Leave
To start a connection, you need to do either a join() or a connect(), which uses the iot_client and iot_client_token credentials. At the end, you must always use either leave() or disconnect().
client.join()
client.leave()
Query
The supported query formats are "SQL" and "NATIVE" for SQL syntax and MongoDB respectively.
Query batch
This query makes successive requests of smaller size, being optimal when you want to request a lot of data. It is built with query + "offset N limit batch_size" o query + .skip(N)-limit(batch_size)" for SQL and MongoDB respectively.
Insert
Api-Manager Client
Create Client
Find APIs
List APIs
Make API request
File-Manager Client
Create Client
When client is created, it is possible to use Bearer token (APIs) or user token (My APIs>user tokens)
Upload file
Download file
BaseModelService
BaseModelService is a class that manages the whole lifecycle of ML/DL models that can be trained and deployed in Onesait Platform. It is a generalistic base class intended to be mother of more specific classes that deal with scpecific models. For example, to manage a model of Sentiment Analysys, a class SentimentAnalysisModelService can be created. This second class must inherit from BaseModelService. BaseModelService is in charge of the interaction with different components of Platform: getting datasets from File Repository or ontologies, saving the resulting models in File Repository, report the creation of a new model in the corresponding ontology, and so on. The developer od the specific class SentimentAnalysisModelService doesn't need to bother about those issues and can focus just in training and inference code:
Once the class has been implemented, it can be used to build an object able to train models and predict with them: