Compartilhar via


KernelPromptTemplate Class

Create a Kernel prompt template.

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
KernelPromptTemplate

Constructor

KernelPromptTemplate(*, prompt_template_config: PromptTemplateConfig, allow_dangerously_set_content: bool = False)

Keyword-Only Parameters

Name Description
prompt_template_config
Required
allow_dangerously_set_content
Required

Methods

extract_blocks

Given the prompt template, extract all the blocks (text, variables, function calls).

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.

render_blocks

Given a list of blocks render each block and compose the final result.

validate_template_format

Validate the template format.

extract_blocks

Given the prompt template, extract all the blocks (text, variables, function calls).

extract_blocks() -> list[Block]

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

Parameters

Name Description
kernel
Required
<xref:<xref:semantic_kernel.prompt_template.kernel_prompt_template."Kernel">>

The kernel to use for functions.

arguments
<xref:<xref:semantic_kernel.prompt_template.kernel_prompt_template."KernelArguments | None">>

The arguments to use for rendering. (Default value = None)

Default value: None

Returns

Type Description
str

The prompt template ready to be used for an AI request

render_blocks

Given a list of blocks render each block and compose the final result.

async render_blocks(blocks: list[Block], kernel: Kernel, arguments: KernelArguments | None = None) -> str

Parameters

Name Description
blocks
Required
list[<xref:Block>]

Template blocks generated by ExtractBlocks

kernel
Required
<xref:<xref:semantic_kernel.prompt_template.kernel_prompt_template."Kernel">>

The kernel to use for functions

arguments
<xref:<xref:semantic_kernel.prompt_template.kernel_prompt_template."KernelArguments | None">>

The arguments to use for rendering (Default value = None)

Default value: None

Returns

Type Description
str

The prompt template ready to be used for an AI request

validate_template_format

Validate the template format.

validate_template_format(v: PromptTemplateConfig) -> PromptTemplateConfig

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