Exploring LLMs: Common Parameters in GPT-like Models - Top-k, Top-p, Temperature
A recent breakthrough in artificial intelligence (AI) is the introduction of language processing technologies that enable us to build more intelligent systems with a richer understanding of language than ever before. Large pre-trained Transformer language models, or simply large language models, vastly extend the capabilities of what systems are able to do with text.
目录8 个章节
Preface
The previous article introduced the environment setup and local deployment for several open-source LLMs. When using the ChatGPT interface or a locally deployed LLM, you often encounter these parameters. This article will briefly introduce them~
- temperature
- top_p
- top_k
About LLM
This was also mentioned in the previous article, but this time I came across a nice diagram.
A recent breakthrough in artificial intelligence (AI) is the introduction of language processing technologies that enable us to build more intelligent systems with a richer understanding of language than ever before. Large pre-trained Transformer language models, or simply large language models, vastly extend the capabilities of what systems are able to do with text.

While LLMs may seem magical, at their core, they are still a matter of probability. A neural network generates a set of candidate words based on the input text, drawing from its pre-trained model, and then selects the ones with the highest probability as the output. The three parameters mentioned above are all related to sampling—that is, how to choose an output from the candidate words.
temperature
Used to control the randomness of model output results; the higher this value, the greater the randomness. Generally, after inputting the same prompt multiple times, the model's output will be different each time.
- Set to 0, generates fixed output for each prompt
- Lower values, output is more concentrated and deterministic
- Higher values, output is more random (more creative 😃)

Generally, the longer the prompt and the clearer the description, the better the quality of the model's generated output and the higher the confidence. In this case, you can appropriately increase the temperature value. Conversely, if the prompt is very short and vague, setting a relatively high temperature value will make the model's output very unstable.
When in doubt, adjust the parameters; give it a try, and maybe you'll generate a good answer?
PS: The example provided by ChatGLM limits the range to between 0-1.
top_k & top_p
These two are also sampling parameters, but they use a different sampling method than temperature.
As mentioned earlier, before outputting, the model generates a bunch of tokens, which are ranked by quality.
For example, in the following image, given the input The name of that country is the, the model generates a bunch of tokens and then selects one for output based on different decoding strategies.

The decoding strategy here can be chosen as:
- greedy decoding: Always selects the token with the highest score. It's useful but has some drawbacks, as detailed below.
- top-k: Selects k tokens from the list as candidates and then samples based on their
likelihood scores. - top-p: The candidate list is dynamic, selecting candidates from the tokens based on a cumulative probability percentage.
top-k and top-p introduce randomness into the token selection process, giving other high-scoring tokens a chance to be chosen, unlike greedy decoding which always picks the highest score.
Greedy Decoding
The advantage is simplicity, the disadvantage is that it easily generates repetitive and circular content.
Greedy decoding is a reasonable strategy but has some drawbacks such as outputs with repetitive loops of text. Think of the suggestions in your smartphone's auto-suggest. When you continually pick the highest suggested word, it may devolve into repeated sentences.
top-k
The larger the setting, the more possibilities the generated content will have;
the smaller the setting, the more fixed the generated content will be;
when set to 1, the effect is the same as greedy decoding.

Changing the top-k parameter sets the size of the shortlist the model samples from as it outputs each token. Setting top-k to 1 gives us greedy decoding.
top-p
top-p, also known as Nucleus Sampling (核采样)
Unlike top-k, which selects the fixed top k tokens, top-p does not select a fixed number of tokens. Instead, this method sets a probability threshold.
Continuing the previous example, if top-p is set to 0.15, it means selecting the top 15% of tokens by cumulative probability as candidates. As shown in the figure below, the probabilities of "United" and "Netherlands" sum up to 15%, so these two become the candidate words. Finally, from these candidates, the word "united" is selected based on the probability score.

Top-p is usually set to a high value (like 0.75) with the purpose of limiting the long tail of low-probability tokens that may be sampled. We can use both top-k and top-p together. If both
kandpare enabled,pacts afterk.
Commonly encountered default top-p values are around 0.7/0.8. The same principle applies: if set too low, the model's output becomes too deterministic; if set too high, the model might become overly unconstrained, which isn't ideal either.
References
_EOF_
Exploring LLMs: Common Parameters in GPT-like Models - Top-k, Top-p, Temperature
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
2GB 显存还能跑大模型吗?我在 MX250 老笔记本上部署 Qwen3.5 2BB
七年前的Matebook14,MX250显存2GB,硬塞Qwen3.5 2B——这届玩家是真的敢。核心思路倒是比模型选择更值得学:先看清楚硬件预算,再决定能跑什么,而不是硬上7B然后等CPU掉帧到怀疑人生。1.81GiB直接顶满2GB显存,KV Cache挤出来的空间里,4K Context刚好够用。至于Agent场景?省省吧,2B让MX250当推理节点做轻量任务已经仁至义尽。
2026-09-15 · LLM · 约 1 分钟
LLM探索:离线部署Ollama和one-api服务
离线部署Ollama?手动下载上传,导入模型文件,啧,麻烦。one-api用Docker,tiktoken需下载重命名,更烦。添加渠道?简单。
2026-05-09 · LLM · 约 1 分钟
用本地大模型驱动中文输入法,我做了一个实验性的项目
一个拿350MB小模型给输入法做语境排序的实验,思路倒是比那些堆参数的玩意儿清醒。不过老哥,先用着Web版就别急着吹原生输入法框架,等你能把打字延迟压到100ms以下再说。
2026-05-09 · LLM · 约 1 分钟
LLM探索:大模型背后的向量魔法,Embedding技术初探
Embedding技术从Word Embedding发展到动态预训练模型,无处不在,但本文只是过时的基础。LLM时代,向量魔法如此关键,作者却拖更太久,内容早该更新了。
2026-05-09 · LLM · 约 1 分钟
讨论
共 0 条加载评论中…