Spring / Spring AI interview questions
What are ChatOptions in Spring AI and how do you apply them per-request?
ChatOptions is the interface through which you pass inference parameters — temperature, max tokens, top-p, stop sequences, model name — to the model for a specific call. Spring AI separates these from the core Prompt messages so they can be set at three different levels: default (in application.properties), per-client (on ChatClient.Builder), and per-request (on the individual call).
The general ChatOptions interface carries provider-agnostic fields like model, temperature, maxTokens, and topP. Provider-specific options (e.g. OpenAI's responseFormat, Anthropic's topK) are available on the concrete subclass.
// Per-request options — override the defaults for one call only
String creative = chatClient.prompt()
.user("Write a haiku about Spring Boot.")
.options(OpenAiChatOptions.builder()
.withModel("gpt-4o")
.withTemperature(0.9f)
.withMaxTokens(60)
.build())
.call().content();
// Or use the provider-neutral interface for portability
String factual = chatClient.prompt()
.user("List Java 21 features.")
.options(ChatOptionsBuilder.builder()
.withTemperature(0.1f)
.build())
.call().content();Options set per-request override any defaults configured in properties or on the ChatClient.Builder. This layering lets you configure sensible defaults globally while still adjusting parameters for specific use cases — a creative writing endpoint might use temperature 0.9 while a factual Q&A endpoint uses 0.1 — without duplicating client configuration.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
