ai_core_sdk package

Subpackages

Submodules

ai_core_sdk.ai_core_v2_client module

class AICoreV2Client

Bases: object

The AICoreV2Client is the class implemented to interact with the AI Core endpoints. The user can use its attributes corresponding to the resources, for interacting with endpoints related to that resource. (i.e., aicoreclient.scenario)

Parameters:
  • base_url (str) -- Base URL of the AI Core. Should include the base path as well. (i.e., "<base_url>/lm/scenarios" should work)

  • auth_url (str, optional) -- URL of the authorization endpoint. Should be the full URL (including /oauth/token), defaults to None

  • client_id (str, optional) -- client id to be used for authorization, defaults to None

  • client_secret (str, optional) -- client secret to be used for authorization, defaults to None

  • cert_str (str, optional) -- certificate file content, needs to be provided alongside the key_str parameter, defaults to None

  • key_str (str, optional) -- key file content, needs to be provided alongside the cert_str parameter, defaults to None

  • cert_file_path (str, optional) -- path to the certificate file, needs to be provided alongside the key_file_path parameter, defaults to None

  • key_file_path (str, optional) -- path to the key file, needs to be provided alongside the cert_file_path parameter, defaults to None

  • token_creator (Callable[[], str], optional) -- the function which returns the Bearer token, when called. Either this, or auth_url & client_id & client_secret should be specified, defaults to None

  • resource_group (str, optional) -- The default resource group which will be used while sending the requests to the server. If not set, the resource_group should be specified with every request to the server, defaults to None

  • client_type (str, optional) -- The client type which will be sent in the User-Agent header of the requests, defaults to "AI Core Python SDK"

  • **kwargs --

    • read_timeout (int): Read timeout for requests in seconds, defaults to 60s

    • connect_timeout (int): Connect timeout for requests in seconds, defaults to 60s

    • num_request_retries (int): Number of retries for failing requests with http status code 429, 500, 502, 503 or 504, defaults to 10

static from_env(profile_name=None, **kwargs)

Alternative way to create an AICoreV2Client object. Parameters for base_url, auth_url, client_id, client_secret, x.509 credentials (either as file path or string) and resource_group can be passed as keyword or are pulled from environment variables. It is also possible to use a profile, which is a json file in the config directory. The profile name can be passed as keyword or is pulled from the environment variable AICORE_PROFILE. If no profile is specified, the default profile is used. A specific path to a config, that should be used, can be set via the environment variable AICORE_CONFIG. The hierarchy of precedence is: 1. keyword argument 2. environment variable 3. configuration file 4. value from VCAP_SERVICES environment variable, if exists

Parameters:

profile_name (optional, str) -- name of the profile to use, defaults to None. If None is passed, the profile is read from the environment variable AICORE_PROFILE. If this is not set, the default profile is used. The default profile is read from $AICORE_HOME/config.json.

**kwargs: check the parameters of the class constructor

__init__(base_url, auth_url=None, client_id=None, client_secret=None, cert_str=None, key_str=None, cert_file_path=None, key_file_path=None, token_creator=None, resource_group=None, client_type='AI Core Python SDK', **kwargs)
Parameters:
  • base_url (str)

  • auth_url (str)

  • client_id (str)

  • client_secret (str)

  • cert_str (str)

  • key_str (str)

  • cert_file_path (str)

  • key_file_path (str)

  • token_creator (Callable[[], str])

  • resource_group (str)

  • client_type (str)

logger = <Logger ai_core_sdk (INFO)>

ai_core_sdk.cli module

confirm_resource_group(resource_group=None)
Parameters:

resource_group (str | None)

create_config(**kwargs)
create_config_file(config_path, auth_url, client_id, client_secret, cert_file_path, key_file_path, base_url, resource_group)
Parameters:
  • config_path (Path)

  • auth_url (str)

  • client_id (str)

  • client_secret (str)

  • cert_file_path (Path)

  • key_file_path (Path)

  • base_url (str)

  • resource_group (str)

get_auth_url(auth_url=None)
Parameters:

auth_url (str | None)

get_base_url(base_url=None)
Parameters:

base_url (str | None)

