Prompt builder
Defines a prompt builder class used in Gen AI applications.
References
NONE
BasePromptBuilder(system_template='', user_template='', ignore_extra_keys=False)
Bases: ABC
A base class for prompt builders 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. |
ignore_extra_keys |
bool
|
Whether to ignore extra keys when formatting the prompt. |
Initializes a new instance of the BasePromptBuilder class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
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 |
''
|
ignore_extra_keys |
bool
|
Whether to ignore extra keys when formatting the prompt. Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If both |
compatible_model_list: list[str]
property
Returns the list of compatible models for the prompt builder.
This property returns the set of models that the prompt builder is compatible with. If the builder is
model-specific, it returns the list of models in _compatible_model_list
. Otherwise, it returns a list
containing "All"
to indicate compatibility with all models.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of compatible model names, or |
format_as_message_list(history=None, attachments=None, system_multimodal_contents=None, user_multimodal_contents=None, is_multimodal=None, **kwargs)
Formats the prompt templates as a list of message tuples (role, formatted content).
This method processes each prompt template, replacing the placeholders in the template content with the
corresponding values from kwargs
. If a required key is missing from kwargs
, it raises a ValueError
. It
returns a list of tuples, where each tuple consists of a role and the corresponding formatted prompt content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
history |
list[tuple[PromptRole, list[Any] | str]] | None
|
The optional chat history to be included in the prompt. Defaults to None. |
None
|
attachments |
list[Attachment] | None
|
The optional attachments to be included in the prompt. Defaults to None. |
None
|
system_multimodal_contents |
list[Any] | None
|
Deprecated parameter to handle attachments. Will be removed in v0.5.0. Defaults to None. |
None
|
user_multimodal_contents |
list[Any] | None
|
Deprecated parameter to handle attachments. Will be removed in v0.5.0. Defaults to None. |
None
|
is_multimodal |
bool | None
|
Whether the prompt supports multimodal inputs. Will be deprecated in v0.5.0, in which multimodality will always be True. Defaults to None. |
None
|
**kwargs |
Any
|
A dictionary of placeholder values to be injected into the prompt templates. |
{}
|
Returns:
Type | Description |
---|---|
list[tuple[PromptRole, list[Any] | str]]
|
list[tuple[PromptRole, list[Any] | str]]: A list of tuples, each containing a role and the corresponding formatted prompt content. |
Raises:
Type | Description |
---|---|
ValueError
|
If a required key for the prompt template is missing from |
ValueError
|
If multimodal contents are provided when |
format_as_string(history=None, **kwargs)
Formats the prompt as a string.
This method formats the prompt as a string by first converting the prompt templates to a list of messages and then formatting the message list as a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
history |
list[tuple[PromptRole, str]] | None
|
The optional chat history to be included in the prompt. Defaults to None. |
None
|
**kwargs |
Any
|
A dictionary of placeholder values to be injected into the prompt templates. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The formatted prompt with the placeholders replaced by the provided values. |
Raises:
Type | Description |
---|---|
ValueError
|
If any required key is missing or there are extra keys in the kwargs. |
PromptBuilder(system_template='', user_template='', ignore_extra_keys=False)
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. |
ignore_extra_keys |
bool
|
Whether to ignore extra keys when 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 |
''
|
user_template |
str
|
The user prompt template. May contain placeholders enclosed in curly
braces |
''
|
ignore_extra_keys |
bool
|
Whether to ignore extra keys when formatting the prompt. Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If both |
format(history=None, extra_contents=None, attachments=None, **kwargs)
Formats the prompt templates into a MultimodalPrompt
.
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 MultimodalPrompt
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
history |
MultimodalPrompt | None
|
The optional history to be included in the prompt. Defaults to None. |
None
|
extra_contents |
list[MultimodalContent] | None
|
The optional extra contents to be included in the user message. Defaults to None. |
None
|
attachments |
list[Attachment] | None
|
Deprecated parameter to handle attachments. Will be removed in v0.5.0. 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:
Name | Type | Description |
---|---|---|
MultimodalPrompt |
MultimodalPrompt
|
A multimodal prompt. |
Raises:
Type | Description |
---|---|
ValueError
|
If a required key for the prompt template is missing from |