Skip to main content

OpenAI API client library for kiarina namespace

Project description

kiarina-lib-openai

PyPI version Python License: MIT

English | 日本語

[!NOTE] What is this? A package for managing OpenAI API credentials and endpoints with pydantic-settings-manager.

Dependencies

Package Version License
Pydantic Settings >=2.10.1 MIT
pydantic-settings-manager >=3.2.0 MIT

The OpenAI Python SDK is not included as a dependency. Add it to the application that creates the client.

Installation

pip install kiarina-lib-openai

Features

  • Configuring an OpenAI Client Generate OpenAI SDK client arguments from settings.
  • Managing Multiple Configurations Switch between named settings for multiple projects or environments.
  • Loading Environment Variables Load environment variables with the KIARINA_LIB_OPENAI_ prefix.
  • Using Compatible APIs Set an OpenAI-compatible API endpoint with base_url.
  • Protecting API Keys Store the API key as SecretStr.

Configuring an OpenAI Client

to_client_kwargs() converts only configured values to OpenAI SDK argument names.

from openai import OpenAI

from kiarina.lib.openai import OpenAISettings

settings = OpenAISettings(api_key="sk-...")
client = OpenAI(**settings.to_client_kwargs())

organization_id becomes organization. When api_key, organization_id, and base_url are all None, the method returns an empty dict.

Managing Multiple Configurations

settings_manager uses multiple-settings mode. Place named settings under configs.

kiarina.lib.openai:
  default: production
  configs:
    development:
      api_key: sk-development
    production:
      api_key: sk-production
      organization_id: org-production
import yaml
from pydantic_settings_manager import load_user_configs

from kiarina.lib.openai import settings_manager

with open("config.yaml", encoding="utf-8") as file:
    load_user_configs(yaml.safe_load(file) or {})

settings = settings_manager.get_settings("production")

To configure only this package directly, assign settings_manager.user_config.

from kiarina.lib.openai import settings_manager

settings_manager.user_config = {
    "default": "development",
    "configs": {
        "development": {"api_key": "sk-development"},
        "production": {
            "api_key": "sk-production",
            "organization_id": "org-production",
        },
    },
}

Loading Environment Variables

Load a single configuration from environment variables.

export KIARINA_LIB_OPENAI_API_KEY="sk-..."
export KIARINA_LIB_OPENAI_ORGANIZATION_ID="org-..."
export KIARINA_LIB_OPENAI_BASE_URL="https://api.openai.com/v1"
from kiarina.lib.openai import OpenAISettings

settings = OpenAISettings()

Using Compatible APIs

from openai import OpenAI

from kiarina.lib.openai import OpenAISettings

settings = OpenAISettings(
    api_key="compatible-api-key",
    base_url="https://example.com/v1",
)
client = OpenAI(**settings.to_client_kwargs())

Protecting API Keys

to_client_kwargs() returns the secret value of api_key so it can be passed to the OpenAI SDK. Do not log the returned dictionary.

API Reference

kiarina.lib.openai

from kiarina.lib.openai import (
    OpenAISettings,
    settings_manager,
)

OpenAISettings

class OpenAISettings(BaseSettings):
    def __init__(
        self,
        *,
        api_key: SecretStr | None = None,
        organization_id: str | None = None,
        base_url: str | None = None,
    ) -> None: ...

    def to_client_kwargs(self) -> dict[str, Any]: ...

OpenAI client settings.

Fields

  • api_key (SecretStr | None): OpenAI API key.
  • organization_id (str | None): OpenAI organization ID.
  • base_url (str | None): Base URL for the OpenAI API.
to_client_kwargs

Converts fields whose values are not None into OpenAI SDK client arguments.

Returns

  • dict[str, Any]: A dictionary containing configured values among api_key, organization, and base_url.

settings_manager

settings_manager: SettingsManager[OpenAISettings]

Global instance that manages named OpenAI client settings.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kiarina_lib_openai-2.3.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kiarina_lib_openai-2.3.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_lib_openai-2.3.1.tar.gz.

File metadata

  • Download URL: kiarina_lib_openai-2.3.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kiarina_lib_openai-2.3.1.tar.gz
Algorithm Hash digest
SHA256 bcacae8867114c3aecde3e789934ec26812a79f259690064dfc099cbcf208ab2
MD5 c8488f2d4d5fa8d507d0ca37105d6759
BLAKE2b-256 289d3e848543b17f1bbe3606cfbd84616ad57776085507418e714ec5833da878

See more details on using hashes here.

Provenance

The following attestation bundles were made for kiarina_lib_openai-2.3.1.tar.gz:

Publisher: release-pypi.yml on kiarina/kiarina-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kiarina_lib_openai-2.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_lib_openai-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2cbf97262b865defc483ea1eb0891f8c71fe84fe4587a9c6023d57a8146f0fb0
MD5 c44d5f3a7501806e25092e3ac7da789a
BLAKE2b-256 6b30118e076fb9bcb39aa997c62c5afede642b7468ed17777ded63428d2b0a74

See more details on using hashes here.

Provenance

The following attestation bundles were made for kiarina_lib_openai-2.3.1-py3-none-any.whl:

Publisher: release-pypi.yml on kiarina/kiarina-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page