IBM watsonx.ai Node.js SDK
npm install @ibm-cloud/watsonx-ai
!NPM Downloads
- IBM watsonx.ai Node.js SDK
- Table of Contents
- Overview
- Prerequisites
- Installation
- Using the SDK
- With environment variables
- IAM authentication
- Bearer token authentication
- IBM watsonx.ai software authentication
- With an external credentials file
- With programmatic approach
- Code examples
- Basic example - text generation/inference
- Lightweight engine
- More examples
- Questions
- Issues
- Open source @ IBM
- Contributing
- License
The IBM watsonx.ai Node.js SDK allows developers to programmatically interact with the IBM watsonx.ai service.
[ibm-cloud-onboarding]: http://cloud.ibm.com/registration
[ibm-cpd-onboarding]: https://www.ibm.com/products/cloud-pak-for-data
sh
npm install @ibm-cloud/watsonx-ai
`Using the SDK
For general SDK usage information, please see
this linkIBM watsonx.ai Node.js SDK documentation can be found here
This library requires configuration with a service URL and platform credentials to authenticate to your account.
There are several ways to set these authentication properties.
$3
You can set the following environment variables for chosen authentication type.
#### IAM authentication
`sh
WATSONX_AI_AUTH_TYPE=iam
WATSONX_AI_APIKEY=
`#### Bearer token authentication
`sh
WATSONX_AI_AUTH_TYPE=bearertoken
WATSONX_AI_BEARER_TOKEN=
`#### IBM watsonx.ai software authentication
`sh
WATSONX_AI_AUTH_TYPE=cp4d
WATSONX_AI_USERNAME=
WATSONX_AI_PASSWORD=
WATSONX_AI_URL=url
`
If any troubles regarding SSL verification appear, such as "Error: self-signed certificate in certificate chain", please try setting up environment variables as below:
`sh
WATSONX_AI_DISABLE_SSL=true
WATSONX_AI_AUTH_DISABLE_SSL=true
`$3
To use an external configuration file, please see the general SDK usage information for guidance. Additionally, please see the following template files for:
- IAM authentication
- Bearer token authentication
- CP4D authentication$3
To learn more about how to use programmatic authentication, see the Node.js SDK Core document about authentication.Code examples
$3
The following code examples authenticate with the environment variables.
Please set environment variables before proceeding with examples:
It is mandatory to set projectId or spaceId unless you are working with lightweight engine.
`ts
const { WatsonXAI } = require('@ibm-cloud/watsonx-ai');// Service instance
const watsonxAIService = WatsonXAI.newInstance({
version: '2024-05-31',
serviceUrl: 'https://us-south.ml.cloud.ibm.com',
});
const textGenRequestParametersModel = {
max_new_tokens: 100,
};
const params = {
input: 'Generate a short greeting for project kick-off meeting.',
modelId: 'ibm/granite-13b-chat-v2',
projectId: '',
parameters: textGenRequestParametersModel,
};
try {
const textGeneration = watsonxAIService
.generateText(params)
.then((res) => {
console.log("\n\n TEXT RESPONSE FROM MODEL ");
console.log(res.result.results[0].generated_text);
})
} catch (err) {
console.warn(err);
}
`When you run this code, you should see result similar to the following output:
`text
TEXT RESPONSE FROM MODEL
Welcome to the project kick-off meeting. I'm glad you could make it.
`$3
For a watsonx.ai lightweight engine, you do not need to provide a projectId or spaceId. Remember to set environment variables (IBM watsonx.ai software authentication) before proceeding.`ts
const { WatsonXAI } = require('@ibm-cloud/watsonx-ai');// Service instance
const watsonxAIService = WatsonXAI.newInstance({
version: '2024-05-31',
serviceUrl: process.env.SERVICE_URL,
});
const params = {
input: 'Generate a short greeting for project kick-off meeting.',
modelId: 'mistralai/ministral-8b-instruct',
};
try {
const textGeneration = watsonxAIService
.generateText(params)
.then((res) => {
console.log("\n\n TEXT RESPONSE FROM MODEL ");
console.log(res.result.results[0].generated_text);
})
} catch (err) {
console.warn(err);
}
``If you are having difficulties using this SDK or have a question about the IBM Cloud services,
please ask a question at
Stack Overflow.
This project is released under the Apache 2.0 license.
The license's full text can be found in
LICENSE.