Spring AI Day 3: Eliminating Hard‑Coded Prompts with Templates and Role Settings

The article explains how to move beyond static prompts in Spring AI by using System and User message roles, PromptTemplate placeholders with .param(), defaultSystem configuration for reusable role definitions, and independent PromptTemplate usage, providing concrete code examples for each technique.

Tech Ocean
Tech Ocean
Tech Ocean
Spring AI Day 3: Eliminating Hard‑Coded Prompts with Templates and Role Settings

Spring AI's chat client supports two message roles—System and User—to define the AI's persona and the actual user query. Using .system() and .user() lets developers inject role‑specific context, as shown by the example where a System message sets the AI as a senior Java architect.

String answer = chatClient.prompt()
        .system("你是一位资深 Java 架构师,回答简洁,必要时给代码示例")
        .user("什么是依赖注入?")
        .call()
        .content();

Prompt templates allow placeholders ( {variable}) that are filled at runtime with .param(). The article demonstrates building a prompt that asks the model to write code in a specified language and algorithm, with the placeholders replaced by actual values when the prompt is executed.

String code = chatClient.prompt()
        .user(u -> u
                .text("用 {language} 写一个 {algorithm},附简短注释")
                .param("language", "Java")
                .param("algorithm", "快速排序"))
        .call()
        .content();

The underlying implementation relies on PromptTemplate with the default StTemplateRenderer (based on the StringTemplate engine) to substitute variables during execution.

For reusable role definitions, the builder’s defaultSystem method can set a System message once, applying it to all subsequent requests. The example shows configuring a friendly, professional Chinese‑language assistant, and also a variant where the voice can be parameterized via .param() at call time.

@Bean
ChatClient chatClient(ChatClient.Builder builder) {
    return builder
            .defaultSystem("你是「技术洋」的编程助手,用中文回答,语气友好专业")
            .build();
}

// Voice‑parameterized example
String reply = chatClient.prompt()
        .system(sp -> sp.param("voice", "郭德纲"))
        .user(message)
        .call()
        .content(); // Fill voice at call time

When more complex prompt construction is needed, developers can instantiate PromptTemplate directly, define a template with placeholders, create a Prompt by supplying a map of values, and then invoke the model with chatModel.call(prompt). A specialized SystemPromptTemplate serves the same purpose for System messages.

PromptTemplate template = new PromptTemplate("讲一个关于 {topic} 的 {adjective} 笑话");
Prompt prompt = template.create(Map.of("topic", "程序员", "adjective", "冷"));
String joke = chatModel.call(prompt).getResult().getOutput().getText();

The article concludes with a concise summary of concepts: System messages set identity, User messages carry the query, {variable}.param() enables templating, defaultSystem provides global role configuration, and PromptTemplate facilitates manual prompt assembly.

Related links: Prompt API and ChatClient API documentation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaPrompt EngineeringSpring BootSpring AIPromptTemplateChatClient
Tech Ocean
Written by

Tech Ocean

Focused on AI programming, sharing ready-to-use development efficiency solutions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.