0 / 0
Creating a custom component for use in the pipeline
Last updated: Oct 09, 2024
Creating a custom component for use in the pipeline

A custom pipeline component executes a script you write. You can use custom components to share reusable scripts between pipelines.

You create custom components as project assets. You can then use the components in pipelines you create in that project. You can create as many custom components for pipelines as needed. Currently, to create a custom component you must create one programmatically, using a Python function.

Creating a component as a project asset

To create a custom component, use the Python client to authenticate with IBM Watson Pipelines, code the component, then publish the component to the specified project. After it is available in the project, you can assign it to a node in a pipeline and run it as part of a pipeline flow.

This example demonstrates the process of publishing a component that adds two numbers together, then assigning the component to a pipeline node.

  1. Publish a function as a component with the latest Python client. Run the following code in a Jupyter notebook in a project of IBM watsonx.

    # Install libraries
    ! pip install ibm-watson-pipelines==1.0.0
    
    # Authentication
    from ibm_watson_pipelines import WatsonPipelines
    
    apikey = ''
    project_id = 'your_project_id'
    
    client = WatsonPipelines.from_apikey(apikey)
    
    # Define the function of the component
    
    # If you define the input parameters, users are required to 
    # input them in the UI
    
    def add_two_numbers(a: int, b: int) -> int:
        print('Adding numbers: {} + {}.'.format(a, b))
        return a + b + 10
    
    # Other possible functions might be sending a Slack message,
    # or listing directories in a storage volume, and so on.
    
    # Publish the component    
    client.publish_component(
        name='Add numbers', # Appears in UI as component name 
        func=add_two_numbers,
        description='Custom component adding numbers', # Appears in UI as component description 
        project_id=project_id,
        overwrite=True, # Overwrites an existing component with the same name 
    )
    
  2. Drag the node called Run Pipelines component under Run to the canvas.
    Retrieving the custom component node

  3. Choose the name of the component that you want to use.
    Choosing the actual component function

  4. Connect and run the node as part of a pipeline job.
    Connecting the component

Manage pipeline components

To manage your components, use the Python client to manage them.

Table 1. Manage pipeline components
Method Function
client.get_components(project_id=project_id) List components from a project
client.get_component(project_id=project_id, component_id=component_id) Get a component by ID
client.get_component(project_id=project_id, name=component_name) Get a component by name
client.publish_component(component name) Publish a new component
client.delete_component(project_id=project_id, component_id=component_id) Delete a component by ID

Import and export

IBM Watson Pipelines can be imported and exported with pipelines only.

Parent topic: Creating a pipeline

Generative AI search and answer
These answers are generated by a large language model in watsonx.ai based on content from the product documentation. Learn more