Close

AI LangChain4j - Using Prompt Templates With AI Services

[Last Updated: Jan 29, 2026]

Prompt templates allow you to create dynamic prompts that can incorporate method parameters, context variables, or external data. AI Services support template variables in both @SystemMessage and @UserMessage annotations, enabling flexible prompt construction.

Please checkout this tutorial to see how to use template variables without AI Services.

Template Variable Syntax

AI Services use Mustache-like syntax for template variables:

  • {{it}} - References the single method parameter
  • {{name}} - References a named parameter (when using @V annotation)
  • {{variable}} - Can reference context variables in more advanced scenarios

@V Annotation

When a method parameter in an AI Service is annotated with @V, it becomes a prompt template variable. Its value is injected into prompt templates defined via @UserMessage, @SystemMessage, and AiServices.systemMessageProvider(Function).

Example
@UserMessage("Hello, my name is {{name}}. I am {{age}} years old.")
String chat(@V("name") String name, @V("age") int age);

Compiler Option Dependency

This annotation is only necessary when the -parameters option is not enabled during Java compilation. If -parameters is enabled, parameter names can directly serve as identifiers, making the @V value definition optional.

Loading Templates from Resources

For complex prompts, you can store templates in external files and load them using the fromResource attribute:

@UserMessage(fromResource = "templates/classification.txt")
String classify(String text);

Use Cases for Prompt Templates

  • Generating emails with dynamic recipient information
  • Creating product descriptions with varying attributes
  • Building SQL queries from natural language requests
  • Formatting reports with dynamic data sections

Example

The following example uses Ollama and phi3:mini-128k which is good for a demo and learning but not good for production-grade applications because it has limited reasoning capabilities and accuracy for complex tasks.

package com.logicbig.example;

import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class PromptTemplatesExample {

    interface EmailGenerator {
        @UserMessage("Write a professional email to {{recipient}} "
                + "about {{topic}}. "
                + "Keep it under 100 words.")
        String generateEmail(@V("recipient") String recipientName,
                             @V("topic") String emailTopic);
    }

    interface ProductDescriber {
        @UserMessage("Create a marketing description for a {{product}} "
                + "that costs ${{price}}. Highlight its {{feature}}."
                + " Keep it less than 100 words")
        String describeProduct(@V("product") String product,
                               @V("price") double price,
                               @V("feature") String feature);
    }

    interface TemplateWithSingleParam {
        @UserMessage("Summarize this text in one sentence: {{it}}")
        String summarize(String text);
    }

    public static void main(String[] args) {
        OllamaChatModel model = OllamaChatModel.builder()
                                               .baseUrl("http://localhost:11434")
                                               .modelName("phi3:mini-128k")
                                               .temperature(0.7)
                                               .numCtx(4096)
                                               .timeout(Duration.of(5, ChronoUnit.MINUTES))
                                               .build();

        EmailGenerator emailGen = AiServices.create(EmailGenerator.class, model);
        ProductDescriber productDesc = AiServices.create(ProductDescriber.class, model);
        TemplateWithSingleParam summarizer = AiServices.create(TemplateWithSingleParam.class, model);

        System.out.println("Generated Email:");
        String email = emailGen.generateEmail("John Smith", "project deadline");
        System.out.println(email);

        System.out.println("\nProduct Description:");
        String description = productDesc.describeProduct("smartphone",
                                                         799.99, "camera quality");
        System.out.println(description);

        System.out.println("\nText Summary:");
        String summary = summarizer.summarize(
                "Artificial Intelligence is transforming industries "
                        + "by automating complex tasks and providing "
                        + "insights from large datasets.");
        System.out.println(summary);
    }
}

Output

Generated Email:

Dear John,


I hope this message finds you well. I wanted to remind you that the final report for our market analysis project is due next Friday at 5 PM sharp as per our agreed timeline with management. Please ensure all sections are completed and reviewed by then so we can proceed without delay. Let me know if additional support or resources are needed ahead of time.


Best regards,

[Your Name]

Product Description:
Experience the pinnacle of photography with our latest flagship, priced competitively at just under $800! With an unparalleled triple-lens system featuring a 64MP primary sensor for crystal clear resolutions and superior low light performance. Capture life's moments in stunning clarity as you explore the vibrant colors with our Full HD+ display that ensures every photo is nothing short of spectaccurate artwork on your desk or social media feed, all while enjoying an immersive 120Hz refresh rate for a buttery-smooth scrolling experience.

Text Summary:
Artificial intelligence revolutionizes various sectors through task automation and data-driven decision-making processes, enhancing efficiency and innovation across industries.

Conclusion

The output demonstrates how prompt templates dynamically incorporate method parameters into structured prompts. The AI Service proxy automatically replaces template variables with actual values before sending the prompt to the LLM. This approach keeps your code clean and separates prompt logic from business logic, making prompts easier to maintain and optimize.

Example Project

Dependencies and Technologies Used:

  • langchain4j 1.10.0 (Build LLM-powered applications in Java: chatbots, agents, RAG, and much more)
  • langchain4j-ollama 1.10.0 (LangChain4j :: Integration :: Ollama)
  • slf4j-simple 2.0.9 (SLF4J Simple Provider)
  • JDK 17
  • Maven 3.9.11

AI LangChain4j - Prompt Templates with AI Services Select All Download
  • ai-services-prompt-templates
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • PromptTemplatesExample.java

    See Also

    Join