August 21st, 2023

Using Planners in the SK Java Kernel

Soham Dasgupta
Cloud Solution Architect

Dynamic AI planners are a useful tool when you are not sure what type of asks are going to come in from your users.  Let’s learn how you can use planners in the Java kernel.  In my previous blog on using the Java kernel, on Step #5 I explained how to use multiple Plugins and their Functions one after another.

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.

A Planner can intelligently choose functions from a pool of available Plugins & Functions and accomplish a result for a given task.

Image Figure1

Figure 1: Semantic Kernel with pool of Functions

 

For a given construct above, if the “Task” is to Summarize and then Translate a given text, the Planner will pick up “Summarizer” and “Translator” functions to generate the “Result”.

Image Figure2

Figure 2: SK selecting Summarizer & Translator functions.

 

Another example is, for a “Task” to Define and then Email generation for a given text, the Planner will pick up “Define” and “Email Gen.” functions to generate the “Result”.

Image Figure3

Figure 3: SK selecting Define & Email Gen. functions.

 

There are different types of Planners available in Semantic Kernel

Basic A simplified version of Sequential Planner that strings together a set of functions.
Action Create a plan with a single step.
Sequential Creates a plan with a series of steps that are interconnected with custom generated input and output variables.
Stepwise A powerful planning object based on a neuro-symbolic architecture

 

Using Sequential Planner

log.debug("== Instantiates the Kernel ==");
Kernel kernel = kernel();

log.debug("== Adding multiple skills to kernel ==");
kernel.importSkillFromDirectory("WriterSkill", "src/main/resources/Skills", "WriterSkill");
kernel.importSkillFromDirectory("SummarizeSkill", "src/main/resources/Skills", "SummarizeSkill");
kernel.importSkillFromDirectory("DesignThinkingSkill", "src/main/resources/Skills", "DesignThinkingSkill");

log.debug("== Create a Planner using the kernel ==");
SequentialPlanner planner = new SequentialPlanner(kernel, null, null);

 

This example code will:

  1. Create a Kernel object.
  2. Add/Import Plugins to the Kernel.
  3. Create a SequentialPlanner object with the above Kernel.

 

Using Action Planner

Kernel kernel = kernel();

log.debug("== Adding multiple skills to kernel ==");
kernel.importSkillFromDirectory("WriterSkill", "src/main/resources/Skills", "WriterSkill");
kernel.importSkillFromDirectory("SummarizeSkill", "src/main/resources/Skills", "SummarizeSkill");
kernel.importSkillFromDirectory("DesignThinkingSkill", "src/main/resources/Skills", "DesignThinkingSkill");

log.debug("== Create a Planner using the kernel ==");
ActionPlanner planner = new ActionPlanner(kernel, null);

 

This example code will:

  1. Create a Kernel object.
  2. Add/Import Plugins to the Kernel.
  3. Create an ActionPlanner object with the above Kernel.

 

Invoking Planner for different Tasks

Now we can invoke a planner to complete actions like Summarize and Translate:

Mono<Plan> result =
        planner.createPlanAsync(
                TextToSummarize + """
                =====
                summarize the above content and then translate it to Dutch.
                =====""");

 

Now let’s invoke it to Rewrite in a different style:

Mono<Plan> result =
        planner.createPlanAsync(
                TextToSummarize + """
            =====
            rewrite the above content in Yoda from Starwars style.
            =====""");

 

Next we can invoke it to Design Think (Empathize>Define> Ideate) and then generate Email:

Mono<Plan> result =
        planner.createPlanAsync(
                CallTranscript + """
            =====
            design a solution for the above problem.
            =====""");

 

The result is a Plan object with all the relevant information on which Plugins and Functions are available and which intermediate inputs & outputs it generated and relevant memory it used.

Example code sample > SequentialPlanner.java & ActionPlanner.java

In your app you can have options of planners to use based on your use case.

  • Action Planner – exactly one Plugin will be executed
  • Sequential Planner will execute one or more Plugins

 

Next Steps

Get involved with the Java Kernel development by contributing and following the public Java board to see what you might want to pick up. Learn more about planners.

Join the Semantic Kernel community and let us know what you think.

Category
JavaSamples

Author

Soham Dasgupta
Cloud Solution Architect

0 comments

Discussion are closed.