Training config: reference name and mains frequency: GET /models/{domain}/{name}/training-config; POST train accepts reference

This commit is contained in:
2026-11-28 13:26:18 -06:00
parent 3438a3135b
commit 7e25dde319
2 changed files with 106 additions and 7 deletions
+41 -7
View File
@@ -4,16 +4,17 @@ scoring runs (Dagster). The API starts runs and reads the registry; it never tra
import mimetypes
import zipfile
from datetime import date
from typing import Annotated
from typing import Annotated, Any
from dagster_graphql import DagsterGraphQLClient
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile
from fastapi.responses import Response
from pydantic import BaseModel
from pydantic import BaseModel, Field
from core import domains
from core.domains import Domain
from persistance import bundle, registry
from persistance.cluster import Execution
from persistance import bundle, cluster, registry
from persistance.cluster import Execution, Reference
from webapi.runs import dagster, execution_config, submit
router = APIRouter(prefix="/models", tags=["models"])
@@ -135,10 +136,37 @@ def promote(domain: Domain, name: str, version: int) -> dict:
return {"champion": version}
class Training(BaseModel):
as_of: date | None = None # the date the data are taken up to (default: today)
models: list[str] = [] # which (default: all but the slow ones, like CaMa's recurrence)
class TrainingForm(BaseModel):
as_of: date | None = Field(
None, title="Datos hasta", description="Fecha de corte de los datos (por omisión, hoy)"
)
execution: Execution | None = None
reference: Reference | None = None
class Training(TrainingForm):
models: list[str] = [] # which (default: all but the slow ones, like CaMa's recurrence)
class TrainingConfig(BaseModel):
domain: str
name: str
json_schema: dict[str, Any]
defaults: dict[str, Any]
@router.get("/{domain}/{name}/training-config")
def training_config(domain: Domain, name: str) -> TrainingConfig:
json_schema = TrainingForm.model_json_schema()
defaults = {"execution": {"mode": cluster.default_mode(), "scheduler": None}}
if "reference" in getattr(domains.part(domain), "TRAINING_OPTIONS", ()):
defaults["reference"] = {"line_hz": 50}
else:
del json_schema["properties"]["reference"]
del json_schema["$defs"]["Reference"]
return TrainingConfig(
domain=domain.value, name=name, json_schema=json_schema, defaults=defaults
)
class Analyze(BaseModel):
@@ -149,11 +177,17 @@ class Analyze(BaseModel):
def train(domain: Domain, request: Training,
client: DagsterGraphQLClient = Depends(dagster)) -> dict[str, str]: # fmt: skip
"""Starts training a new version of every model of the domain; follow it at /runs."""
if request.reference is not None and "reference" not in getattr(
domains.part(domain), "TRAINING_OPTIONS", ()
):
raise HTTPException(422, f"{domain.value} models take no reference cohort")
config: dict = {"domain": domain.value, "execution": execution_config(request.execution)}
if request.as_of:
config["as_of"] = request.as_of.isoformat()
if request.models:
config["models"] = request.models
if request.reference is not None:
config["reference"] = request.reference.model_dump()
return submit(client, "train", "train_models", config, domain=domain.value)