IBM watsonx.ai
This will help you get started with IBM text completion models
(LLMs) using LangChain. For detailed
documentation on IBM watsonx.ai
features and configuration options,
please refer to the IBM
watsonx.ai.
Overviewβ
Integration detailsβ
Class | Package | Local | Serializable | PY support | Package downloads | Package latest |
---|---|---|---|---|---|---|
WatsonxLLM | @langchain/community | β | β | β |
Setupβ
To access IBM WatsonxAI models youβll need to create an IBM watsonx.ai
account, get an API key or any other type of credentials, and install
the @langchain/community
integration package.
Credentialsβ
Head to IBM Cloud to sign up to IBM watsonx.ai and generate an API key or provide any other authentication form as presented below.
IAM authenticationβ
export WATSONX_AI_AUTH_TYPE=iam
export WATSONX_AI_APIKEY=<YOUR-APIKEY>
Bearer token authenticationβ
export WATSONX_AI_AUTH_TYPE=bearertoken
export WATSONX_AI_BEARER_TOKEN=<YOUR-BEARER-TOKEN>
CP4D authenticationβ
export WATSONX_AI_AUTH_TYPE=cp4d
export WATSONX_AI_USERNAME=<YOUR_USERNAME>
export WATSONX_AI_PASSWORD=<YOUR_PASSWORD>
export WATSONX_AI_URL=<URL>
Once these are placed in your environment variables and object is initialized authentication will proceed automatically.
Authentication can also be accomplished by passing these values as parameters to a new instance.
IAM authenticationβ
import { WatsonxLLM } from "@langchain/community/llms/ibm";
const props = {
version: "YYYY-MM-DD",
serviceUrl: "<SERVICE_URL>",
projectId: "<PROJECT_ID>",
watsonxAIAuthType: "iam",
watsonxAIApikey: "<YOUR-APIKEY>",
};
const instance = new WatsonxLLM(props);
Bearer token authenticationβ
import { WatsonxLLM } from "@langchain/community/llms/ibm";
const props = {
version: "YYYY-MM-DD",
serviceUrl: "<SERVICE_URL>",
projectId: "<PROJECT_ID>",
watsonxAIAuthType: "bearertoken",
watsonxAIBearerToken: "<YOUR-BEARERTOKEN>",
};
const instance = new WatsonxLLM(props);
CP4D authenticationβ
import { WatsonxLLM } from "@langchain/community/llms/ibm";
const props = {
version: "YYYY-MM-DD",
serviceUrl: "<SERVICE_URL>",
projectId: "<PROJECT_ID>",
watsonxAIAuthType: "cp4d",
watsonxAIUsername: "<YOUR-USERNAME>",
watsonxAIPassword: "<YOUR-PASSWORD>",
watsonxAIUrl: "<url>",
};
const instance = new WatsonxLLM(props);
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
# export LANGCHAIN_TRACING_V2="true"
# export LANGCHAIN_API_KEY="your-api-key"
Installationβ
The LangChain IBM watsonx.ai integration lives in the
@langchain/community
package:
- npm
- yarn
- pnpm
npm i @langchain/community @langchain/core
yarn add @langchain/community @langchain/core
pnpm add @langchain/community @langchain/core
Instantiationβ
Now we can instantiate our model object and generate chat completions:
import { WatsonxLLM } from "@langchain/community/llms/ibm";
const props = {
decoding_method: "sample",
maxNewTokens: 100,
minNewTokens: 1,
temperature: 0.5,
topK: 50,
topP: 1,
};
const instance = new WatsonxLLM({
version: "YYYY-MM-DD",
serviceUrl: process.env.API_URL,
projectId: "<PROJECT_ID>",
spaceId: "<SPACE_ID>",
idOrName: "<DEPLOYMENT_ID>",
model: "<MODEL_ID>",
...props,
});
Note:
- You must provide
spaceId
,projectId
oridOrName
(deployment id) in order to proceed. - Depending on the region of your provisioned service instance, use correct serviceUrl.
- You need to specify the model you want to use for inferencing through model_id.
Invocation and generationβ
const result = await instance.invoke("Print hello world.");
console.log(result);
const results = await instance.generate([
"Print hello world.",
"Print bye, bye world!",
]);
console.log(results);
print('Hello world.')<|endoftext|>
{
generations: [ [ [Object] ], [ [Object] ] ],
llmOutput: { tokenUsage: { generated_token_count: 28, input_token_count: 10 } }
}
Chainingβ
We can chain our completion model with a prompt template like so:
import { PromptTemplate } from "@langchain/core/prompts";
const prompt = PromptTemplate.fromTemplate(
"How to say {input} in {output_language}:\n"
);
const chain = prompt.pipe(instance);
await chain.invoke({
output_language: "German",
input: "I love programming.",
});
Ich liebe Programmieren.
To express that you are passionate about programming in German,
Props overwritingβ
Passed props at initialization will last for the whole life cycle of the object, however you may overwrite them for a single methodβs call by passing second argument as below
const result2 = await instance.invoke("Print hello world.", {
parameters: {
maxNewTokens: 100,
},
});
console.log(result2);
print('Hello world.')<|endoftext|>
Tokenizationβ
This package has itβs custom getNumTokens implementation which returns exact amount of tokens that would be used.
const tokens = await instance.getNumTokens("Print hello world.");
console.log(tokens);
4
API referenceβ
For detailed documentation of all IBM watsonx.ai
features and
configurations head to the API reference: API
docs
Relatedβ
- LLM conceptual guide
- LLM how-to guides