Spring / Spring AI interview questions
How does PromptTemplate work in Spring AI?
PromptTemplate in Spring AI lets you define a prompt with named placeholders using {variableName} syntax and fill them in at runtime. This keeps prompt strings readable, testable as separate files, and decoupled from Java string concatenation.
// Inline template
PromptTemplate template = new PromptTemplate(
"Explain {concept} to a {level} developer in plain English."
);
Prompt prompt = template.create(Map.of(
"concept", "Java generics",
"level", "junior"
));
String answer = chatModel.call(prompt)
.getResult().getOutput().getContent();For multi-line prompts you should externalise the template to a classpath resource:
// src/main/resources/prompts/code-review.st
PromptTemplate template = new PromptTemplate(
new ClassPathResource("prompts/code-review.st")
);
Prompt prompt = template.create(Map.of("code", sourceCode));When using ChatClient, the fluent API supports inline variable substitution without constructing a PromptTemplate object explicitly:
chatClient.prompt()
.user(u -> u.text("Summarise {topic} in three bullet points")
.param("topic", userInput))
.call().content();The {} placeholder syntax means you must escape any literal curly braces in your prompts as \{ and \}. Store template files in src/main/resources/prompts/ so prompt engineers can iterate on them without touching compiled Java.
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...
