package adminapi import ( "context" "errors" "net/http" "slices" "gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog" "gitea.mynodes.xyz/ilker/llmgw/internal/store" ) // Training from the portal's "customise a model" page: the page decides who // may do what (members of the job's project), and these do it. // TrainableModels are the catalog's models an adapter can be trained for // from the portal: chat models llama.cpp or vLLM serve whose weights came // from the registry, so the weights to train on can be found. func (a *API) TrainableModels() []string { var out []string for _, m := range a.ctl.Catalog().Models() { if m.IsGuard() || m.IsExternal() || m.Capabilities.Embeddings || m.MakesImages() || m.MakesMeshes() || m.Origin == nil || m.Origin.Repo == "" { continue } if slices.ContainsFunc(m.Profiles, func(p catalog.Profile) bool { return p.Engine == "llamacpp" || p.Engine == "vllm" }) { out = append(out, m.Name) } } slices.Sort(out) return out } // Trainings are the jobs started in projects, or by createdBy outside any. func (a *API) Trainings(ctx context.Context, projects []string, createdBy string) ([]store.TrainJob, error) { if a.train == nil { return nil, nil } return a.train.Store.TrainJobsFor(ctx, projects, createdBy, 100) } // Training is one job. func (a *API) Training(ctx context.Context, id string) (store.TrainJob, error) { if a.train == nil { return store.TrainJob{}, store.ErrNotFound } return a.train.Store.TrainJob(ctx, id) } // Published is whether a job's adapter is its model's in the catalog. func (a *API) Published(j store.TrainJob) bool { m, ok := a.ctl.Catalog().Resolve(j.Model) return ok && slices.ContainsFunc(m.Adapters, func(ad catalog.Adapter) bool { return ad.Name == j.Name }) } // ShareTraining changes who a job's adapter belongs to — owner as scope:ID, // or "" for every caller its model allows — on the job and, once // published, in the catalog. Refused wider than the knowledge bases its // pairs came from may be read. func (a *API) ShareTraining(ctx context.Context, id, owner, actor string) error { j, err := a.Training(ctx, id) if err != nil { return err } _, colls, err := countPairs(string(j.Data)) if err != nil { return err } resolved, err := a.trainOwner(ctx, TrainRequest{Owner: owner, Public: owner == ""}, colls) if err != nil { return refused(http.StatusBadRequest, "%v", err) } if err := a.train.Store.SetTrainOwner(ctx, id, resolved); err != nil { return err } m, ok := a.ctl.Catalog().Resolve(j.Model) if !ok || a.models == nil { return nil } ads := slices.Clone(m.Adapters) i := slices.IndexFunc(ads, func(ad catalog.Adapter) bool { return ad.Name == j.Name }) if i < 0 { return nil } ads[i].Owner = resolved _, err = a.models.SetAdapters(ctx, m.Name, ads, actor) return err } // DeleteTraining stops a job if it trains, takes its adapter out of the // catalog if it was published, and forgets it. func (a *API) DeleteTraining(ctx context.Context, id, actor string) error { j, err := a.Training(ctx, id) if err != nil { return err } if !store.TrainOver(j.State) { if err := a.train.Service.Cancel(ctx, id); err != nil { return err } } if m, ok := a.ctl.Catalog().Resolve(j.Model); ok && a.models != nil { ads := slices.DeleteFunc(slices.Clone(m.Adapters), func(ad catalog.Adapter) bool { return ad.Name == j.Name }) if len(ads) != len(m.Adapters) { if _, err := a.models.SetAdapters(ctx, m.Name, ads, actor); err != nil { return err } } } if err := a.train.Store.DeleteTrainJob(ctx, id); err != nil && !errors.Is(err, store.ErrNotFound) { return err } return nil }