Path: blob/master/cpd5.2/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and LangChain to make a series of calls to a language model.ipynb
6412 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.12.
Learning goal
The goal of this notebook is to demonstrate how to chain meta-llama/llama-3-3-70b-instruct
and mistralai/ministral-8b-instruct
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 dependencies
Note: ibm-watsonx-ai
documentation can be found here.
Successfully installed PyYAML-6.0.2 SQLAlchemy-2.0.40 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.59 langchain-text-splitters-0.3.8 langsmith-0.3.42 orjson-3.10.18 packaging-24.2 pydantic-2.11.4 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.0 urllib3-2.4.0 zstandard-0.23.0
Successfully installed ibm-cos-sdk-2.14.0 ibm-cos-sdk-core-2.14.0 ibm-cos-sdk-s3transfer-2.14.0 ibm-watsonx-ai-1.3.13 jmespath-1.0.1 langchain_ibm-0.3.10 lomond-0.3.3 numpy-2.2.5 pandas-2.2.3 pytz-2025.2 requests-2.32.2 tabulate-0.9.0 tzdata-2025.2
Successfully installed anyio-4.9.0 h11-0.16.0 httpcore-1.0.9 httpx-0.28.1 ibm-cos-sdk-2.14.0 ibm-cos-sdk-core-2.14.0 ibm-cos-sdk-s3transfer-2.14.0 ibm-watsonx-ai-1.3.13 jmespath-1.0.1 lomond-0.3.3 requests-2.32.2 sniffio-1.3.1 tabulate-0.9.0
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_id
from url and paste it below
Action: Assign project ID below
Create APIClient
instance
List available models
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 previously 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_llama
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.
llama_to_ministral
chain formats the prompt template whose task is to answer the question we got from prompt_to_llama
chain, passes the formatted string to LLM and returns the LLM output.
This is the overall chain where we run prompt_to_llama
and llama_to_ministral
chains in sequence.
Generate random question and answer to topic.
Summary and next steps
You successfully completed this notebook!
You learned how to use Simple Sequential 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 watsonx.ai.
Copyright © 2023-2025 IBM. This notebook and its source code are released under the terms of the MIT License.