What is ChatOptions
What is ChatOptions?
Q1. What is ChatOptions?
Answer
ChatOptions is a configuration object in Spring AI that controls how the underlying language model generates responses.
It allows developers to configure parameters such as model selection, temperature, maximum output tokens, sampling behavior, and other provider-supported settings.
Instead of changing the prompt, it changes how the model generates the answer.
Architecture
User │ ▼ ChatClient │ ├── Prompt ├── Advisors └── ChatOptions │ ▼ OpenAI / Ollama │ ▼ AI Response
Notice:
- Prompt → tells the model what to do.
- ChatOptions → tells the model how to do it.
Example
String response = chatClient.prompt() .user("Explain Kafka") .options(OpenAiChatOptions.builder() .temperature(0.7) .maxTokens(300) .build()) .call() .content();
Here:
-
Prompt =
"Explain Kafka" -
ChatOptions =
temperature,maxTokens
Why do we need ChatOptions?
Without ChatOptions:
Explain Java
The model uses its default settings.
With ChatOptions:
Temperature = 0 Max Tokens = 100 TopP = 0.8
Now you control the response style.
Common ChatOptions
1. temperature ⭐⭐⭐⭐⭐
Controls creativity/randomness.
. temperature (0)
Output:
Very factual Very consistent
Best for:
- Banking
- Healthcare
- SQL generation
- Code generation
. temperature (1.5)
Output:
Creative Different every time
Best for:
- Story writing
- Poems
- Marketing
- Brainstorming
Example
Question
Write a birthday wish.
Temperature = 0
Happy Birthday. I wish you good health and happiness.
Temperature = 1.5
May your birthday sparkle with joy, laughter, and unforgettable memories...2. maxTokens ⭐⭐⭐⭐⭐
Limits response length.
.maxTokens(100)Means
AI cannot generate more than 100 output tokens.Useful when:
- SMS
- Tweet generation
- Short answers
Example
Without
1000 wordsWith
.maxTokens(50)2 paragraphs only.
3. frequencyPenalty
Prevents repetition.
Without
Kafka is... Kafka is... Kafka is... Kafka is...
With
.frequencyPenalty(2)
Less repetition.
Useful for
- Long articles
- Blogs
4. presencePenalty
Encourages new topics.
Without
Java Java Java Java
With
.presencePenalty(2)
The model is more likely to introduce related ideas like:
Java Spring Boot Microservices JVM5. stop
Stops generation when a specified sequence appears.
Example
.stop(List.of("END"))If AI produces
Hello END Thank YouOutput becomes
HelloGeneration stops at
"END".
Top-P and Top-K
Suppose you ask the LLM:
Java is a ______ programming language.
The model predicts the next word with probabilities.
Word Probability object-oriented 40% popular 25% powerful 15% compiled 10% interpreted 5% banana 3% elephant 2%
The total probability is 100%.
The model now has to decide which word to pick.
6. Top K Sampling
Top-K means:
Only consider the top K most probable words.
Suppose
.topK(3)Then only these 3 words are considered:
Word Probability object-oriented 40% popular 25% powerful 15%
The remaining words are discarded.
object-oriented ✔ popular ✔ powerful ✔ compiled ❌ interpreted ❌ banana ❌ elephant ❌
The model randomly picks one of these 3 according to their probabilities.
Example
If
.topK(1)Only one word is available.
object-orientedThe answer becomes almost deterministic.
If
.topK(100)Many words are available.
The response becomes more diverse.
2. Top-P (Nucleus Sampling)
Top-P doesn't look at a fixed number of words.
Instead, it keeps adding the most probable words until their cumulative probability reaches P.
Suppose
.topP(0.80)Let's calculate:
Word Probability Cumulative object-oriented 40% 40% popular 25% 65% powerful 15% 80% ✅ compiled 10% 90% interpreted 5% 95%
We stop at 80%.
Only these words remain:
object-oriented popular powerful
Everything else is discarded.
Suppose
.topP(0.95)Now
Word Probability Cumulative object-oriented 40% 40% popular 25% 65% powerful 15% 80% compiled 10% 90% interpreted 5% 95% ✅
Now five words are available.
More variety.
Comments
Post a Comment