#r "nuget: Microsoft.SemanticKernel, 1.11.1"
#r "nuget: Microsoft.SemanticKernel.Planners.Handlebars, 1.11.1-preview"
#!import config/Settings.cs
#!import config/Utils.cs
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Kernel = Microsoft.SemanticKernel.Kernel;
var builder = Kernel.CreateBuilder();
// Configure AI backend used by the kernel
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();
if (useAzureOpenAI)
builder.AddAzureOpenAIChatCompletion(model, azureEndpoint, apiKey);
else
builder.AddOpenAIChatCompletion(model, apiKey, orgId);
var kernel = builder.Build();
Handlebars Planner is located in the Microsoft.SemanticKernel.Planning.Handlebars
package.
using Microsoft.SemanticKernel.Planning.Handlebars;
#pragma warning disable SKEXP0060
var planner = new HandlebarsPlanner();
The planner needs to know what plugins are available to it. Here we'll import the SummarizePlugin
and WriterPlugin
we have defined on disk.
var pluginsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "..", "..", "prompt_template_samples");
kernel.ImportPluginFromPromptDirectory(Path.Combine(pluginsDirectory, "SummarizePlugin"));
kernel.ImportPluginFromPromptDirectory(Path.Combine(pluginsDirectory, "WriterPlugin"));
Define your ASK. What do you want the Kernel to do?
#pragma warning disable SKEXP0060
var ask = "Tomorrow is Valentine's day. I need to come up with a few date ideas. My significant other likes poems so write them in the form of a poem.";
var originalPlan = await planner.CreatePlanAsync(kernel, ask);
Console.WriteLine("Original plan:\n");
Console.WriteLine(originalPlan);
As you can see in the above plan, the Planner has taken the user's ask and converted it into a Plan object detailing how the AI would go about solving this task.
It makes use of the plugins that the Kernel has available to it and determines which functions to call in order to fulfill the user's ask.
Let's also define an inline plugin and have it be available to the Planner. Be sure to give it a function name and plugin name.
string skPrompt = """
{{$input}}
Rewrite the above in the style of Shakespeare.
""";
var executionSettings = new OpenAIPromptExecutionSettings
{
MaxTokens = 2000,
Temperature = 0.7,
TopP = 0.5
};
var shakespeareFunction = kernel.CreateFunctionFromPrompt(skPrompt, executionSettings, "Shakespeare");
Let's update our ask using this new plugin.
#pragma warning disable SKEXP0060
var ask = @"Tomorrow is Valentine's day. I need to come up with a few date ideas.
She likes Shakespeare so write using his style. Write them in the form of a poem.";
var newPlan = await planner.CreatePlanAsync(kernel, ask);
Console.WriteLine("Updated plan:\n");
Console.WriteLine(newPlan);
Now that we have different plans, let's try to execute them! The Kernel can execute the plan using RunAsync.
#pragma warning disable SKEXP0060
var originalPlanResult = await originalPlan.InvokeAsync(kernel, new KernelArguments());
Console.WriteLine("Original Plan results:\n");
Console.WriteLine(Utils.WordWrap(originalPlanResult.ToString(), 100));
Now lets execute and print the new plan:
#pragma warning disable SKEXP0060
var newPlanResult = await newPlan.InvokeAsync(kernel, new KernelArguments());
Console.WriteLine("New Plan results:\n");
Console.WriteLine(newPlanResult);