liveBook · Manning

Chapter 3: Submitting prompts for generation

What you will learn in chapter 3 of Spring AI in Action

The chapter introduction explains why more advanced Spring AI applications may need richer prompt and response handling than simple strings.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why more advanced Spring AI applications may need richer prompt and response handling than simple strings.

Key terms: String-based prompt, ChatClient, metadata, usage data, billing, stream

3.1 Working with prompt templates

The section introduces prompt templates and prepares a board-game question-answering example with game context and request validation.

Key terms: prompt templates, placeholders, model data, String concatenation, Question, Answer, gameTitle, @NotBlank, @Valid, Problem Details

3.1.1 Defining a prompt template

Shows how to replace prompt-string concatenation with a parameterized template and improve the prompt’s instructions.

Key terms: prompt template, StringTemplate, placeholders, String concatenation, askQuestion(), user(), Consumer<UserSpec>, text(), param(), call()

3.1.2 Importing the template as a resource

The section moves a prompt template into a classpath resource and explains why adding context to prompts can be a practical response to fabricated answers.

Key terms: StringTemplate, Resource, @Value, classpath:, text(), hallucinations, Burger Battle, additional context, training, fine-tuning

3.2 Stuffing the prompt with context

The section shows how to add game rules to a prompt as context and explains the token costs and limits of doing so.

Key terms: stuffing the prompt, context, gameRules, GameRulesService, getRulesFor(), Resource, {rules}, param(), input tokens, context window

3.3 Assigning prompt roles

This section explains prompt message roles and shows how to separate application instructions from a user’s question in a Spring AI request.

Key terms: User, System, Assistant, Tool, message roles, system message, user message, prompt roles, systemPromptTemplate.st, questionPromptTemplate.st

3.4 Influencing response generation

Introduces three ways Spring AI can influence response generation and delivery: generation options, output conversion, and streaming.

Key terms: generation options, output conversion, streaming, next token, Java object

3.4.1 Specifying chat options

This section explains how Spring AI chat options are set and how temperature, Top-P, and Top-K influence token selection during response generation.

Key terms: application.properties, defaultOptions(), ChatOptions, options(), token, candidate tokens, probability, random selection, temperature, probabilities

3.4.2 Formatting response output

This section shows how Spring AI converts LLM responses into Java objects and lists by adding formatting instructions to prompts, while noting that conversion depends on the model following those instructions.

Key terms: ChatClient, content(), entity(), Answer.class, output conversion, formatting instructions, JsonParseException, non-JSON response, List<String>, ParameterizedTypeReference

3.4.3 Streaming the response

This section explains why streaming improves chat responses, how to stream with Spring AI, how response MIME types affect clients, and why streaming does not pair well with output conversion.

Key terms: streaming the response, ChatClient, stream(), call(), Flux<String>, Project Reactor, AskController, @PostMapping, text/event-stream, application/json

3.5 Working with response metadata

This section shows how to retrieve and log token usage metadata alongside a generated answer, and notes that availability depends on the AI service.

Key terms: usage statistics, tokens, token usage, responseEntity(), ResponseEntity<ChatResponse, Answer>, ChatResponse, getMetadata(), logUsage(), usage metrics, Ollama

3.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Evaluating generated responses Chapter 4: Talking with your documents →

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.