Ein Vorgeschmack auf Spring AI
Spring AI bringt das vertraute Spring-Modell zu LLMs: ein ChatClient, Prompts und strukturierte Ausgaben - dieselben Muster, eine neue Fähigkeit.
Deutsche Übersetzung in Arbeit
Diese Lektion ist noch nicht ins Deutsche übersetzt und wird daher auf Englisch angezeigt. Der Rest der Seite ist vollständig lokalisiert.
Auf dieser Seite
Large language models are everywhere now, and reaching one is just an HTTP call to an API. But wiring prompts, model clients, retries, and output parsing by hand, in every app, gets messy fast. Spring AI does for LLMs what Spring Data did for databases: wraps them in familiar, portable Spring abstractions so calling a model feels like calling any other injected bean.
The same Spring model, a new capability
You already know the pattern: add a starter, get an auto-configured, injectable bean, program
against a portable interface. Spring AI's central abstraction is the ChatClient - inject it
and ask the model a question:
@Service
class BlurbService {
private final ChatClient chat;
BlurbService(ChatClient.Builder builder) {
this.chat = builder.build();
}
String blurbFor(String title, String author) {
return chat.prompt()
.user(u -> u.text('Write a one-sentence enticing blurb for {title} by {author}.')
.param('title', title)
.param('author', author))
.call()
.content();
}
}Notice what's not here: no HTTP client, no JSON assembly, no vendor SDK in your code. The model is chosen by configuration - swap OpenAI for Anthropic, Azure, Ollama, or a local model by changing a starter and properties, not your code - the same portability lesson as JDBC drivers and Micrometer registries.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>Prompts, structured output, and grounding
Spring AI covers the patterns you need to use a model seriously:
-
Prompt templates - parameterized prompts (as above), and system prompts to set the model's role and rules.
-
Structured output - map the model's reply straight into a typed Java object, so downstream code gets a
BookSummary, not a blob of text:BookSummary summary = chat.prompt() .user('Summarize this review as a rating (1-5) and a one-line takeaway: ' + review) .call() .entity(BookSummary.class); // parsed into your record -
RAG (Retrieval-Augmented Generation) - combine the model with a vector store so it answers from your data (e.g. BookVault's catalog), grounding replies and reducing hallucination.
-
Advisors and tool/function calling - cross-cutting hooks (like memory) and letting the model call your methods.
Every TV brand ships its own remote with its own buttons. A universal remote learns them all and
gives you one consistent set of controls - you press 'volume up' and it works whichever TV is in
front of you. Spring AI's ChatClient is that universal remote for language models: one API, and the
specific model behind it is a setting - so switching providers doesn't mean relearning the buttons.
LLMs are probabilistic - engineer around it
A model can be confidently wrong (hallucinate) and its output varies. Treat replies as untrusted input: validate structured output, ground answers in your data with RAG, never expose secrets in prompts, and keep a human in the loop for consequential actions. Spring AI gives you the plumbing; sound judgment about when and how to trust the model is still yours.
You want BookVault to turn a member's free-text review into a structured { rating: int, takeaway: String } your code can store. Explain how Spring AI's ChatClient gets you there without manual HTTP
or JSON parsing, and one safeguard you'd add given the model is probabilistic.
How does Spring AI fit the Spring programming model?
Key takeaways
- Spring AI wraps LLMs in familiar Spring abstractions - an injectable, auto-configured ChatClient you call like any bean.
- The model provider (OpenAI, Anthropic, Azure, Ollama, local...) is chosen by starter + config, not by changing code - the same portability as JDBC drivers.
- It supports prompt/system templates, structured output mapped to typed objects, RAG with vector stores, advisors, and tool calling.
- LLMs are probabilistic and can hallucinate: validate output, ground with RAG, protect secrets, and keep humans in the loop for consequential actions.
- The takeaway is conceptual continuity: adding AI is 'more of the same Spring', not a new paradigm.