prompt_template
This module provides utilities for managing and rendering string templates that use double braces {{}}
to denote placeholders for variables. It includes a PromptTemplate
class for creating, managing,
and rendering these templates, along with functions to check if a template is fully filled and to
perform the rendering.
Templates are strings that use double curly braces to enclose variable names, e.g., {{variable}}
,
which can be dynamically replaced with actual values.
Classes:
Name | Description |
---|---|
PromptTemplate |
Encapsulates template management and rendering. |
Raises:
Type | Description |
---|---|
PromptTemplateError
|
Custom exception for handling template errors. |
Functions:
Name | Description |
---|---|
render_prompt |
str, **variables) -> str: Renders a template with variables. |
is_prompt_fully_filled |
str) -> bool: Checks if a template has unfilled placeholders. |
find_unfilled_variables |
str) -> list[str]: Finds and returns the list of unfilled variable names in a template string. |
PromptTemplate
A class for managing and rendering templates with placeholders.
These templates are strings that use double curly braces {{}}
to denote variable names,
which are intended to be replaced by actual values when the template is rendered.
Example of a template: "Hello, {{ name }}!"
Attributes:
Name | Type | Description |
---|---|---|
prompt_template |
str
|
The current state of the template string, initially set with placeholders. |
prompt |
str
|
Returns the fully filled template if all placeholders have been replaced, otherwise raises PromptTemplateError. |
unfilled_variables |
list[str]
|
Returns the list of unfilled variable names. |
Methods:
Name | Description |
---|---|
load_template |
str) -> PromptTemplate: Class method to create a new instance with the specified template. |
render |
Renders the template with provided variables and updates the instance.
This method replaces placeholders in the format |
prompt |
Returns the fully filled template or raises an error if unfilled placeholders remain. This method is used to retrieve the final rendered string if it is fully filled. |
is_fully_filled |
Checks if the template has any unfilled placeholders. This method determines whether all placeholders have been replaced. |
Source code in src/agere/utils/prompt_template.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
prompt: str
property
Returns the fully filled template if all placeholders have been replaced, otherwise raises an PromptTemplateError.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The fully filled template string. |
Raises:
Type | Description |
---|---|
PromptTemplateError
|
If the template contains unfilled placeholders. |
unfilled_variables: list[str]
property
Returns the list of unfilled variable names.
Returns:
Name | Type | Description |
---|---|---|
list |
list[str]
|
The list of unfilled variable names. |
__init__(prompt_template)
Initializes a new instance of PromptTemplate with a prompt template string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_template |
str
|
The initial template string with placeholders. |
required |
Source code in src/agere/utils/prompt_template.py
__str__()
Returns the string representation of the current template, which may or may not be fully filled.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The current state of the template string. |
is_fully_filled()
Checks if the template has any unfilled placeholders remaining.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if no placeholders remain unfilled, False otherwise. |
Source code in src/agere/utils/prompt_template.py
load_template(prompt_template)
classmethod
Class method to create a new instance of PromptTemplate from a given template string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_template |
str
|
The template string to initialize the template. |
required |
Returns:
Name | Type | Description |
---|---|---|
PromptTemplate |
PromptTemplate
|
A new instance of PromptTemplate initialized with the specified template. |
Source code in src/agere/utils/prompt_template.py
render(**variables)
Renders the template with provided variables and updates the instance. This method replaces
placeholders in the format {{ variable }}
with values from variables
. It allows for chainable
method calls, enabling a fluent interface pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**variables |
Arbitrary keyword arguments where the key is the placeholder variable name and the value is the replacement value. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
PromptTemplate |
Self
|
The instance itself with updated template, allowing for method chaining. |
Example of usage
template = PromptTemplate("Hello, {{ name }}! Today is {{ day }}.") template.render(name="Alice").render(day="Wednesday")
Source code in src/agere/utils/prompt_template.py
PromptTemplateError
find_unfilled_variables(prompt_template)
Finds and returns the list of unfilled variable names in a template string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_template |
str
|
The template string to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list[str]
|
The list of unfilled variable names. |
Source code in src/agere/utils/prompt_template.py
is_prompt_fully_filled(prompt)
Checks if a template string contains any unfilled placeholders.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
The template string to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if no unfilled placeholders are found, False otherwise. |
Source code in src/agere/utils/prompt_template.py
render_prompt(prompt_template, **variables)
Renders a template string by replacing placeholders formatted as {{ variable }}
with values provided in the variables keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_template |
str
|
The template string containing placeholders. |
required |
**variables |
Arbitrary keyword arguments where the key is the placeholder variable name and the value is the replacement value. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The template string with placeholders replaced by actual values. |