Skip to content

Prompt Builder

Modules concerning the prompt builders used in Gen AI applications.

PromptBuilder(system_template='', user_template='', key_defaults=None, ignore_extra_keys=None, use_jinja=False, jinja_env=JinjaEnvType.RESTRICTED)

A prompt builder class used in Gen AI applications.

Attributes:

Name Type Description
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}.

user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}.

prompt_key_set set[str]

A set of expected keys that must be present in the prompt templates.

key_defaults dict[str, str]

Default values for the keys in the prompt templates.

strategy BasePromptFormattingStrategy

The format strategy to be used for formatting the prompt.

Initializes a new instance of the PromptBuilder class.

Parameters:

Name Type Description Default
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
key_defaults dict[str, str] | None

Default values for the keys in the prompt templates. Applied when the corresponding keys are not provided in the runtime input. Defaults to None, in which case no default values will be assigned to the keys.

None
ignore_extra_keys bool | None

Deprecated parameter. Will be removed in v0.6. Extra keys will always raise a warning only instead of raising an error.

None
use_jinja bool

Whether to use Jinja for rendering the prompt templates. Defaults to False.

False
jinja_env JinjaEnvType | SandboxedEnvironment

The environment for Jinja rendering. It can be one of the following: 1. JinjaEnvType.RESTRICTED: Uses a minimal, restricted Jinja environment. Safest for most cases. 2. JinjaEnvType.JINJA_DEFAULT: Uses the full Jinja environment. Allows more powerful templating, but with fewer safety restrictions. 3. SandboxedEnvironment instance: A custom Jinja SandboxedEnvironment object provided by the user. Offers fine-grained control over template execution. Defaults to JinjaEnvType.RESTRICTED

RESTRICTED

Raises:

Type Description
ValueError

If both system_template and user_template are empty.

format(history=None, extra_contents=None, **kwargs)

Formats the prompt templates into a list of messages.

This method processes each prompt template, replacing the placeholders in the template content with the corresponding values from kwargs. If any required key is missing from kwargs, it raises a ValueError. It also handles the provided history and extra contents. It formats the prompt as a list of messages.

Parameters:

Name Type Description Default
history list[Message] | None

The history to be included in the prompt. Defaults to None.

None
extra_contents list[MessageContent] | None

The extra contents to be included in the user message. Defaults to None.

None
**kwargs Any

A dictionary of placeholder values to be injected into the prompt templates. Values must be either a string or an object that can be serialized to a string.

{}

Returns:

Type Description
list[Message]

list[Message]: A list of formatted messages.

Raises:

Type Description
ValueError

If a required key for the prompt template is missing from kwargs.