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 许可协议。转载请注明出处!
相关文章
LLM探索:为ChatGLM2的gRPC后端增加连续对话功能
给ChatGLM2的gRPC后端硬塞了连续对话功能,改了proto、C#客户端和Python服务端代码。OpenAI格式转ChatGLM格式?真够折腾的,幸好有loguru。搞定了,但调试gRPC还是麻烦,考虑改OpenAI接口了。
2026-05-09 · LLM · 约 1 分钟
LLM探索:DeepSeek火爆全网,官网宕机?本地部署一个随便玩
DeepSeek官网又崩?本地部署14b模型,web界面倒还凑合。国产大模型热度高,服务器不行就自己搭,呵。代码能力待验证,反正比官网强。
2026-05-09 · LLM · 约 1 分钟
用本地大模型驱动中文输入法,我做了一个实验性的项目
一个拿350MB小模型给输入法做语境排序的实验,思路倒是比那些堆参数的玩意儿清醒。不过老哥,先用着Web版就别急着吹原生输入法框架,等你能把打字延迟压到100ms以下再说。
2026-05-09 · LLM · 约 1 分钟
本地部署大模型哪个性能好?在 8845HS (780M核显) 上的 LM Studio 基准测试
省流:目前用下来 `google/gemma-4-e4b` 是比较均衡的,在速度和性能方面表现都不错
2026-09-15 · LLM · 约 1 分钟
讨论
共 0 条加载评论中…