{"id":1254,"date":"2023-09-25T14:40:39","date_gmt":"2023-09-25T21:40:39","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=1254"},"modified":"2023-09-29T10:31:08","modified_gmt":"2023-09-29T17:31:08","slug":"supercharge-your-semantic-kernel-experience-unleashing-the-power-of-pre-and-post-hooks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/supercharge-your-semantic-kernel-experience-unleashing-the-power-of-pre-and-post-hooks\/","title":{"rendered":"Supercharge Your Semantic Kernel Experience: Unleashing the Power of Pre and Post Hooks"},"content":{"rendered":"<h2><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-89\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge.png\" alt=\"Image skpatternlarge\" width=\"1638\" height=\"136\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge.png 1638w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-300x25.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-1024x85.png 1024w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-768x64.png 768w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-1536x128.png 1536w\" sizes=\"(max-width: 1638px) 100vw, 1638px\" \/><\/a><\/h2>\n<p>Semantic Kernel is a powerful SDK that enables developers to build intelligent applications with ease. With the latest update to the .net SDK, the addition of pre and post hooks takes the functionality of Semantic Kernel to new heights.<\/p>\n<h2>Benefits of Pre and Post Hooks<\/h2>\n<p>Pre and post hooks provide developers with a way to intercept function executions using\u00a0Kernel.RunAsync\u00a0and perform additional actions before and after the execution. This opens a world of possibilities for customization and fine-tuning of application behavior. Here are some key benefits of using pre and post hooks in Semantic Kernel:<\/p>\n<ul>\n<li><strong>Enhanced Control<\/strong>: Pre and post hooks allow developers to gain granular control over function executions. By intercepting the execution flow, developers can modify the\u00a0SKContextvariables, cancel further\u00a0RunAsync\u00a0function executions, or perform any other custom action based on their application\u2019s requirements.<\/li>\n<li><strong>Customization<\/strong>: With pre and post hooks, developers can customize the behavior of their applications at various stages of function execution. This enables them to tailor the application\u2019s response, handle errors gracefully, or add additional functionality seamlessly.<\/li>\n<li><strong>Event-Driven Architecture<\/strong>: The addition of pre and post hooks introduces an event-driven architecture to Semantic Kernel. Developers can subscribe to the\u00a0FunctionInvokingevent to handle pre-execution actions and the\u00a0IKernel.FunctionInvoked\u00a0event to handle post-execution actions. This allows for modular and extensible application design.<\/li>\n<\/ul>\n<h2>Key Scenarios for Using Pre and Post Hooks<\/h2>\n<p>Pre and post hooks can be used in a wide range of scenarios to enhance application functionality. Here are some key scenarios where app developers can leverage pre and post hooks in Semantic Kernel:<\/p>\n<p><strong>Pre-Execution (Invoking):<\/strong><\/p>\n<ul>\n<li>Get: Access the\u00a0SKContextobject before function execution.<\/li>\n<li>Set: Modify the\u00a0SKContextvariables based on specific conditions.<\/li>\n<li>Set: Skip the next RunAsyncfunction in the pipeline.<\/li>\n<li>Set: Cancel further RunAsyncfunction executions.<\/li>\n<\/ul>\n<p><strong>Post-Execution (Invoked):<\/strong><\/p>\n<ul>\n<li>Get: Access the\u00a0SKContextobject after function execution.<\/li>\n<li>Set: Modify the\u00a0SKContextvariables based on the results of function execution.<\/li>\n<li>Set: Repeat RunAsync function execution.<\/li>\n<li>Set: Cancel further\u00a0RunAsyncfunction executions if required.<\/li>\n<\/ul>\n<p>These scenarios represent just a few examples of how pre and post hooks can be utilized in Semantic Kernel. The flexibility provided by these hooks empowers developers to create intelligent applications that meet their unique requirements.<\/p>\n<h2>Implementation Details<\/h2>\n<p>The implementation of pre and post hooks in Semantic Kernel involves adding two new public interface events to the\u00a0IKernel\u00a0interface:<\/p>\n<ol>\n<li>IKernel.FunctionInvoking: This event is triggered before function execution starts. Developers can subscribe to this event to perform pre-execution actions such as accessing the SKContext, modifying variables, skipping to the next function in the pipeline or canceling further RunAsync function executions.<\/li>\n<li>IKernel.FunctionInvoked: This event is triggered after function execution completes. Developers can subscribe to this event to perform post-execution actions such as accessing the SKContext, modifying variables based on the results, adding retries or canceling further RunAsync function executions.<\/li>\n<\/ol>\n<p>These events provide a powerful mechanism to extend the functionality of Semantic Kernel and create highly customizable applications.<\/p>\n<h2>Conclusion<\/h2>\n<p>Adding pre and post hooks to the Semantic Kernel SDK unlocks a new level of control, customization, and extensibility for app developers. By leveraging these hooks, developers can fine-tune their application\u2019s behavior, handle errors gracefully, and add additional functionality seamlessly. The implementation details provided in this blog post will help you get started with using pre and post hooks effectively in your Semantic Kernel applications.<\/p>\n<p>Stay tuned for more updates on Semantic Kernel as we continue to explore its capabilities!<\/p>\n<p>Remember: With Semantic Kernel, you have the power to build intelligent applications that make a difference! Happy coding! \ud83d\ude0a<\/p>\n<h2>Next Steps:<\/h2>\n<ul>\n<li>\u00a0Get the latest SDK version <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/pkgs\/nuget\/Microsoft.SemanticKernel\">here<\/a> to benefit from these changes. Find a syntax example <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/dotnet\/samples\/KernelSyntaxExamples\/Example57_FunctionEventHandlers.cs\">here<\/a>.<\/li>\n<li>Join the <a href=\"https:\/\/aka.ms\/sk-community\" target=\"_blank\" rel=\"noopener\">community<\/a>\u00a0and let us know what you think.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-92\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw.png\" alt=\"Image skpatternsmallbw\" width=\"1211\" height=\"137\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw.png 1211w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw-300x34.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw-1024x116.png 1024w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw-768x87.png 768w\" sizes=\"(max-width: 1211px) 100vw, 1211px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Semantic Kernel is a powerful SDK that enables developers to build intelligent applications with ease. With the latest update to the .net SDK, the addition of pre and post hooks takes the functionality of Semantic Kernel to new heights. Benefits of Pre and Post Hooks Pre and post hooks provide developers with a way to [&hellip;]<\/p>\n","protected":false},"author":116113,"featured_media":1257,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17,1],"tags":[],"class_list":["post-1254","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcements","category-semantic-kernel"],"acf":[],"blog_post_summary":"<p>Semantic Kernel is a powerful SDK that enables developers to build intelligent applications with ease. With the latest update to the .net SDK, the addition of pre and post hooks takes the functionality of Semantic Kernel to new heights. Benefits of Pre and Post Hooks Pre and post hooks provide developers with a way to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/1254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/116113"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=1254"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/1254\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/1257"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=1254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=1254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=1254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}