Base

APIClient

class client.APIClient(credentials=None, project_id=None, space_id=None, verify=None, **kwargs)[source]

The main class of ibm_watsonx_ai. The very heart of the module. APIClient contains objects that manage the service reasources.

To explore how to use APIClient, refer to:
  • Setup - to check correct initialization of APIClient for a specific environment.

  • Core - to explore core properties of an APIClient object.

Parameters:
  • url (str) – URL of the service

  • credentials (Credentials) – credentials used to connect with the service

  • project_id (str, optional) – ID of the project that is used

  • space_id (str, optional) – ID of deployment space that is used

  • verify (bool, optional) – certificate verification flag, deprecated, use Credentials(verify=…) to set verify

Example:

from ibm_watsonx_ai import APIClient, Credentials

credentials = Credentials(
    url = "<url>",
    api_key = "<api_key>"
)

client = APIClient(credentials, space_id="<space_id>")

client.models.list()
client.deployments.get_details()

client.set.default_project("<project_id>")

...
set_headers(headers)[source]

Method which allows refresh/set new User Request Headers.

Parameters:

headers (dict) – User Request Headers

Examples

headers = {
    'Authorization': 'Bearer <USER AUTHORIZATION TOKEN>',
    'User-Agent': 'ibm-watsonx-ai/1.0.1 (lang=python; arch=x86_64; os=darwin; python.version=3.10.13)',
    'X-Watson-Project-ID': '<PROJECT ID>',
    'Content-Type': 'application/json'
}

client.set_headers(headers)
set_token(token)[source]

Method which allows refresh/set new User Authorization Token.

Parameters:

token (str) – User Authorization Token

Examples

client.set_token("<USER AUTHORIZATION TOKEN>")

Credentials

class credentials.Credentials(*, url=None, api_key=None, name=None, iam_serviceid_crn=None, token=None, projects_token=None, username=None, password=None, instance_id=None, version=None, bedrock_url=None, platform_url=None, proxies=None, verify=None)[source]

This class encapsulate passed credentials and additional params.

Parameters:
  • url (str) – URL of the service

  • api_key (str, optional) – service API key used in API key authentication

  • name (str, optional) – service name used during space creation for a Cloud environment

  • iam_serviceid_crn (str, optional) – service CRN used during space creation for a Cloud environment

  • token (str, optional) – service token, used in token authentication

  • projects_token (str, optional) – service projects token used in token authentication

  • username (str, optional) – username, used in username/password or username/api_key authentication, applicable for ICP only

  • password (str, optional) – password, used in username/password authentication, applicable for ICP only

  • instance_id (str, optional) – instance ID, mandatory for ICP

  • version (str, optional) – ICP version, mandatory for ICP

  • bedrock_url (str, optional) – Bedrock URL, applicable for ICP only

  • proxies (dict, optional) – dictionary of proxies, containing protocol and URL mapping (example: { “https”: “https://example.url.com” })

  • verify (bool, optional) – certificate verification flag

Example of create Credentials object

  • IBM watsonx.ai for IBM Cloud

from ibm_watsonx_ai import Credentials

# Example of creating the credentials using an API key:
credentials = Credentials(
    url = "https://us-south.ml.cloud.ibm.com",
    api_key = "***********"
)

# Example of creating the credentials using a token:
credentials = Credentials(
    url = "https://us-south.ml.cloud.ibm.com",
    token = "***********"
)
  • IBM watsonx.ai software

import os
from ibm_watsonx_ai import Credentials

# Example of creating the credentials using username and password:
credentials = Credentials(
    url = "<URL>",
    username = "<USERNAME>",
    password = "<PASSWORD>",
    instance_id = "openshift",
    version = "5.0"
)

# Example of creating the credentials using username and apikey:
credentials = Credentials(
    url = "<URL>",
    username = "<USERNAME>",
    api_key = "<API_KEY>",
    instance_id = "openshift",
    version = "5.0"
)

# Example of creating the credentials using a token:
access_token = os.environ['USER_ACCESS_TOKEN']
credentials = Credentials(
    url = "<URL>",
    token = access_token,
    instance_id = "openshift"
    version = "5.0"
)
static from_dict(credentials, _verify=None)[source]

Create a Credentials object from dictionary.

Parameters:

credentials (dict) – credentials in the dictionary

Returns:

initialised credentials object

Return type:

Credentials

Example:

from ibm_watsonx_ai import Credentials

credentials = Credentials.from_dict({
    'url': "<url>",
    'apikey': "<api_key>"
})
to_dict()[source]

Get dictionary from the Credentials object.

Returns:

dictionary with credentials

Return type:

dict

Example:

from ibm_watsonx_ai import Credentials

credentials = Credentials.from_dict({
    'url': "<url>",
    'apikey': "<api_key>"
})

credentials_dict = credentials.to_dict()