Files
llmgw/internal/control/adminapi/train_portal.go
T
Ilker ManapandClaude Opus 5.5 a53b2f4be9 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>
2026-11-19 11:31:12 +01:00

115 lines
3.6 KiB
Go

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
}