Commit 18d9a8e
authored
Python: .Net: Updated encoding logic in prompt templates (#12983)
### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
1. Why is this change required?
2. What problem does it solve?
3. What scenario does it contribute to?
4. If it fixes an open issue, please link to the issue here.
-->
Resolves: #11821
Today, the encoding of template arguments is performed only if argument
type is `string`. In case of custom type, anonymous type or collection -
the encoding is not performed.
This PR contains changes to throw an exception in case if encoding is
enabled but complex type is used. In case of complex type, the encoding
should be performed manually according to business logic and automatic
encoding should be explicitly disabled.
This enforces stricter, but more secure template rendering rules.
**Note**: this is a breaking change for customers who use Handlebars or
Liquid template with complex type arguments. Code changes are required
when initializing template arguments:
```diff
var arguments = new KernelArguments()
{
{ "customer", new
{
- firstName = userInput.FirstName,
- lastName = userInput.LastName,
+ firstName = HttpUtility.HtmlEncode(userInput.FirstName),
+ lastName = HttpUtility.HtmlEncode(userInput.LastName),
}
}
};
var templateFactory = new LiquidPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
TemplateFormat = "liquid"
+ InputVariables = new()
+ {
+ // We set AllowDangerouslySetContent to 'true' because each property of this argument is encoded manually.
+ new() { Name = "customer", AllowDangerouslySetContent = true },
+ }
};
var promptTemplate = templateFactory.Create(promptTemplateConfig);
var renderedPrompt = await promptTemplate.RenderAsync(kernel, arguments);
```
### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄1 parent 2a47ba9 commit 18d9a8e
File tree
45 files changed
+895
-159
lines changed- dotnet
- samples/Concepts/PromptTemplates
- src
- Extensions
- Extensions.UnitTests/PromptTemplates/Handlebars
- Helpers
- PromptTemplates.Handlebars
- PromptTemplates.Liquid.UnitTests
- PromptTemplates.Liquid
- IntegrationTests/CrossLanguage
- SemanticKernel.Core
- Functions
- python
- samples
- concepts
- auto_function_calling
- chat_completion
- filtering
- resources/chat
- grounding
- images
- prompt_templates
- resources
- function_choice_json/ChatBot
- function_choice_yaml
- service_selector
- demos/mcp_server
- getting_started
- third_party
- learn_resources
- semantic_kernel
- agents/orchestration
- functions
- prompt_template
- tests
- integration/cross_language
- unit
- contents
- prompt_template
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
45 files changed
+895
-159
lines changedLines changed: 27 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| |||
67 | 69 | | |
68 | 70 | | |
69 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
70 | 80 | | |
71 | 81 | | |
72 | 82 | | |
| |||
93 | 103 | | |
94 | 104 | | |
95 | 105 | | |
96 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
97 | 114 | | |
98 | 115 | | |
99 | 116 | | |
| 117 | + | |
100 | 118 | | |
101 | 119 | | |
102 | 120 | | |
103 | 121 | | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
108 | 126 | | |
109 | 127 | | |
110 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
50 | | - | |
51 | | - | |
| 52 | + | |
| 53 | + | |
52 | 54 | | |
53 | | - | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| |||
67 | 69 | | |
68 | 70 | | |
69 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
70 | 80 | | |
71 | 81 | | |
72 | 82 | | |
| |||
93 | 103 | | |
94 | 104 | | |
95 | 105 | | |
96 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
97 | 114 | | |
98 | 115 | | |
99 | 116 | | |
| 117 | + | |
100 | 118 | | |
101 | 119 | | |
102 | 120 | | |
103 | 121 | | |
104 | | - | |
105 | | - | |
| 122 | + | |
| 123 | + | |
106 | 124 | | |
107 | | - | |
| 125 | + | |
108 | 126 | | |
109 | 127 | | |
110 | 128 | | |
| |||
Lines changed: 6 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
15 | | - | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
Lines changed: 34 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
121 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
122 | 127 | | |
123 | 128 | | |
124 | 129 | | |
| |||
389 | 394 | | |
390 | 395 | | |
391 | 396 | | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
392 | 424 | | |
393 | 425 | | |
394 | 426 | | |
| |||
Lines changed: 26 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| 64 | + | |
| 65 | + | |
63 | 66 | | |
64 | | - | |
| 67 | + | |
65 | 68 | | |
66 | 69 | | |
67 | 70 | | |
| |||
90 | 93 | | |
91 | 94 | | |
92 | 95 | | |
| 96 | + | |
| 97 | + | |
93 | 98 | | |
94 | | - | |
| 99 | + | |
95 | 100 | | |
96 | 101 | | |
97 | 102 | | |
| |||
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| 115 | + | |
| 116 | + | |
110 | 117 | | |
111 | | - | |
| 118 | + | |
112 | 119 | | |
113 | 120 | | |
114 | 121 | | |
| |||
124 | 131 | | |
125 | 132 | | |
126 | 133 | | |
127 | | - | |
128 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
129 | 138 | | |
130 | 139 | | |
131 | 140 | | |
| |||
155 | 164 | | |
156 | 165 | | |
157 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
158 | 173 | | |
159 | | - | |
| 174 | + | |
160 | 175 | | |
161 | 176 | | |
162 | 177 | | |
| |||
283 | 298 | | |
284 | 299 | | |
285 | 300 | | |
286 | | - | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
287 | 305 | | |
288 | | - | |
| 306 | + | |
289 | 307 | | |
290 | 308 | | |
291 | 309 | | |
| |||
0 commit comments