Skip to content
January 3, 2025

Prompts in Spring AI

· 5 min read · GenAI, Spring AI

  • A prompt is a question, instruction, or description given to an AI to guide its response, and prompt engineering is the process of refining prompts to ensure accurate and desired outputs.
  • In spring AI Prompt, PromptTemplate and Messages are classes designed to streamline the process of creating dynamic and structured prompts for interaction with AI models.
  • Therefore understanding the interplay between Prompt, Message, and PromptTemplate is essential.
  • Prompt is the blueprint for a conversation, encompassing all input, instructions, context, and messages to guide the AI’s responses .
  • Within a Prompt, individual Messages represent single units of text exchanged.
  • Each Message has a crucial attribute: its role.
    • System: Provides initial instructions or constraints to the LLM.
    • User: Represents the input from the user.
    • Assistant: Represents the AI’s response.
    • Tool: Allows the AI to interact with external services (e.g., search, database).
  • PromptTemplate provides a mechanism for creating dynamic and reusable prompts.It helps in creating structured prompts by defining placeholders that can be replaced with actual values at runtime. This is useful for generating prompts based on varying inputs or contexts.
  • Let us examine a a sample code that showcases various prompt configurations and demonstrates how to leverage the power of LLMs within a Spring Boot application.
  • Prompt class encapsulates all the information needed for the LLM to generate a response. In the provided code snippet, a basic Prompt object is created using a simple string.
 @GetMapping("/simplePrompt/")
    public String simplePrompt(@RequestParam(defaultValue = "java") String inputVal) {
        return chatClient.prompt(new Prompt("Tell me just one fact about " + inputVal)).call().content();

 }
  • When you create a Prompt directly from a string, it implicitly assumes that the string represents a user message.
  • This is the default behavior.
  • You can explicitly mention the User Message role also , which is shown in the below code snippet .
 @GetMapping("/simpleUserPrompt")
    public String simpleUserPrompt() {
       String userText = """
          Tell me about 2 famous book and a small summary.
          Write at least a sentence for each book.
          """;
    Message userMessage = new UserMessage(userText);
     return chatClient.prompt(
           newPrompt(userMessage)).call().content();
 }
  • In the below code, you can see how to construct a Prompt with both User and System messages.
 @GetMapping("/simpleUserSystemPrompt")
    public String simpleUserSystemPrompt() {

        String userText = """
          Tell me about 2 famous book and a small summary.
          Write at least a sentence for each book.
           """;
        Message userMessage = new UserMessage(userText);

 String systemText = """
You are a helpful AI assistant that helps people find information.
Your name is Rajesh
You should reply to the user's request with your name and a greeting.
          """;

        Message systemMessage = new SystemPromptTemplate(systemText).createMessage();

        Prompt prompt = new Prompt(List.of(userMessage, systemMessage));

        return chatClient.prompt(prompt).call().content();
    }
  • userText defines the user’s request: “Tell me about 2 famous book and a small summary. Write at least a sentence for each book.”
  • UserMessage(userText) creates a Message object specifically representing the user’s input.
  • systemText provides instructions and context for the LLM.
  • SystemPromptTemplate(systemText).createMessage() creates a Message object with the “system” role, defining the LLM’s behavior.
  • new Prompt(List.of(userMessage, systemMessage)) constructs a Prompt object containing both the userMessage and the systemMessage. This allows the LLM to consider both the user’s request and the provided system instructions when generating a response.
  • By using Message objects with distinct roles like “user” and “system”, you provide a structured and more informative input to the LLM, which can lead to more accurate and relevant responses.
  • The below code snippet also shows how to create a role with Assistant role as well
@GetMapping("/simpleUserAssistantPrompt")
    public String simpleUserAssistantPrompt() {

        String userText = "Me and my friend went to the store.";

        Message userMessage = new UserMessage("Is this sentence correct: " + userText);

        String systemText = """
                You are a helpful AI assistant that .
                Your name is Rajesh
                You should reply to the user's request with your name and a greeting.
                """;

        Message systemMessage = new SystemPromptTemplate(systemText).createMessage();

        String instructions = """
                You are a helpful AI assistant that helps correct grammar mistakes.
                You should provide feedback for the sentence: {text} 
                and correct if there are any issues.
                """;

        AssistantPromptTemplate assistantPromptTemplate = new AssistantPromptTemplate(instructions);

        Message assistantPromptTemplateMessage = assistantPromptTemplate.createMessage(Map.of("text", userText));

        Prompt prompt = new Prompt(List.of(userMessage, systemMessage, assistantPromptTemplateMessage));

        return chatClient.prompt(prompt).call().content();
    }
  • Assistant Instructions: An AssistantPromptTemplate is created with specific instructions for the assistant role: to correct grammar mistakes in the provided sentence.
  • Prompt Construction: A Prompt object is created, combining the userMessage, systemMessage, and the assistantPromptTemplateMessage. This structured prompt provides clear instructions for the LLM on how to interact and respond.
  • By using this approach, you can guide the LLM’s behavior and ensure that the assistant fulfills its specific role within the conversation.
  • This next code snippet demonstrates how to load a system prompt from an external resource file
  @GetMapping("/simpleUserAssistantResPrompt")
    public String simpleUserAssistantResPrompt() {

        String userText = """
                 Tell me about 2 famous book and a small summary.
                  Write at least a sentence for each book.
                """;

        UserMessage userMessage = new UserMessage(userText);

        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(ytPromptAssistantResource);
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "AI Chat Bot"));

        Prompt prompt = new Prompt(List.of(userMessage, systemMessage));

        return chatClient.prompt(prompt).call().content();
    }
  • Resource-Based Prompt: Instead of hardcoding the system instructions, it loads them from a file. This improves code maintainability and allows for easier updates to the system instructions without modifying the controller code direct
  • Dynamic Parameters: The code also demonstrates how to pass dynamic parameters to the system prompt template. In this case, the “name” parameter is replaced with “AI Chat Bot” during the createMessage() call.
  • This approach promotes better code organization and flexibility by separating system instructions from the controller logic.
  • Source code : https://github.com/rajeshsgr/prompt-demo-spring-ai

This blog explores various prompt engineering techniques, including simple prompts, user-defined prompts, system-guided prompts, assistant-role prompts, and resource-based prompts. The PromptDemoController class showcases these techniques through different endpoints, allowing users to interact with the LLM in different ways. By understanding these concepts and utilizing the Spring AI ChatClient effectively, developers can create more sophisticated and engaging LLM-powered applications.

Tags: , ,

Leave a Reply