Spring AI: PromptTemplate

Introduction to PromptTemplate

The PromptTemplate in Spring AI is a powerful tool that allows developers to create structured prompts for interacting with large language models (LLMs). By using templates, you can dynamically generate prompts with placeholders that can be filled with variable data at runtime. This approach ensures that your interactions with LLMs are consistent, maintainable, and flexible, enabling you to handle various use cases without hardcoding different prompts for each scenario.

In this tutorial, we will guide you through setting up a Spring Boot application and demonstrate how to use the PromptTemplate feature to create structured prompts for generating responses from an AI model ChatGPT-4.

1. Setting Up the Project

Step 1: Create a New Spring Boot Project

You can create a new Spring Boot project using Spring Initializr or your preferred IDE. Ensure you include the necessary dependencies for Spring Web and Spring AI.

Using Spring Initializr:

  • Go to start.spring.io
  • Select:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 3.0.0 (or latest)
    • Dependencies: Spring Web, Spring AI
  • Generate the project and unzip it.

Step 2: Add spring-ai-openai-spring-boot-starter Dependency

In your project's pom.xml, add the following dependency:


2. Configuring the Spring Boot Starter

Step 1: Add API Key to Configuration

Create a application.properties or application.yml file in your src/main/resources directory and add your OpenAI API key.

For application.properties:


For application.yml:

    key: your_openai_api_key

Step 2: Create a Configuration Class

Create a new configuration class to set up the OpenAI client and the ChatClient abstraction.

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiClient;

public class OpenAiConfig {

    public OpenAiClient openAiClient() {
        return new OpenAiClient();

    public ChatClient chatClient(OpenAiClient openAiClient) {
        return new OpenAiChatClient(openAiClient);

3. Implementing the PromptTemplate

Step 1: Create a Service for Prompt Templates

Create a service class that will handle interactions with the ChatClient abstraction and use PromptTemplate to create structured prompts.

package com.example.demo.service;

import org.springframework.ai.openai.ChatClient;
import org.springframework.ai.openai.model.ChatRequest;
import org.springframework.ai.openai.model.ChatResponse;
import org.springframework.ai.openai.prompt.PromptTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

public class PromptTemplateService {

    private ChatClient chatClient;

    public String generateTextWithTemplate(String userName, String taskDescription) {
        // Define the prompt template
        String template = "User {userName} has the following task: {taskDescription}";

        // Create a map of variables to fill the template
        Map<String, String> variables = new HashMap<>();
        variables.put("userName", userName);
        variables.put("taskDescription", taskDescription);

        // Create the PromptTemplate instance
        PromptTemplate promptTemplate = new PromptTemplate(template, variables);

        // Generate the prompt
        String prompt = promptTemplate.generatePrompt();

        // Create and send the chat request
        ChatRequest request = new ChatRequest();

        ChatResponse response = chatClient.sendMessage(request);
        return response.getReply();

Step 2: Create a Controller for the Service

Create a controller to expose an endpoint for generating text using the PromptTemplate.

package com.example.demo.controller;

import com.example.demo.service.PromptTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

public class PromptTemplateController {

    private PromptTemplateService promptTemplateService;

    public String generateWithTemplate(@RequestParam String userName, @RequestParam String taskDescription) {
        return promptTemplateService.generateTextWithTemplate(userName, taskDescription);

4. Testing the Integration

Step 1: Run the Application

Run your Spring Boot application. Ensure the application starts without errors.

Step 2: Access the Endpoint

Use Postman, curl, or your browser to test the endpoint. For example:

http://localhost:8080/generateWithTemplate?userName=John&taskDescription=complete the project report

You should receive a response generated by OpenAI based on the structured prompt.


In this tutorial, you learned how to set up and use the PromptTemplate feature in a Spring Boot application with Spring AI. You created a service to handle structured prompts, a controller to expose an endpoint, and a method to generate text based on a template. This setup allows you to create dynamic and structured interactions with LLMs, making your AI integrations more flexible and powerful. 

Explore further customization and enhancements to leverage the full potential of PromptTemplate in your applications.