@ibm-cloud/watsonx-ai
Version:
IBM watsonx.ai Node.js SDK
1,008 lines • 294 kB
JavaScript
"use strict";
/**
* (C) Copyright IBM Corp. 2025.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
const ibm_cloud_sdk_core_1 = require("ibm-cloud-sdk-core");
const get_authenticator_from_environment_1 = require("../auth/utils/get-authenticator-from-environment");
const common_1 = require("../lib/common");
/**
* SDK entrypoint for IBM watsonx.ai product
*
* API Version: v1
*/
const PLATFORM_URLS_MAP = {
'https://ca-tor.ml.cloud.ibm.com': 'https://api.ca-tor.dai.cloud.ibm.com',
'https://jp-tok.ml.cloud.ibm.com': 'https://api.jp-tok.dataplatform.cloud.ibm.com/wx',
'https://eu-gb.ml.cloud.ibm.com': 'https://api.eu-gb.dataplatform.cloud.ibm.com/wx',
'https://eu-de.ml.cloud.ibm.com': 'https://api.eu-de.dataplatform.cloud.ibm.com/wx',
'https://us-south.ml.cloud.ibm.com': 'https://api.dataplatform.cloud.ibm.com/wx',
'https://private.ca-tor.ml.cloud.ibm.com': 'https://private.api.ca-tor.dai.cloud.ibm.com',
'https://private.jp-tok.ml.cloud.ibm.com': 'https://api.jp-tok.dataplatform.cloud.ibm.com/wx',
'https://private.eu-gb.ml.cloud.ibm.com': 'https://api.eu-gb.dataplatform.cloud.ibm.com/wx',
'https://private.eu-de.ml.cloud.ibm.com': 'https://api.eu-de.dataplatform.cloud.ibm.com/wx',
'https://private.us-south.ml.cloud.ibm.com': 'https://api.dataplatform.cloud.ibm.com/wx',
};
class WatsonxAiMlVml_v1 extends ibm_cloud_sdk_core_1.BaseService {
/**
* Constructs a service URL by formatting the parameterized service URL.
*
* The parameterized service URL is:
* 'https://{region}.ml.cloud.ibm.com'
*
* The default variable values are:
* - 'region': 'us-south'
*
* @param {Map<string, string>} | null providedUrlVariables Map from variable names to desired values.
* If a variable is not provided in this map,
* the default variable value will be used instead.
* @returns {string} The formatted URL with all variable placeholders replaced by values.
*/
static constructServiceUrl(providedUrlVariables) {
return (0, ibm_cloud_sdk_core_1.constructServiceUrl)(WatsonxAiMlVml_v1.PARAMETERIZED_SERVICE_URL, WatsonxAiMlVml_v1.defaultUrlVariables, providedUrlVariables);
}
/*************************
* Factory method
************************/
/**
* Constructs an instance of WatsonxAiMlVml_v1 with passed in options and external configuration.
*
* @param {UserOptions} [options] - The parameters to send to the service.
* @param {string} [options.serviceName] - The name of the service to configure
* @param {Authenticator} [options.authenticator] - The Authenticator object used to authenticate requests to the service
* @param {string} [options.serviceUrl] - The base URL for the service
* @returns {WatsonxAiMlVml_v1}
*
* @category constructor
*
*/
static newInstance(options) {
options = options || {};
if (!options.serviceName) {
options.serviceName = this.DEFAULT_SERVICE_NAME;
}
if (!options.authenticator) {
options.authenticator = (0, get_authenticator_from_environment_1.getAuthenticatorFromEnvironment)(options.serviceName, options.requestToken);
}
if (!options.platformUrl) {
options.platformUrl = (0, ibm_cloud_sdk_core_1.readExternalSources)(options.serviceName).platformUrl;
}
const service = new WatsonxAiMlVml_v1(options);
service.configureService(options.serviceName);
if (options.serviceUrl) {
service.setServiceUrl(options.serviceUrl);
}
return service;
}
/**
* Construct a WatsonxAiMlVml_v1 object.
*
* @param {Object} options - Options for the service.
* @param {string} options.version - The version date for the API of the form `YYYY-MM-DD`.
* @param {string} [options.serviceUrl] - The base URL for the service
* @param {OutgoingHttpHeaders} [options.headers] - Default headers that shall be included with every request to the service.
* @param {Authenticator} options.authenticator - The Authenticator object used to authenticate requests to the service
* @constructor
* @returns {WatsonxAiMlVml_v1}
*/
constructor(options) {
options = options || {};
const _requiredParams = ['version'];
// @ts-expect-error
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(options, _requiredParams, null);
if (_validationErrors) {
throw _validationErrors;
}
else {
options.version = options.version;
}
super(options);
if (options.serviceUrl) {
this.setServiceUrl(options.serviceUrl);
}
else {
this.setServiceUrl(WatsonxAiMlVml_v1.DEFAULT_SERVICE_URL);
}
if (!this.baseOptions.serviceUrl)
throw new Error('Something went wrong with setting up serviceUrl');
if (options.platformUrl) {
this.wxServiceUrl = options.platformUrl.concat('/wx');
this.serviceUrl = options.platformUrl;
}
else if (Object.keys(PLATFORM_URLS_MAP).includes(this.baseOptions.serviceUrl)) {
this.wxServiceUrl = PLATFORM_URLS_MAP[this.baseOptions.serviceUrl];
[this.serviceUrl] =
PLATFORM_URLS_MAP[this.baseOptions.serviceUrl].split('/wx');
}
else {
this.wxServiceUrl = this.baseOptions.serviceUrl.concat('/wx');
this.serviceUrl = this.baseOptions.serviceUrl;
}
this.version = options.version;
}
/*************************
* deployments
************************/
/**
* Create a new watsonx.ai deployment.
*
* Create a new deployment, currently the only supported type is `online`.
*
* If this is a deployment for a prompt tune then the `asset` object must exist and the `id` must be the `id` of the
* `model` that was created after the prompt training.
*
* If this is a deployment for a prompt template then the `prompt_template` object should exist and the `id` must be
* the `id` of the prompt template to be deployed.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.name - The name of the resource.
* @param {OnlineDeployment} params.online - Indicates that this is an online deployment. An object has to be
* specified but can be empty.
* The `serving_name` can be provided in the `online.parameters`.
* @param {string} [params.projectId] - The project that contains the resource. Either `space_id` or `project_id` has
* to be given.
* @param {string} [params.spaceId] - The space that contains the resource. Either `space_id` or `project_id` has to
* be given.
* @param {string} [params.description] - A description of the resource.
* @param {string[]} [params.tags] - A list of tags for this resource.
* @param {JsonObject} [params.custom] - User defined properties specified as key-value pairs.
* @param {SimpleRel} [params.promptTemplate] - A reference to a resource.
* @param {HardwareSpec} [params.hardwareSpec] - A hardware specification.
* @param {HardwareRequest} [params.hardwareRequest] - The requested hardware for deployment.
* @param {Rel} [params.asset] - A reference to a resource.
* @param {string} [params.baseModelId] - The base model that is required for this deployment if this is for a prompt
* template or a prompt tune for an IBM foundation model.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.DeploymentResource>>}
*
* @category Deployments
*/
createDeployment(params) {
const _params = Object.assign({}, params);
const _requiredParams = ['name', 'online'];
const _validParams = [
'name',
'online',
'projectId',
'spaceId',
'description',
'tags',
'custom',
'promptTemplate',
'hardwareSpec',
'hardwareRequest',
'asset',
'baseModelId',
'headers',
'signal',
'baseDeploymentId',
];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const body = {
'name': _params.name,
'online': _params.online,
'project_id': _params.projectId,
'space_id': _params.spaceId,
'description': _params.description,
'tags': _params.tags,
'custom': _params.custom,
'prompt_template': _params.promptTemplate,
'hardware_spec': _params.hardwareSpec,
'hardware_request': _params.hardwareRequest,
'asset': _params.asset,
'base_model_id': _params.baseModelId,
'base_deployment_id': _params.baseDeploymentId,
};
const query = {
'version': this.version,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'createDeployment');
const parameters = {
options: {
url: '/ml/v4/deployments',
method: 'POST',
body,
qs: query,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { Accept: 'application/json', 'Content-Type': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/**
* Retrieve the deployments.
*
* Retrieve the list of deployments for the specified space or project.
*
* @param {Object} [params] - The parameters to send to the service.
* @param {string} [params.spaceId] - The space that contains the resource. Either `space_id` or `project_id` query
* parameter has to be given.
* @param {string} [params.projectId] - The project that contains the resource. Either `space_id` or `project_id`
* query parameter has to be given.
* @param {string} [params.servingName] - Retrieves the deployment, if any, that contains this `serving_name`.
* @param {string} [params.tagValue] - Retrieves only the resources with the given tag value.
* @param {string} [params.assetId] - Retrieves only the resources with the given asset_id, asset_id would be the
* model id.
* @param {string} [params.promptTemplateId] - Retrieves only the resources with the given prompt_template_id.
* @param {string} [params.name] - Retrieves only the resources with the given name.
* @param {string} [params.type] - Retrieves the resources filtered with the given type. There are the deployment
* types as well as an additional
* `prompt_template` if the deployment type includes a prompt template.
*
* The supported deployment types are (see the description for `deployed_asset_type` in the deployment entity):
*
* 1. `prompt_tune` - when a prompt tuned model is deployed. 2. `foundation_model` - when a prompt template is used on
* a pre-deployed IBM provided model. 3. `custom_foundation_model` - when a custom foundation model is deployed.
*
* These can be combined with the flag `prompt_template` like this:
*
* 1. `type=prompt_tune` - return all prompt tuned model deployments. 2. `type=prompt_tune and prompt_template` -
* return all prompt tuned model deployments with a prompt template. 3. `type=foundation_model` - return all prompt
* template deployments. 4. `type=foundation_model and prompt_template` - return all prompt template deployments -
* this is the same as the previous query because a `foundation_model` can only exist with a prompt template. 5.
* `type=prompt_template` - return all deployments with a prompt template.
* @param {string} [params.state] - Retrieves the resources filtered by state. Allowed values are `initializing`,
* `updating`, `ready` and `failed`.
* @param {boolean} [params.conflict] - Returns whether `serving_name` is available for use or not. This query
* parameter cannot be combined with any other parameter except for `serving_name`.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.DeploymentResourceCollection>>}
*
* @category Deployments
*/
listDeployments(params) {
const _params = Object.assign({}, params);
const _requiredParams = [];
const _validParams = [
'spaceId',
'projectId',
'servingName',
'tagValue',
'assetId',
'promptTemplateId',
'name',
'type',
'state',
'conflict',
'headers',
'signal',
];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const query = {
'version': this.version,
'space_id': _params.spaceId,
'project_id': _params.projectId,
'serving_name': _params.servingName,
'tag.value': _params.tagValue,
'asset_id': _params.assetId,
'prompt_template_id': _params.promptTemplateId,
'name': _params.name,
'type': _params.type,
'state': _params.state,
'conflict': _params.conflict,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'listDeployments');
const parameters = {
options: {
url: '/ml/v4/deployments',
method: 'GET',
qs: query,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/**
* Retrieve the deployment details.
*
* Retrieve the deployment details with the specified identifier.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.deploymentId - The deployment id.
* @param {string} [params.spaceId] - The space that contains the resource. Either `space_id` or `project_id` query
* parameter has to be given.
* @param {string} [params.projectId] - The project that contains the resource. Either `space_id` or `project_id`
* query parameter has to be given.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.DeploymentResource>>}
*
* @category Deployments
*/
getDeployment(params) {
const _params = Object.assign({}, params);
const _requiredParams = ['deploymentId'];
const _validParams = ['deploymentId', 'spaceId', 'projectId', 'headers', 'signal'];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const query = {
'version': this.version,
'space_id': _params.spaceId,
'project_id': _params.projectId,
};
const path = {
'deployment_id': _params.deploymentId,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'deploymentsGet');
const parameters = {
options: {
url: '/ml/v4/deployments/{deployment_id}',
method: 'GET',
qs: query,
path,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/**
* Update the deployment metadata.
*
* Update the deployment metadata. The following parameters of deployment metadata are supported for the patch
* operation.
*
* - `/name`
* - `/description`
* - `/tags`
* - `/custom`
* - `/online/parameters`
* - `/asset` - `replace` only
* - `/prompt_template` - `replace` only
* - `/hardware_spec`
* - `/hardware_request`
* - `/base_model_id` - `replace` only (applicable only to prompt template deployments referring to IBM base
* foundation models)
*
* The PATCH operation with path specified as `/online/parameters` can be used to update the `serving_name`.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.deploymentId - The deployment id.
* @param {JsonPatchOperation[]} params.jsonPatch - The json patch.
* @param {string} [params.spaceId] - The space that contains the resource. Either `space_id` or `project_id` query
* parameter has to be given.
* @param {string} [params.projectId] - The project that contains the resource. Either `space_id` or `project_id`
* query parameter has to be given.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.DeploymentResource>>}
*
* @category Deployments
*/
updateDeployment(params) {
const _params = Object.assign({}, params);
const _requiredParams = ['deploymentId', 'jsonPatch'];
const _validParams = ['deploymentId', 'jsonPatch', 'spaceId', 'projectId', 'headers', 'signal'];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const body = _params.jsonPatch;
const query = {
'version': this.version,
'space_id': _params.spaceId,
'project_id': _params.projectId,
};
const path = {
'deployment_id': _params.deploymentId,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'deploymentsUpdate');
const parameters = {
options: {
url: '/ml/v4/deployments/{deployment_id}',
method: 'PATCH',
body,
qs: query,
path,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json', 'Content-Type': 'application/json-patch+json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/**
* Delete the deployment.
*
* Delete the deployment with the specified identifier.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.deploymentId - The deployment id.
* @param {string} [params.spaceId] - The space that contains the resource. Either `space_id` or `project_id` query
* parameter has to be given.
* @param {string} [params.projectId] - The project that contains the resource. Either `space_id` or `project_id`
* query parameter has to be given.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.EmptyObject>>}
*
* @category Deployments
*/
deleteDeployment(params) {
const _params = Object.assign({}, params);
const _requiredParams = ['deploymentId'];
const _validParams = ['deploymentId', 'spaceId', 'projectId', 'headers', 'signal'];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const query = {
'version': this.version,
'space_id': _params.spaceId,
'project_id': _params.projectId,
};
const path = {
'deployment_id': _params.deploymentId,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'deploymentsDelete');
const parameters = {
options: {
url: '/ml/v4/deployments/{deployment_id}',
method: 'DELETE',
qs: query,
path,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign({}, sdkHeaders), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/**
* Infer text.
*
* Infer the next tokens for a given deployed model with a set of parameters. If a `serving_name` is used then it must
* match the `serving_name` that is returned in the `inference` section when the deployment was created.
*
* ### Return options
*
* Note that there is currently a limitation in this operation when using `return_options`, for input only
* `input_text` will be returned if requested, for output the `input_tokens` and `generated_tokens` will not be
* returned.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.idOrName - The `id_or_name` can be either the `deployment_id` that identifies the deployment
* or a `serving_name` that allows a predefined URL to be used to post a prediction.
*
* The `project` or `space` for the deployment must have a WML instance that will be used for limits and billing (if a
* paid plan).
* @param {string} [params.input] - The prompt to generate completions. Note: The method tokenizes the input
* internally. It is recommended not to leave any trailing spaces.
*
*
* This field is ignored if there is a prompt template.
* @param {DeploymentTextGenProperties} [params.parameters] - The template properties if this request refers to a
* prompt template.
* @param {Moderations} [params.moderations] - Properties that control the moderations, for usages such as `Hate and
* profanity` (HAP) and `Personal identifiable information` (PII) filtering. This list can be extended with new types
* of moderations.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @param {Object} callbacks - The parameters to send to the service.
* @param {InvokeRequestCallback} [callbacks.requestCallback] - Callback invoked with paramteres payload for API call
* @param {ReceiveResponseCallback} [callbacks.responseCallback] - Callback invoked with paramteres response from API call
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.TextGenResponse>>}
*
* @category Deployments
*/
deploymentGenerateText(params, callbacks) {
const _params = Object.assign({}, params);
const _requiredParams = ['idOrName'];
const _validParams = ['idOrName', 'input', 'parameters', 'moderations', 'headers', 'signal'];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const body = {
'input': _params.input,
'parameters': _params.parameters,
'moderations': _params.moderations,
};
const query = {
'version': this.version,
};
const path = {
'id_or_name': _params.idOrName,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'deploymentsTextGeneration');
const parameters = {
options: {
url: '/ml/v1/deployments/{id_or_name}/text/generation',
method: 'POST',
body,
qs: query,
path,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json', 'Content-Type': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
const callbackHandler = callbacks
? new WatsonxAiMlVml_v1.CallbackHandler(callbacks)
: undefined;
callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleRequest(parameters);
const response = this.createRequest(parameters);
callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleResponse(response);
return response;
}
deploymentGenerateTextStream(params, callbacks) {
return __awaiter(this, void 0, void 0, function* () {
const _params = Object.assign({}, params);
const _requiredParams = ['idOrName'];
const _validParams = [
'idOrName',
'input',
'parameters',
'moderations',
'headers',
'signal',
'returnObject',
];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const body = {
'input': _params.input,
'parameters': _params.parameters,
'moderations': _params.moderations,
};
const query = {
'version': this.version,
};
const path = {
'id_or_name': _params.idOrName,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'deploymentsTextGenerationStream');
const parameters = {
options: {
url: '/ml/v1/deployments/{id_or_name}/text/generation_stream',
method: 'POST',
body,
qs: query,
path,
responseType: 'stream',
adapter: 'fetch',
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'text/event-stream', 'Connection': 'keep-alive', 'Content-Type': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
const callbackHandler = callbacks
? new WatsonxAiMlVml_v1.CallbackHandler(callbacks)
: undefined;
callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleRequest(parameters);
const apiResponse = yield this.createRequest(parameters);
callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleResponse(apiResponse);
const stream = _params.returnObject
? (0, common_1.transformStreamToObjectStream)(apiResponse)
: (0, common_1.transformStreamToStringStream)(apiResponse);
return stream;
});
}
/**
* Infer text chat.
*
* Infer the next chat message for a given deployment. The deployment must reference a prompt template which has
* `input_mode` set to `chat`. The model to the chat request will be from the deployment `base_model_id`. Parameters
* to the chat request will be from the prompt template `model_parameters`. Related guides:
* [Deployment](https://cloud.ibm.com/apidocs/watsonx-ai#create-deployment), [Prompt
* template](https://cloud.ibm.com/apidocs/watsonx-ai#post-prompt), [Text
* chat](https://cloud.ibm.com/apidocs/watsonx-ai#text-chat).
*
* If a `serving_name` is used then it must match the `serving_name` that is returned in the `inference` section when
* the deployment was created.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.idOrName - The `id_or_name` can be either the `deployment_id` that identifies the deployment
* or a `serving_name` that allows a predefined URL to be used to post a prediction. The deployment must reference a
* prompt template with `input_mode` `chat`.
*
* The WML instance that is associated with the deployment will be used for limits and billing (if a paid plan).
* @param {DeploymentTextChatMessages[]} params.messages - The messages for this chat session. You cannot specify
* `system` `role` in the messages. Depending on the model, the `content` of `system` `role` may be from
* `system_prompt` of the prompt template, and will be automatically inserted into `messages`.
*
* As an example, depending on the model, if `system_prompt` of a prompt template is "You are Granite Chat, an AI
* language model developed by IBM. You are a cautious assistant. You carefully follow instructions. You are helpful
* and harmless and you follow ethical guidelines and promote positive behavior.", a message with `system` `role`
* having `content` the same as `system_prompt` is inserted.
* @param {string} [params.context] - If specified, `context` will be inserted into `messages`. Depending on the
* model, `context` may be inserted into the `content` with `system` `role`; or into the `content` of the last message
* of `user` `role`.
*
*
* In the example, `context` "Today is Wednesday" is inserted as such
* `content` of `user` becomes "Today is Wednesday. Who are you and which day is tomorrow?".
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.TextChatResponse>>}
*
* @category Deployments
*/
deploymentsTextChat(params, callbacks) {
const _params = Object.assign({}, params);
const _requiredParams = ['idOrName', 'messages'];
const _validParams = ['idOrName', 'messages', 'context', 'headers', 'signal'];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const body = {
'messages': _params.messages,
'context': _params.context,
};
const query = {
'version': this.version,
};
const path = {
'id_or_name': _params.idOrName,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'deploymentsTextChat');
const parameters = {
options: {
url: '/ml/v1/deployments/{id_or_name}/text/chat',
method: 'POST',
body,
qs: query,
path,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json', 'Content-Type': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
const callbackHandler = callbacks
? new WatsonxAiMlVml_v1.CallbackHandler(callbacks)
: undefined;
callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleRequest(parameters);
const response = this.createRequest(parameters);
callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleResponse(response);
return response;
}
deploymentsTextChatStream(params, callbacks) {
return __awaiter(this, void 0, void 0, function* () {
const _params = Object.assign({}, params);
const _requiredParams = ['idOrName', 'messages'];
const _validParams = ['idOrName', 'messages', 'context', 'headers', 'signal', 'returnObject'];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const body = {
'messages': _params.messages,
'context': _params.context,
};
const query = {
'version': this.version,
};
const path = {
'id_or_name': _params.idOrName,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'deploymentsTextChatStream');
const parameters = {
options: {
url: '/ml/v1/deployments/{id_or_name}/text/chat_stream',
method: 'POST',
body,
qs: query,
path,
responseType: 'stream',
adapter: 'fetch',
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'text/event-stream', 'Connection': 'keep-alive', 'Content-Type': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
const callbackHandler = callbacks
? new WatsonxAiMlVml_v1.CallbackHandler(callbacks)
: undefined;
callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleRequest(parameters);
const apiResponse = yield this.createRequest(parameters);
callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleResponse(apiResponse);
const stream = _params.returnObject
? (0, common_1.transformStreamToObjectStream)(apiResponse)
: (0, common_1.transformStreamToStringStream)(apiResponse);
return stream;
});
}
/*************************
* foundationModelSpecs
************************/
/**
* List the available foundation models.
*
* Retrieve the list of deployed foundation models.
*
* @param {Object} [params] - The parameters to send to the service.
* @param {string} [params.start] - Token required for token-based pagination. This token cannot be determined by end
* user. It is generated by the service and it is set in the href available in the `next` field.
* @param {number} [params.limit] - How many resources should be returned. By default limit is 100. Max limit allowed
* is 200.
* @param {string} [params.filters] - A set of filters to specify the list of models, filters are described as the
* `pattern` shown below.
* ```text
* pattern: tfilter[,tfilter][:(or|and)]
* tfilter: filter | !filter
* filter: Requires existence of the filter.
* !filter: Requires absence of the filter.
* filter: one of
* modelid_*: Filters by model id.
* Namely, select a model with a specific model id.
* provider_*: Filters by provider.
* Namely, select all models with a specific provider.
* source_*: Filters by source.
* Namely, select all models with a specific source.
* input_tier_*: Filters by input tier.
* Namely, select all models with a specific input tier.
* output_tier_*: Filters by output tier.
* Namely, select all models with a specific output tier.
* tier_*: Filters by tier.
* Namely, select all models with a specific input or output tier.
* task_*: Filters by task id.
* Namely, select all models that support a specific task id.
* lifecycle_*: Filters by lifecycle state.
* Namely, select all models that are currently in the specified lifecycle state.
* function_*: Filters by function.
* Namely, select all models that support a specific function.
* ```.
* @param {boolean} [params.techPreview] - See all the `Tech Preview` models if entitled.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.FoundationModels>>}
*
* @category Foundation Model Specs
*/
listFoundationModelSpecs(params) {
const _params = Object.assign({}, params);
const _requiredParams = [];
const _validParams = ['start', 'limit', 'filters', 'techPreview', 'headers', 'signal'];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const query = {
'version': this.version,
'start': _params.start,
'limit': _params.limit,
'filters': _params.filters,
'tech_preview': _params.techPreview,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'listFoundationModelSpecs');
const parameters = {
options: {
url: '/ml/v1/foundation_model_specs',
method: 'GET',
qs: query,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/**
* List the supported tasks.
*
* Retrieve the list of tasks that are supported by the foundation models.
*
* @param {Object} [params] - The parameters to send to the service.
* @param {string} [params.start] - Token required for token-based pagination. This token cannot be determined by end
* user. It is generated by the service and it is set in the href available in the `next` field.
* @param {number} [params.limit] - How many resources should be returned. By default limit is 100. Max limit allowed
* is 200.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.FoundationModelTasks>>}
*
* @category Foundation Model Specs
*/
listFoundationModelTasks(params) {
const _params = Object.assign({}, params);
const _requiredParams = [];
const _validParams = ['start', 'limit', 'headers', 'signal'];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const query = {
'version': this.version,
'start': _params.start,
'limit': _params.limit,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'listFoundationModelTasks');
const parameters = {
options: {
url: '/ml/v1/foundation_model_tasks',
method: 'GET',
qs: query,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/*************************
* prompts
************************/
/**
* Create a new prompt / prompt template.
*
* This creates a new prompt with the provided parameters.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.name - Name used to display the prompt.
* @param {PromptWithExternal} params.prompt -
* @param {string} [params.description] - An optional description for the prompt.
* @param {number} [params.createdAt] - Time the prompt was created.
* @param {string[]} [params.taskIds] -
* @param {PromptLock} [params.lock] -
* @param {WxPromptPostModelVersion} [params.modelVersion] -
* @param {JsonObject} [params.promptVariables] -
* @param {string} [params.inputMode] - Input mode in use for the prompt.
* @param {string} [params.projectId] - [REQUIRED] Specifies the project ID as the target. One target must be supplied
* per request.
* @param {string} [params.spaceId] - [REQUIRED] Specifies the space ID as the target. One target must be supplied per
* request.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.WxPromptResponse>>}
*
* @category Prompts / Prompt Templates
*/
createPrompt(params) {
const _params = Object.assign({}, params);
const _requiredParams = ['name', 'prompt'];
const _validParams = [
'name',
'prompt',
'description',
'createdAt',
'taskIds',
'lock',
'modelVersion',
'promptVariables',
'inputMode',
'projectId',
'spaceId',
'headers',
'signal',
];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const body = {
'name': _params.name,
'prompt': _params.prompt,
'description': _params.description,
'created_at': _params.createdAt,
'task_ids': _params.taskIds,
'lock': _params.lock,
'model_version': _params.modelVersion,
'prompt_variables': _params.promptVariables,
'input_mode': _params.inputMode,
};
const query = {
'project_id': _params.projectId,
'space_id': _params.spaceId,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'postPrompt');
const parameters = {
options: {
url: '/v1/prompts',
method: 'POST',
body,
qs: query,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { serviceUrl: this.wxServiceUrl, headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json', 'Content-Type': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/**
* Get a prompt.
*
* This retrieves a prompt / prompt template with the given id.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.promptId - Prompt ID.
* @param {string} [params.projectId] - [REQUIRED] Specifies the project ID as the target. One target must be supplied
* per request.
* @param {string} [params.spaceId] - [REQUIRED] Specifies the space ID as the target. One target must be supplied per
* request.
* @param {string} [params.restrictModelParameters] - Only return a set of model parameters compatiable with
* inferencing.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<WatsonxAiMlVml_v1.Response<WatsonxAiMlVml_v1.WxPromptResponse>>}
*
* @category Prompts / Prompt Templates
*/
getPrompt(params) {
const _params = Object.assign({}, params);
const _requiredParams = ['promptId'];
const _validParams = [
'promptId',
'projectId',
'spaceId',
'restrictModelParameters',
'headers',
'signal',
];
const _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
const query = {
'project_id': _params.projectId,
'space_id': _params.spaceId,
'restrict_model_parameters': _params.restrictModelParameters,
};
const path = {
'prompt_id': _params.promptId,
};
const sdkHeaders = (0, common_1.getSdkHeaders)(WatsonxAiMlVml_v1.DEFAULT_SERVICE_NAME, 'vml_v1', 'getPrompt');
const parameters = {
options: {
url: '/v1/prompts/{prompt_id}',
method: 'GET',
qs: query,
path,
},
defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { serviceUrl: this.wxServiceUrl, headers: Object.assign(Object.assign(Object.assign({}, sdkHeaders), { 'Accept': 'application/json' }), _params.headers), axiosOptions: {
signal: _params.signal,
} }),
};
return this.createRequest(parameters);
}
/**
* List all prompts.
*
* This retrieves all prompts within the given project/space.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} [params.projectId] - [REQUIRED] Specifies the project ID as the target. One target must be supplied
* per request.
* @param {string} [params.spaceId] - [REQUIRED] Specifies the space ID as the target. One target must be supplied per
* request.
* @param {string} [params.limit] - The limit request body field can be specified to limit the number of assets in the search results. The default limit is 200. The maximum limit value is 200, and any greater value is ignored.
* @param {string} [params.counts] - Returns the number of query results for each unique value of each named field.
* @param {string} [params.drilldown] - Restrict results to documents with a dimension equal to the specified label. Note that, multiple values for a single key in a drilldown means an OR relation between them and there is an AND relation between multiple keys.
* @param {string} [params.bookmark] - Bookmark of the query result
* @param {string} [params.sort]