Path: blob/master/cloud/notebooks/python_sdk/lifecycle-management/Use python API to automate AutoAI deployment lifecycle.ipynb
6405 views
Re-train and re-deploy AutoAI pipelines with ibm-watsonx-ai
This notebook contains the steps and code to demonstrate support of AI Lifecycle features of the AutoAI model in watsonx.ai Runtime Service in watsonx.ai Runtime service. It contains steps and code to work with ibm-watsonx-ai library available in PyPI repository. It also introduces commands for training, persisting and deploying model, scoring it, updating the model and redeploying it.
Some familiarity with Python is helpful. This notebook uses Python 3.11.
Learning goals
The learning goals of this notebook are:
List all deprecated and unsupported deployments.
Identify AutoAI models that need to be retrained.
Work with watsonx.ai Runtime experiments to re-train AutoAI models.
Persist an updated AutoAI model in watsonx.ai Runtime repository.
Redeploy model in-place.
Score sample records using client library.
Contents
This notebook contains the following parts:
1. Set up the environment
Before you use the sample code in this notebook, you must perform the following setup tasks:
Create a watsonx.ai Runtime Service instance (a free plan is offered and information about how to create the instance can be found here).
Create a Cloud Object Storage (COS) instance (a lite plan is offered and information about how to order storage can be found here).
Note: When using Watson Studio, you already have a COS instance associated with the project you are running the notebook in.
Install and import the ibm-watsonx-ai
and dependecies
Note: ibm-watsonx-ai
documentation can be found here.
Connection to watsonx.ai Runtime
Authenticate the watsonx.ai Runtime service on IBM Cloud. You need to provide Cloud API key
and location
.
Tip: Your Cloud API key
can be generated by going to the Users section of the Cloud console. From that page, click your name, scroll down to the API Keys section, and click Create an IBM Cloud API key. Give your key a name and click Create, then copy the created key and paste it below. You can also get a service specific url by going to the Endpoint URLs section of the watsonx.ai Runtime docs. You can check your instance location in your watsonx.ai Runtime Service instance details.
You can use IBM Cloud CLI to retrieve the instance location
.
NOTE: You can also get a service specific apikey by going to the Service IDs section of the Cloud Console. From that page, click Create, and then copy the created key and paste it in the following cell.
Action: Enter your api_key
and location
in the following cell.
Working with spaces
You need to create a space that will be used for your work. If you do not have a space, you can use Deployment Spaces Dashboard to create one.
Click New Deployment Space
Create an empty space
Select Cloud Object Storage
Select watsonx.ai Runtime instance and press Create
Copy
space_id
and paste it below
Tip: You can also use SDK to prepare the space for your work. More information can be found here.
Action: assign space ID below
You can use the list
method to print all existing spaces.
Extract all spaces id
's.
You can also list deployments under particular space. The output contains SPEC_STATE
and SPEC_REPLACEMENT
. Set the working space.
List deployments under this space.
3. Identification of model requiring retraining
Pick up deployment of the AutoAI model you wish to retrain.
Hint: You can also do that programatically in the loop sequence over spaces check (Check the state of your deployments
cell).
Hint: You can also use software_specification information (model details) to identify models and deployments that are not yet deprecated but can be retrained (updated software specification is available).
Extract the deployed model's details (including the pipeline information).
Find the AutoAI experiment runs matching the extracted pipeline
Extract the project_id
where the training took place.
Note: If the training took place in the space please update accordingly.
List all training runs matching this pipeline name.
Extract AutoAI experiment training_id
Check if the training_id
is available in model's details.
If not, we will use the latest run of AutoAI experiment matching the pipeline name.
Hint: It is also possible to extract the run_id
parsing model's details (location
property):
Get the training definition using pipeline_id
linked with experiment run.
NOTE: We need to extract the pipeline_id
from the same context as experiment run (project
). The one linked with deployed model has been promoted to space and cannot be used for re-training purposes (different scope than training data reference).
Explore experiment's results
Connect to finished experiment and preview the results.
Evaluate the best model locally
Load the model for test purposes.
Hint: The best model is returned automatically if no pipeline_name
provided.
This cell constructs the cell scorer based on the experiment metadata.
Read the train and holdout data.
Hint: You can also use external test dataset.
Calculate the score
List stored models.
6. Redeploy and score new version of the model
In this section, you'll learn how to redeploy new version of the model by using the watsonx.ai Client.
Hint: As a best practice please consider using the test space before moving to production.
Promote model to deployment space
Check current deployment details before update.
Update the deployment with new model
Note: The update is asynchronous.
Wait for the deployment update:
Get updated deployment details
Score updated model
Create sample payload and score the deployed model.
Use client.deployments.score() method to run scoring.
If you want to clean up all created assets:
experiments
trainings
pipelines
models
deployments
please follow up this sample notebook.
You successfully completed this notebook! You learned how to use scikit-learn machine learning as well as watsonx.ai Runtime for model creation and deployment. Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors
Lukasz Cmielowski, PhD, is a Senior Technical Staff Member at IBM with a track record of developing enterprise-level applications that substantially increases clients' ability to turn data into actionable knowledge.
Copyright © 2023-2025 IBM. This notebook and its source code are released under the terms of the MIT License.