IBM watsonx.ai large language models class.
To use the large language models, you need to have the langchain_ibm python
package installed, and the environment variable WATSONX_API_KEY set with your
API key or pass it as a named parameter api_key to the constructor.
pip install -U langchain-ibm
# or using uv
uv add langchain-ibm
export WATSONX_API_KEY="your-api-key"
apikey and WATSONX_APIKEY are deprecated and will be removed in
version 2.0.0. Use api_key and WATSONX_API_KEY instead.
from langchain_ibm import WatsonxLLM
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames
parameters = {
GenTextParamsMetaNames.DECODING_METHOD: "sample",
GenTextParamsMetaNames.MAX_NEW_TOKENS: 100,
GenTextParamsMetaNames.MIN_NEW_TOKENS: 1,
GenTextParamsMetaNames.TEMPERATURE: 0.5,
GenTextParamsMetaNames.TOP_K: 50,
GenTextParamsMetaNames.TOP_P: 1,
}
model = WatsonxLLM(
model_id="google/flan-t5-xl",
url="https://us-south.ml.cloud.ibm.com",
project_id="*****",
params=parameters,
# api_key="*****"
)input_text = "The meaning of life is "
response = model.invoke(input_text)
print(response)
"42, but what was the question?
The answer to the ultimate question of life, the universe, and everything is 42.
But what was the question? This is a reference to Douglas Adams' science fiction
series "The Hitchhiker's Guide to the Galaxy."for chunk in model.stream(input_text):
print(chunk, end="")
"42, but what was the question?
The answer to the ultimate question of life, the universe, and everything is 42.
But what was the question? This is a reference to Douglas Adams' science fiction
series "The Hitchhiker's Guide to the Galaxy."response = await model.ainvoke(input_text)
# stream:
# async for chunk in model.astream(input_text):
# print(chunk, end="")
# batch:
# await model.abatch([input_text])
"42, but what was the question?
The answer to the ultimate question of life, the universe, and everything is 42.
But what was the question? This is a reference to Douglas Adams' science fiction
series "The Hitchhiker's Guide to the Galaxy."Type of model to use.
Name or alias of the foundation model to use. When using IBM's watsonx.ai Model Gateway (public preview), you can specify any supported third-party model-OpenAI, Anthropic, NVIDIA, Cerebras, or IBM's own Granite series—via a single, OpenAI-compatible interface. Models must be explicitly provisioned (opt-in) through the Gateway to ensure secure, vendor-agnostic access and easy switch-over without reconfiguration.
For more details on configuration and usage, see IBM watsonx Model Gateway docs
Type of deployed model to use.
ID of the Watson Studio project.
ID of the Watson Studio space.
URL to the Watson Machine Learning or CPD instance.
API key to the Watson Machine Learning or CPD instance.
Token to the CPD instance.
Password to the CPD instance.
Username to the CPD instance.
Instance_id of the CPD instance.
Version of the CPD instance.
Model parameters to use during request generation.
You can pass one of following as verify:
Whether to stream the results or not.
Mapping of secret environment variables.
Is lc serializable.
Validate that credentials and python package exists in environment.
Get num tokens.
Get token ids.