Skip to content

Commit

Permalink
Merge branch 'main' into users/markwallace/10571
Browse files Browse the repository at this point in the history
  • Loading branch information
markwallace-microsoft authored Feb 17, 2025
2 parents 8e70cdd + 29da41e commit 1d60afe
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,41 @@ public async Task ItTrustsAllTemplatesAsync()
Assert.Equal(expected, result);
}

[Fact]
public async Task ItRendersContentWithHtmlEntitiesAsync()
{
// Arrange
var template =
"""
<message role="user">Can you help me tell &amp; the time in Seattle right now?</message>
<message role="assistant">Sure! The time in Seattle is currently 3:00 PM.</message>
<message role="user">What about New York?</message>
""";

var factory = new HandlebarsPromptTemplateFactory(options: new() { EnableHtmlDecoder = false });

var target = factory.Create(new PromptTemplateConfig(template)
{
TemplateFormat = HandlebarsPromptTemplateFactory.HandlebarsTemplateFormat,
});

// Act
var prompt = await target.RenderAsync(this._kernel);
bool result = ChatPromptParser.TryParse(prompt, out var chatHistory);

// Assert
Assert.True(result);
Assert.NotNull(chatHistory);
Assert.Collection(chatHistory,
c => Assert.Equal(AuthorRole.User, c.Role),
c => Assert.Equal(AuthorRole.Assistant, c.Role),
c => Assert.Equal(AuthorRole.User, c.Role));
Assert.Collection(chatHistory,
c => Assert.Equal("Can you help me tell & the time in Seattle right now?", c.Content),
c => Assert.Equal("Sure! The time in Seattle is currently 3:00 PM.", c.Content),
c => Assert.Equal("What about New York?", c.Content));
}

#region private

private HandlebarsPromptTemplateFactory _factory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public async Task<string> RenderAsync(Kernel kernel, KernelArguments? arguments
this.RegisterHelpers(handlebarsInstance, kernel, arguments, cancellationToken);

var template = handlebarsInstance.Compile(this._promptModel.Template);
return System.Net.WebUtility.HtmlDecode(template(arguments).Trim());
var text = template(arguments).Trim();
return this._options.EnableHtmlDecoder ? System.Net.WebUtility.HtmlDecode(text) : text;
}

#region private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public sealed class HandlebarsPromptTemplateOptions : HandlebarsHelpersOptions
/// </value>
public Action<RegisterHelperCallback, HandlebarsPromptTemplateOptions, KernelArguments>? RegisterCustomHelpers { get; set; }

/// <summary>
/// Flag indicating whether to enable HTML decoding of the rendered template.
/// </summary>
public bool EnableHtmlDecoder { get; set; } = true;

/// <summary>
/// Initializes a new instance of the <see cref="HandlebarsPromptTemplateOptions"/> class.
/// </summary>
Expand Down

0 comments on commit 1d60afe

Please sign in to comment.