-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Fix filters cloning when registered via Kernel properties (#6241)
### 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. --> Based on: #6240 Since filters are cloned when they are registered through DI container, in the same way they should be cloned when registered through Kernel properties (i.e. `kernel.FunctionInvocationFilters`). ### 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 😄
- Loading branch information
1 parent
1692207
commit af207dc
Showing
3 changed files
with
80 additions
and
6 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
68 changes: 68 additions & 0 deletions
68
dotnet/src/SemanticKernel.UnitTests/Filters/KernelFilterTests.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,68 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.SemanticKernel; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.UnitTests.Filters; | ||
|
||
public class KernelFilterTests : FilterBaseTest | ||
{ | ||
[Fact] | ||
public void FiltersAreClonedWhenRegisteredWithDI() | ||
{ | ||
// Arrange | ||
var functionFilter = new FakeFunctionFilter(onFunctionInvocation: async (context, next) => { await next(context); }); | ||
var promptFilter = new FakePromptFilter(onPromptRender: async (context, next) => { await next(context); }); | ||
var autoFunctionFilter = new FakeAutoFunctionFilter(onAutoFunctionInvocation: async (context, next) => { await next(context); }); | ||
|
||
var builder = Kernel.CreateBuilder(); | ||
|
||
builder.Services.AddSingleton<IFunctionInvocationFilter>(functionFilter); | ||
builder.Services.AddSingleton<IPromptRenderFilter>(promptFilter); | ||
builder.Services.AddSingleton<IAutoFunctionInvocationFilter>(autoFunctionFilter); | ||
|
||
var kernel = builder.Build(); | ||
|
||
// Act | ||
var clonedKernel = kernel.Clone(); | ||
|
||
// Assert | ||
Assert.Single(kernel.FunctionInvocationFilters); | ||
Assert.Single(kernel.PromptRenderFilters); | ||
Assert.Single(kernel.AutoFunctionInvocationFilters); | ||
|
||
Assert.Single(clonedKernel.FunctionInvocationFilters); | ||
Assert.Single(clonedKernel.PromptRenderFilters); | ||
Assert.Single(clonedKernel.AutoFunctionInvocationFilters); | ||
} | ||
|
||
[Fact] | ||
public void FiltersAreClonedWhenRegisteredWithKernelProperties() | ||
{ | ||
// Arrange | ||
var functionFilter = new FakeFunctionFilter(onFunctionInvocation: async (context, next) => { await next(context); }); | ||
var promptFilter = new FakePromptFilter(onPromptRender: async (context, next) => { await next(context); }); | ||
var autoFunctionFilter = new FakeAutoFunctionFilter(onAutoFunctionInvocation: async (context, next) => { await next(context); }); | ||
|
||
var builder = Kernel.CreateBuilder(); | ||
|
||
var kernel = builder.Build(); | ||
|
||
kernel.FunctionInvocationFilters.Add(functionFilter); | ||
kernel.PromptRenderFilters.Add(promptFilter); | ||
kernel.AutoFunctionInvocationFilters.Add(autoFunctionFilter); | ||
|
||
// Act | ||
var clonedKernel = kernel.Clone(); | ||
|
||
// Assert | ||
Assert.Single(kernel.FunctionInvocationFilters); | ||
Assert.Single(kernel.PromptRenderFilters); | ||
Assert.Single(kernel.AutoFunctionInvocationFilters); | ||
|
||
Assert.Single(clonedKernel.FunctionInvocationFilters); | ||
Assert.Single(clonedKernel.PromptRenderFilters); | ||
Assert.Single(clonedKernel.AutoFunctionInvocationFilters); | ||
} | ||
} |