π Prompt Template
Language models process textual data, which is commonly known as a prompt. This text is usually not a fixed string but a blend of templates, examples, and user inputs.
PromptTemplate Class
PromptTemplate
is a class representing a template with variables where variables are defined using double curly braces, e.g., {{$variable}}
. The class provides methods to replace variables with values and validate the template.
Creating a PromptTemplate instance:
Use the builder() method to create a new PromptTemplate.Builder object. Set the template, examples, example header, and variables as needed, then call build() to create the PromptTemplate.
Example:
Working with LLM provider:
Straightforward way:
Now, we can make it more generic: to make a synonym function that returns the synonym of the input.
Here we introduce PromptTemplate to help you conveniently format the prompt.
Define the string template where you can define variables by making it a special pattern:
{{$variable}}
where the variable must be one or more word characters (letters, digits, or underscores).set variable value with
.setVariables(new HashMap<>(Map.of("variable", "some value"))).
Formatted prompt looks like this: What is the synonym of sad?
Last updated