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

First, install the SDK:

$pip install -Uqq sarvamai

Then use the following Python code:

1from sarvamai import SarvamAI
2
3# Initialize the SarvamAI client with your API key
4client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")
5
6# Example: Apply frequency_penalty to avoid repetition
7response = client.chat.completions(
8 messages=[
9 {"role": "system", "content": "You are a travel blogger assistant."},
10 {"role": "user", "content": "Write a paragraph about the beaches of Goa."}
11 ],
12 frequency_penalty=1.0 # Reduce repetitive phrases
13)
14
15# Receive assistant's reply as output
16print(response.choices[0].message.content)