orka.registry module

Resource Registry

The Resource Registry is a central dependency injection and resource management system for the OrKa framework. It provides a consistent interface for initializing, accessing, and managing shared resources such as language models, embedding models, databases, and custom tools.

Key responsibilities:

  1. Lazy initialization of resources based on configuration

  2. Centralized access to shared dependencies

  3. Resource lifecycle management (initialization and cleanup)

  4. Consistent error handling for resource failures

  5. Support for custom resource types through dynamic imports

Supported resource types:

  • sentence_transformer: Text embedding models

  • redis: Redis database client

  • openai: OpenAI API client

  • custom: Dynamically loaded custom resources

The registry pattern helps maintain clean separation of concerns by isolating resource initialization logic and providing a single source of truth for shared dependencies across the framework.

class orka.registry.ResourceRegistry(config: Dict[str, ResourceConfig])[source]

Bases: object

Manages resource initialization, access, and lifecycle.

The ResourceRegistry is responsible for lazily initializing resources when needed, providing access to them via a simple interface, and ensuring proper cleanup when the application shuts down. It acts as a central dependency provider for all OrKa components.

__init__(config: Dict[str, ResourceConfig])[source]

Initialize the registry with resource configurations.

Parameters:

config – Dictionary mapping resource names to their configurations

async initialize() None[source]

Initialize all resources based on their configurations.

This method lazily initializes all configured resources, handling any initialization errors and ensuring resources are only initialized once.

Raises:

Exception – If any resource fails to initialize

get(name: str) Any[source]

Get a resource by name.

Parameters:

name – Name of the resource to retrieve

Returns:

The requested resource instance

Raises:
  • RuntimeError – If the registry has not been initialized

  • KeyError – If the requested resource does not exist

async close() None[source]

Clean up and close all resources.

This method ensures proper cleanup of all resources when the application is shutting down, closing connections and releasing system resources.

orka.registry.init_registry(config: Dict[str, ResourceConfig]) ResourceRegistry[source]

Create and initialize a new resource registry.

Parameters:

config – Dictionary mapping resource names to their configurations

Returns:

Initialized ResourceRegistry instance