Skip to main content
ChatWatsonx is a wrapper for IBM watsonx.ai foundation models.
The aim of these examples is to show how to communicate with watsonx.ai models using LangChain LLMs API.

Overview

Integration details

Model features

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.
Additionally 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 Available TextChatParameters.
Initialize the WatsonxLLM class with the previously set parameters. Note: In this example, we’ll use the project_id and Dallas URL. You need to specify the model_id that will be used for inferencing. You can find the list of all the available models in Supported chat models.
Alternatively, you can use Cloud Pak for Data credentials. For details, see watsonx.ai software setup.
Instead of model_id, you can also pass the deployment_id of the previously deployed model with reference to a Prompt Template.
For certain requirements, there is an option to pass the IBM’s APIClient object into the ChatWatsonx class.

Invocation

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

Chaining

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

Streaming the model output

You can stream the model output.

Batch the model output

You can batch the model output.

Tool calling

ChatWatsonx.bind_tools()

AIMessage.tool_calls

Notice that the AIMessage has a tool_calls attribute. This contains in a standardized ToolCall format that is model-provider agnostic.

API reference

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