Path: blob/master/cloud/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 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 (SequentialChain) with the WatsonxLLM.
Contents
This notebook contains the following parts:
Set up the environment
Before you use the sample code in this notebook, you must perform the following setup tasks:
Create a watsonx.ai Runtime Service instance (a free plan is offered and information about how to create the instance can be found here).
Install and import the datasets
and dependecies
Defining the watsonx.ai credentials
This cell defines the watsonx.ai credentials required to work with watsonx Foundation Model inferencing.
Action: Provide the IBM Cloud user API key. For details, see documentation.
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.
Create an instance of APIClient with authentication details.
List available models
All avaliable models are presented under TextModels
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.
LangChain integration
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.
You can print all set data about the WatsonxLLM object using the dict()
method.
Sequential Chain experiment
The simplest type of sequential chain is called a SequentialChain
, 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.
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 SequentialChain chain...
> Finished chain.
Function implementation.
Let's wrap the above chain code into function.
Test the function
It is good practice to validate the code locally first.
Custom software specification
Create new software specification based on default Python 3.11 environment extended by langchain package.
Now you need to store new package extension with APIClient.
Create new software specification and add created package extension to it.
Store the function
Create online deployment
Summary and next steps
You successfully completed this notebook!
You learned how to use Sequential Chain using custom llm WastonxLLM.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors:
Lukasz Cmielowski, PhD, Senior Technical Staff Member at watsonx.ai.
Mateusz Szewczyk, Software Engineer at watsonx.ai.
Copyright © 2024-2025 IBM. This notebook and its source code are released under the terms of the MIT License.