public final class PromptTemplate

  1. Object
  2. PromptTemplate

Trivial {placeholder} substitution. Designed for the common “build a prompt from a handful of fields” pattern without pulling in a templating library. For anything more sophisticated (loops, conditionals) just compose strings directly.

String prompt = PromptTemplate.of(
    "You are an expert {role}. Reply in {style}."
).put("role", "tax accountant").put("style", "bullet points").build();

Methods

public static PromptTemplate of(String template)Starts a template instance.
public PromptTemplate put(String key, String value)Binds one placeholder.
public PromptTemplate putAll(Map<String, String> map)Adds all supplied placeholder values.
public String build()Renders the final string.
public ChatMessage asUser()Convenience: render and wrap as a ChatMessage with USER role.
public ChatMessage asSystem()Convenience: render and wrap as a ChatMessage with SYSTEM role.

Inherited methods

Method details

of

public static PromptTemplate of(String template)
Starts a template instance.

Parameters

template String
text containing optional {name} placeholders

Returns

mutable template values bound to the supplied text

put

public PromptTemplate put(String key, String value)
Binds one placeholder. A null value renders as an empty string.

Parameters

key String
placeholder name without braces
value String
replacement value

Returns

this template

putAll

public PromptTemplate putAll(Map<String, String> map)
Adds all supplied placeholder values.

Parameters

map Map<String, String>
replacements keyed by placeholder name; null is ignored

Returns

this template

build

public String build()
Renders the final string. Unknown placeholders are left intact ({like_this}) so they’re easy to spot in test output – silently dropping them tends to hide bugs.

Returns

rendered prompt text

asUser

public ChatMessage asUser()
Convenience: render and wrap as a ChatMessage with USER role.

Returns

rendered user message

asSystem

public ChatMessage asSystem()
Convenience: render and wrap as a ChatMessage with SYSTEM role.

Returns

rendered system message