|
| 1 | +"""Memory admin controller -- fine-tuning and embedder endpoints. |
| 2 | +
|
| 3 | +All endpoints require CEO or the internal SYSTEM role |
| 4 | +(used by the CLI for admin operations). |
| 5 | +""" |
| 6 | + |
| 7 | +from litestar import Controller, get, post |
| 8 | +from litestar.datastructures import State # noqa: TC002 |
| 9 | +from pydantic import BaseModel, ConfigDict, Field |
| 10 | + |
| 11 | +from synthorg.api.dto import ApiResponse |
| 12 | +from synthorg.api.guards import HumanRole, require_roles |
| 13 | +from synthorg.api.state import AppState # noqa: TC001 |
| 14 | +from synthorg.core.types import NotBlankStr # noqa: TC001 |
| 15 | +from synthorg.memory.embedding.fine_tune import FineTuneStage |
| 16 | +from synthorg.memory.embedding.fine_tune_models import ( |
| 17 | + FineTuneRequest, |
| 18 | + FineTuneStatus, |
| 19 | +) |
| 20 | +from synthorg.observability import get_logger |
| 21 | +from synthorg.observability.events.memory import ( |
| 22 | + MEMORY_EMBEDDER_SETTINGS_READ_FAILED, |
| 23 | + MEMORY_FINE_TUNE_REQUESTED, |
| 24 | +) |
| 25 | + |
| 26 | +logger = get_logger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +class ActiveEmbedderResponse(BaseModel): |
| 30 | + """Active embedder configuration read from settings.""" |
| 31 | + |
| 32 | + model_config = ConfigDict(frozen=True, allow_inf_nan=False) |
| 33 | + |
| 34 | + provider: NotBlankStr | None = Field( |
| 35 | + default=None, |
| 36 | + description="Embedding provider name", |
| 37 | + ) |
| 38 | + model: NotBlankStr | None = Field( |
| 39 | + default=None, |
| 40 | + description="Embedding model identifier", |
| 41 | + ) |
| 42 | + dims: int | None = Field( |
| 43 | + default=None, |
| 44 | + ge=1, |
| 45 | + description="Embedding vector dimensions", |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +class MemoryAdminController(Controller): |
| 50 | + """Admin endpoints for memory management. |
| 51 | +
|
| 52 | + Provides fine-tuning pipeline control and embedder configuration |
| 53 | + queries. All endpoints require CEO or SYSTEM role. |
| 54 | + """ |
| 55 | + |
| 56 | + path = "/admin/memory" |
| 57 | + tags = ("admin", "memory") |
| 58 | + guards = [require_roles(HumanRole.CEO, HumanRole.SYSTEM)] # noqa: RUF012 |
| 59 | + |
| 60 | + @post("/fine-tune") |
| 61 | + async def start_fine_tune( |
| 62 | + self, |
| 63 | + state: State, # noqa: ARG002 |
| 64 | + data: FineTuneRequest, |
| 65 | + ) -> ApiResponse[FineTuneStatus]: |
| 66 | + """Trigger a fine-tuning pipeline run. |
| 67 | +
|
| 68 | + Args: |
| 69 | + state: Application state. |
| 70 | + data: Fine-tuning request parameters. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + Current pipeline status. |
| 74 | + """ |
| 75 | + logger.info( |
| 76 | + MEMORY_FINE_TUNE_REQUESTED, |
| 77 | + source_dir=data.source_dir, |
| 78 | + base_model=data.base_model, |
| 79 | + ) |
| 80 | + # Pipeline stages are not yet implemented -- return status |
| 81 | + # indicating the pipeline is idle with a descriptive error. |
| 82 | + # See issue #1001 for the implementation roadmap. |
| 83 | + return ApiResponse( |
| 84 | + data=FineTuneStatus( |
| 85 | + stage=FineTuneStage.FAILED, |
| 86 | + error=( |
| 87 | + "Fine-tuning pipeline stages are not yet " |
| 88 | + "implemented. Install synthorg[fine-tune] " |
| 89 | + "and check back in a future release." |
| 90 | + ), |
| 91 | + ), |
| 92 | + ) |
| 93 | + |
| 94 | + @get("/fine-tune/status") |
| 95 | + async def get_fine_tune_status( |
| 96 | + self, |
| 97 | + state: State, # noqa: ARG002 |
| 98 | + ) -> ApiResponse[FineTuneStatus]: |
| 99 | + """Get the current fine-tuning pipeline status. |
| 100 | +
|
| 101 | + Args: |
| 102 | + state: Application state. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + Current pipeline status. |
| 106 | + """ |
| 107 | + return ApiResponse( |
| 108 | + data=FineTuneStatus(stage=FineTuneStage.IDLE), |
| 109 | + ) |
| 110 | + |
| 111 | + @get("/embedder") |
| 112 | + async def get_active_embedder( |
| 113 | + self, |
| 114 | + state: State, |
| 115 | + ) -> ApiResponse[ActiveEmbedderResponse]: |
| 116 | + """Get the active embedder configuration. |
| 117 | +
|
| 118 | + Args: |
| 119 | + state: Application state. |
| 120 | +
|
| 121 | + Returns: |
| 122 | + Active embedder provider, model, and dims. |
| 123 | + """ |
| 124 | + app_state: AppState = state.app_state |
| 125 | + result = ActiveEmbedderResponse() |
| 126 | + if app_state.has_settings_service: |
| 127 | + svc = app_state.settings_service |
| 128 | + try: |
| 129 | + provider_sv = await svc.get("memory", "embedder_provider") |
| 130 | + model_sv = await svc.get("memory", "embedder_model") |
| 131 | + dims_sv = await svc.get("memory", "embedder_dims") |
| 132 | + dims_value: int | None = None |
| 133 | + if dims_sv.value: |
| 134 | + try: |
| 135 | + dims_value = int(dims_sv.value) |
| 136 | + except ValueError, TypeError: |
| 137 | + logger.warning( |
| 138 | + MEMORY_EMBEDDER_SETTINGS_READ_FAILED, |
| 139 | + setting="embedder_dims", |
| 140 | + value=dims_sv.value, |
| 141 | + reason="invalid integer value", |
| 142 | + ) |
| 143 | + result = ActiveEmbedderResponse( |
| 144 | + provider=provider_sv.value or None, |
| 145 | + model=model_sv.value or None, |
| 146 | + dims=dims_value, |
| 147 | + ) |
| 148 | + except MemoryError, RecursionError: |
| 149 | + raise |
| 150 | + except Exception: |
| 151 | + logger.warning( |
| 152 | + MEMORY_EMBEDDER_SETTINGS_READ_FAILED, |
| 153 | + exc_info=True, |
| 154 | + ) |
| 155 | + return ApiResponse(data=result) |
0 commit comments