package router import ( "context" "encoding/json" "errors" "io" "net/http" "slices" "strings" "time" "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi" "gitea.mynodes.xyz/ilker/llmgw/internal/control/train" "gitea.mynodes.xyz/ilker/llmgw/internal/store" "gitea.mynodes.xyz/ilker/llmgw/internal/version" "gitea.mynodes.xyz/ilker/llmgw/internal/web" ) // The "customise a model" page: a person in a project uploads questions // and answers, picks a model, and presses a button. The node, the weights // to train on and how much of a card it takes are worked out; what it // makes is published as a model of its own, seen by the project unless it // is shared wider. Members of the project see its jobs and manage them; // one trains at a time per project. // Training is what the page needs of the control plane. type Training interface { TrainableModels() []string StartTraining(ctx context.Context, in adminapi.TrainRequest, actor string) (store.TrainJob, error) Trainings(ctx context.Context, projects []string, createdBy string) ([]store.TrainJob, error) Training(ctx context.Context, id string) (store.TrainJob, error) Published(j store.TrainJob) bool ShareTraining(ctx context.Context, id, owner, actor string) error DeleteTraining(ctx context.Context, id, actor string) error } // SetTraining mounts the page's actions on t. func (r *Router) SetTraining(t Training) { r.training.Store(&t) } func (r *Router) trainer() Training { if t := r.training.Load(); t != nil { return *t } return nil } // trainMaxFile bounds an uploaded file of pairs. const trainMaxFile = 32 << 20 // The widths an adapter can be shared at, narrowest first. var trainShares = []string{"me", "project", "team", "org", "all"} func (r *Router) registerTrainPage(mux *http.ServeMux, pages *web.AccountPages) { pages.SetTrain(func() bool { t := r.trainer() return t != nil && r.store != nil && len(t.TrainableModels()) > 0 }) mux.HandleFunc("GET /train", pages.Guard(func(w http.ResponseWriter, req *http.Request, user store.User) { t := r.trainer() if t == nil || r.store == nil { http.NotFound(w, req) return } projects, _ := r.store.ProjectsFor(req.Context(), user.ID) var ps []web.ChatContext for _, p := range projects { ps = append(ps, web.ChatContext{Value: p.ID, Label: p.Path}) } web.Train(user, t.TrainableModels(), ps, version.Version)(w, req) })) mux.HandleFunc("POST /train/preview", pages.Guard(r.trainPreview)) mux.HandleFunc("POST /train/start", pages.Guard(r.trainStart)) mux.HandleFunc("GET /train/jobs", pages.Guard(r.trainJobs)) mux.HandleFunc("POST /train/jobs/{id}/share", pages.Guard(r.trainShare)) mux.HandleFunc("POST /train/jobs/{id}/delete", pages.Guard(r.trainDelete)) } func trainFail(w http.ResponseWriter, code int, msg string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(code) _ = json.NewEncoder(w).Encode(map[string]string{"error": msg}) } // trainFile reads the pairs of the uploaded file. func trainFile(w http.ResponseWriter, req *http.Request) (train.Read, bool) { req.Body = http.MaxBytesReader(w, req.Body, trainMaxFile+1<<20) f, h, err := req.FormFile("file") if err != nil { trainFail(w, http.StatusBadRequest, "choose a file of questions and answers") return train.Read{}, false } defer f.Close() b, err := io.ReadAll(io.LimitReader(f, trainMaxFile+1)) if err != nil || len(b) > trainMaxFile { trainFail(w, http.StatusBadRequest, "the file is over 32 MB") return train.Read{}, false } rd, err := train.ReadPairs(h.Filename, b) if err != nil { trainFail(w, http.StatusBadRequest, err.Error()) return train.Read{}, false } return rd, true } // trainPreview says what a file holds before anything is trained on it. func (r *Router) trainPreview(w http.ResponseWriter, req *http.Request, _ store.User) { rd, ok := trainFile(w, req) if !ok { return } sample := rd.Pairs[:min(len(rd.Pairs), 5)] writeJSONBody(w, map[string]any{"pairs": len(rd.Pairs), "skipped": rd.Skipped, "columns": rd.Columns, "sample": sample}) } // trainPlace is where a project is in the tree. func (r *Router) trainPlace(ctx context.Context, user store.User, project string) (store.Effective, error) { projects, err := r.store.ProjectsFor(ctx, user.ID) if err != nil { return store.Effective{}, err } if !slices.ContainsFunc(projects, func(p store.ProjectPath) bool { return p.ID == project }) { return store.Effective{}, errors.New("you are not a member of that project") } return r.store.ResolveFor(ctx, "", "", project) } // trainOwner is the adapter's owner for a share width, in a place. func trainOwner(share string, user store.User, e store.Effective) (string, bool) { switch share { case "me": return "user:" + user.ID, true case "project": return "project:" + e.ProjectID, true case "team": return "team:" + e.TeamID, true case "org": return "org:" + e.OrgID, true case "all": return "", true } return "", false } // trainShareOf is the width an owner is, seen from the job's place. func trainShareOf(owner string, e store.Effective) string { switch { case owner == "": return "all" case strings.HasPrefix(owner, "user:"): return "me" case owner == "project:"+e.ProjectID: return "project" case owner == "team:"+e.TeamID: return "team" case owner == "org:"+e.OrgID: return "org" } return "other" } func (r *Router) trainStart(w http.ResponseWriter, req *http.Request, user store.User) { t := r.trainer() if t == nil { trainFail(w, http.StatusNotFound, "training is not offered here") return } rd, ok := trainFile(w, req) if !ok { return } e, err := r.trainPlace(req.Context(), user, req.FormValue("project")) if err != nil { trainFail(w, http.StatusForbidden, err.Error()) return } share := req.FormValue("share") owner, ok := trainOwner(share, user, e) if !ok { trainFail(w, http.StatusBadRequest, "who may use it: me, the project, the team, the organization or everyone") return } if slices.Index(trainShares, share) > slices.Index(trainShares, "project") && req.FormValue("confirm") != "yes" { trainFail(w, http.StatusBadRequest, "confirm that the file holds nothing the wider group must not see") return } in := adminapi.TrainRequest{Name: strings.TrimSpace(req.FormValue("name")), Model: req.FormValue("model"), Owner: owner, Public: owner == "", Data: train.NDJSON(rd.Pairs), Project: e.ProjectID, Publish: true} if !slices.Contains(t.TrainableModels(), in.Model) { trainFail(w, http.StatusBadRequest, "choose one of the models offered") return } j, err := t.StartTraining(req.Context(), in, user.Email) if err != nil { msg, theirs := adminapi.TrainRefusal(err) code := http.StatusBadRequest if !theirs { code = http.StatusInternalServerError r.log.Warn("training could not be started from the portal", "user", user.Email, "err", err) } trainFail(w, code, msg) return } r.log.Info("training started from the portal", "job", j.ID, "adapter", j.Name, "model", j.Model, "project", j.Project, "user", user.Email, "pairs", j.Pairs) writeJSONBody(w, map[string]string{"id": j.ID}) } // trainRow is a job as the page lists it. type trainRow struct { ID string `json:"id"` Name string `json:"name"` Model string `json:"model"` Project string `json:"project"` State string `json:"state"` Step int `json:"step"` Steps int `json:"steps"` Message string `json:"message,omitempty"` Pairs int `json:"pairs"` Share string `json:"share"` Published bool `json:"published"` CreatedBy string `json:"created_by"` CreatedAt time.Time `json:"created_at"` Minutes int `json:"minutes"` } func (r *Router) trainJobs(w http.ResponseWriter, req *http.Request, user store.User) { t := r.trainer() if t == nil || r.store == nil { writeJSONBody(w, []trainRow{}) return } projects, err := r.store.ProjectsFor(req.Context(), user.ID) if err != nil { trainFail(w, http.StatusInternalServerError, err.Error()) return } ids, names := []string{}, map[string]string{} for _, p := range projects { ids = append(ids, p.ID) names[p.ID] = p.Path } jobs, err := t.Trainings(req.Context(), ids, user.Email) if err != nil { trainFail(w, http.StatusInternalServerError, err.Error()) return } out := []trainRow{} for _, j := range jobs { e, _ := r.store.ResolveFor(req.Context(), "", "", j.Project) row := trainRow{ID: j.ID, Name: j.Name, Model: j.Model, Project: names[j.Project], State: j.State, Step: j.Step, Steps: j.Steps, Message: j.Message, Pairs: j.Pairs, Share: trainShareOf(j.Owner, e), Published: j.State == store.TrainDone && t.Published(j), CreatedBy: j.CreatedBy, CreatedAt: j.CreatedAt} if !j.StartedAt.IsZero() { end := j.FinishedAt if end.IsZero() { end = time.Now() } row.Minutes = int(end.Sub(j.StartedAt).Minutes()) } out = append(out, row) } writeJSONBody(w, out) } // trainMine is a job a person may manage: their project's, or their own. func (r *Router) trainMine(w http.ResponseWriter, req *http.Request, user store.User) (store.TrainJob, Training, bool) { t := r.trainer() if t == nil || r.store == nil { trainFail(w, http.StatusNotFound, "training is not offered here") return store.TrainJob{}, nil, false } j, err := t.Training(req.Context(), req.PathValue("id")) if err == nil { if j.Project == "" { if j.CreatedBy == user.Email { return j, t, true } } else if _, err := r.trainPlace(req.Context(), user, j.Project); err == nil { return j, t, true } } trainFail(w, http.StatusNotFound, "no such training") return store.TrainJob{}, nil, false } func (r *Router) trainShare(w http.ResponseWriter, req *http.Request, user store.User) { j, t, ok := r.trainMine(w, req, user) if !ok { return } e, _ := r.store.ResolveFor(req.Context(), "", "", j.Project) share := req.FormValue("share") owner, ok := trainOwner(share, user, e) if !ok || (j.Project == "" && share != "me" && share != "all") { trainFail(w, http.StatusBadRequest, "who may use it: me, the project, the team, the organization or everyone") return } if slices.Index(trainShares, share) > slices.Index(trainShares, "project") && req.FormValue("confirm") != "yes" { trainFail(w, http.StatusBadRequest, "confirm that the data holds nothing the wider group must not see") return } if err := t.ShareTraining(req.Context(), j.ID, owner, user.Email); err != nil { msg, _ := adminapi.TrainRefusal(err) trainFail(w, http.StatusBadRequest, msg) return } r.log.Info("a trained model's sharing changed from the portal", "job", j.ID, "adapter", j.Name, "share", share, "user", user.Email) writeJSONBody(w, map[string]string{"share": share}) } func (r *Router) trainDelete(w http.ResponseWriter, req *http.Request, user store.User) { j, t, ok := r.trainMine(w, req, user) if !ok { return } if err := t.DeleteTraining(req.Context(), j.ID, user.Email); err != nil { msg, _ := adminapi.TrainRefusal(err) trainFail(w, http.StatusInternalServerError, msg) return } r.log.Info("a trained model was deleted from the portal", "job", j.ID, "adapter", j.Name, "user", user.Email) writeJSONBody(w, map[string]bool{"deleted": true}) } func writeJSONBody(w http.ResponseWriter, v any) { w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(v) }