Single Go application with:
- Go backend API
- Go-served static frontend (no Node.js, no Vite, no TypeScript)
- Small educational character-level transformer/autograd implementation
Important scope note:
- This is a toy model trained from scratch on user-provided strings at runtime.
- It is not a pretrained production GPT model.
- Go 1.21+
go run .Open:
Live website:
go build ./...main.go: app bootstrap (embed assets, register routes, start server)server.go: HTTP handlers and shared server stateapi_types.go: request/response structs for APIautograd.go: tiny autodiff engine (Value, ops,Backward)model.go: model config/state, initialization, math helpers, optimizerforward.go: transformer forward passinference_and_training.go: training step + sampling + trace generationweb/index.html: main UIweb/app.js: browser logicweb/docs/index.html: help page
POST /api/init
- Purpose: initialize model with documents and hyperparameters.
- Body:
{
"docs": ["alex", "anna", "john"],
"config": {
"n_embd": 16,
"n_head": 4,
"n_layer": 1,
"block_size": 16,
"learning_rate": 0.05
}
}POST /api/train
- Purpose: train model parameters.
- Body is optional.
- Optional body fields:
{
"steps_per_call": 1,
"batch_size": 6
}- Defaults when omitted:
steps_per_call = 1batch_size = 6
POST /api/generate
- Purpose: sample generated text.
- Body is optional.
- Optional body fields:
{
"options": {
"temperature": 0.7,
"top_k": 5,
"min_len": 3
}
}- Defaults when omitted:
temperature = 0.7top_k = 5min_len = 3
POST /api/generate_trace
- Purpose: sample generated text and return per-step sampling trace.
- Accepts the same optional
optionsas/api/generate.