Skip to main contentwatsonx Developer Hub

Model tuning

Free token limit increased!
New 300k token limit for all new, free trials to use for LLM API calls and more. Sign up for free here.

Overview

Prompt tuning is an efficient and low-cost way to adapt an AI foundation model to new downstream tasks without retraining the model and updating its weights. You can tune foundation models in IBM watsonx.ai programmatically by using the Python library.

Quick start

The TuneExperiment class is responsible for creating experiments and scheduling tunings. All experiment results are stored automatically in the user-specified IBM Cloud Object Storage for SaaS or in the cluster’s file system for Cloud Pak for Data. Then, the TuneExperiment feature can fetch the results and provide them directly to the user for further usage.

Import and configure PromptTuner

Replace {watsonx_ai_url}, {apikey}, {project_id}, and {space_id} with your information.

1from ibm_watsonx_ai import Credentials
2from ibm_watsonx_ai.experiment import TuneExperiment
3
4credentials = Credentials(
5  url = "{watsonx_ai_url}",
6  apikey = "{apikey}",
7)
8
9experiment = TuneExperiment(
10    credentials=Credentials(...),
11    project_id = "{project_id}"
12    space_id="{space_id}")
13
14prompt_tuner = experiment.prompt_tuner(
15    name="prompt tuning name",
16    task_id=experiment.Tasks.CLASSIFICATION,
17    base_model=ModelTypes.FLAN_T5_XL,
18    accumulate_steps=32,
19    batch_size=16,
20    learning_rate=0.2,
21    max_input_tokens=256,
22    max_output_tokens=2,
23    num_epochs=6,
24    tuning_type=experiment.PromptTuningTypes.PT,
25    verbalizer="Extract the satisfaction from the comment. Return simple '1' for satisfied customer or '0' for unsatisfied. Input: {{input}} Output: ",
26    auto_update_model=True
27)

Run prompt tuning

1from ibm_watsonx_ai.helpers import DataConnection, ContainerLocation, S3Location
2
3tuning_details = prompt_tuner.run(
4    training_data_references=[DataConnection(
5        connection_asset_id=connection_id,
6        location=S3Location(
7            bucket='prompt_tuning_data',
8            path='pt_train_data.json'
9        )
10    )],
11    background_mode=False)
12
13# OR
14
15tuning_details = prompt_tuner.run(
16    training_data_references=[DataConnection(
17        data_asset_id='5d99c11a-2060-4ef6-83d5-dc593c6455e2')
18    ],
19    background_mode=True)
20
21# OR
22
23tuning_details = prompt_tuner.run(
24    training_data_references=[DataConnection(
25        location=ContainerLocation("path_to_file.json"))
26    ],
27    background_mode=True)

The watsonx.ai Python library has methods and helper classes for tuning foundation models. For more information about the library, see Prompt tuning.