|
| 1 | +""" |
| 2 | +Abstract base layer for instance and LLM processor types. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from abc import ABC, abstractmethod |
| 8 | +from typing import TYPE_CHECKING |
| 9 | + |
| 10 | +from pydantic import ConfigDict, Field, PrivateAttr |
| 11 | +from ulid import ULID |
| 12 | + |
| 13 | +from contextgem.internal.base.mixins import _PostInitCollectorMixin |
| 14 | +from contextgem.internal.base.serialization import _InstanceSerializer |
| 15 | + |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from contextgem.internal.data_models import ( |
| 19 | + _LLMCostOutputContainer, |
| 20 | + _LLMUsageOutputContainer, |
| 21 | + ) |
| 22 | + from contextgem.internal.typings.aliases import LLMRoleAny |
| 23 | + |
| 24 | + |
| 25 | +class _AbstractInstanceBase(_PostInitCollectorMixin, _InstanceSerializer, ABC): |
| 26 | + """ |
| 27 | + Abstract base for instance-like Pydantic models. |
| 28 | + """ |
| 29 | + |
| 30 | + custom_data: dict = Field( |
| 31 | + default_factory=dict, |
| 32 | + description="A serializable dictionary for storing additional custom data " |
| 33 | + "related to the instance.", |
| 34 | + ) |
| 35 | + |
| 36 | + _unique_id: str = PrivateAttr(default_factory=lambda: str(ULID())) |
| 37 | + |
| 38 | + model_config = ConfigDict(extra="forbid", validate_assignment=True) |
| 39 | + |
| 40 | + @property |
| 41 | + def unique_id(self) -> str: |
| 42 | + """ |
| 43 | + Returns the ULID of the instance. |
| 44 | + """ |
| 45 | + return self._unique_id |
| 46 | + |
| 47 | + |
| 48 | +class _AbstractGenericLLMProcessor(_PostInitCollectorMixin, _InstanceSerializer, ABC): |
| 49 | + """ |
| 50 | + Abstract base for LLM-backed processors (single or grouped). |
| 51 | + """ |
| 52 | + |
| 53 | + model_config = ConfigDict(extra="forbid", validate_assignment=True) |
| 54 | + |
| 55 | + @property |
| 56 | + @abstractmethod |
| 57 | + def is_group(self) -> bool: |
| 58 | + """ |
| 59 | + Abstract property, to be implemented by subclasses. |
| 60 | +
|
| 61 | + Whether the LLM is a single instance or a group. |
| 62 | + """ |
| 63 | + pass |
| 64 | + |
| 65 | + @property |
| 66 | + @abstractmethod |
| 67 | + def list_roles(self) -> list[LLMRoleAny]: |
| 68 | + """ |
| 69 | + Abstract property, to be implemented by subclasses. |
| 70 | +
|
| 71 | + Returns the list of all LLM roles in the LLM group or LLM. |
| 72 | + """ |
| 73 | + pass |
| 74 | + |
| 75 | + @abstractmethod |
| 76 | + def _set_private_attrs(self) -> None: |
| 77 | + """ |
| 78 | + Abstract method, to be implemented by subclasses. |
| 79 | +
|
| 80 | + Sets private attributes for the LLM group or LLM, e.g. prompts, capabilities, etc. |
| 81 | + """ |
| 82 | + pass |
| 83 | + |
| 84 | + @abstractmethod |
| 85 | + def get_usage(self, *args, **kwargs) -> list[_LLMUsageOutputContainer]: |
| 86 | + """ |
| 87 | + Abstract method, to be implemented by subclasses. |
| 88 | +
|
| 89 | + Returns the usage data for the LLM group or LLM. |
| 90 | + """ |
| 91 | + pass |
| 92 | + |
| 93 | + @abstractmethod |
| 94 | + def get_cost(self, *args, **kwargs) -> list[_LLMCostOutputContainer]: |
| 95 | + """ |
| 96 | + Abstract method, to be implemented by subclasses. |
| 97 | +
|
| 98 | + Returns the cost data for the LLM group or LLM as a list of |
| 99 | + `_LLMCostOutputContainer` entries. Implementations may accept optional |
| 100 | + filter parameters (e.g., role) where applicable. |
| 101 | + """ |
| 102 | + pass |
| 103 | + |
| 104 | + @abstractmethod |
| 105 | + def reset_usage_and_cost(self) -> None: |
| 106 | + """ |
| 107 | + Abstract method, to be implemented by subclasses. |
| 108 | +
|
| 109 | + Resets the usage and cost data for the LLM group or LLM. Implementations |
| 110 | + may support optional filters (e.g., by role) where applicable. |
| 111 | + """ |
| 112 | + pass |
0 commit comments