Jinja2PromptTemplate Class
Creates and renders Jinja2 prompt templates to text.
Jinja2 templates support advanced features such as variable substitution, control structures, and inheritance, making it possible to dynamically generate text based on input arguments and predefined functions. This class leverages Jinja2's flexibility to render prompts that can include conditional logic, loops, and functions, based on the provided template configuration and arguments.
Note that the fully qualified function name (in the form of "plugin-function") is not allowed in Jinja2 because of the hyphen. Therefore, the function name is replaced with an underscore, which are allowed in Python function names.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Inheritance
-
Jinja2PromptTemplate
Constructor
Jinja2PromptTemplate(*, prompt_template_config: PromptTemplateConfig, allow_dangerously_set_content: bool = False)
Parameters
Name | Description |
---|---|
prompt_template_config
Required
|
<xref:semantic_kernel.prompt_template.jinja2_prompt_template.PromptTemplateConfig>
The configuration object for the prompt template. This should specify the template format as 'jinja2' and include any necessary configuration details required for rendering the template. |
allow_dangerously_set_content
Required
|
<xref:<xref:semantic_kernel.prompt_template.jinja2_prompt_template.bool = False>>
Allow content without encoding throughout, this overrides the same settings in the prompt template config and input variables. This reverts the behavior to unencoded input. |
Keyword-Only Parameters
Name | Description |
---|---|
prompt_template_config
Required
|
|
allow_dangerously_set_content
Required
|
|
Methods
model_post_init |
We need to both initialize private attributes and call the user-defined model_post_init method. |
render |
Render the prompt template. Using the prompt template, replace the variables with their values and execute the functions replacing their reference with the function result. |
validate_template_format |
Validate the template format. |
model_post_init
We need to both initialize private attributes and call the user-defined model_post_init method.
model_post_init(context: Any, /) -> None
Positional-Only Parameters
Name | Description |
---|---|
context
Required
|
|
render
Render the prompt template.
Using the prompt template, replace the variables with their values and execute the functions replacing their reference with the function result.
async render(kernel: Kernel, arguments: KernelArguments | None = None) -> str
Positional-Only Parameters
Name | Description |
---|---|
context
Required
|
|
Parameters
Name | Description |
---|---|
kernel
Required
|
The kernel instance |
arguments
|
The kernel arguments Default value: None
|
Returns
Type | Description |
---|---|
The prompt template ready to be used for an AI request |
validate_template_format
Validate the template format.
validate_template_format(v: PromptTemplateConfig) -> PromptTemplateConfig
Positional-Only Parameters
Name | Description |
---|---|
context
Required
|
|
Parameters
Name | Description |
---|---|
v
Required
|
|
Attributes
model_computed_fields
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}
model_config
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'validate_assignment': True}
model_fields
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.fields from Pydantic V1.
model_fields: ClassVar[Dict[str, FieldInfo]] = {'allow_dangerously_set_content': FieldInfo(annotation=bool, required=False, default=False), 'prompt_template_config': FieldInfo(annotation=PromptTemplateConfig, required=True)}
allow_dangerously_set_content
allow_dangerously_set_content: bool
prompt_template_config
prompt_template_config: PromptTemplateConfig