Semantic Kernel Python SDK - comprehensive AI development framework for building AI agents and multi-agent systems
—
Template systems for dynamic prompt generation supporting Handlebars, Jinja2, and native kernel template formats. Enables parameterized prompts with variable substitution and complex templating logic.
Multiple template formats for different use cases and preferences.
class KernelPromptTemplate:
"""
Native Semantic Kernel prompt template engine.
"""
def __init__(
self,
template: str,
config: PromptTemplateConfig | None = None,
template_format: str = "semantic-kernel"
):
"""
Initialize kernel prompt template.
Parameters:
- template: Template string with {{$variable}} syntax
- config: Template configuration
- template_format: Template format identifier
"""
async def render(
self,
kernel: Kernel,
arguments: KernelArguments | None = None
) -> str:
"""
Render the template with provided arguments.
Parameters:
- kernel: Kernel instance for context
- arguments: Arguments for variable substitution
Returns:
Rendered template string
"""
class HandlebarsPromptTemplate:
"""
Handlebars template engine for prompt generation.
"""
def __init__(self, template: str):
"""
Initialize Handlebars prompt template.
Parameters:
- template: Handlebars template string
"""
async def render(
self,
kernel: Kernel,
arguments: KernelArguments | None = None
) -> str:
"""
Render Handlebars template.
Parameters:
- kernel: Kernel instance for context
- arguments: Arguments for template variables
Returns:
Rendered template string
"""
class Jinja2PromptTemplate:
"""
Jinja2 template engine for prompt generation.
"""
def __init__(self, template: str):
"""
Initialize Jinja2 prompt template.
Parameters:
- template: Jinja2 template string
"""
async def render(
self,
kernel: Kernel,
arguments: KernelArguments | None = None
) -> str:
"""
Render Jinja2 template.
Parameters:
- kernel: Kernel instance for context
- arguments: Arguments for template variables
Returns:
Rendered template string
"""Configuration classes for template behavior and input variables.
class PromptTemplateConfig:
"""
Configuration for prompt templates.
"""
def __init__(
self,
template: str | None = None,
name: str | None = None,
description: str | None = None,
template_format: str = "semantic-kernel",
input_variables: list[InputVariable] | None = None,
execution_settings: dict[str, PromptExecutionSettings] | None = None
):
"""
Initialize prompt template configuration.
Parameters:
- template: Template string
- name: Template name
- description: Template description
- template_format: Template format (semantic-kernel, handlebars, jinja2)
- input_variables: List of input variable definitions
- execution_settings: Execution settings by service ID
"""
@property
def template(self) -> str | None:
"""Get the template string."""
@property
def name(self) -> str | None:
"""Get the template name."""
@property
def description(self) -> str | None:
"""Get the template description."""
@property
def template_format(self) -> str:
"""Get the template format."""
@property
def input_variables(self) -> list[InputVariable]:
"""Get the input variables."""
@property
def execution_settings(self) -> dict[str, PromptExecutionSettings]:
"""Get the execution settings."""
class InputVariable:
"""
Definition for a template input variable.
"""
def __init__(
self,
name: str,
description: str | None = None,
default: str | None = None,
is_required: bool = False,
json_schema: dict | None = None
):
"""
Initialize input variable definition.
Parameters:
- name: Variable name
- description: Variable description
- default: Default value
- is_required: Whether variable is required
- json_schema: JSON schema for validation
"""
@property
def name(self) -> str:
"""Get the variable name."""
@property
def description(self) -> str | None:
"""Get the variable description."""
@property
def default(self) -> str | None:
"""Get the default value."""
@property
def is_required(self) -> bool:
"""Check if variable is required."""from semantic_kernel.prompt_template import KernelPromptTemplate, PromptTemplateConfig
from semantic_kernel.functions import KernelArguments
# Create a simple template
template = KernelPromptTemplate(
template="Hello {{$name}}, welcome to {{$platform}}!",
config=PromptTemplateConfig(
name="greeting_template",
description="A greeting template"
)
)
# Render the template
arguments = KernelArguments(name="Alice", platform="Semantic Kernel")
rendered = await template.render(kernel, arguments)
print(rendered) # "Hello Alice, welcome to Semantic Kernel!"from semantic_kernel.prompt_template import HandlebarsPromptTemplate, Jinja2PromptTemplate
# Handlebars template
handlebars_template = HandlebarsPromptTemplate("""
{{#if user_name}}
Hello {{user_name}}!
{{else}}
Hello there!
{{/if}}
Please help me with: {{request}}
{{#each items}}
- {{this}}
{{/each}}
""")
# Jinja2 template
jinja2_template = Jinja2PromptTemplate("""
{% if user_name %}
Hello {{ user_name }}!
{% else %}
Hello there!
{% endif %}
Please help me with: {{ request }}
{% for item in items %}
- {{ item }}
{% endfor %}
""")
# Use templates
arguments = KernelArguments(
user_name="Bob",
request="code review",
items=["check syntax", "improve performance", "add comments"]
)
handlebars_result = await handlebars_template.render(kernel, arguments)
jinja2_result = await jinja2_template.render(kernel, arguments)Install with Tessl CLI
npx tessl i tessl/pypi-semantic-kernel