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

+205
View File
@@ -0,0 +1,205 @@
package train
import (
"bufio"
"bytes"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"path"
"strings"
"unicode/utf8"
"gitea.mynodes.xyz/ilker/llmgw/internal/kb/extract"
)
// Pairs from a file a person uploads: a spreadsheet of questions and
// answers (Excel, CSV, tab-separated) or llmgw's own NDJSON of {"q", "a"}.
// The columns are found by their header — Soru/Cevap, Question/Answer and
// the like — or are the first two when the first row is not a header.
// Pair is one question and the answer to learn for it.
type Pair struct {
Q string `json:"q"`
A string `json:"a"`
}
// Read is what a file gave.
type Read struct {
Pairs []Pair
// Skipped counts rows with a question and no answer, or the other way.
Skipped int
// Columns names the columns read, as the header has them; empty for
// NDJSON and for a table with no header.
Columns [2]string
}
// MaxPairs bounds one file: past it a job trains for hours on a page that
// was meant to take minutes.
const MaxPairs = 20000
var (
questionNames = []string{"soru", "sorular", "question", "questions", "q", "prompt", "instruction", "input", "girdi"}
answerNames = []string{"cevap", "cevaplar", "yanıt", "yanit", "answer", "answers", "a", "response", "output", "completion", "çıktı", "cikti"}
)
// ReadPairs reads a file's pairs; name says what kind it is.
func ReadPairs(name string, b []byte) (Read, error) {
var rows [][]string
switch strings.ToLower(path.Ext(name)) {
case ".xlsx", ".xlsm":
r, err := extract.XLSXRows(b)
if err != nil {
return Read{}, fmt.Errorf("the Excel file could not be read: %w", err)
}
rows = r
case ".jsonl", ".ndjson", ".json":
return readNDJSON(b)
case ".csv", ".tsv", ".txt":
r, err := readDelimited(b)
if err != nil {
return Read{}, err
}
rows = r
case ".xls":
return Read{}, errors.New("an old Excel file (.xls): save it as .xlsx and choose it again")
default:
return Read{}, errors.New("choose an Excel (.xlsx), CSV or JSONL file")
}
return fromRows(rows)
}
func readDelimited(b []byte) ([][]string, error) {
b = bytes.TrimPrefix(b, []byte("\xef\xbb\xbf"))
if !utf8.Valid(b) {
return nil, errors.New("the file is not UTF-8 text: save it as \"CSV UTF-8\"")
}
first, _, _ := strings.Cut(string(b), "\n")
// Whichever separator the header line has most of: Excel writes ";"
// where the decimal mark is a comma, as it is in Turkish.
sep, most := ',', strings.Count(first, ",")
for _, c := range []rune{';', '\t'} {
if n := strings.Count(first, string(c)); n > most {
sep, most = c, n
}
}
r := csv.NewReader(bytes.NewReader(b))
r.Comma, r.FieldsPerRecord, r.LazyQuotes = sep, -1, true
rows, err := r.ReadAll()
if err != nil {
return nil, fmt.Errorf("the file could not be read as a table: %w", err)
}
return rows, nil
}
func fromRows(rows [][]string) (Read, error) {
var out Read
qc, ac := 0, 1
if len(rows) > 0 {
q, a := -1, -1
for i, c := range rows[0] {
n := strings.ToLower(strings.TrimSpace(c))
switch {
case q < 0 && contains(questionNames, n):
q = i
case a < 0 && contains(answerNames, n):
a = i
}
}
if q >= 0 && a >= 0 {
qc, ac = q, a
out.Columns = [2]string{strings.TrimSpace(rows[0][q]), strings.TrimSpace(rows[0][a])}
rows = rows[1:]
}
}
for _, r := range rows {
var q, a string
if qc < len(r) {
q = strings.TrimSpace(r[qc])
}
if ac < len(r) {
a = strings.TrimSpace(r[ac])
}
switch {
case q == "" && a == "":
case q == "" || a == "":
out.Skipped++
default:
out.Pairs = append(out.Pairs, Pair{Q: q, A: a})
}
}
return out, check(out)
}
func contains(names []string, n string) bool {
for _, x := range names {
if x == n {
return true
}
}
return false
}
func readNDJSON(b []byte) (Read, error) {
var out Read
sc := bufio.NewScanner(bytes.NewReader(b))
sc.Buffer(make([]byte, 64<<10), 1<<20)
line := 0
for sc.Scan() {
line++
l := bytes.TrimSpace(sc.Bytes())
if len(l) == 0 {
continue
}
var p struct {
Q, A string
Question, Answer string
Prompt, Completion string
}
if err := json.Unmarshal(l, &p); err != nil {
return Read{}, fmt.Errorf("line %d is not JSON: %v", line, err)
}
q, a := first(p.Q, p.Question, p.Prompt), first(p.A, p.Answer, p.Completion)
if q == "" || a == "" {
out.Skipped++
continue
}
out.Pairs = append(out.Pairs, Pair{Q: q, A: a})
}
if err := sc.Err(); err != nil {
return Read{}, err
}
return out, check(out)
}
func first(s ...string) string {
for _, x := range s {
if x = strings.TrimSpace(x); x != "" {
return x
}
}
return ""
}
func check(r Read) error {
switch {
case len(r.Pairs) == 0:
return errors.New("no question and answer found: the first column is the question and the second the answer, " +
"or columns headed Soru and Cevap")
case len(r.Pairs) > MaxPairs:
return fmt.Errorf("%d pairs, more than the %d one training takes", len(r.Pairs), MaxPairs)
}
return nil
}
// NDJSON is the pairs as a job's data.
func NDJSON(pairs []Pair) string {
var b strings.Builder
enc := json.NewEncoder(&b)
enc.SetEscapeHTML(false)
for _, p := range pairs {
_ = enc.Encode(p)
}
return b.String()
}
+65
View File
@@ -0,0 +1,65 @@
package train
import (
"strings"
"testing"
)
func TestPairsFromATableByItsHeaderOrItsFirstColumns(t *testing.T) {
for _, c := range []struct {
name, file string
want []Pair
skipped int
cols [2]string
}{
{"headed.csv", "No;Cevap;Soru\n1;Yirmi gün.;İzin kaç gün?\n2;;Boş cevap\n3;Evet;Uzaktan çalışabilir miyim?\n",
[]Pair{{"İzin kaç gün?", "Yirmi gün."}, {"Uzaktan çalışabilir miyim?", "Evet"}}, 1, [2]string{"Soru", "Cevap"}},
{"plain.csv", "\ufeffİzin kaç gün?,\"Yirmi gün, yılda.\"\n\nMasraf?,Formla.\n",
[]Pair{{"İzin kaç gün?", "Yirmi gün, yılda."}, {"Masraf?", "Formla."}}, 0, [2]string{}},
{"tabs.tsv", "Question\tAnswer\nA?\tB.\n", []Pair{{"A?", "B."}}, 0, [2]string{"Question", "Answer"}},
{"d.jsonl", `{"q":"A?","a":"B."}` + "\n" + `{"question":"C?","answer":"D."}` + "\n" + `{"q":"E?"}` + "\n",
[]Pair{{"A?", "B."}, {"C?", "D."}}, 1, [2]string{}},
} {
got, err := ReadPairs(c.name, []byte(c.file))
if err != nil {
t.Errorf("%s: %v", c.name, err)
continue
}
if len(got.Pairs) != len(c.want) || got.Skipped != c.skipped || got.Columns != c.cols {
t.Errorf("%s: %+v", c.name, got)
continue
}
for i := range c.want {
if got.Pairs[i] != c.want[i] {
t.Errorf("%s: pair %d = %+v, want %+v", c.name, i, got.Pairs[i], c.want[i])
}
}
}
}
func TestPairsRefusedWithAReason(t *testing.T) {
for name, file := range map[string]string{
"a.csv": "only one column\n",
"a.xls": "x",
"a.pdf": "x",
"a.json": "not json\n",
} {
if _, err := ReadPairs(name, []byte(file)); err == nil {
t.Errorf("%s read", name)
}
}
var b strings.Builder
for range MaxPairs + 1 {
b.WriteString("x,y\n")
}
if _, err := ReadPairs("big.csv", []byte(b.String())); err == nil || !strings.Contains(err.Error(), "more than") {
t.Errorf("too many: %v", err)
}
}
func TestNDJSONIsWhatAJobReads(t *testing.T) {
got := NDJSON([]Pair{{"<b>?", "a & b"}})
if got != `{"q":"<b>?","a":"a & b"}`+"\n" {
t.Errorf("%q", got)
}
}
+35 -1
View File
@@ -35,11 +35,26 @@ type Service struct {
agents Agents
log *slog.Logger
// pick chooses the node for a job that names none, among those not
// busy; "" is none can take it now. Nil leaves such jobs queued.
pick func(needMiB int, busy map[string]bool) string
// done is told of a job that finished, after what it made is kept.
done func(j store.TrainJob)
mu sync.Mutex
// pieces are artifacts arriving, by job and kind.
pieces map[[2]string][]byte
}
// SetPicker has jobs that name no node sent where pick says.
func (s *Service) SetPicker(pick func(needMiB int, busy map[string]bool) string) { s.pick = pick }
// SetOnDone has done told of each job that finishes.
func (s *Service) SetOnDone(done func(j store.TrainJob)) { s.done = done }
// Waiting is what a job that names no node says while no node can take it.
const Waiting = "waiting for a card with enough memory free"
// New is a service.
func New(s Store, a Agents, log *slog.Logger) *Service {
if log == nil {
@@ -81,13 +96,27 @@ func (s *Service) Dispatch(ctx context.Context) {
busy[j.Node] = true
}
for _, q := range queued {
if busy[q.Node] || !s.agents.Connected(q.Node) {
node := q.Node
if node == "" {
if s.pick != nil {
node = s.pick(q.GPUMemoryMiB, busy)
}
if node == "" {
if q.Message != Waiting {
q.Message = Waiting
_ = s.store.UpdateTrainJob(ctx, q)
}
continue
}
}
if busy[node] || !s.agents.Connected(node) {
continue
}
j, err := s.store.TrainJob(ctx, q.ID)
if err != nil {
continue
}
j.Node = node
err = s.agents.Send(j.Node, &llmgwv1.ControlMessage{Msg: &llmgwv1.ControlMessage_TrainStart{TrainStart: &llmgwv1.TrainStart{
JobId: j.ID, BaseRepo: j.BaseRepo, BaseRevision: j.BaseRevision, Data: j.Data, Params: j.Params,
GpuMemoryMib: int32(j.GPUMemoryMiB), Gguf: j.GGUF}}}) //nolint:gosec // MiB
@@ -135,6 +164,11 @@ func (s *Service) OnTrainStatus(node string, st *llmgwv1.TrainStatus) {
}
if err := s.store.UpdateTrainJob(ctx, j); err != nil {
s.log.Warn("could not record a training job's progress", "job", j.ID, "err", err)
return
}
// What it made came before it said it was done, on the same stream.
if j.State == store.TrainDone && s.done != nil {
go s.done(j)
}
}
+38
View File
@@ -122,3 +122,41 @@ func TestCancellingAJob(t *testing.T) {
t.Errorf("the node was not told: %+v", a.sent)
}
}
func TestAJobNamingNoNodeGoesWhereThePickerSaysAndIsToldWhenDone(t *testing.T) {
ctx := context.Background()
svc, s, a := setup(t)
job(t, s, "j1", "")
asked := 0
free := ""
svc.SetPicker(func(need int, busy map[string]bool) string {
asked++
if need != 2000 {
t.Errorf("asked for %d MiB", need)
}
return free
})
done := make(chan store.TrainJob, 1)
svc.SetOnDone(func(j store.TrainJob) { done <- j })
svc.Dispatch(ctx)
if j, _ := s.TrainJob(ctx, "j1"); j.State != store.TrainQueued || j.Message != Waiting || len(a.sent) != 0 {
t.Fatalf("with no card free: %+v, sent %d", j, len(a.sent))
}
free = "gpu2"
a.connected["gpu2"] = true
svc.Dispatch(ctx)
j, _ := s.TrainJob(ctx, "j1")
if len(a.sent) != 1 || j.Node != "gpu2" || j.State != store.TrainStarting {
t.Fatalf("picked: %+v, sent %d", j, len(a.sent))
}
// Its node is the one it was sent to, which alone reports for it.
svc.OnTrainStatus("cuda01", &llmgwv1.TrainStatus{JobId: "j1", State: "done"})
if j, _ := s.TrainJob(ctx, "j1"); j.State == store.TrainDone {
t.Fatal("another node finished it")
}
svc.OnTrainStatus("gpu2", &llmgwv1.TrainStatus{JobId: "j1", State: "done"})
if got := <-done; got.ID != "j1" || got.Node != "gpu2" {
t.Errorf("told of %+v", got)
}
}