Skip to content

Commit e6413c3

Browse files
authored
.Net: Removed Kernel events (#9748)
### 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. --> Related: #9499 Removed the usage of obsolete Kernel Events in favor of Filters as part of Filters graduation process. ### 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 😄
1 parent 297eb16 commit e6413c3

File tree

9 files changed

+2
-969
lines changed

9 files changed

+2
-969
lines changed

dotnet/samples/Concepts/Filtering/Legacy_KernelHooks.cs

Lines changed: 0 additions & 278 deletions
This file was deleted.

dotnet/samples/GettingStarted/Step7_Observability.cs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,61 +37,6 @@ public async Task ObservabilityWithFiltersAsync()
3737
Console.WriteLine(await kernel.InvokePromptAsync("How many days until Christmas? Explain your thinking.", new(settings)));
3838
}
3939

40-
/// <summary>
41-
/// Shows how to observe the execution of a <see cref="KernelPlugin"/> instance with hooks.
42-
/// </summary>
43-
[Fact]
44-
[Obsolete("Events are deprecated in favor of filters.")]
45-
public async Task ObservabilityWithHooksAsync()
46-
{
47-
// Create a kernel with OpenAI chat completion
48-
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
49-
kernelBuilder.AddOpenAIChatCompletion(
50-
modelId: TestConfiguration.OpenAI.ChatModelId,
51-
apiKey: TestConfiguration.OpenAI.ApiKey);
52-
53-
kernelBuilder.Plugins.AddFromType<TimeInformation>();
54-
55-
Kernel kernel = kernelBuilder.Build();
56-
57-
// Handler which is called before a function is invoked
58-
void MyInvokingHandler(object? sender, FunctionInvokingEventArgs e)
59-
{
60-
Console.WriteLine($"Invoking {e.Function.Name}");
61-
}
62-
63-
// Handler which is called before a prompt is rendered
64-
void MyRenderingHandler(object? sender, PromptRenderingEventArgs e)
65-
{
66-
Console.WriteLine($"Rendering prompt for {e.Function.Name}");
67-
}
68-
69-
// Handler which is called after a prompt is rendered
70-
void MyRenderedHandler(object? sender, PromptRenderedEventArgs e)
71-
{
72-
Console.WriteLine($"Rendered prompt: {e.RenderedPrompt}");
73-
}
74-
75-
// Handler which is called after a function is invoked
76-
void MyInvokedHandler(object? sender, FunctionInvokedEventArgs e)
77-
{
78-
if (e.Result.Metadata is not null && e.Result.Metadata.ContainsKey("Usage"))
79-
{
80-
Console.WriteLine("Token usage: {0}", e.Result.Metadata?["Usage"]?.AsJson());
81-
}
82-
}
83-
84-
// Add the handlers to the kernel
85-
kernel.FunctionInvoking += MyInvokingHandler;
86-
kernel.PromptRendering += MyRenderingHandler;
87-
kernel.PromptRendered += MyRenderedHandler;
88-
kernel.FunctionInvoked += MyInvokedHandler;
89-
90-
// Invoke the kernel with a prompt and allow the AI to automatically invoke functions
91-
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
92-
Console.WriteLine(await kernel.InvokePromptAsync("How many days until Christmas? Explain your thinking.", new(settings)));
93-
}
94-
9540
/// <summary>
9641
/// A plugin that returns the current time.
9742
/// </summary>

dotnet/src/SemanticKernel.Abstractions/Functions/KernelFunction.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,6 @@ public async Task<FunctionResult> InvokeAsync(
240240
// Quick check for cancellation after logging about function start but before doing any real work.
241241
cancellationToken.ThrowIfCancellationRequested();
242242

243-
// Invoke pre-invocation event handler. If it requests cancellation, throw.
244-
#pragma warning disable CS0618 // Events are deprecated
245-
var invokingEventArgs = kernel.OnFunctionInvoking(this, arguments);
246-
#pragma warning restore CS0618 // Events are deprecated
247-
248-
if (invokingEventArgs?.Cancel is true)
249-
{
250-
throw new OperationCanceledException($"A {nameof(Kernel)}.{nameof(Kernel.FunctionInvoking)} event handler requested cancellation before function invocation.");
251-
}
252-
253243
var invocationContext = await kernel.OnFunctionInvocationAsync(this, arguments, functionResult, isStreaming: false, async (context) =>
254244
{
255245
// Invoking the function and updating context with result.
@@ -259,22 +249,6 @@ public async Task<FunctionResult> InvokeAsync(
259249
// Apply any changes from the function filters context to final result.
260250
functionResult = invocationContext.Result;
261251

262-
// Invoke the post-invocation event handler. If it requests cancellation, throw.
263-
#pragma warning disable CS0618 // Events are deprecated
264-
var invokedEventArgs = kernel.OnFunctionInvoked(this, arguments, functionResult);
265-
#pragma warning restore CS0618 // Events are deprecated
266-
267-
if (invokedEventArgs is not null)
268-
{
269-
// Apply any changes from the event handlers to final result.
270-
functionResult = new FunctionResult(this, invokedEventArgs.ResultValue, functionResult.Culture, invokedEventArgs.Metadata ?? functionResult.Metadata);
271-
}
272-
273-
if (invokedEventArgs?.Cancel is true)
274-
{
275-
throw new OperationCanceledException($"A {nameof(Kernel)}.{nameof(Kernel.FunctionInvoked)} event handler requested cancellation after function invocation.");
276-
}
277-
278252
logger.LogFunctionInvokedSuccess(this.PluginName, this.Name);
279253

280254
this.LogFunctionResult(logger, this.PluginName, this.Name, functionResult);
@@ -370,16 +344,6 @@ public async IAsyncEnumerable<TResult> InvokeStreamingAsync<TResult>(
370344
// Quick check for cancellation after logging about function start but before doing any real work.
371345
cancellationToken.ThrowIfCancellationRequested();
372346

373-
// Invoke pre-invocation event handler. If it requests cancellation, throw.
374-
#pragma warning disable CS0618 // Events are deprecated
375-
var invokingEventArgs = kernel.OnFunctionInvoking(this, arguments);
376-
#pragma warning restore CS0618 // Events are deprecated
377-
378-
if (invokingEventArgs?.Cancel is true)
379-
{
380-
throw new OperationCanceledException($"A {nameof(Kernel)}.{nameof(Kernel.FunctionInvoking)} event handler requested cancellation before function invocation.");
381-
}
382-
383347
FunctionResult functionResult = new(this, culture: kernel.Culture);
384348

385349
var invocationContext = await kernel.OnFunctionInvocationAsync(this, arguments, functionResult, isStreaming: true, (context) =>

0 commit comments

Comments
 (0)