Class PromptTemplate

java.lang.Object
dev.langchain4j.model.input.PromptTemplate

public class PromptTemplate extends Object
Represents a template of a prompt that can be reused multiple times. A template typically contains one or more variables (placeholders) defined as {{variable_name}} that are replaced with actual values to produce a Prompt. Special variables {{current_date}}, {{current_time}}, and {{current_date_time}} are automatically filled with LocalDate.now(), LocalTime.now(), and LocalDateTime.now() respectively.
  • Constructor Details

    • PromptTemplate

      public PromptTemplate(String template)
      Create a new PromptTemplate.

      The Clock will be the system clock.

      Parameters:
      template - the template string of the prompt.
  • Method Details

    • template

      public String template()
      Returns:
      A prompt template string.
    • apply

      public Prompt apply(Object value)
      Applies a value to a template containing a single variable. The single variable should have the name {{it}}.
      Parameters:
      value - The value that will be injected in place of the {{it}} placeholder in the template.
      Returns:
      A Prompt object where the {{it}} placeholder in the template has been replaced by the provided value.
    • apply

      public Prompt apply(Map<String,Object> variables)
      Applies multiple values to a template containing multiple variables.
      Parameters:
      variables - A map of variable names to values that will be injected in place of the corresponding placeholders in the template.
      Returns:
      A Prompt object where the placeholders in the template have been replaced by the provided values.
    • from

      public static PromptTemplate from(String template)
      Create a new PromptTemplate.
      Parameters:
      template - the template string of the prompt.
      Returns:
      the PromptTemplate.