Prompt Template Class

The PromptTemplate class is part of the ai.knowly.langtoch.prompt.template package. It represents a string template with variables that can be replaced with specified values. The template uses the format {{$var}} for variables, where variables must be one or more word characters (letters, digits, or underscores).

Usage

To create an instance of the PromptTemplate, use the builder() method to create a new Builder object. Use the builder's methods to set the template, examples, example header, and variables as needed, then call build() to create the PromptTemplate.

PromptTemplate promptTemplate = PromptTemplate.builder()
    .setTemplate("Hello, {{$name}}!")
    .addVariableValuePair("name", "John")
    .build();

Public Methods

format()

The format() method replaces the variables in the template with their corresponding values and returns the formatted string. If an optional example header and examples are provided, they are included in the formatted string.

String formattedTemplate = promptTemplate.format(); // "Hello, John!"

Builder

The Builder class is an inner class of PromptTemplate. It is used to create instances of the PromptTemplate class. The following methods are available:

setTemplate(String template)

Sets the template string containing variables in the form of {{$var}}.

setExamples(List<String> examples)

Sets a list of strings to be used as examples for few-shot prompting.

setExampleHeader(String exampleHeader)

Sets an optional string that can be used to describe the examples.

addVariableValuePair(String variableName, String value)

Adds a variable name-value pair to the variables map.

addAllVariableValuePairs(Map<String, String> variables)

Adds all variable name-value pairs from the provided map to the variables map.

build()

Creates and returns a new instance of the PromptTemplate class with the specified values.

Last updated