SDK entrypoint for IBM watsonx.ai product. Provides access to IBM watsonx.ai services including text generation, embeddings, deployments, and model management.

Example

   const service = new WatsonXAI({
version: '2024-03-14',
serviceUrl: 'https://us-south.ml.cloud.ibm.com',
authenticator: new IamAuthenticator({ apikey: 'your-api-key' })
});
```;

WatsonXAI

Hierarchy (view full)

Constructors - Constructor

Properties

Methods - Constructor

Methods - Deployments

Methods - Embeddings

Methods - Foundation Model Specs

Methods - Prompt Sessions

Methods - Prompts / Prompt Templates

Methods - Schema Management

Methods - Spaces

Methods - Text Chat

Methods - Text Generation

Methods - Text Rerank

Methods - Tokenization

Methods - Trainings

Methods - Other

Constructors - Constructor

Properties

httpsAgentMap: WatsonXAI.HttpsAgentMap = ...
projectId?: string
serviceUrl: string

URL required for watsonx inference endpoints

spaceId?: string
version: string

The version date for the API of the form YYYY-MM-DD.

wxServiceUrl: string

URL required for dataplatform endpoints

PLATFORM_URLS_MAP: Record<string, string> = PLATFORM_URL_MAPPINGS

Methods - Constructor

  • Constructs an instance of WatsonXAI with passed in options and external configuration.

    Ensuring backwards compatibility from v1.7.1. You can now use:

    const service = new WatsonXAI(options);
    

    Parameters

    Returns WatsonXAI

    • An instance of the WatsonXAI service

Methods - 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.

    Parameters

    Returns Promise<WatsonXAI.Response<WatsonXAI.DeploymentResource>>

    A promise that resolves to the response with deployment's data

  • 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.

    Parameters

    Returns Promise<WatsonXAI.Response<WatsonXAI.DeploymentResource>>

    A Promise that resolves to a deployment resource

Methods - Embeddings

Methods - Foundation Model Specs

Methods - Prompt Sessions

Methods - Prompts / Prompt Templates

Methods - Schema Management

  • Get a cluster schema request.

    Retrieve the cluster schema request details with the specified identifier.

    Parameters

    • params: GetClusterSchemaParams

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<ClusterSchemaResponse>>

  • Get a create schema request.

    Retrieve the create schema request details with the specified identifier.

    Parameters

    • params: GetCreateSchemaParams

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<CreateSchemaResponse>>

  • Get an improve schema request.

    Retrieve the improve schema request details with the specified identifier.

    Parameters

    • params: GetImproveSchemaParams

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<ImproveSchemaResponse>>

  • Get a merge schema request.

    Retrieve the merge schema request details with the specified identifier.

    Parameters

    • params: GetMergeSchemaParams

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<MergeSchemaResponse>>

  • List cluster schema requests.

    Retrieve the list of cluster schema requests for the specified space or project.

    Parameters

    • Optional params: ListClusterSchemaParams = {}

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<ClusterSchemaResources>>

  • List create schema requests.

    Retrieve the list of create schema requests for the specified space or project.

    Parameters

    • Optional params: ListCreateSchemaParams = {}

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<CreateSchemaResources>>

  • List improve schema requests.

    Retrieve the list of improve schema requests for the specified space or project.

    Parameters

    • Optional params: ListImproveSchemaParams = {}

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<ImproveSchemaResources>>

  • List merge schema requests.

    Retrieve the list of merge schema requests for the specified space or project.

    Parameters

    • Optional params: ListMergeSchemaParams = {}

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<MergeSchemaResources>>

  • Start a cluster schema request.

    Start a request to cluster multiple schemas for text extraction.

    Parameters

    • params: ClusterSchemaParams

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<ClusterSchemaResponse>>

  • Start a create schema request.

    Start a request to create the custom schema for text extraction.

    Parameters

    • params: CreateSchemaParams

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<CreateSchemaResponse>>

  • Start an improve schema request.

    Start a request to improve an existing schema for text extraction.

    Parameters

    • params: ImproveSchemaParams

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<ImproveSchemaResponse>>

  • Start a merge schema request.

    Start a request to merge multiple schemas for text extraction.

    Parameters

    • params: MergeSchemaParams

      The parameters to send to the service.

    Returns Promise<WatsonXAI.Response<MergeSchemaResponse>>

Methods - Spaces

Methods - Text Chat

  • Infer text event stream.

    Infer the next tokens for a given deployed model with a set of parameters. This operation will return the output tokens as a stream of events

    Response

    Stream<string | WatsonXAI.ObjectStreamed<WatsonXAI.TextGenResponse>> represents a source of streaming data. If request performed successfully Stream<string | WatsonXAI.ObjectStreamed<WatsonXAI.TextGenResponse>> returns either stream line by line. Output will stream as follow:

    • Id: 1
    • Event: message
    • Data: {data}
    • Empty line which separates the next Event Message

    Or stream of objects. Output will stream as follow: { id: 2, event: 'message', data: {data} } Here is one of the possibilities to read streaming output:

    Const stream = await watsonxAIServiceenerateTextStream(parameters); for await (const line of stream) { console.log(line); }.

    Parameters

    Returns Promise<Stream<string>>

    Return

    • Promise resolving to Stream object. Stream object is AsyncIterable based class. Stream object contains an additional property holding an AbortController, read more below.
  • Parameters

    Returns Promise<Stream<WatsonXAI.ObjectStreamed<WatsonXAI.TextChatStreamResponse>>>

Methods - Text Generation

  • Infer text event stream.

    Infer the next tokens for a given deployed model with a set of parameters. This operation will return the output tokens as a stream of events.

    Response

    Stream<string | WatsonXAI.ObjectStreamed<WatsonXAI.TextGenResponse>> represents a source of streaming data. If request performed successfully Stream<string | WatsonXAI.ObjectStreamed<WatsonXAI.TextGenResponse>> returns either stream line by line. Output will stream as follow:

    • Id: 1
    • Event: message
    • Data: {data}
    • Empty line which separates the next Event Message

    Or stream of objects. Output will stream as follow: { id: , event: 'message', data: {data} }

    Here is one of the possibilities to read streaming output:

    Const stream = await watsonxAIServiceenerateTextStream(parameters); for await (const line of stream) { console.log(line); }

    Parameters

    Returns Promise<Stream<string>>

    Return

    • Promise resolving to Stream object. Stream object is AsyncIterable based class. Stream object contains an additional property holding an AbortController, read more below.
  • Parameters

    Returns Promise<Stream<WatsonXAI.ObjectStreamed<WatsonXAI.TextGenResponse>>>

Methods - Text Rerank

Methods - Tokenization

Methods - Trainings

Methods - Other

  • Create a new model.

    Create a new model with the given payload. A model represents a machine learning model asset. If a 202 status is returned then the model will be ready when the content_import_state in the model status (/ml/v4/models/{model_id}) is completed. If content_import_state is not used then a 201 status is returned.

    Parameters

    Returns Promise<WatsonXAI.Response<WatsonXAI.ModelResource>>

    A Promise that resolves to a model resource

  • Protected

    Creates and executes an HTTP request to the watsonx.ai API. Handles HTTPS agent configuration, encryption parameters, and callback hooks.

    Type Parameters

    • T

      The expected response type

    Parameters

    • parameters: CreateRequestParams

      Request parameters including URL, method, headers, and body

    • Optional additionalParameters: AdditionalCreateRequestParams

      Optional additional parameters

    Returns Promise<T>

    Promise resolving to the API response

  • Create a new watsonx.ai training.

    Create a new watsonx.ai training in a project or a space.

    The details of the base model and parameters for the training must be provided in the prompt_tuning object.

    In order to deploy the tuned model you need to follow the following steps:

    1. Create a WML model asset, in a space or a project, by providing the request.json as shown below:

      curl -X POST "https://{cpd_cluster}/ml/v4/models?version=2024-01-29" \
      -H "Authorization: Bearer <replace with your token>" \
      -H "content-type: application/json" \
      --data '{
      "name": "replace_with_a_meaningful_name",
      "space_id": "replace_with_your_space_id",
      "type": "prompt_tune_1.0",
      "software_spec": {
      "name": "watsonx-textgen-fm-1.0"
      },
      "metrics": [ from the training job ],
      "training": {
      "id": "05859469-b25b-420e-aefe-4a5cb6b595eb",
      "base_model": {
      "model_id": "google/flan-t5-xl"
      },
      "task_id": "generation",
      "verbalizer": "Input: {{input}} Output:"
      },
      "training_data_references": [
      {
      "connection": {
      "id": "20933468-7e8a-4706-bc90-f0a09332b263"
      },
      "id": "file_to_tune1.json",
      "location": {
      "bucket": "wxproject-donotdelete-pr-xeyivy0rx3vrbl",
      "path": "file_to_tune1.json"
      },
      "type": "connection_asset"
      }
      ]
      }'
      ```


      Notes:
      1. If you used the training request field auto_update_model: true then you can skip this step as the model will have been saved at the end of the training job.
      2. Rather than creating the payload for the model you can use the generated request.json that was stored in the results_reference field, look for the path in the field entity.results_reference.location.model_request_path.
      3. The model type must be prompt_tune_1.0.
      4. The software spec name must be watsonx-textgen-fm-1.0.
    2. Create a tuned model deployment as described in the create deployment documentation.

    @category Trainings @param {Object} params - The parameters to send to the service. @param {string} params.name - The name of the training. @param {ObjectLocation} params.resultsReference - The training results. Normally this is specified as type=container which means that it is stored in the space or project. @param {string} [params.spaceId] - The space that contains the resource. Either space_id or project_id has to be given. @param {string} [params.projectId] - The project that contains the resource. Either space_id or project_id has to be given. @param {string} [params.description] - A description of the training. @param {string[]} [params.tags] - A list of tags for this resource. @param {PromptTuning} [params.promptTuning] - Properties to control the prompt tuning. @param {DataConnectionReference[]} [params.trainingDataReferences]

    • Training datasets.

    @param {JsonObject} [params.custom] - User defined properties specified as key-value pairs. @param {boolean} [params.autoUpdateModel] - If set to true then the result of the training, if successful, will be uploaded to the repository as a model. @param {OutgoingHttpHeaders} [params.headers] - Custom request headers @returns {Promise<WatsonXAI.Response<WatsonXAI.TrainingResource>>} A Promise that resolves to a training resource

    @category Trainings

    Returns Promise<WatsonXAI.Response<WatsonXAI.TrainingResource>>

  • Private

    Resolves projectId and spaceId with fallback to instance values.

    This method implements a priority-based resolution strategy:

    1. If both instance values (this.projectId/this.spaceId) AND parameter values exist, parameter values take absolute precedence and instance values are ignored
    2. Otherwise, parameter values are used if provided, falling back to instance values

    This ensures that explicitly passed parameters always override instance configuration when both are present, preventing unintended mixing of project and space contexts.

    Parameters

    • params: {
          projectId?: string;
          spaceId?: string;
      }

      Object containing optional projectId and spaceId

      • Optional projectId?: string

        The project ID to use for the request

      • Optional spaceId?: string

        The space ID to use for the request

    Returns {
        projectId?: string;
        spaceId?: string;
    }

    Resolved projectId and spaceId values

    • Optional projectId?: string
    • Optional spaceId?: string
  • 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'

    Parameters

    • Optional providedUrlVariables: null | Map<string, string>

      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.