How to encourage new topics with presence_penalty

The presence_penalty parameter helps you steer the model toward introducing new concepts or topics, instead of sticking to the same words.

  • Positive values → penalize tokens that have already appeared → model becomes more likely to explore new ideas.
  • Negative values → encourage staying on the same topic (rarely used).

How it works:

  • As the model generates text, it tracks which tokens have already been used.
  • Higher presence_penalty nudges the model to shift away from familiar tokens, helping it bring in new topics or angles.

Parameter details:

ParameterTypeRangeDefault
presence_penaltyDouble-2.0 to 2.00.0

When to use presence_penalty:

ScenarioSuggested Value
You want the model to introduce new topics0.5 to 1.0
Avoid getting stuck on the same subject0.5 to 1.0
Brainstorming ideas1.0 to 1.5
Writing diverse, exploratory content0.5 to 1.5
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# Example: Encourage model to introduce new ideas
2response = client.chat.completions(
3 messages=[
4 {"role": "system", "content": "You are a creative brainstorming assistant."},
5 {"role": "user", "content": "Suggest some ideas for eco-friendly products."}
6 ],
7 presence_penalty=1.0 # Encourage exploration of new ideas
8)
1# Receive assistant's reply as output.
2print(response.choices[0].message.content)