How to control response randomness with temperature

The temperature parameter controls how random or deterministic the model’s responses will be.

Range: 0 to 2
Default: 0.2

  • Lower temperature → more focused, predictable answers (e.g. 0.2)
  • Higher temperature → more creative, varied responses (e.g. 0.8 or 1.0)

👉 Tip: For most use cases, values between 0.2 and 0.8 give good results.

How it works:

ModeRecommended temperatureBehavior
Non-thinking mode0.2 (default)Straightforward, factual responses
Thinking mode0.5 or higherDeeper reasoning, more exploration
Highly creative0.8 - 1.0Storytelling, brainstorming, poetry
Very random / playful> 1.0Unexpected, experimental output
1# Install SarvamAI
2!pip install -Uqq sarvamai
3from sarvamai import SarvamAI
1# Initialize the SarvamAI client with your API key
2client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")
1# Example1: Using default temperature (0.2) — straightforward, factual response
2response = client.chat.completions(
3 messages=[
4 {"role": "system", "content": "You are a helpful assistant."},
5 {"role": "user", "content": "Explain the concept of gravity."}
6 ]
7 # temperature is not specified → uses default 0.2
8)
1# Example2: Using temperature = 0.9 — more creative, varied response
2response = client.chat.completions(
3 messages=[
4 {"role": "system", "content": "You are a creative storyteller."},
5 {"role": "user", "content": "Tell me a story about a magical tiger."}
6 ]
7 temperature=0.9 # More creative storytelling
8)
1# Receive assistant's reply as output.
2print(response.choices[0].message.content)