Zero To AI Hero, Pt 2: Plugins in Semantic Kernel – DZone – Uplaza

I talked a little bit about semantic kernel in Half 1 however determined to go away out the juicy half. There are a number of gears in SK. They spin and join like the within of the watch to make it do the magic. A type of gears is plugins. Plugins are the robotic arms of Semantic Kernel, able to performing some work past chit-chatting.

Why Do We Want Plugins?

Everyone knows GenAI is about — shock, shock: “Generative AI”. What do they practice on? Tokens. These tokens are then mapped onto vector databases and can be utilized to foretell the subsequent token, and so on, and the story goes on and on. What are these fashions able to? Predicting subsequent tokens, and that is about it (no less than as of now). So, for many who suppose GAI is the same as AGI, no! Not but!

Now, again to plugins. These token-predicting machines weren’t able to dealing with real-world challenges to start with. What if we ask a easy query like, “What is the time now?” It could not inform us because it was not a part of the coaching information. That is the place plugins come into play. Plugins give Semantic Kernel these little code snippets, which it will probably run to get a solution to a particular question it would not in any other case know the reply to.

SK Plugins are constructed utilizing a way referred to as operate/device calling, which is baked into most Giant Language Fashions and a few Small Language Fashions these days (we are going to dig into SLMs later). In easy phrases, operate calling permits language fashions to create planning and invoking code snippets/APIs of your present code. As soon as the language mannequin requests a operate, SK serves as a router to this and calls an present code snippet, then serves the return worth again into the language mannequin. As you possibly can see, the potential for this expands when a mannequin and a bunch of plugins begin speaking to one another and exchanging info.

Let’s Cost an Electrical Automobile With the Assist of Plugins

Let’s begin with our former instance from Half 1. The top purpose of our plugin is to cost an electrical car (or hybrid) throughout off-peak hours. A little bit of context: The electrical energy firm the place I dwell, PSE&G, rewards Electrical/Hybrid car homeowners who cost their automobiles throughout off-peak hours by way of an EV residential Charging Program. Let’s begin by benefiting from this.

utilizing Microsoft.SemanticKernel;
utilizing Microsoft.SemanticKernel.Connectors.OpenAI;
utilizing System.ComponentModel;

var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(
    deploymentName: "",
    endpoint: "",
    apiKey: ""
);

builder.Plugins.AddFromType(); // (); //  ");
    string userMessage = Console.ReadLine();
    Console.WriteLine(await kernel.InvokePromptAsync(userMessage, new(settings)));
    Console.WriteLine("--------------------------------------------------------------");
}

public class TimeTeller //  DateTime.Now.ToString("F");

    [Description("This function checks if in off-peak period between 9pm and 7am")]
    [KernelFunction]
    public bool IsOffPeak() => DateTime.Now.Hour = 21;
}

public class ElectricCar // 

We’ll dive into the code later. For now, let’s play with our automobile charging plugins a bit. Please check out the dialog beneath. It’s fascinating, is not it? To take specific motion, it wants:

  1. To know the present time
  2. If the present time is within the off-peak interval
  3. To have the ability to cease or begin the charging of the automobile

And curiously sufficient, we’ve two plugins: one for time dealing with and one for automobile dealing with. Would not it really feel like they’re conscious of one another and in a position to work collectively? That is the magic of the semantic kernel.

Semantic Kernel, the Orchestrator

The semantic kernel (SK) is that this stunning orchestrator that passes the ball between the mannequin and accessible plugins, thus producing the specified output by getting a collaborative effort. In our instance above, After we requested the query, “Can you start charging it if it is an off-peak period?” SK has to find out if I’m on an off-peak interval utilizing the TimeTeller plugin after which name the capabilities of the ElectricCar plugin to cost the automobile if wanted. Since I did not ask throughout an off-peak interval, SK determined to not cost the car. Fairly good.

How Is This Attainable? Let’s Dissect.

Let us take a look at the plugins TimeTeller and ElectricCar. They’re only a C# class with a number of public capabilities. Every operate is embellished with a KernelFunction attribute and a Description attribute. This Description attribute, additionally referred to as Semantic Description, tells the SK/mannequin what this operate truly does in easy language. After we ask a query, SK can decide if it must invoke any of those capabilities primarily based on this description and performance calling.

Plugins as Courses

If we had been to ask, “What is the current time?” SK would use GetCurrentTime() to get a solution. If we had been to ask, “Charge car if it is an off-peak period now.” SK would name IsOffPeak() to find out if the present time is on an off-peak interval; whether it is, SK would then name the operate StartCharging() to begin charging the automobile, like chaining.

public class TimeTeller //  DateTime.Now.ToString("F");
    
    [Description("This function checks if in off-peak period between 9pm and 7am")]
    [KernelFunction]
    public bool IsOffPeak() => DateTime.Now.Hour = 21;
}

public class ElectricCar // 

Registering Plugins With Semantic Kernel

Now that we’ve plugins, we have to register them onto Semantic Kernel — with out it, it will not be capable of use them. We’ll register it like this.

var builder = Kernel.CreateBuilder();

// Omitted for brevity

builder.Plugins.AddFromType(); (); 

Wire It All Up

We’ve got plugins — we added them to the Semantic Kernel. Now, we have to inform the kernel that it’s able to utilizing any of those plugins in the way in which it needs. That offers the kernel confidence to make use of them as mandatory. That is carried out by way of OpenAIPromptExecutionSettings handed as a parameter to the InvokePrompt name.

OpenAIPromptExecutionSettings settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
Console.WriteLine(await kernel.InvokePromptAsync("Can you start charging if it is past 10pm?", new(settings)));

That is it! We related all of the items. Now, we must always be capable of get the kernel to do some precise work for us past being tremendous chatty on a regular basis! This is not a lot, however yow will discover all this code on GitHub. I promise that by Half 3, we might be rocking the dance flooring.

Wrap Up

The code we wrote is “just fine.” It would not make the most of the very best of Semantic Kernel but. Plugins are cool items of the Agent puzzle. Plugins are highly effective instruments for constructing absolutely autonomous brokers if wanted by mixing with plans and persona. Now that we’ve a transparent understanding of plugins, we’re able to step foot onto the precise realm of Semantic Kernel, the place it shines and amazes oldie C# people like me most — brokers. We’ll discover brokers within the subsequent a part of this collection and construct an precise agent to plan our day journey, together with guaranteeing our electrical automobile is charged sufficient earlier than we begin the journey.

What’s Subsequent?

Plugins are nice. However they alone cannot go far. In our case, Semantic Kernel might be able to select the precise plugin, however as the issue will get advanced, it will likely be stretched skinny. To realize autonomous decision-making, we should leap to the subsequent step: Brokers. Brokers mix the facility of plugins with a persona, planner, and some different elements to begin behaving as if it has a thoughts. We’d like that to construct absolute autonomy, no less than to an extent. We’ll discover constructing brokers within the subsequent a part of this collection. We’ll use the plugins we constructed right here and construct an precise journey planner that’s able to understanding that our automobile battery doesn’t have sufficient juice to go for a day journey.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version