Customizing with third-party and private Python libraries
Customizing runtimes with external libraries and packages
Last updated: Nov 21, 2024
Customizing runtimes with external libraries and packages
You can use custom software specification to reference any third-party libraries, user-created Python packages, or both. Third-party libraries or user-created Python packages must be specified as package extensions so that they can be referenced
in a custom software specification.
You can customize deployment runtimes in these ways:
Defining customizations in a watsonx.ai Studio project and then promoting them to a deployment space
Copy link to section
Environments in watsonx.ai Studio projects can be customized to include third-party libraries that can be installed from Anaconda or from the PyPI repository.
As part of custom environment creation, these steps are performed internally (visible to the user):
A package extension that contains the details of third-party libraries is created in conda YAML format.
A custom software specification with the same name as the custom environment is created and the package extension that is created is associated with this custom software specification.
The models or Python functions/scripts created with the custom environment must reference the custom software specification when they are saved in watsonx.ai Runtime repository in the project scope.
Propagating software specifications and package extensions from projects to deployment spaces
Copy link to section
To export custom software specifications and package extensions that were created in a watsonx.ai Studio project to a deployment space:
From your project interface, click the Manage tab.
Select Environments.
Click the Templates tab.
From your custom environment's Options menu, select Promote to space.
Alternatively, when you promote any model or Python function that is associated with a custom environment from a watsonx.ai Studio project to a deployment space, the associated custom software specification and package extension is also promoted
to the deployment space.
If you want to update software specifications and package extensions after you promote them to deployment space, follow these steps:
In the deployment space, delete the software specifications, package extensions, and associated models (optional) by using the watsonx.ai Python client.
In a project, promote the model, function, or script that is associated with the changed custom software specification and package extension to the space.
Software specifications are also included when you import a project or space that includes one.
Creating package extensions and custom software specifications in a deployment space by using the watsonx.ai Runtime Python client
Copy link to section
You can use the watsonx.ai Runtime APIs or Python client to define a custom software specification that is derived from a base specification.
High-level steps to create a custom software specification that uses third-party libraries or user-created Python packages:
Note: This step is not required if the model does not have any dependency on a third-party library or a user-created Python library.
Create a custom software specification
Add a reference of the package extensions to the custom software specification that you created.
Saving a conda YAML file that contains a list of third-party libraries
Copy link to section
To save a conda YAML file that contains a list of third-party libraries as a package extension and create a custom software specification that is linked to the package extension:
Authenticate and create the client.
Create and set the default deployment space, then list available software specifications.
metadata = {
wml_client.spaces.ConfigurationMetaNames.NAME:
'examples-create-software-spec',
wml_client.spaces.ConfigurationMetaNames.DESCRIPTION:
'For my models'
}
space_details = wml_client.spaces.store(meta_props=metadata)
space_uid = wml_client.spaces.get_id(space_details)
# set the default space
wml_client.set.default_space(space_uid)
# see available meta names for software specsprint('Available software specs configuration:', wml_client.software_specifications.ConfigurationMetaNames.get())
wml_client.software_specifications.list()
asset_id = 'undefined'
pe_asset_id = 'undefined'
Copy to clipboardCopied to clipboardShow more
Create the metadata for package extensions to add to the base specification.
Create the metadata for the software specification and store the software specification.
# Get the id of the base software specification
base_id = wml_client.software_specifications.get_id_by_name('default_py3.9')
# create the metadata for software specs
ss_metadata = {
wml_client.software_specifications.ConfigurationMetaNames.NAME:
'Python 3.9 with pre-installed ML package',
wml_client.software_specifications.ConfigurationMetaNames.DESCRIPTION:
'Adding some custom libraries like regex', # optional
wml_client.software_specifications.ConfigurationMetaNames.BASE_SOFTWARE_SPECIFICATION:
{'guid': base_id},
wml_client.software_specifications.ConfigurationMetaNames.PACKAGE_EXTENSIONS:
[{'guid': pe_asset_id}]
}
# store the software spec
ss_asset_details = wml_client.software_specifications.store(meta_props=ss_metadata)
# get the id of the new asset
asset_id = wml_client.software_specifications.get_id(ss_asset_details)
# view new software specification detailsimport pprint as pp
ss_asset_details = wml_client.software_specifications.get_details(asset_id)
print('Package extensions', pp.pformat(
ss_asset_details['entity']['software_specification']['package_extensions']
))
Copy to clipboardCopied to clipboardShow more
Saving a user-created Python library and creating a package extension
Create the metadata for the software specification and store the software specification.
# Get the id of the base software specification
base_id = wml_client.software_specifications.get_id_by_name('default_py3.9')
# create the metadata for software specs
ss_metadata = {
wml_client.software_specifications.ConfigurationMetaNames.NAME:
'Python 3.9 with pre-installed ML package',
wml_client.software_specifications.ConfigurationMetaNames.DESCRIPTION:
'Adding some custom libraries like regex', # optional
wml_client.software_specifications.ConfigurationMetaNames.BASE_SOFTWARE_SPECIFICATION:
{'guid': base_id},
wml_client.software_specifications.ConfigurationMetaNames.PACKAGE_EXTENSIONS:
[{'guid': pe_asset_id}]
}
# store the software spec
ss_asset_details = wml_client.software_specifications.store(meta_props=ss_metadata)
# get the id of the new asset
asset_id = wml_client.software_specifications.get_id(ss_asset_details)
# view new software specification detailsimport pprint as pp
ss_asset_details = wml_client.software_specifications.get_details(asset_id)
print('Package extensions', pp.pformat(
ss_asset_details['entity']['software_specification']['package_extensions']
))
Copy to clipboardCopied to clipboardShow more
Troubleshooting
Copy link to section
When a conda yml-based custom library installation fails with this error:
Encountered error while installing custom library, try these alternatives:
Use a different version of the same package that is available in Anaconda for the concerned Python version.
Install the library from the pypi repository, by using pip. Edit the conda yml installation file contents:
About cookies on this siteOur websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising.For more information, please review your cookie preferences options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.