haive.core.engine.base.factory¶
Factory implementation for creating runtime components in the Haive system.
This module provides a factory pattern implementation that separates the serializable configuration of components from their non-serializable runtime instances. This enables lazy instantiation, caching, and runtime configuration of components.
Classes¶
Factory for creating runtime components from engine configurations. |
Module Contents¶
- class haive.core.engine.base.factory.ComponentFactory(/, **data)[source]¶
Bases:
pydantic.BaseModel
,Generic
[T
]Factory for creating runtime components from engine configurations.
This class implements the factory pattern to separate serializable configurations from non-serializable runtime components. It provides lazy instantiation, caching, and the ability to override configuration at runtime.
- Attributes:
- engine_ref (ComponentRef): Reference to the engine configuration that will
be used to create the component.
- runtime_config (Optional[Dict[str, Any]]): Runtime configuration overrides
that will be applied when creating the component.
_component (Optional[T]): Private cache for the created component instance.
- Type Parameters:
T: The type of component that will be created by this factory.
- Examples:
>>> from haive.core.engine.base import Engine
- from haive.core.engine.base.types import EngineType
>>> from haive.core.engine.base.factory import ComponentFactory >>> # Create an engine >>> engine = Engine(name="my_engine", engine_type=EngineType.LLM) >>> # Create a factory for the engine >>> factory = ComponentFactory.for_engine(engine, {"temperature": 0.7}) >>> # Create the runtime component >>> component = factory.create()
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:
data (Any)
- create()[source]¶
Create the runtime component.
Resolves the engine reference and creates a runnable component instance using the engine’s create_runnable method. Caches the created component for future calls.
- Returns:
The created runtime component.
- Return type:
T
- Raises:
ValueError – If the engine reference cannot be resolved.
Examples
>>> # Import inline to avoid circular imports in examples >>> from haive.core.engine.base.reference import ComponentRef >>> from haive.core.engine.base.types import EngineType >>> factory = ComponentFactory(engine_ref=ComponentRef(name="gpt-4", type=EngineType.LLM)) >>> llm = factory.create() >>> response = llm.generate("Hello, world!")
- classmethod for_engine(engine, runtime_config=None)[source]¶
Create a factory for a specific engine.
Factory method to create a component factory that will use the specified engine and runtime configuration.
- Args:
engine (Any): The engine instance to use for component creation. runtime_config (Optional[Dict[str, Any]]): Optional runtime configuration
overrides to apply when creating the component.
- Returns:
ComponentFactory[T]: A new component factory configured for the engine.
- Examples:
>>> from haive.core.engine.base import Engine
- from haive.core.engine.base.types import EngineType
>>> engine = Engine(name="text-embedding-ada-002", engine_type=EngineType.EMBEDDINGS) >>> factory = ComponentFactory.for_engine( ... engine, ... {"batch_size": 10} ... ) >>> embeddings = factory.create()
- Parameters:
- Return type:
- invalidate_cache()[source]¶
Invalidate the cached component.
Clears the cached component instance and invalidates the engine reference cache. This forces the next call to create() to instantiate a fresh component.
Examples
>>> factory = ComponentFactory.for_engine(some_engine) >>> component1 = factory.create() # Creates and caches >>> factory.invalidate_cache() >>> component2 = factory.create() # Creates fresh >>> component1 is component2 False
- Return type:
None
- model_config¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].