Beginner LevelAI, LLMs, Embeddings & MCP
How do Temperature and Top-P (Nucleus Sampling) parameters control LLM creativity?
❓ Interview Prompt: Explain logit sampling, Temperature scaling, and Top-P (Nucleus) probability truncation in text generation.
During text generation, an LLM outputs raw scores (logits) for every token in its vocabulary. Sampling parameters control how these logits are converted into probabilities for selecting the next token:
1. Temperature ($T$):
Scales logits before applying Softmax ($z_i / T$):
•
Low Temperature (0.0 – 0.2): Makes output deterministic and focused. High-probability tokens dominate. Ideal for coding and math.
•
High Temperature (0.7 – 1.0+): Flattens probability distribution, making outputs more diverse and creative.
2. Top-P (Nucleus Sampling):
Truncates the token pool to the smallest set of tokens whose cumulative probability exceeds $P$ (e.g. $P = 0.9$).
•
Ignores low-probability "tail" tokens completely, preventing nonsensical word selections.
Code Implementation Example
typescript
const response = await aiClient.generateContent({
model: 'gemini-1.5-flash',
contents: 'Generate 5 creative startup names for a code review tool',
config: {
temperature: 0.8, // Increased randomness for creative ideas
topP: 0.95 // Sample from top 95% cumulative probability pool
}
});💡Setting Temperature and Top-P parameters in Gemini API request config.
🎯 Interview Key Takeaway
Low temperature produces deterministic factual outputs; high temperature combined with Top-P encourages creative generation.
#Temperature#Top-P#Sampling#Beginner
Back to all AI, LLMs, Embeddings & MCP Questions→