How to reduce repetition with frequency_penalty

The frequency_penalty parameter helps you control how often the model repeats words or phrases.

  • Positive values → penalize tokens that have already appeared → model is less likely to repeat itself.
  • Negative values → encourage more repetition (rarely used).

How it works:

  • After each token is generated, the model adjusts the probability of generating the same tokens again, based on how often they have already been used.
  • This helps you create more varied and natural-sounding text.

Parameter details:

ParameterTypeRangeDefault
frequency_penaltyDouble-2.0 to 2.00.0

When to use frequency_penalty:

ScenarioSuggested Value
Model is repeating itself too much0.5 to 1.0
Long-form content, essays, articles0.5 to 1.0
Dialogue-heavy apps (avoid echoing user)0.5 to 1.0
Want creative, varied phrasing0.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: Apply frequency_penalty to avoid repetition
2response = client.chat.completions(
3 messages=[
4 {"role": "system", "content": "You are a travel blogger assistant."},
5 {"role": "user", "content": "Write a paragraph about the beaches of Goa."}
6 ],
7 frequency_penalty=1.0 # Reduce repetitive phrases
8)
1# Receive assistant's reply as output.
2print(response.choices[0].message.content)