Path: blob/master/cpd4.8/notebooks/python_sdk/deployments/foundation_models/Use watsonx to manage Prompt Template assets and create deployment.ipynb
6405 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.10.
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 dependecies
Connection to WML
Authenticate the Watson Machine Learning service on IBM Cloud Pack for Data. You need to provide platform url
, your username
and api_key
.
Alternatively you can use username
and password
to authenticate WML services.
Defining the project id
The Prompt Template requires project id that provides the context for the call. We will obtain the id from the project in which this notebook runs. Otherwise, please provide the project id.
Instantiate PromptTemplateManager
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 Watson Machine Learning.
Mateusz Szewczyk, Software Engineer at Watson Machine Learning.
Copyright © 2023-2025 IBM. This notebook and its source code are released under the terms of the MIT License.