or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

agents.mdai-connectors.mdcontent-types.mdcore-kernel.mdcore-plugins.mdfilters.mdindex.mdmemory-stores.mdprocesses.mdprompt-templates.md

prompt-templates.mddocs/

0

# Prompt Templates

1

2

Template systems for dynamic prompt generation supporting Handlebars, Jinja2, and native kernel template formats. Enables parameterized prompts with variable substitution and complex templating logic.

3

4

## Capabilities

5

6

### Template Engine Support

7

8

Multiple template formats for different use cases and preferences.

9

10

```python { .api }

11

class KernelPromptTemplate:

12

"""

13

Native Semantic Kernel prompt template engine.

14

"""

15

16

def __init__(

17

self,

18

template: str,

19

config: PromptTemplateConfig | None = None,

20

template_format: str = "semantic-kernel"

21

):

22

"""

23

Initialize kernel prompt template.

24

25

Parameters:

26

- template: Template string with {{$variable}} syntax

27

- config: Template configuration

28

- template_format: Template format identifier

29

"""

30

31

async def render(

32

self,

33

kernel: Kernel,

34

arguments: KernelArguments | None = None

35

) -> str:

36

"""

37

Render the template with provided arguments.

38

39

Parameters:

40

- kernel: Kernel instance for context

41

- arguments: Arguments for variable substitution

42

43

Returns:

44

Rendered template string

45

"""

46

47

class HandlebarsPromptTemplate:

48

"""

49

Handlebars template engine for prompt generation.

50

"""

51

52

def __init__(self, template: str):

53

"""

54

Initialize Handlebars prompt template.

55

56

Parameters:

57

- template: Handlebars template string

58

"""

59

60

async def render(

61

self,

62

kernel: Kernel,

63

arguments: KernelArguments | None = None

64

) -> str:

65

"""

66

Render Handlebars template.

67

68

Parameters:

69

- kernel: Kernel instance for context

70

- arguments: Arguments for template variables

71

72

Returns:

73

Rendered template string

74

"""

75

76

class Jinja2PromptTemplate:

77

"""

78

Jinja2 template engine for prompt generation.

79

"""

80

81

def __init__(self, template: str):

82

"""

83

Initialize Jinja2 prompt template.

84

85

Parameters:

86

- template: Jinja2 template string

87

"""

88

89

async def render(

90

self,

91

kernel: Kernel,

92

arguments: KernelArguments | None = None

93

) -> str:

94

"""

95

Render Jinja2 template.

96

97

Parameters:

98

- kernel: Kernel instance for context

99

- arguments: Arguments for template variables

100

101

Returns:

102

Rendered template string

103

"""

104

```

105

106

### Template Configuration

107

108

Configuration classes for template behavior and input variables.

109

110

```python { .api }

111

class PromptTemplateConfig:

112

"""

113

Configuration for prompt templates.

114

"""

115

116

def __init__(

117

self,

118

template: str | None = None,

119

name: str | None = None,

120

description: str | None = None,

121

template_format: str = "semantic-kernel",

122

input_variables: list[InputVariable] | None = None,

123

execution_settings: dict[str, PromptExecutionSettings] | None = None

124

):

125

"""

126

Initialize prompt template configuration.

127

128

Parameters:

129

- template: Template string

130

- name: Template name

131

- description: Template description

132

- template_format: Template format (semantic-kernel, handlebars, jinja2)

133

- input_variables: List of input variable definitions

134

- execution_settings: Execution settings by service ID

135

"""

136

137

@property

138

def template(self) -> str | None:

139

"""Get the template string."""

140

141

@property

142

def name(self) -> str | None:

143

"""Get the template name."""

144

145

@property

146

def description(self) -> str | None:

147

"""Get the template description."""

148

149

@property

150

def template_format(self) -> str:

151

"""Get the template format."""

152

153

@property

154

def input_variables(self) -> list[InputVariable]:

155

"""Get the input variables."""

156

157

@property

158

def execution_settings(self) -> dict[str, PromptExecutionSettings]:

159

"""Get the execution settings."""

160

161

class InputVariable:

162

"""

163

Definition for a template input variable.

164

"""

165

166

def __init__(

167

self,

168

name: str,

169

description: str | None = None,

170

default: str | None = None,

171

is_required: bool = False,

172

json_schema: dict | None = None

173

):

174

"""

175

Initialize input variable definition.

176

177

Parameters:

178

- name: Variable name

179

- description: Variable description

180

- default: Default value

181

- is_required: Whether variable is required

182

- json_schema: JSON schema for validation

183

"""

184

185

@property

186

def name(self) -> str:

187

"""Get the variable name."""

188

189

@property

190

def description(self) -> str | None:

191

"""Get the variable description."""

192

193

@property

194

def default(self) -> str | None:

195

"""Get the default value."""

196

197

@property

198

def is_required(self) -> bool:

199

"""Check if variable is required."""

200

```

201

202

## Usage Examples

203

204

### Basic Template Usage

205

206

```python

207

from semantic_kernel.prompt_template import KernelPromptTemplate, PromptTemplateConfig

208

from semantic_kernel.functions import KernelArguments

209

210

# Create a simple template

211

template = KernelPromptTemplate(

212

template="Hello {{$name}}, welcome to {{$platform}}!",

213

config=PromptTemplateConfig(

214

name="greeting_template",

215

description="A greeting template"

216

)

217

)

218

219

# Render the template

220

arguments = KernelArguments(name="Alice", platform="Semantic Kernel")

221

rendered = await template.render(kernel, arguments)

222

print(rendered) # "Hello Alice, welcome to Semantic Kernel!"

223

```

224

225

### Complex Template with Multiple Formats

226

227

```python

228

from semantic_kernel.prompt_template import HandlebarsPromptTemplate, Jinja2PromptTemplate

229

230

# Handlebars template

231

handlebars_template = HandlebarsPromptTemplate("""

232

{{#if user_name}}

233

Hello {{user_name}}!

234

{{else}}

235

Hello there!

236

{{/if}}

237

238

Please help me with: {{request}}

239

240

{{#each items}}

241

- {{this}}

242

{{/each}}

243

""")

244

245

# Jinja2 template

246

jinja2_template = Jinja2PromptTemplate("""

247

{% if user_name %}

248

Hello {{ user_name }}!

249

{% else %}

250

Hello there!

251

{% endif %}

252

253

Please help me with: {{ request }}

254

255

{% for item in items %}

256

- {{ item }}

257

{% endfor %}

258

""")

259

260

# Use templates

261

arguments = KernelArguments(

262

user_name="Bob",

263

request="code review",

264

items=["check syntax", "improve performance", "add comments"]

265

)

266

267

handlebars_result = await handlebars_template.render(kernel, arguments)

268

jinja2_result = await jinja2_template.render(kernel, arguments)

269

```