Skip to main content

LLM utilities and context management for kiarina namespace

Project description

kiarina-llm

A Python library for LLM utilities and context management with type safety and configuration management.

Features

  • RunContext Management: Structured context information for LLM pipeline processing
  • Type Safety: Full type hints and Pydantic validation
  • Configuration Management: Use pydantic-settings-manager for flexible configuration
  • Filesystem Safe Names: Validated names for cross-platform compatibility
  • ID Validation: Structured ID types with pattern validation

Installation

pip install kiarina-llm

Quick Start

Basic RunContext Usage

from kiarina.llm.run_context import create_run_context

# Create a run context with default settings
context = create_run_context(
    tenant_id="tenant-123",
    user_id="user-456",
    agent_id="my-agent",
    time_zone="Asia/Tokyo",
    language="ja",
    currency="JPY"
)

print(f"User: {context.user_id}")
print(f"Agent: {context.agent_id}")
print(f"Time Zone: {context.time_zone}")
print(f"Language: {context.language}")
print(f"Currency: {context.currency}")

Configuration Management

from kiarina.llm.run_context import settings_manager

# Configure default values
settings_manager.user_config = {
    "app_author": "MyCompany",
    "app_name": "MyAIApp",
    "tenant_id": "default-tenant",
    "user_id": "default-user",
    "time_zone": "America/New_York",
    "language": "en"
}

# Create context with configured defaults
context = create_run_context(
    agent_id="specialized-agent"  # Override only specific values
)

Environment Variable Configuration

Configure defaults using environment variables:

export KIARINA_LLM_RUN_CONTEXT_APP_AUTHOR="MyCompany"
export KIARINA_LLM_RUN_CONTEXT_APP_NAME="MyAIApp"
export KIARINA_LLM_RUN_CONTEXT_TENANT_ID="prod-tenant"
export KIARINA_LLM_RUN_CONTEXT_TIME_ZONE="Asia/Tokyo"
export KIARINA_LLM_RUN_CONTEXT_LANGUAGE="ja"

RunContext Fields

The RunContext model includes the following fields:

Field Type Description Example
app_author FSName Application author (filesystem safe) "MyCompany"
app_name FSName Application name (filesystem safe) "MyAIApp"
tenant_id IDStr Tenant identifier "tenant-123"
user_id IDStr User identifier "user-456"
agent_id IDStr Agent identifier "my-agent"
runner_id IDStr Runner identifier "linux" (auto-detected)
time_zone str IANA time zone "Asia/Tokyo"
language str ISO 639-1 language code "ja"
currency str ISO 4217 currency code "USD"
metadata dict[str, Any] Additional metadata {"version": "1.0"}

Type Validation

FSName (Filesystem Safe Name)

The FSName type ensures names are safe for use across different filesystems:

from kiarina.llm.run_context import create_run_context

# Valid names
context = create_run_context(
    app_author="My Company",      # Spaces allowed
    app_name="My-App_v1.0"       # Hyphens, underscores, dots allowed
)

# Invalid names (will raise ValidationError)
try:
    create_run_context(app_author="My App.")  # Ends with dot
except ValueError as e:
    print(f"Validation error: {e}")

try:
    create_run_context(app_author=".hidden")  # Starts with dot
except ValueError as e:
    print(f"Validation error: {e}")

try:
    create_run_context(app_author="CON")  # Windows reserved name
except ValueError as e:
    print(f"Validation error: {e}")

IDStr (ID String)

The IDStr type validates identifiers:

# Valid IDs
context = create_run_context(
    tenant_id="tenant-123",
    user_id="user.456",
    agent_id="agent_v1.0"
)

# Invalid IDs (will raise ValidationError)
try:
    create_run_context(tenant_id="")  # Empty string
except ValueError as e:
    print(f"Validation error: {e}")

try:
    create_run_context(user_id="user@domain")  # Invalid character
