-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Implementation for NopPromptTemplateFactory (#6630)
### 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. --> ### Description Closes #6575 ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] 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 - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 --------- Co-authored-by: westey <[email protected]>
- Loading branch information
1 parent
c075832
commit 54b936c
Showing
5 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
dotnet/src/SemanticKernel.Core/PromptTemplate/EchoPromptTemplate.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,33 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.SemanticKernel; | ||
|
||
/// <summary> | ||
/// Implementation of <see cref="IPromptTemplate"/> that just returns the prompt template. | ||
/// </summary> | ||
internal sealed class EchoPromptTemplate : IPromptTemplate | ||
{ | ||
private readonly PromptTemplateConfig _promptConfig; | ||
private readonly Task<string> _renderResult; | ||
|
||
/// <summary> | ||
/// Constructor for <see cref="EchoPromptTemplate"/>. | ||
/// </summary> | ||
/// <param name="promptConfig">Prompt template configuration</param> | ||
internal EchoPromptTemplate(PromptTemplateConfig promptConfig) | ||
{ | ||
Verify.NotNull(promptConfig, nameof(promptConfig)); | ||
Verify.NotNull(promptConfig.Template, nameof(promptConfig.Template)); | ||
|
||
this._promptConfig = promptConfig; | ||
this._renderResult = Task.FromResult(this._promptConfig.Template); | ||
} | ||
|
||
/// <inheritdoc/> | ||
#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks | ||
public Task<string> RenderAsync(Kernel kernel, KernelArguments? arguments = null, CancellationToken cancellationToken = default) => this._renderResult; | ||
#pragma warning restore VSTHRD003 // Avoid awaiting foreign Tasks | ||
} |
24 changes: 24 additions & 0 deletions
24
dotnet/src/SemanticKernel.Core/PromptTemplate/EchoPromptTemplateFactory.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,24 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Microsoft.SemanticKernel; | ||
|
||
/// <summary> | ||
/// Provides an implementation of <see cref="IPromptTemplateFactory"/> which creates no operation instances of <see cref="IPromptTemplate"/>. | ||
/// </summary> | ||
public sealed class EchoPromptTemplateFactory : IPromptTemplateFactory | ||
{ | ||
/// <summary> | ||
/// Singleton instance of <see cref="EchoPromptTemplateFactory"/>. | ||
/// </summary> | ||
public static EchoPromptTemplateFactory Instance { get; } = new EchoPromptTemplateFactory(); | ||
|
||
/// <inheritdoc/> | ||
public bool TryCreate(PromptTemplateConfig templateConfig, [NotNullWhen(true)] out IPromptTemplate? result) | ||
{ | ||
result = new EchoPromptTemplate(templateConfig); | ||
|
||
return true; | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
dotnet/src/SemanticKernel.UnitTests/PromptTemplate/EchoPromptTemplateTests.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,44 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.UnitTests.PromptTemplate; | ||
|
||
public sealed class EchoPromptTemplateTests | ||
{ | ||
[Fact] | ||
public async Task ItDoesNothingForSemanticKernelFormatAsync() | ||
{ | ||
// Arrange | ||
var template = """This {{$x11}} {{$a}}{{$missing}} test template {{p.bar $b}} and {{p.foo c='literal "c"' d = $d}} and {{p.baz ename=$e}}"""; | ||
var promptTemplateConfig = new PromptTemplateConfig(template); | ||
var templateFactory = new EchoPromptTemplateFactory(); | ||
|
||
// Act | ||
var target = templateFactory.Create(promptTemplateConfig); | ||
var result = await target.RenderAsync(new Kernel()); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal(template, result); | ||
} | ||
|
||
[Fact] | ||
public async Task ItDoesNothingForHandlebarsFormatAsync() | ||
{ | ||
// Arrange | ||
var template = """This {{x11}} {{a}}{{missing}} test template {{p.bar b}} and {{p.foo c='literal "c"' d = d}} and {{p.baz ename=e}}"""; | ||
var promptTemplateConfig = new PromptTemplateConfig(template) { TemplateFormat = "handlebars" }; | ||
var templateFactory = new EchoPromptTemplateFactory(); | ||
|
||
// Act | ||
var target = templateFactory.Create(promptTemplateConfig); | ||
var result = await target.RenderAsync(new Kernel()); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal(template, result); | ||
} | ||
} |