OpenAI API client library for kiarina namespace
Project description
kiarina-lib-openai
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 amongapi_key,organization, andbase_url.
settings_manager
settings_manager: SettingsManager[OpenAISettings]
Global instance that manages named OpenAI client settings.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcacae8867114c3aecde3e789934ec26812a79f259690064dfc099cbcf208ab2
|
|
| MD5 |
c8488f2d4d5fa8d507d0ca37105d6759
|
|
| BLAKE2b-256 |
289d3e848543b17f1bbe3606cfbd84616ad57776085507418e714ec5833da878
|
Provenance
The following attestation bundles were made for kiarina_lib_openai-2.3.1.tar.gz:
Publisher:
release-pypi.yml on kiarina/kiarina-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kiarina_lib_openai-2.3.1.tar.gz -
Subject digest:
bcacae8867114c3aecde3e789934ec26812a79f259690064dfc099cbcf208ab2 - Sigstore transparency entry: 2044312885
- Sigstore integration time:
-
Permalink:
kiarina/kiarina-python@443dbb48340706ef8cf35551de167d64bc11ee82 -
Branch / Tag:
refs/tags/v2.3.1 - Owner: https://github.com/kiarina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-pypi.yml@443dbb48340706ef8cf35551de167d64bc11ee82 -
Trigger Event:
push
-
Statement type:
File details
Details for the file kiarina_lib_openai-2.3.1-py3-none-any.whl.
File metadata
- Download URL: kiarina_lib_openai-2.3.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cbf97262b865defc483ea1eb0891f8c71fe84fe4587a9c6023d57a8146f0fb0
|
|
| MD5 |
c44d5f3a7501806e25092e3ac7da789a
|
|
| BLAKE2b-256 |
6b30118e076fb9bcb39aa997c62c5afede642b7468ed17777ded63428d2b0a74
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kiarina_lib_openai-2.3.1-py3-none-any.whl -
Subject digest:
2cbf97262b865defc483ea1eb0891f8c71fe84fe4587a9c6023d57a8146f0fb0 - Sigstore transparency entry: 2044313290
- Sigstore integration time:
-
Permalink:
kiarina/kiarina-python@443dbb48340706ef8cf35551de167d64bc11ee82 -
Branch / Tag:
refs/tags/v2.3.1 - Owner: https://github.com/kiarina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-pypi.yml@443dbb48340706ef8cf35551de167d64bc11ee82 -
Trigger Event:
push
-
Statement type: