Path: blob/master/cpd5.2/notebooks/python_sdk/deployments/foundation_models/Use watsonx to manage Prompt Template assets and create deployment.ipynb
9173 views

Use watsonx.ai python SDK to manage Prompt Template assets and create deployment
Disclaimers
Use only Projects and Spaces that are available in watsonx context.
Notebook content
This notebook contains the steps and code to demonstrate support for Prompt Template inference and their deployments.
Some familiarity with Python is helpful. This notebook uses Python 3.12.
Learning goal
The goal of this notebook is to demonstrate how to create a Prompt Template asset and deployment pointing on it. In general, a Prompt Template is a pattern for generating prompts for language models. A template may contain instruction, input/output prefixes, few-shot examples and appropriate context that may vary depending on different tasks.
Contents
This notebook contains the following parts:
Install dependencies
Note: ibm-watsonx-ai documentation can be found here.
Successfully installed PyYAML-6.0.2 SQLAlchemy-2.0.41 annotated-types-0.7.0 anyio-4.9.0 certifi-2025.4.26 charset-normalizer-3.4.2 h11-0.16.0 httpcore-1.0.9 httpx-0.28.1 idna-3.10 jsonpatch-1.33 jsonpointer-3.0.0 langchain-0.3.25 langchain-core-0.3.62 langchain-text-splitters-0.3.8 langsmith-0.3.43 orjson-3.10.18 packaging-24.2 pydantic-2.11.5 pydantic-core-2.33.2 requests-2.32.3 requests-toolbelt-1.0.0 sniffio-1.3.1 tenacity-9.1.2 typing-extensions-4.13.2 typing-inspection-0.4.1 urllib3-2.4.0 zstandard-0.23.0
Successfully installed ibm-cos-sdk-2.14.1 ibm-cos-sdk-core-2.14.1 ibm-cos-sdk-s3transfer-2.14.1 ibm-watsonx-ai-1.3.23 jmespath-1.0.1 lomond-0.3.3 numpy-2.2.6 pandas-2.2.3 pytz-2025.2 requests-2.32.2 tabulate-0.9.0 tzdata-2025.2
Define credentials
Authenticate the watsonx.ai Runtime service on IBM Cloud Pak® for Data. You need to provide the admin's username and the platform url.
Use the admin's api_key to authenticate watsonx.ai Runtime services:
Alternatively you can use the admin's password:
Working with projects
First of all, you need to create a project that will be used for your work. If you do not have a project created already, follow the steps below:
Open IBM Cloud Pak® main page
Click all projects
Create an empty project
Copy
project_idfrom url and paste it below
Action: Assign project ID below
Create APIClient instance
Create PromptTemplateManager instance
List text models available on CPD cluster
Create a Prompt Template object and store it in the project
We use a PromptTemplate object to store the properties of a newly created prompt template. Prompt text is composed of the instruction, input/output prefixes, few-shot examples and input text. All of the mentioned fields may contain placeholders ({...}) with input variables and for the template to be valid, these input variables must be also specified in input_variables parameter.
Using store_prompt(prompt_template_id) method, one can store newly created prompt template in your ptoject.
We can also store a template which is a langchain Prompt Template object.
Manage Prompt Templates
To retrive Prompt Template asset from project and return string that contains Prompt Template input we use load_prompt(prompt_template_id, astype=...). Returned input string is composed of the fields: instruction, input_prefix, output_prefix, examples and input_text. After substituting prompt variables, one can evaluate a language model on the obtained string.
To update Prompt Template data use prompt_mgr.update_prompt method.
Furthermore, to get information about locked state of Prompt Template run following method
Using lock or unlock method, one can change locked state of Prompt Template asset.
Once the prompt template is unlocked it can be deleted. You can also use the list method to check the available prompt templates (passing limit=2 parameter will return only 2 recently created templates).
To create deployment pointing to a Prompt template asset we have to initialized APIClient object.
In the deployment exmaple we will use the prompt with the following input
Now, we create deployment providing the ID of Prompt Template asset and meta props.
Substitute prompt variables and generate text
Generate text using ModelInference
You can also generate text based on your Prompt Template deployment using ModelInference class.
Summary and next steps
You successfully completed this notebook!
You learned how to create valid Prompt Template and store it in watsonx.ai project. Furthermore, you also learned how to create deployment pointing to a Prompt Template asset and generate text using base model.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors
Mateusz Świtała, Software Engineer at watsonx.ai.
Mateusz Szewczyk, Software Engineer at watsonx.ai.
Copyright © 2023-2026 IBM. This notebook and its source code are released under the terms of the MIT License.