genai package#

class genai.ApiClient[source]#

Bases: object

Class which provides methods for making API requests. It provides methods for obtaining synchronous and asynchronous HTTP clients.

Config#

alias of BaseConfig

__init__(*, credentials, config=None)[source]#
Parameters:
  • credentials (Credentials) –

  • config (dict | BaseConfig | None) –

get_async_http_client(rate_limit_options=None, retry_options=None, client_options=None, **kwargs)[source]#

Gets cached instance of AsyncHttpxClient (cache key is created from provided parameters).

Raises:
  • ValueError – If both rate_limit_options and retry_options are provided.

  • RuntimeError – If the method is not called within an asynchronous environment.

Parameters:
  • rate_limit_options (dict | None) –

  • retry_options (dict | None) –

  • client_options (dict | None) –

Return type:

AsyncHttpxClient

get_http_client(retry_options=None, **kwargs)[source]#
Parameters:

retry_options (dict | None) –

Return type:

HttpxClient

class genai.Client[source]#

Bases: BaseService[BaseConfig, BaseServices]

The Client class provides an interface for interacting with various services through an API client. It can be initialized with either an api_client or credentials along with optional configurations and services.

Example:

from genai import Credentials, Client

credentials = Credentials.from_env()
client = Client(credentials=credentials)
text#

An instance of the TextService class for text-related operations.

request#

An instance of the RequestService class for making request-related operations.

tune#

An instance of the TuneService class for tuning models.

model#

An instance of the ModelService class for managing models.

file#

An instance of the FileService class for managing files.

prompt#

An instance of the PromptService class for working with prompts.

system_prompt#

An instance of the SystemPromptService class for working with system prompts.

user#

An instance of the UserService class for managing user-related operations.

tag#

An instance of the TagService class for working with tags.

folder#

An instance of the FolderService class for working with folder.

task#

An instance of the TaskService class for working with tasks.

Config#

alias of BaseConfig

Services#

alias of BaseServices

__init__(*, api_client: ApiClient, config: dict | BaseConfig | None = None, services: BaseServices | None = None) None[source]#
__init__(*, credentials: Credentials, config: dict | BaseConfig | None = None, services: BaseServices | None = None) None
Parameters:
  • credentials – The credentials used to authenticate the API client.

  • api_client – The API client used to make requests to the API.

  • config – The configuration for the API client.

  • services – The services object containing instances of various service classes.

Raises:

ValueError – Either ‘api_client’ or ‘credentials’ needs to be passed.

Note

The api_client parameter must be provided either directly or through credentials. If no services parameter is provided, the client will use the default one.

pydantic model genai.Credentials[source]#

Bases: BaseModel

The Credentials class represents credentials required for accessing the GENAI API.

- `api_key`

API key which can be retrieved from the UI.

- `api_endpoint`

A string representing the GENAI API endpoint (default is BAM).

Type:

optional

Examples

Create a Credentials instance with explicit api_endpoint:

credentials = Credentials(api_key="your_api_key", api_endpoint="https://bam-api.res.ibm.com")

Create a Credentials instance with default api_endpoint:

credentials = Credentials(api_key="your_api_key")

Create a Credentials instance from environment variables:

credentials = Credentials.from_env()
Validators:
field api_endpoint: str [Required]#

GENAI API Endpoint

Constraints:
  • min_length = 1

Validated by:
  • _validate_api_endpoint

field api_key: SecretStr [Required]#

The GENAI API Key

__init__(api_key, api_endpoint=None, **kwargs)[source]#

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:
  • api_key (str) –

  • api_endpoint (str | None) –

classmethod from_env(*, api_key_name=None, api_endpoint_name=None)[source]#
Parameters:
  • api_key_name (str | None) –

  • api_endpoint_name (str | None) –

Return type:

Credentials

genai.handle_shutdown_event(*_args)[source]#

Handles the shutdown event.

Return type:

None

Subpackages#

Submodules#