Skip to content

Commit 52e317a

Browse files
Fixing logic in custom or HB helper (microsoft#4804)
### 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. --> The custom `or` helper had a bug where it was checking for null values, rather than checking for non-null values. This PR changes the condition to `not null`. Also, renamed a heading in the CreatePlan prompt to better guide the model in identifying Handlebars built-in _block_ helpers. ### Description Additional changes to the `GetArgumentValue` method in `KernelHelperUtils.cs` to return null if variable isn't defined. Before, if the template referenced a variable that wasn't defined in `KernelArguments`, the method would just return the key as a string. Now, it just returns null (as expected). > I had it returning a string before because I thought Handlebars would default the argument to its string representation since it templates regular text, but I double checked [HB docs](https://handlebarsjs.com/guide/expressions.html#helpers-with-multiple-parameters), and generally, string literals are surrounded by quotes when a raw value is passed as an argument. Should resolve these issues: - microsoft#4661 - microsoft#4732 ### 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: Gina Triolo <[email protected]>
1 parent d9800fe commit 52e317a

4 files changed

Lines changed: 71 additions & 7 deletions

File tree

‎dotnet/src/Extensions/PromptTemplates.Handlebars/Helpers/KernelHelperUtils.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static void RegisterHelperSafe(IHandlebars handlebarsInstance, string h
4141
// Thus, we try to get the value from the kernel arguments dictionary.
4242
if (argument is UndefinedBindingResult result)
4343
{
44-
return kernelArguments.TryGetValue(result.Value, out var variable) ? variable : result.Value;
44+
return kernelArguments.TryGetValue(result.Value, out var variable) ? variable : null;
4545
}
4646

4747
return argument;

‎dotnet/src/Extensions/PromptTemplates.Handlebars/Helpers/KernelHelpers/KernelSystemHelpers.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static void RegisterSystemHelpers(
136136
return arg switch
137137
{
138138
bool booleanArg => booleanArg,
139-
_ => arg is null
139+
_ => arg is not null
140140
};
141141
});
142142
});

‎dotnet/src/IntegrationTests/Planners/Handlebars/HandlebarsPlannerTests.cs‎

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System;
4+
using System.ComponentModel;
45
using System.Threading.Tasks;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.SemanticKernel;
@@ -30,7 +31,7 @@ public HandlebarsPlannerTests(ITestOutputHelper output)
3031

3132
[Theory]
3233
[InlineData(true, "Write a joke and send it in an e-mail to Kai.", "SendEmail", "test")]
33-
public async Task CreatePlanFunctionFlowAsync(bool useChatModel, string prompt, string expectedFunction, string expectedPlugin)
34+
public async Task CreatePlanFunctionFlowAsync(bool useChatModel, string goal, string expectedFunction, string expectedPlugin)
3435
{
3536
// Arrange
3637
bool useEmbeddings = false;
@@ -39,7 +40,7 @@ public async Task CreatePlanFunctionFlowAsync(bool useChatModel, string prompt,
3940
TestHelpers.ImportSamplePlugins(kernel, "FunPlugin");
4041

4142
// Act
42-
var plan = await new HandlebarsPlanner().CreatePlanAsync(kernel, prompt);
43+
var plan = await new HandlebarsPlanner().CreatePlanAsync(kernel, goal);
4344

4445
// Assert expected function
4546
Assert.Contains(
@@ -51,14 +52,14 @@ public async Task CreatePlanFunctionFlowAsync(bool useChatModel, string prompt,
5152

5253
[RetryTheory]
5354
[InlineData("Write a novel about software development that is 3 chapters long.", "NovelChapter", "WriterPlugin")]
54-
public async Task CreatePlanWithDefaultsAsync(string prompt, string expectedFunction, string expectedPlugin)
55+
public async Task CreatePlanWithDefaultsAsync(string goal, string expectedFunction, string expectedPlugin)
5556
{
5657
// Arrange
5758
Kernel kernel = this.InitializeKernel();
5859
TestHelpers.ImportSamplePlugins(kernel, "WriterPlugin", "MiscPlugin");
5960

6061
// Act
61-
var plan = await new HandlebarsPlanner().CreatePlanAsync(kernel, prompt);
62+
var plan = await new HandlebarsPlanner().CreatePlanAsync(kernel, goal);
6263

6364
// Assert
6465
Assert.Contains(
@@ -68,6 +69,51 @@ public async Task CreatePlanWithDefaultsAsync(string prompt, string expectedFunc
6869
);
6970
}
7071

72+
[Theory]
73+
[InlineData(true, "List each property of the default Qux object.", "## Complex types", @"### Qux:
74+
{
75+
""type"": ""Object"",
76+
""properties"": {
77+
""Bar"": {
78+
""type"": ""String"",
79+
},
80+
""Baz"": {
81+
""type"": ""Int32"",
82+
},
83+
}
84+
}", "GetDefaultQux", "Foo")]
85+
public async Task CreatePlanWithComplexTypesDefinitionsAsync(bool useChatModel, string goal, string expectedSectionHeader, string expectedTypeHeader, string expectedFunction, string expectedPlugin)
86+
{
87+
// Arrange
88+
bool useEmbeddings = false;
89+
var kernel = this.InitializeKernel(useEmbeddings, useChatModel);
90+
kernel.ImportPluginFromObject(new Foo());
91+
92+
// Act
93+
var plan = await new HandlebarsPlanner().CreatePlanAsync(kernel, goal);
94+
95+
// Assert expected section header for Complex Types in prompt
96+
Assert.Contains(
97+
expectedSectionHeader,
98+
plan.Prompt,
99+
StringComparison.CurrentCulture
100+
);
101+
102+
// Assert expected complex parameter type in prompt
103+
Assert.Contains(
104+
expectedTypeHeader,
105+
plan.Prompt,
106+
StringComparison.CurrentCulture
107+
);
108+
109+
// Assert expected function in plan
110+
Assert.Contains(
111+
$"{expectedPlugin}-{expectedFunction}",
112+
plan.ToString(),
113+
StringComparison.CurrentCulture
114+
);
115+
}
116+
71117
private Kernel InitializeKernel(bool useEmbeddings = false, bool useChatModel = true)
72118
{
73119
AzureOpenAIConfiguration? azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get<AzureOpenAIConfiguration>();
@@ -110,6 +156,24 @@ private Kernel InitializeKernel(bool useEmbeddings = false, bool useChatModel =
110156
private readonly RedirectOutput _testOutputHelper;
111157
private readonly IConfigurationRoot _configuration;
112158

159+
private sealed class Foo
160+
{
161+
public sealed class Qux
162+
{
163+
public string Bar { get; set; } = string.Empty;
164+
public int Baz { get; set; }
165+
166+
public Qux(string bar, int baz)
167+
{
168+
this.Bar = bar;
169+
this.Baz = baz;
170+
}
171+
}
172+
173+
[KernelFunction, Description("Returns default Qux object.")]
174+
public Qux GetDefaultQux() => new("bar", 42);
175+
}
176+
113177
public void Dispose()
114178
{
115179
this._testOutputHelper.Dispose();

‎dotnet/src/Planners/Planners.Handlebars/Handlebars/CreatePlanPrompt.handlebars‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ What's the sum of 5+10+15?
7272
{{#message role="user"}}
7373
The following helpers are available to you:
7474

75-
## Out-of-the-box helpers
75+
## Built-in block helpers
7676
- `\{{#if}}\{{/if}}`
7777
- `\{{#unless}}\{{/unless}}`{{#if allowLoops}}
7878
- `\{{#each}}\{{/each}}`{{/if}}

0 commit comments

Comments
 (0)