Customise a model from the portal: a file, a model, a button

A page for anyone in a project, built for the one who knows least: an
Excel, CSV or JSONL file of questions and answers, previewed; a model; a
name; who may use it; and a button. The weights to train on are found
from where the model's came (a GGUF repository's base model), the card's
memory from their size, the node as the one with the most free. When it
is done the adapter is published by itself (a llama.cpp model's replicas
replaced to load it) and is a model of its own in the chat and the API.

The project's by default; its members share it wider (confirmed, and
never wider than the knowledge bases its pairs came from) or delete it.
One trains at a time per project. ctl train start needs only --model and
--data now, and --publish publishes when done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-11-19 11:31:12 +01:00
1 parent fcda4d3f85
commit a53b2f4be9
33 files changed
+2199 -134

No files matched your search

+1
View File
@@ -58,6 +58,7 @@ func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) {
}))
r.registerImagesPage(mux, pages)
r.registerMeshesPage(mux, pages)
r.registerTrainPage(mux, pages)
if r.store != nil {
pages.SetLicences(r.licenceChoices, r.store)
}
+1 -1
View File
@@ -384,7 +384,7 @@ func (r *Router) Handler() http.Handler {
}
exempt := []string{"/health", "/ready", "/leader", "/v1/openapi.json", "/", "/about", "/install", "/install/", "/_static/", "/_lang/", "/claim/",
"/signup", "/login", "/login/", "/logout", "/profile", "/profile/", "/scim/",
"/admin", "/admin/", "/chat", "/chat/", "/images", "/images/", "/3d", "/3d/", "/org", "/org/", "/setup/"}
"/admin", "/admin/", "/chat", "/chat/", "/images", "/images/", "/3d", "/3d/", "/train", "/train/", "/org", "/org/", "/setup/"}
// The docs are public unless the site said otherwise, and then guarded by
// the session check in front of them — or, on a gateway with keys and no
// accounts to sign in to, by the key like any other path.
+1 -1
View File
@@ -17,7 +17,7 @@ func TestSessionPagesAreNotAskedForAnAPIKey(t *testing.T) {
h := rt.Handler()
// The /admin pages are mounted only with a cluster view, which this
// router has none of; these are the session pages it does mount.
for _, path := range []string{"/profile", "/chat", "/images", "/3d", "/org", "/org/org-x"} {
for _, path := range []string{"/profile", "/chat", "/images", "/3d", "/train", "/org", "/org/org-x"} {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
if strings.Contains(w.Body.String(), "api key") {
+3
View File
@@ -192,6 +192,9 @@ type Router struct {
// registry browses a model registry from the mirror page. Nil where the
// deployment has no way out to the internet.
registry web.Registry
// training is the "customise a model" page's control plane; nil
// leaves the page off.
training atomic.Pointer[Training]
// catalogAdmin puts a mirrored model into service. Nil without a store,
// where a model added at runtime would not survive a restart.
catalogAdmin web.CatalogAdmin
+331
View File
@@ -0,0 +1,331 @@
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)
}
+165
View File
@@ -0,0 +1,165 @@
package router
import (
"bytes"
"context"
"encoding/json"
"mime/multipart"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
type fakeTraining struct {
started []adminapi.TrainRequest
jobs map[string]store.TrainJob
shared map[string]string
deleted []string
}
func (f *fakeTraining) TrainableModels() []string { return []string{"qwen"} }
func (f *fakeTraining) StartTraining(_ context.Context, in adminapi.TrainRequest, actor string) (store.TrainJob, error) {
f.started = append(f.started, in)
j := store.TrainJob{ID: "train-" + in.Name, Name: in.Name, Model: in.Model, Owner: in.Owner, Project: in.Project,
CreatedBy: actor, State: store.TrainQueued, Pairs: strings.Count(in.Data, "\n"), CreatedAt: time.Now()}
f.jobs[j.ID] = j
return j, nil
}
func (f *fakeTraining) Trainings(_ context.Context, projects []string, by string) ([]store.TrainJob, error) {
var out []store.TrainJob
for _, j := range f.jobs {
for _, p := range projects {
if j.Project == p {
out = append(out, j)
}
}
}
return out, nil
}
func (f *fakeTraining) Training(_ context.Context, id string) (store.TrainJob, error) {
j, ok := f.jobs[id]
if !ok {
return j, store.ErrNotFound
}
return j, nil
}
func (f *fakeTraining) Published(store.TrainJob) bool { return false }
func (f *fakeTraining) ShareTraining(_ context.Context, id, owner, _ string) error {
f.shared[id] = owner
return nil
}
func (f *fakeTraining) DeleteTraining(_ context.Context, id, _ string) error {
f.deleted = append(f.deleted, id)
return nil
}
func trainForm(t *testing.T, fields map[string]string, file string) *http.Request {
t.Helper()
var b bytes.Buffer
mw := multipart.NewWriter(&b)
for k, v := range fields {
_ = mw.WriteField(k, v)
}
if file != "" {
w, _ := mw.CreateFormFile("file", "ornekler.csv")
_, _ = w.Write([]byte(file))
}
_ = mw.Close()
req := httptest.NewRequest("POST", "/train/start", &b)
req.Header.Set("Content-Type", mw.FormDataContentType())
return req
}
func TestThePortalTrainsForAProjectItsMembersManage(t *testing.T) {
cfg := baseConfig()
cfg.Auth.Enabled = true
cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db")
rt := newTestRouter(t, cfg)
ft := &fakeTraining{jobs: map[string]store.TrainJob{}, shared: map[string]string{}}
rt.SetTraining(ft)
ctx := t.Context()
org, _ := rt.store.CreateOrg(ctx, "acme")
team, _ := rt.store.CreateTeam(ctx, org, "support")
project, _ := rt.store.CreateProject(ctx, team, "bot")
ayse := store.User{ID: "usr-a", Email: "ayse@acme.example", State: store.UserActive, CreatedAt: time.Now()}
bob := store.User{ID: "usr-b", Email: "bob@else.example", State: store.UserActive, CreatedAt: time.Now()}
for _, u := range []store.User{ayse, bob} {
if err := rt.store.CreateUser(ctx, u); err != nil {
t.Fatal(err)
}
}
_ = rt.store.AddMembership(ctx, ayse.ID, store.ScopeProject, project, "member", "test", time.Now())
csv := "Soru,Cevap\nİzin kaç gün?,Yirmi gün.\nMasraf nasıl?,Formla.\n"
start := func(u store.User, fields map[string]string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
rt.trainStart(rec, trainForm(t, fields, csv), u)
return rec
}
good := map[string]string{"name": "destek", "model": "qwen", "project": project, "share": "project"}
if rec := start(bob, good); rec.Code != http.StatusForbidden {
t.Errorf("someone outside the project started one: %d %s", rec.Code, rec.Body.String())
}
wide := map[string]string{"name": "destek", "model": "qwen", "project": project, "share": "org"}
if rec := start(ayse, wide); rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "confirm") {
t.Errorf("shared with the organization unconfirmed: %d %s", rec.Code, rec.Body.String())
}
other := map[string]string{"name": "destek", "model": "llama-70b", "project": project, "share": "project"}
if rec := start(ayse, other); rec.Code != http.StatusBadRequest {
t.Errorf("a model not offered: %d", rec.Code)
}
if rec := start(ayse, good); rec.Code != http.StatusOK {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
in := ft.started[0]
if in.Owner != "project:"+project || in.Project != project || !in.Publish || in.Public ||
in.Data != `{"q":"İzin kaç gün?","a":"Yirmi gün."}`+"\n"+`{"q":"Masraf nasıl?","a":"Formla."}`+"\n" {
t.Errorf("asked for %+v", in)
}
list := func(u store.User) []trainRow {
rec := httptest.NewRecorder()
rt.trainJobs(rec, httptest.NewRequest("GET", "/train/jobs", nil), u)
var rows []trainRow
_ = json.Unmarshal(rec.Body.Bytes(), &rows)
return rows
}
if rows := list(ayse); len(rows) != 1 || rows[0].Share != "project" || rows[0].Project != "acme / support / bot" ||
rows[0].CreatedBy != ayse.Email {
t.Errorf("ayse sees %+v", rows)
}
if rows := list(bob); len(rows) != 0 {
t.Errorf("bob sees %+v", rows)
}
act := func(u store.User, what string, fields map[string]string) int {
req := trainForm(t, fields, "")
req.SetPathValue("id", "train-destek")
rec := httptest.NewRecorder()
if what == "share" {
rt.trainShare(rec, req, u)
} else {
rt.trainDelete(rec, req, u)
}
return rec.Code
}
if code := act(bob, "delete", nil); code != http.StatusNotFound || len(ft.deleted) != 0 {
t.Errorf("bob deleted it: %d", code)
}
if code := act(ayse, "share", map[string]string{"share": "team", "confirm": "yes"}); code != http.StatusOK ||
ft.shared["train-destek"] != "team:"+team {
t.Errorf("shared with the team: %d %v", code, ft.shared)
}
if code := act(ayse, "share", map[string]string{"share": "all"}); code != http.StatusBadRequest {
t.Errorf("shared with everyone unconfirmed: %d", code)
}
if code := act(ayse, "delete", nil); code != http.StatusOK || len(ft.deleted) != 1 {
t.Errorf("ayse deleting: %d", code)
}
}