except ValueError as e:
    print(f"Validation error: {e}")

Advanced Usage

Custom Metadata

context = create_run_context(
    tenant_id="tenant-123",
    user_id="user-456",
    metadata={
        "session_id": "session-789",
        "request_id": "req-abc123",
        "version": "1.0.0",
        "features": ["feature-a", "feature-b"]
    }
)

print(f"Session: {context.metadata['session_id']}")
print(f"Features: {context.metadata['features']}")

Integration with PlatformDirs

The app_author and app_name fields are designed to work with libraries like platformdirs:

from platformdirs import user_data_dir
from kiarina.llm.run_context import create_run_context

context = create_run_context(
    app_author="MyCompany",
    app_name="MyAIApp"
)

# Use with platformdirs
data_dir = user_data_dir(
    appname=context.app_name,
    appauthor=context.app_author
)
print(f"Data directory: {data_dir}")

Configuration Reference

Setting Environment Variable Default Description
app_author KIARINA_LLM_RUN_CONTEXT_APP_AUTHOR "kiarina" Default application author
app_name KIARINA_LLM_RUN_CONTEXT_APP_NAME "myaikit" Default application name
tenant_id KIARINA_LLM_RUN_CONTEXT_TENANT_ID "" Default tenant ID
user_id KIARINA_LLM_RUN_CONTEXT_USER_ID "" Default user ID
agent_id KIARINA_LLM_RUN_CONTEXT_AGENT_ID "" Default agent ID
runner_id KIARINA_LLM_RUN_CONTEXT_RUNNER_ID platform.system().lower() Default runner ID
time_zone KIARINA_LLM_RUN_CONTEXT_TIME_ZONE "UTC" Default time zone
language KIARINA_LLM_RUN_CONTEXT_LANGUAGE "en" Default language
currency KIARINA_LLM_RUN_CONTEXT_CURRENCY "USD" Default currency code

Development

Prerequisites

  • Python 3.12+

Setup

# Clone the repository
git clone https://github.com/kiarina/kiarina-python.git
cd kiarina-python

# Setup development environment (installs tools, syncs dependencies, downloads test data)
mise run setup

Running Tests

# Run format, lint, type checks and tests
mise run package kiarina-llm

# Coverage report
mise run package:test kiarina-llm --coverage

# Run specific tests
uv run --group test pytest packages/kiarina-llm/tests/run_context/

Dependencies

Roadmap

This package is in active development. Planned features include:

  • Chat Model Management: Unified interface for different LLM providers
  • Agent Framework: Tools for building LLM agents
  • Pipeline Management: Workflow management for LLM processing
  • Memory Management: Context and conversation memory handling
  • Tool Integration: Framework for LLM tool calling

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

This is a personal project, but contributions are welcome! Please feel free to submit issues or pull requests.

Related Projects

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_llm-1.13.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

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

kiarina_llm-1.13.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_llm-1.13.0.tar.gz.

File metadata

  • Download URL: kiarina_llm-1.13.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kiarina_llm-1.13.0.tar.gz
Algorithm Hash digest
SHA256 22e1e842a320faed08fcd7b185de5195e6673cebaa1e21006fbfbe334b158e7f
MD5 6b210d8de73aa0c5aedd521472623d47
BLAKE2b-256 ee7067e9185866d6e65351053b850cdb879ed76c28d8effa933e80a552232558

See more details on using hashes here.

File details

Details for the file kiarina_llm-1.13.0-py3-none-any.whl.

File metadata

  • Download URL: kiarina_llm-1.13.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kiarina_llm-1.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95440b28e9a5459a689b820e66e0754fbf074c5aa048a89b763d45c3ef4aa0b3
MD5 1b9b192a3abf7c70dd23066dedb64ae6
BLAKE2b-256 6f4a733d5a4fd04788759197dc1c9b6c56c4b08aa8f840147dac011cc6e0cf2b

See more details on using hashes here.

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