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

Overview

Integration details

Setup

To access IBM watsonx.ai models you’ll need to create an IBM watsonx.ai account, get an API key, and install the langchain-ibm integration package.

Credentials

The cell below defines the credentials required to work with watsonx Foundation Model inferencing. Action: Provide the IBM Cloud user API key. For details, see Managing user API keys.
Additionaly you are able to pass additional secrets as an environment variable.

Installation

The LangChain IBM integration lives in the langchain-ibm package:

Instantiation

You might need to adjust model parameters for different models or tasks. For details, refer to documentation.
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. You need to specify model_id that will be used for inferencing. All available models you can find in documentation.
Alternatively you can use Cloud Pak for Data credentials. For details, see documentation.
Instead of model_id, you can also pass the deployment_id of the previously tuned model. The entire model tuning workflow is described in Working with TuneExperiment and PromptTuner.
For certain requirements, there is an option to pass the IBM’s APIClient object into the WatsonxLLM class.
You can also pass the IBM’s ModelInference object into the WatsonxLLM class.

Invocation

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

Streaming the Model output

You can stream the model output.

Chaining

Create PromptTemplate objects which will be responsible for creating a random question.
Provide a topic and run the chain.

API reference

For detailed documentation of all WatsonxLLM features and configurations head to the API reference.