get_profile_config_path(profile)
Parameters:

profile (str)

get_str_value(msg, value=None)
Parameters:
  • msg (str)

  • value (str | None)

is_valid_url(url, path_forbidden=True)
load_service_key(service_key_json)
Parameters:

service_key_json (str)

prompt_for_input(prompt_text, is_url=False, path_forbidden=True)

ai_core_sdk.credentials module

class CredentialsValue

Bases: object

CredentialsValue(name: 'str', vcap_key: 'Optional[Tuple[str, ...]]' = None, transform_fn: 'Optional[Callable]' = None)

__init__(name, vcap_key=None, transform_fn=None)
Parameters:
  • name (str)

  • vcap_key (Tuple[str, ...] | None)

  • transform_fn (Callable | None)

Return type:

None

name: str
transform_fn: Callable | None = None
vcap_key: Tuple[str, ...] | None = None
class Service

Bases: object

__init__(env)
Parameters:

env (Dict[str, Any])

get(key, default=NoDefault)
property label: str | None
property name: str | None
class Source

Bases: object

Source(name: 'str', get: 'Callable[[CredentialsValue], Optional[str]]')

__init__(name, get)
Parameters:
Return type:

None

get: Callable[[CredentialsValue], str | None]
name: str
class VCAPEnvironment

Bases: object

VCAPEnvironment(services: 'List[Service]')

classmethod from_dict(env)
Parameters:

env (Dict[str, Any])

classmethod from_env(env_var=None)
Parameters:

env_var (str | None)

__init__(services)
Parameters:

services (List[Service])

Return type:

None

get_service(label, exactly_one=True)
Parameters:

exactly_one (bool)

Return type:

Service

get_service_by_name(name, exactly_one=True)
Parameters:

exactly_one (bool)

Return type:

Service

services: List[Service]
fetch_credentials(profile=None, credential_values=[CredentialsValue(name='client_id', vcap_key=('credentials', 'clientid'), transform_fn=None), CredentialsValue(name='client_secret', vcap_key=('credentials', 'clientsecret'), transform_fn=None), CredentialsValue(name='auth_url', vcap_key=('credentials', 'url'), transform_fn=<lambda>), CredentialsValue(name='base_url', vcap_key=('credentials', 'serviceurls', 'AI_API_URL'), transform_fn=<lambda>), CredentialsValue(name='resource_group', vcap_key=None, transform_fn=None), CredentialsValue(name='cert_url', vcap_key=('credentials', 'certurl'), transform_fn=<lambda>), CredentialsValue(name='cert_file_path', vcap_key=None, transform_fn=None), CredentialsValue(name='key_file_path', vcap_key=None, transform_fn=None), CredentialsValue(name='cert_str', vcap_key=('credentials', 'certificate'), transform_fn=<lambda>), CredentialsValue(name='key_str', vcap_key=('credentials', 'key'), transform_fn=<lambda>)], validate=True, **kwargs)

Fetch credentials from a single source based on precedence.

Precedence order: kwargs > environment variables > config file > VCAP service

Once a source is selected (first one with any credential), all credentials come from that source only. Resource group is an exception and follows precedence independently.

If credential_values is provided and it's not extended from the CORE_CREDENTIAL_VALUES, set validate to False

Parameters:
Return type:

Dict[str, str]

get_nested_value(data_dict, keys)

Retrieve a nested value from a dictionary using a list of strings.

Parameters:
  • data_dict -- The dictionary to search.

  • keys (List[str]) -- A list of strings representing nested keys.

Returns:

The value associated with the nested keys, or None if not found.

init_conf(profile=None)
Parameters:

profile (str)

resolve_resource_group(sources)

Find resource_group from the first source that defines it.

Parameters:

sources (List[Source])

Return type:

str | None

validate_credentials(credentials)

Validate that we have a complete authentication method.

Parameters:

credentials (Dict[str, str])

Return type:

None

ai_core_sdk.exception module

exception AICoreInvalidInputException

Bases: AICoreSDKException

Exception thrown in case of invalid input

exception AICoreSDKException

Bases: Exception

Base Exception class for AI Core SDK exceptions