Text generation works by encoding a prompt into token IDs, feeding each token through the model one at a time to populate the KV cache, then autoregressively sampling new tokens from the output logits until the end token (Documentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/gpt/llms.txt
Use this file to discover all available pages before exploring further.
<|endoftext|>) is produced or maxNewTokens is reached. Each forward pass operates on a single token, making generation efficient when the model is loaded with caching enabled.
CLI usage
Thecmd/generate command loads a pre-trained model and tokenizer from .gob files, prints model parameters, and streams token IDs to stdout before printing the decoded text.
| Flag | Default | Description |
|---|---|---|
-merge-rules-path | testdata/merge_rules.gob | Path to the BPE merge rules file |
-model-path | testdata/model_gpt.gob | Path to the pre-trained model file |
-prompt | def | Prompt string to start generation from |
-temperature | 1.0 | Sampling temperature |
-max-new-tokens | 256 | Maximum number of new tokens to generate |
-count | 1 | Number of generation runs to perform |
temperature = 1.0 (the default), sampling is stochastic so token IDs and the decoded text will vary between runs. Use --temperature 0.3 for more predictable output, as shown in the examples below.
Examples with make example
The example Makefile target runs several generation tasks at lower temperature for more deterministic output:
--prompt 'def add(a, b):' at temperature 0.3:
--prompt 'def is_prime(n):' at temperature 0.3:
Library usage
Importmodel and tokenizer to generate text directly in Go code.
Batch generation — GenerateText blocks until all tokens are produced and returns the decoded string:
GenerateChan returns a channel of token IDs so you can process each token as it is produced:
GenerateText is implemented on top of GenerateChan — it collects all IDs from the channel and decodes them at once. Use GenerateChan when you want to stream output token-by-token, for example to print characters as they are generated.Temperature
Temperature controls how peaked or flat the probability distribution over the vocabulary is before sampling:temperature = 0— greedy decoding: the token with the highest logit (argmax) is always chosen. Output is fully deterministic.temperature > 0— stochastic sampling: logits are divided by the temperature and passed through softmax (softmax(logits / temperature)), then a token is drawn from the resulting multinomial distribution.- Lower values (e.g.
0.3) concentrate probability on likely tokens, producing more predictable and coherent code. - Higher values (e.g.
1.0or above) spread probability more evenly, introducing more variation and creativity.
- Lower values (e.g.
model/generate.go:
Generation algorithm
The core logic lives inmodel.GenerateChan (model/generate.go):
- Disable gradient tracking —
variable.Nograd()is deferred at the start of generation so no computation graph is built throughout, reducing memory usage. - Clear the KV cache —
model.ClearCache()resets cached key/value tensors from any previous generation run. - Encode the prompt —
tokenizer.Encode(prompt)converts the input string to a slice of token IDs. - Feed prompt tokens — each prompt token ID is reshaped to
(1, 1)and passed throughmodel.Forward()one at a time, populating the KV cache. Each token ID is also emitted immediately to the output channel. - Sample the next token — after the last prompt token, logits are reshaped from
(1, 1, VocabSize)to(VocabSize)and thesample()function selects the next token ID according to the configured temperature. - Autoregressive loop — the sampled token is emitted to the channel, then fed back as the next single-token input (
(1, 1)). This repeats until either theendTokenIDis sampled ormaxNewTokenstokens have been generated.
The model must be loaded with
useCache=true (the second argument to model.NewGPTFrom) for the KV cache to be active. Without caching the model still works correctly, but every forward pass recomputes attention over the full sequence, making generation significantly slower for long outputs.