TheDocumentation 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.
model package provides two generation functions — GenerateText for batch string output and GenerateChan for streaming token-by-token output via a Go channel. Both accept a Model interface and a Tokenizer interface, keeping them decoupled from the concrete GPT struct and allowing custom model or tokenizer implementations to be swapped in.
Tokenizer and Model interfaces
Both generation functions operate against these two interfaces. Any type that satisfies them can be passed directly.tokenizer.BPETokenizer is the canonical Tokenizer implementation, and *model.GPT is the canonical Model implementation — both are verified at compile time via interface checks in the source.
GenerateText()
GenerateText generates text from a prompt and returns the complete result as a decoded string. It is a thin wrapper around GenerateChan that collects all emitted token IDs and decodes them in one call.
The GPT model to run generation on. Must implement the
Model interface (Forward + ClearCache). Create with useCache=true for efficient inference.Maximum context window length. Accepted for API symmetry and forwarded directly to
GenerateChan; the value is not used inside the generation body itself. Conventionally pass m.MaxContextLen from your *GPT instance.Tokenizer used to encode the prompt and decode the output token IDs. Must implement
Encode, Decode, and EndTokenID.The input text to condition generation on. It is encoded with
tokenizer.Encode and fed through the model to populate the KV cache before new tokens are sampled.Maximum number of new tokens to generate beyond the prompt. Generation may stop earlier if the model emits the end token.
Sampling temperature. Pass
0 for greedy (deterministic) decoding via argmax. Pass a positive value (e.g., 0.3–1.0) for stochastic sampling from the softmax distribution.string — the full decoded text spanning both prompt tokens and all newly generated tokens.
GenerateChan()
GenerateChan runs generation asynchronously in a goroutine and streams token IDs — prompt tokens first, then generated tokens — over a read-only channel. The channel is closed when generation finishes.
The GPT model to run generation on. Must implement the
Model interface. Create with useCache=true.Maximum context window length. Present in the signature for API symmetry with
GenerateText; the value is not used inside the generation body. Conventionally pass m.MaxContextLen.Tokenizer used to encode the prompt. Token IDs sent on the channel can be decoded individually with
tokenizer.Decode([]int{id}).The input text to condition generation on.
Maximum number of new tokens to generate. The channel will emit at most
len(encodedPrompt) + maxNewTokens IDs.Sampling temperature.
0 selects the argmax deterministically; positive values sample stochastically.<-chan int — a receive-only channel of token IDs. The channel is closed after the last token is sent, making it safe to range over directly.
Temperature sampling
The internalsample function selects the next token ID from the model’s output logits using one of two strategies depending on the temperature value.
Greedy (temperature == 0)
temperature > 0)
temperature before computing softmax, then draws from the resulting distribution using cumulative-sum (inverse CDF) sampling.
| Temperature | Effect |
|---|---|
0 | Greedy argmax — deterministic, picks the single most likely token |
0.1–0.5 | Focused output — high-probability tokens strongly preferred, low variance |
0.8–1.0 | Balanced — close to the model’s learned distribution |
> 1.0 | Flattened distribution — more randomness, higher diversity, risk of incoherence |
Temperature only affects which token is sampled; it does not change the model’s weights or the logit values stored in the computation graph. Gradient tracking is disabled during generation via
variable.Nograd().Generation algorithm
BothGenerateText and GenerateChan share the same underlying algorithm, which runs inside a goroutine launched by GenerateChan.
-
Disable gradient tracking —
variable.Nograd()is deferred so no computation graph is built during generation, minimizing memory usage. -
Clear the KV cache —
model.ClearCache()is called to discard any keys and values left over from a previous generation sequence. -
Encode the prompt —
tokenizer.Encode(prompt)converts the prompt string into a slice of integer token IDs. -
Warm up the KV cache — each prompt token ID is individually reshaped to
(1, 1)and passed throughmodel.Forward. This populates the attention KV cache for allNtransformer blocks with the prompt’s context. Each prompt token ID is emitted to the channel as it is processed. -
Sample the first new token — the logits from the last prompt-token forward pass are reshaped to
(VocabSize,)and passed tosample(logits, temperature). -
Emit and check stopping conditions — the sampled token ID is sent on the channel. If
nextID == tokenizer.EndTokenID(), generation stops and the channel is closed. -
Continue generating — the new token is reshaped to
(1, 1)and fed back intomodel.Forward. Repeat from step 5 untilmaxNewTokensis reached or the end token is produced.