-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Handlebars Prompt Template Helpers (#4017)
### 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. --> This PR extends the Handlebars Template Factory with built-in and custom helpers to provide a consistent and convenient way for prompt and planner engineers to write Handlebars prompt templates. These helpers bake in functionality for: - Marking a block of text as a message with a role for chat completion connectors. - Invoking functions from the kernel and passing parameters to them. - Setting and getting variables in the template context. - Performing common operations such as concatenation, arithmetic, comparison, and JSON serialization. - Supporting different output types and formats for the rendered template. ADR here: https://github.com/microsoft/semantic-kernel/pull/3600/files ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Effectively, there are four buckets of helpers enabled in the Handlebars Template Engine: 1. Default helpers from the Handlebars library, including: - [Built-in helpers](https://handlebarsjs.com/guide/builtin-helpers.html) that enable loops and conditions (#if, #each, #with, #unless) - [Handlebars.Net.Helpers](https://github.com/Handlebars-Net/Handlebars.Net.Helpers/wiki) 2. Functions in the kernel 3. Helpers helpful to prompt engineers (i.e., message, or) 4. Utility helpers that can be used to perform simple logic or transformations on the template data or arguments (i.e., set, get, json, concat, equals, range, array) When importing kernel functions as helpers, parameters are checked both positionally and as hash arguments to ensure expected types match and all required parameters are present. This PR also refactors the Handlebars Planner to leverage the new Handlebars Prompt Template Factory. Some follow-up (marked with TODOs) - Isolate Handlebars Kernel System helpers in their own class (Issue #3947) - Support override of default options - Add Categories filter for KernelSystemHelpers (i.e., KernelHelperCategories) - Add support for complex types (in parameter type checks) ### 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 - [x] I didn't break anyone 😄 --------- Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
- Loading branch information
1 parent
81be8a6
commit 46f3dbc
Showing
43 changed files
with
1,567 additions
and
1,049 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 0 additions & 118 deletions
118
...xtensions/Extensions.UnitTests/PromptTemplate/Handlebars/HandlebarsPromptTemplateTests.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ions/Extensions.UnitTests/PromptTemplates/Handlebars/HandlebarsPromptTemplateTestUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.PromptTemplates.Handlebars; | ||
|
||
namespace Extensions.UnitTests.PromptTemplates.Handlebars; | ||
|
||
internal static class TestUtilities | ||
{ | ||
public static PromptTemplateConfig InitializeHbPromptConfig(string template) | ||
{ | ||
return new PromptTemplateConfig() | ||
{ | ||
TemplateFormat = HandlebarsPromptTemplateFactory.HandlebarsTemplateFormat, | ||
Template = template | ||
}; | ||
} | ||
} |
Oops, something went wrong.