Path: blob/master/cpd5.0/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and LangChain to make a series of calls to a language model.ipynb
6405 views
Use watsonx, and LangChain to make a series of calls to a language model
Disclaimers
Use only Projects and Spaces that are available in watsonx context.
Notebook content
This notebook contains the steps and code to demonstrate Simple Sequential Chain using langchain integration with watsonx models.
Some familiarity with Python is helpful. This notebook uses Python 3.11.
Learning goal
The goal of this notebook is to demonstrate how to chain google/flan-ul2
and google/flan-t5-xxl
models to generate a sequence of creating a random question on a given topic and an answer to that question and also to make the user friends with LangChain framework, using simple chain (LLMChain) and the extended chain (SimpleSequentialChain) with the WatsonxLLM.
Contents
This notebook contains the following parts:
Install and import the ibm-watsonx-ai
and dependecies
Note: ibm-watsonx-ai
documentation can be found here.
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 Foundation Model 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.
List available models
All avaliable models are presented under ModelTypes
class.
You need to specify model_id
's that will be used for inferencing:
Defining the model parameters
You might need to adjust model parameters
for different models or tasks, to do so please refer to documentation under GenTextParamsMetaNames
class.
Action: If any complications please refer to the documentation.
Initialize the model
Initialize the ModelInference
class with previous set params.
WatsonxLLM interface
WatsonxLLM
is a wrapper around watsonx.ai models that provide chain integration around the models.
Action: For more details about CustomLLM
check the LangChain documentation
Initialize the WatsonxLLM
class.
Hint: To use Chain interface from LangChain with watsonx.ai models you must call model.to_langchain()
method.
It returns WatsonxLLM
wrapper compatible with LangChain CustomLLM specification.
You can print all set data about the WatsonxLLM object using the dict()
method.
Simple Sequential Chain experiment
The simplest type of sequential chain is called a SimpleSequentialChain
, in which each step has a single input and output and the output of one step serves as the input for the following step.
The experiment will consist in generating a random question about any topic and answer the following question.
An object called PromptTemplate
assists in generating prompts using a combination of user input, additional non-static data, and a fixed template string.
In our case we would like to create two PromptTemplate
objects which will be responsible for creating a random question and answering it.
We would like to add functionality around language models using LLMChain
chain.
prompt_to_flan_ul2
chain formats the prompt template whose task is to generate random question, passes the formatted string to LLM and returns the LLM output.
Hint: To use Chain interface from LangChain with watsonx.ai models you must call model.to_langchain()
method.
It returns WatsonxLLM
wrapper compatible with LangChain CustomLLM specification.
flan_to_t5
chain formats the prompt template whose task is to answer the question we got from prompt_to_flan_ul2
chain, passes the formatted string to LLM and returns the LLM output.
This is the overall chain where we run prompt_to_flan_ul2
and flan_to_t5
chains in sequence.
Generate random question and answer to topic.
> Entering new SimpleSequentialChain chain...
What is the smallest unit of life?
cell
> Finished chain.
Summary and next steps
You successfully completed this notebook!.
You learned how to use Simple Squential Chain using custom llm WastonxLLM.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors:
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.