Chapter 11: Applying generative AI patterns
This introduction explains that Spring AI’s ChatClient can do more than answer questions, with the task shaped largely by the prompt.
After this chapter you will be able to
- ChatClient beyond question answering. Explain that ChatClient is not limited to answering user questions: other tasks can use much the same surrounding code, with the prompt shaping what the model is asked to do.
- Prompt-driven rules summarization. Explain how the summarization prompt and ChatClient turn board game rules into a brief answer.
- Uploaded document processing path. Trace how the summarization endpoint converts an uploaded rules document into a returned Answer.
- LLM translation of generated text. Describe the section’s proposed way to translate text that cannot be mapped in advance, and relate it to LLM summarization.
- Translation prompt template. Explain how the prompt template tells the LLM to translate text and which values are supplied at request time.
- Translation API request flow. Trace how the controller turns an HTTP translation request into a translated response using ChatClient.
- Single-request multilingual answers. Explain why Board Game Buddy asks for an answer in the target language as part of the rules question rather than translating the answer afterward.
- Target language request wiring. Describe how the target language reaches Board Game Buddy’s rules-answer prompt, including what happens when the client omits it.
- Sentiment from user language. Explain why a natural-language application can assess user sentiment and how it might use that assessment.
- Graded sentiment scores. Describe the sentiment scale requested by the prompt and explain what the changing example scores illustrate.
- Sentiment endpoint flow. Trace how the controller sends submitted text through ChatClient and returns the sentiment analysis, including why it receives the system prompt template in its constructor.
What it builds on
- Submitting prompts (chapter 1) · Introduces submitting prompts with Spring AI.
- Assigning prompt roles (chapter 3) · Using system and user messages in a prompt
- Creating the pipeline components (chapter 4) · Using a Tika document reader to load document contents
- Understanding RAG (chapter 4) · The distinction between sending document chunks for RAG and sending a whole document for this summarization example
- Defining a prompt template (chapter 3) · Uses the earlier idea of defining a prompt template.
Section by section
Introduction
This introduction explains that Spring AI’s ChatClient can do more than answer questions, with the task shaped largely by the prompt.
- ChatClient beyond question answering: Explain that ChatClient is not limited to answering user questions: other tasks can use much the same surrounding code, with the prompt shaping what the model is asked to do.
Why it matters: It helps you recognize that a familiar ChatClient workflow can serve purposes beyond answering questions.
Key terms: ChatClient, Q&A tasks, prompt
11.1 Summarizing content
Shows how Board Game Buddy turns uploaded board game rules into quick-start summaries with Spring AI, and explains practical model constraints for summarizing long documents.
- Prompt-driven rules summarization: Explain how the summarization prompt and ChatClient turn board game rules into a brief answer.
Why it matters: A useful summary depends on supplying both the source material and a clear summarization request. - Uploaded document processing path: Trace how the summarization endpoint converts an uploaded rules document into a returned Answer.
Why it matters: The endpoint must extract text from the uploaded file before the service can ask the LLM to summarize it. - Summarization model constraints: Explain why model choice, context-window size, and token cost matter when summarizing long documents.
Why it matters: A model that summarizes well may still be unsuitable if it cannot accept the document or costs too much to use.
Key terms: ChatClient, system prompt message, user message, summarizeRules(), Answer, MultipartFile, rulesDocument, TikaDocumentReader, BoardGameService, context window
11.2 Translating messages
This section explains why static translation mappings do not suit unpredictable LLM responses and introduces asking an LLM to translate text instead.
- Limits of translation mappings: Explain why per-language mappings work for text known in advance but fall short for unpredictable LLM responses.
Why it matters: This explains why an application may need translation beyond its existing UI language files. - LLM translation of generated text: Describe the section’s proposed way to translate text that cannot be mapped in advance, and relate it to LLM summarization.
Why it matters: It gives the reader a way to think about translating responses that were not available when the application was built.
Key terms: internationalization, mappings, nondeterministic response, LLMs, translating text, summarization
11.2.1 Building a simple translator
Shows how to build a Spring Boot translation API that uses a prompt template and ChatClient to translate text between languages.
- Translation prompt template: Explain how the prompt template tells the LLM to translate text and which values are supplied at request time.
Why it matters: The template supplies the translation instruction while letting each request provide different languages and text. - Translation API request flow: Trace how the controller turns an HTTP translation request into a translated response using ChatClient.
Why it matters: This flow connects a caller’s translation request to a structured translated response.
Key terms: prompt template, translator, source language, target language, placeholder parameters, Translation record, @RestController, HTTP POST, ChatClient, TranslatorController
11.2.2 Translating game rule answers
Shows how Board Game Buddy can answer game-rule questions in a requested language in one model request instead of translating the answer in a second request.
- Single-request multilingual answers: Explain why Board Game Buddy asks for an answer in the target language as part of the rules question rather than translating the answer afterward.
Why it matters: The language can be part of answer generation rather than a separate translation step. - Target language request wiring: Describe how the target language reaches Board Game Buddy’s rules-answer prompt, including what happens when the client omits it.
Why it matters: The client’s language choice has to flow into the prompt, while requests without a language still get an English answer.
Key terms: target language, additional API request, latency, token usage, translation mappings, Question record, default language, English, askQuestion(), targetLanguage
11.3 Analyzing sentiment
This section shows how to use a prompt and Spring AI’s ChatClient to score the sentiment of user text and return an explanation.
- Sentiment from user language: Explain why a natural-language application can assess user sentiment and how it might use that assessment.
Why it matters: Sentiment gives an application a way to respond to how a user seems to feel, not just to what they asked. - Graded sentiment scores: Describe the sentiment scale requested by the prompt and explain what the changing example scores illustrate.
Why it matters: A graded score and explanation convey more nuance than a simple positive-or-negative label. - Sentiment endpoint flow: Trace how the controller sends submitted text through ChatClient and returns the sentiment analysis, including why it receives the system prompt template in its constructor.
Why it matters: The prompt and message flow turn a general-purpose ChatClient call into a usable sentiment-analysis endpoint.
Key terms: sentiment analysis, user sentiment, natural language experiences, sentiment score, prompt template, neutral sentiment, ChatClient, system message, user message, SentimentAnalysis
11.4 Summary
Concepts explained in this chapter
- templateA prompt template is a reusable prompt structure containing static text and placeholders that are filled with varying model data.
- sentiment analysisSentiment analysis is the computational classification of text according to expressed opinions or emotional polarity.
- summarizationSummarization is the technique of condensing lengthy text into a shorter representation that preserves its essential meaning.
- promptA prompt is input supplied to a language model to elicit a generated response, potentially including text and image context.
- sentiment scoreA sentiment score is a numerical measure representing the polarity and degree of sentiment expressed by text.
- ChatClientChatClient is a Spring AI API component for sending conversational requests to language models and receiving responses.
- placeholderA placeholder is a marked position in a prompt template that can be replaced with a value when the prompt is created.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
