{"id":1065,"date":"2023-08-21T11:08:21","date_gmt":"2023-08-21T18:08:21","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=1065"},"modified":"2023-08-21T11:08:21","modified_gmt":"2023-08-21T18:08:21","slug":"using-planners-in-the-sk-java-kernel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/using-planners-in-the-sk-java-kernel\/","title":{"rendered":"Using Planners in the SK Java Kernel"},"content":{"rendered":"<p>Dynamic AI planners are a useful tool when you are not sure what type of <strong>asks<\/strong> are going to come in from your users.\u00a0 Let&#8217;s learn how you can use planners in the <a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/introducing-semantic-kernel-for-java\/\">Java<\/a> kernel.\u00a0 In my previous <a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/ai-tooling-for-java-developers-with-sk\/\">blog<\/a> on using the Java kernel, on <a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/ai-tooling-for-java-developers-with-sk\/#step-5-plugin-function-as-code\">Step #5<\/a> I explained how to use multiple Plugins and their Functions one after another.<\/p>\n<p>In this post, I will show you an example of a more complex app that can handle a variety of requests depending on the task and we will see how the planner can chain Functions to complete tasks like translate text that is passed in and generate an email.<\/p>\n<p>A Planner can intelligently choose functions from a pool of available Plugins &amp; Functions and accomplish a result for a given task.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure1.png\"><img decoding=\"async\" class=\"aligncenter wp-image-1068 size-full\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure1.png\" alt=\"Image Figure1\" width=\"601\" height=\"322\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure1.png 601w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure1-300x161.png 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><em>Figure 1: Semantic Kernel with pool of Functions<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>For a given construct above, if the \u201cTask\u201d is to Summarize and then Translate a given text, the Planner will pick up \u201cSummarizer\u201d and \u201cTranslator\u201d functions to generate the \u201cResult\u201d.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure2.png\"><img decoding=\"async\" class=\"size-full wp-image-1069 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure2.png\" alt=\"Image Figure2\" width=\"601\" height=\"301\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure2.png 601w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure2-300x150.png 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><em>Figure 2: SK selecting Summarizer &amp; Translator functions.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Another example is, for a \u201cTask\u201d to Define and then Email generation for a given text, the Planner will pick up \u201cDefine\u201d and \u201cEmail Gen.\u201d functions to generate the \u201cResult\u201d.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure3.png\"><img decoding=\"async\" class=\"size-full wp-image-1070 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure3.png\" alt=\"Image Figure3\" width=\"601\" height=\"301\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure3.png 601w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/08\/Figure3-300x150.png 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><em>Figure 3: SK selecting Define &amp; Email Gen. functions.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>There are different types of <a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/ai-orchestration\/planner?tabs=Csharp\">Planners<\/a> available in Semantic Kernel<\/p>\n<table style=\"width: 83.2908%;\">\n<tbody>\n<tr>\n<td style=\"width: 13.6337%;\" width=\"301\">Basic<\/td>\n<td style=\"width: 146.766%;\" width=\"301\">A simplified version of Sequential Planner that strings together a set of functions.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.6337%;\" width=\"301\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/semantic-kernel-planners-actionplanner\/\">Action<\/a><\/td>\n<td style=\"width: 146.766%;\" width=\"301\">Create a plan with a single step.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.6337%;\" width=\"301\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/semantic-kernel-planners-sequential-planner\/\">Sequential<\/a><\/td>\n<td style=\"width: 146.766%;\" width=\"301\">Creates a plan with a series of steps that are interconnected with custom generated input and output variables.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.6337%;\"><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/semantic-kernel-planners-stepwise-planner\/\">Stepwise<\/a><\/td>\n<td style=\"width: 146.766%;\">A powerful planning object based on a neuro-symbolic architecture<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3>Using Sequential Planner<\/h3>\n<pre><em>log<\/em>.debug(\"== Instantiates the Kernel ==\");\r\nKernel kernel = <em>kernel<\/em>();\r\n\r\n<em>log<\/em>.debug(\"== Adding multiple skills to kernel ==\");\r\nkernel.importSkillFromDirectory(\"WriterSkill\", \"src\/main\/resources\/Skills\", \"WriterSkill\");\r\nkernel.importSkillFromDirectory(\"SummarizeSkill\", \"src\/main\/resources\/Skills\", \"SummarizeSkill\");\r\nkernel.importSkillFromDirectory(\"DesignThinkingSkill\", \"src\/main\/resources\/Skills\", \"DesignThinkingSkill\");\r\n\r\n<em>log<\/em>.debug(\"== Create a Planner using the kernel ==\");\r\nSequentialPlanner planner = new SequentialPlanner(kernel, null, null);<\/pre>\n<p>&nbsp;<\/p>\n<p>This example code will:<\/p>\n<ol>\n<li>Create a Kernel object.<\/li>\n<li>Add\/Import Plugins to the Kernel.<\/li>\n<li>Create a SequentialPlanner object with the above Kernel.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3>Using Action Planner<\/h3>\n<pre>Kernel kernel = <em>kernel<\/em>();\r\n\r\n<em>log<\/em>.debug(\"== Adding multiple skills to kernel ==\");\r\nkernel.importSkillFromDirectory(\"WriterSkill\", \"src\/main\/resources\/Skills\", \"WriterSkill\");\r\nkernel.importSkillFromDirectory(\"SummarizeSkill\", \"src\/main\/resources\/Skills\", \"SummarizeSkill\");\r\nkernel.importSkillFromDirectory(\"DesignThinkingSkill\", \"src\/main\/resources\/Skills\", \"DesignThinkingSkill\");\r\n\r\n<em>log<\/em>.debug(\"== Create a Planner using the kernel ==\");\r\nActionPlanner planner = new ActionPlanner(kernel, null);<\/pre>\n<p>&nbsp;<\/p>\n<p>This example code will:<\/p>\n<ol>\n<li>Create a Kernel object.<\/li>\n<li>Add\/Import Plugins to the Kernel.<\/li>\n<li>Create an ActionPlanner object with the above Kernel.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3>Invoking Planner for different Tasks<\/h3>\n<p>Now we can invoke a planner to complete actions like Summarize and Translate:<\/p>\n<pre>Mono&lt;Plan&gt; result =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 planner.createPlanAsync(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>TextToSummarize <\/em>+ \"\"\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =====\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 summarize the above content and then translate it to Dutch.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =====\"\"\");<\/pre>\n<p>&nbsp;<\/p>\n<p>Now let&#8217;s invoke it to Rewrite in a different style:<\/p>\n<pre>Mono&lt;Plan&gt; result =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 planner.createPlanAsync(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>TextToSummarize <\/em>+ \"\"\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =====\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 rewrite the above content in Yoda from Starwars style.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =====\"\"\");<\/pre>\n<p>&nbsp;<\/p>\n<p>Next we can invoke it to Design Think (Empathize&gt;Define&gt; Ideate) and then generate Email:<\/p>\n<pre>Mono&lt;Plan&gt; result =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 planner.createPlanAsync(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>CallTranscript <\/em>+ \"\"\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =====\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 design a solution for the above problem.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =====\"\"\");<\/pre>\n<p>&nbsp;<\/p>\n<p>The result is a Plan object with all the relevant information on which Plugins and Functions are available and which intermediate inputs &amp; outputs it generated and relevant memory it used.<\/p>\n<p>Example code sample &gt; <a href=\"https:\/\/github.com\/sohamda\/SemanticKernel-Basics\/blob\/main\/src\/main\/java\/soham\/sksamples\/Example08_SequentialPlanner.java\">SequentialPlanner.java<\/a> &amp; <a href=\"https:\/\/github.com\/sohamda\/SemanticKernel-Basics\/blob\/main\/src\/main\/java\/soham\/sksamples\/Example09_ActionPlanner.java\">ActionPlanner.java<\/a><\/p>\n<p>In your app you can have options of planners to use based on your use case.<\/p>\n<ul>\n<li>Action Planner &#8211; exactly one Plugin will be executed<\/li>\n<li>Sequential Planner will execute one or more Plugins<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>Next Steps<\/h3>\n<p>Get involved with the Java Kernel development by\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/CONTRIBUTING.md\" target=\"_blank\" rel=\"noopener\">contributing<\/a>\u00a0and following the public\u00a0<a href=\"https:\/\/github.com\/orgs\/microsoft\/projects\/868\" target=\"_blank\" rel=\"noopener\">Java board<\/a> to see what you might want to pick up. Learn more about <a href=\"https:\/\/learn.microsoft.com\/en-us\/semantic-kernel\/ai-orchestration\/planner\">planners<\/a>.<\/p>\n<p>Join the Semantic Kernel\u00a0<a href=\"https:\/\/aka.ms\/sk-community\" target=\"_blank\" rel=\"noopener\">community<\/a>\u00a0and let us know what you think.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dynamic AI planners are a useful tool when you are not sure what type of asks are going to come in from your users.\u00a0 Let&#8217;s learn how you can use planners in the Java kernel.\u00a0 In my previous blog on using the Java kernel, on Step #5 I explained how to use multiple Plugins and [&hellip;]<\/p>\n","protected":false},"author":125575,"featured_media":5048,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[24,2],"tags":[],"class_list":["post-1065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-samples"],"acf":[],"blog_post_summary":"<p>Dynamic AI planners are a useful tool when you are not sure what type of asks are going to come in from your users.\u00a0 Let&#8217;s learn how you can use planners in the Java kernel.\u00a0 In my previous blog on using the Java kernel, on Step #5 I explained how to use multiple Plugins and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/1065","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\/125575"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=1065"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/1065\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/5048"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}