Skip to main content

IBM watsonx.ai

WatsonxLLM is a wrapper for IBM watsonx.ai foundation models. This example shows how to communicate with watsonx.ai models using LangChain.

Install the package ibm-watsonx-ai.

%pip install ibm-watsonx-ai

This cell defines the WML credentials required to work with watsonx Foundation Model inferencing.

Action: Provide the IBM Cloud user API key. For details, see documentation.

import os
from getpass import getpass

watsonx_api_key = getpass()
os.environ["WATSONX_APIKEY"] = watsonx_api_key

Load the model

You might need to adjust model parameters for different models or tasks. For details, refer to documentation.

from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams

parameters = {
GenParams.DECODING_METHOD: "sample",
GenParams.MAX_NEW_TOKENS: 100,
GenParams.MIN_NEW_TOKENS: 1,
GenParams.TEMPERATURE: 0.5,
GenParams.TOP_K: 50,
GenParams.TOP_P: 1,
}

Initialize the WatsonxLLM class with previously set parameters.

Note:

  • To provide context for the API call, you must add project_id or space_id. For more information see documentation.
  • Depending on the region of your provisioned service instance, use one of the urls described here.

In this example, we’ll use the project_id and Dallas url.

from langchain_community.llms import WatsonxLLM

watsonx_llm = WatsonxLLM(
model_id="google/flan-ul2",
url="https://us-south.ml.cloud.ibm.com",
project_id="PASTE YOUR PROJECT_ID HERE",
params=parameters,
)

Alternatively you can use Cloud Pak for Data credentials. For details, see documentation.

watsonx_llm = WatsonxLLM(
model_id="google/flan-ul2",
url="PASTE YOUR URL HERE",
username="PASTE YOUR USERNAME HERE",
password="PASTE YOUR PASSWORD HERE",
instance_id="openshift",
version="4.8",
project_id="PASTE YOUR PROJECT_ID HERE",
params=parameters,
)

Instead of model_id, you can also pass the deployment_id of the previously tuned model. The entire model tuning workflow is described here.

watsonx_llm = WatsonxLLM(
deployment_id="PASTE YOUR DEPLOYMENT_ID HERE",
url="https://us-south.ml.cloud.ibm.com",
project_id="PASTE YOUR PROJECT_ID HERE",
params=parameters,
)

Create Chain

Create PromptTemplate objects which will be responsible for creating a random question.

from langchain.prompts import PromptTemplate

template = "Generate a random question about {topic}: Question: "
prompt = PromptTemplate.from_template(template)

Provide a topic and run the LLMChain.

from langchain.chains import LLMChain

llm_chain = LLMChain(prompt=prompt, llm=watsonx_llm)
llm_chain.run("dog")
'How many breeds of dog are there?'

Calling the Model Directly

To obtain completions, you can call the model directly using a string prompt.

# Calling a single prompt

watsonx_llm("Who is man's best friend?")
'dog'
# Calling multiple prompts

watsonx_llm.generate(
[
"The fastest dog in the world?",
"Describe your chosen dog breed",
]
)
LLMResult(generations=[[Generation(text='greyhounds', generation_info={'generated_token_count': 4, 'input_token_count': 8, 'finish_reason': 'eos_token'})], [Generation(text='The Basenji is a dog breed from South Africa.', generation_info={'generated_token_count': 13, 'input_token_count': 7, 'finish_reason': 'eos_token'})]], llm_output={'model_id': 'google/flan-ul2'}, run=[RunInfo(run_id=UUID('03c73a42-db68-428e-ab8d-8ae10abc84fc')), RunInfo(run_id=UUID('c289f67a-87d6-4c8b-a8b7-0b5012c94ca8'))])

Streaming the Model output

You can stream the model output.

for chunk in watsonx_llm.stream(
"Describe your favorite breed of dog and why it is your favorite."
):
print(chunk, end="")
The golden retriever is my favorite dog because it is very friendly and good with children.