UNPKG

@ibm-cloud/watsonx-ai

Version:
1,034 lines (1,033 loc) 301 kB
/** * (C) Copyright IBM Corp. 2024-2026. * * 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; }; import fs from 'fs'; import { constructServiceUrl, getQueryParam } from 'ibm-cloud-sdk-core'; import FormData from 'form-data'; import { getSdkHeaders, transformStreamToObjectStream, transformStreamToStringStream, } from "./lib/common.mjs"; import { validateRequestParams } from "./helpers/validators.mjs"; import { WatsonxBaseService } from "./base/index.mjs"; import * as Types from "./types/index.mjs"; import { ML_CLOUD_BASE_URL, ENDPOINTS } from "./config/index.mjs"; /** * SDK entrypoint for IBM watsonx.ai product. Provides access to IBM watsonx.ai services including * text generation, embeddings, deployments, and model management. * * @example * ```typescript * const service = new WatsonXAI({ * version: '2024-03-14', * serviceUrl: 'https://us-south.ml.cloud.ibm.com', * authenticator: new IamAuthenticator({ apikey: 'your-api-key' }) * }); * ```; * * @class WatsonXAI * @extends WatsonxBaseService */ class WatsonXAI extends WatsonxBaseService { /** * 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 constructServiceUrl(WatsonXAI.PARAMETERIZED_SERVICE_URL, WatsonXAI.defaultUrlVariables, providedUrlVariables); } /** Factory method */ /** * Constructs an instance of WatsonXAI with passed in options and external configuration. * * Ensuring backwards compatibility from v1.7.1. You can now use: * * ```typescript * const service = new WatsonXAI(options); * ``` * * @category Constructor * @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 {WatsonXAI} - An instance of the WatsonXAI service */ // Ensuring backwards compatibility static newInstance(options) { return new WatsonXAI(options); } /** * Creates and executes an HTTP request to the watsonx.ai API. Handles HTTPS agent configuration, * encryption parameters, and callback hooks. * * @template T - The expected response type * @param {CreateRequestParams} parameters - Request parameters including URL, method, headers, * and body * @param {AdditionalCreateRequestParams} [additionalParameters] - Optional additional parameters * @param {object} [additionalParameters.crypto] - Encryption configuration for the request body * @param {object} [additionalParameters.callbacks] - Callback handlers for request/response * lifecycle * @returns {Promise<T>} Promise resolving to the API response * @protected */ createRequest(parameters, additionalParameters) { const _super = Object.create(null, { createRequest: { get: () => super.createRequest } }); var _a; var _b; return __awaiter(this, void 0, void 0, function* () { const apiType = parameters.defaultOptions.serviceUrl && parameters.defaultOptions.serviceUrl.includes('api') ? 'dataplatform' : 'service'; parameters.defaultOptions.axiosOptions.httpsAgent = this.httpsAgentMap[apiType]; if (additionalParameters) { const { crypto } = additionalParameters; if (crypto) { (_a = (_b = parameters.options).body) !== null && _a !== void 0 ? _a : (_b.body = {}); parameters.options.body = Object.assign({ crypto }, parameters.options.body); } } const callbackHandler = (additionalParameters === null || additionalParameters === void 0 ? void 0 : additionalParameters.callbacks) ? new WatsonXAI.CallbackHandler(additionalParameters === null || additionalParameters === void 0 ? void 0 : additionalParameters.callbacks) : undefined; callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleRequest(parameters); const response = _super.createRequest.call(this, parameters); callbackHandler === null || callbackHandler === void 0 ? void 0 : callbackHandler.handleResponse(response); return response; }); } /** 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. * * @category Deployments * @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<WatsonXAI.Response<WatsonXAI.DeploymentResource>>} A promise that resolves to * the response with deployment's data */ createDeployment(params) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['name', 'online']; const validParams = [ 'projectId', 'spaceId', 'description', 'tags', 'custom', 'promptTemplate', 'hardwareSpec', 'hardwareRequest', 'asset', 'baseModelId', 'baseDeploymentId', ]; validateRequestParams(params, requiredParams, validParams); const { projectId, spaceId } = this.resolveContextId(params); const body = { 'name': params.name, 'online': params.online, 'project_id': projectId, 'space_id': 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 = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.BASE, 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. * * @category Deployments * @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<WatsonXAI.Response<WatsonXAI.DeploymentResourceCollection>>} A Promise that * resolves to a collection of deployment resources */ listDeployments(params = {}) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = []; const validParams = [ 'spaceId', 'projectId', 'servingName', 'tagValue', 'assetId', 'promptTemplateId', 'name', 'type', 'state', 'conflict', ]; validateRequestParams(params, requiredParams, validParams); const { projectId, spaceId } = this.resolveContextId(params); const query = { 'version': this.version, 'space_id': spaceId, 'project_id': 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 = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.BASE, 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. * * @category Deployments * @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<WatsonXAI.Response<WatsonXAI.DeploymentResource>>} A Promise that resolves to * a deployment resource */ getDeployment(params) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['deploymentId']; const validParams = ['spaceId', 'projectId']; validateRequestParams(params, requiredParams, validParams); const { projectId, spaceId } = this.resolveContextId(params); const query = { 'version': this.version, 'space_id': spaceId, 'project_id': projectId, }; const path = { 'deployment_id': params.deploymentId, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.BY_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`. * * @category Deployments * @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<WatsonXAI.Response<WatsonXAI.DeploymentResource>>} A Promise that resolves to * a deployment resource */ updateDeployment(params) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['deploymentId', 'jsonPatch']; const validParams = ['spaceId', 'projectId']; validateRequestParams(params, requiredParams, validParams); const { projectId, spaceId } = this.resolveContextId(params); const body = params.jsonPatch; const query = { 'version': this.version, 'space_id': spaceId, 'project_id': projectId, }; const path = { 'deployment_id': params.deploymentId, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.BY_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. * * @category Deployments * @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<WatsonXAI.Response<WatsonXAI.EmptyObject>>} A Promise that resolves to an * empty response object */ deleteDeployment(params) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['deploymentId']; const validParams = ['spaceId', 'projectId']; validateRequestParams(params, requiredParams, validParams); const { projectId, spaceId } = this.resolveContextId(params); const query = { 'version': this.version, 'space_id': spaceId, 'project_id': projectId, }; const path = { 'deployment_id': params.deploymentId, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.BY_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. * * @category Deployments * @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<WatsonXAI.Response<WatsonXAI.TextGenResponse>>} A Promise that resolves to a * text generation response */ deploymentGenerateText(params, callbacks) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['idOrName']; const validParams = ['input', 'parameters', 'moderations']; validateRequestParams(params, requiredParams, validParams); 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 = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.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, } }), }; return this.createRequest(parameters, { callbacks, }); }); } deploymentGenerateTextStream(params, callbacks) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['idOrName']; const validParams = ['input', 'parameters', 'moderations', 'returnObject']; validateRequestParams(params, requiredParams, validParams); 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 = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.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 apiResponse = yield this.createRequest(parameters, { callbacks }); const stream = params.returnObject ? transformStreamToObjectStream(apiResponse) : 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. * * @category Deployments * @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 * @param {WatsonXAI.RequestCallbacks<WatsonXAI.Response<WatsonXAI.TextChatResponse>>} callbacks * - The object containing callbacks for requests and response * * @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<WatsonXAI.Response<WatsonXAI.TextChatResponse>>} A Promise that resolves to a * text chat response */ deploymentsTextChat(params, callbacks) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['idOrName', 'messages']; const validParams = [ 'context', 'tools', 'toolChoiceOption', 'toolChoice', 'frequencyPenalty', 'logitBias', 'logprobs', 'topLogprobs', 'maxTokens', 'maxCompletionTokens', 'n', 'presencePenalty', 'responseFormat', 'seed', 'stop', 'temperature', 'topP', 'timeLimit', 'repetitionPenalty', 'lengthPenalty', 'includeReasoning', 'reasoningEffort', ]; validateRequestParams(params, requiredParams, validParams); const body = { 'messages': params.messages, 'tools': params.tools, 'tool_choice_option': params.toolChoiceOption, 'tool_choice': params.toolChoice, 'frequency_penalty': params.frequencyPenalty, 'logit_bias': params.logitBias, 'logprobs': params.logprobs, 'top_logprobs': params.topLogprobs, 'max_tokens': params.maxTokens, 'max_completion_tokens': params.maxCompletionTokens, 'n': params.n, 'presence_penalty': params.presencePenalty, 'response_format': params.responseFormat, 'seed': params.seed, 'stop': params.stop, 'temperature': params.temperature, 'top_p': params.topP, 'repetition_penalty': params.repetitionPenalty, 'length_penalty': params.lengthPenalty, 'include_reasoning': params.includeReasoning, 'reasoning_effort': params.reasoningEffort, 'time_limit': params.timeLimit, }; const query = { 'version': this.version, }; const path = { 'id_or_name': params.idOrName, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.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, } }), }; return this.createRequest(parameters, { callbacks }); }); } deploymentsTextChatStream(params, callbacks) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['idOrName', 'messages']; const validParams = [ 'context', 'tools', 'toolChoiceOption', 'toolChoice', 'frequencyPenalty', 'logitBias', 'logprobs', 'topLogprobs', 'maxTokens', 'maxCompletionTokens', 'n', 'presencePenalty', 'responseFormat', 'seed', 'stop', 'temperature', 'topP', 'timeLimit', 'repetitionPenalty', 'lengthPenalty', 'includeReasoning', 'reasoningEffort', 'returnObject', ]; validateRequestParams(params, requiredParams, validParams); const body = { 'messages': params.messages, 'tools': params.tools, 'tool_choice_option': params.toolChoiceOption, 'tool_choice': params.toolChoice, 'frequency_penalty': params.frequencyPenalty, 'logit_bias': params.logitBias, 'logprobs': params.logprobs, 'top_logprobs': params.topLogprobs, 'max_tokens': params.maxTokens, 'max_completion_tokens': params.maxCompletionTokens, 'n': params.n, 'presence_penalty': params.presencePenalty, 'response_format': params.responseFormat, 'seed': params.seed, 'stop': params.stop, 'temperature': params.temperature, 'top_p': params.topP, 'repetition_penalty': params.repetitionPenalty, 'length_penalty': params.lengthPenalty, 'include_reasoning': params.includeReasoning, 'reasoning_effort': params.reasoningEffort, 'time_limit': params.timeLimit, }; const query = { 'version': this.version, }; const path = { 'id_or_name': params.idOrName, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.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 apiResponse = yield this.createRequest(parameters, { callbacks }); const stream = params.returnObject ? transformStreamToObjectStream(apiResponse) : transformStreamToStringStream(apiResponse); return stream; }); } /** * Time series forecast. * * Generate forecasts, or predictions for future time points, given historical time series data. * * @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 WML instance that is associated with the deployment will be used for limits and billing (if * a paid plan). * @param {JsonObject} params.data - A payload of data matching `schema`. We assume the following * about your data: All timeseries are of equal length and are uniform in nature (the time * difference between two successive rows is constant). This implies that there are no missing * rows of data; The data meet the minimum model-dependent historical context length which can * be any number of rows per timeseries; * * Note that the example payloads shown are for illustration purposes only. An actual payload * would necessary be much larger to meet minimum model-specific context lengths. * @param {TSForecastInputSchema} params.schema - Contains metadata about your timeseries data * input. * @param {DeploymentTSForecastParameters} [params.parameters] * * - The parameters for the forecast request. * * @param {JsonObject} [params.futureData] - Exogenous or supporting features that extend into the * forecasting horizon (e.g., a weather forecast or calendar of special promotions) which are * known in advance. `future_data` would be in the same format as `data` except that all * timestamps would be in the forecast horizon and it would not include previously specified * `target_columns`. * @param {OutgoingHttpHeaders} [params.headers] - Custom request headers * @returns {Promise<WatsonXAI.Response<WatsonXAI.TSForecastResponse>>} A Promise that resolves to * a time series forecast response */ deploymentsTimeSeriesForecast(params) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['idOrName', 'data', 'schema']; const validParams = ['parameters', 'futureData']; validateRequestParams(params, requiredParams, validParams); const body = { 'data': params.data, 'schema': params.schema, 'parameters': params.parameters, 'future_data': params.futureData, }; const query = { 'version': this.version, }; const path = { 'id_or_name': params.idOrName, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.DEPLOYMENT.TIME_SERIES_FORECAST, method: 'POST', body, qs: query, path, }, defaultOptions: Object.assign(Object.assign({}, this.baseOptions), { serviceUrl: this.serviceUrl, 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); }); } /** FoundationModelSpecs */ /** * List the available foundation models. * * Retrieve the list of deployed foundation models. * * @category Foundation Model Specs * @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<WatsonXAI.Response<WatsonXAI.FoundationModels>>} A Promise that resolves to a list * of foundation models */ listFoundationModelSpecs(params = {}) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = []; const validParams = ['start', 'limit', 'filters', 'techPreview']; validateRequestParams(params, requiredParams, validParams); const query = { 'version': this.version, 'start': params.start, 'limit': params.limit, 'filters': params.filters, 'tech_preview': params.techPreview, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.FOUNDATION_MODEL.LIST_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. * * @category Foundation Model Specs * @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<WatsonXAI.Response<WatsonXAI.FoundationModelTasks>>} A Promise that resolves * to a list of foundation model tasks */ listFoundationModelTasks(params = {}) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = []; const validParams = ['start', 'limit']; validateRequestParams(params, requiredParams, validParams); const query = { 'version': this.version, 'start': params.start, 'limit': params.limit, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.FOUNDATION_MODEL.LIST_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. * * @category Prompts / Prompt Templates * @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<WatsonXAI.Response<WatsonXAI.WxPromptResponse>>} A Promise that resolves to a * prompt response */ createPrompt(params) { return __awaiter(this, void 0, void 0, function* () { const requiredParams = ['name', 'prompt']; const validParams = [ 'description', 'createdAt', 'taskIds', 'lock', 'modelVersion', 'promptVariables', 'inputMode', 'projectId', 'spaceId', ]; validateRequestParams(params, requiredParams, validParams); const { projectId, spaceId } = this.resolveContextId(params); 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': projectId, 'space_id': spaceId, }; const sdkHeaders = getSdkHeaders(); const parameters = { options: { url: ENDPOINTS.PROMPT.BASE, method: 'POST',