Now that you're familiar with Kernel basics, let's see how the kernel allows you to run Semantic Plugins and Semantic Functions stored on disk.
A Semantic Plugin is a collection of Semantic Functions, where each function is defined with natural language that can be provided with a text file.
Refer to our glossary for an in-depth guide to the terms.
The repository includes some examples under the samples folder.
For instance, this is the Joke function part of the FunPlugin plugin:
WRITE EXACTLY ONE JOKE or HUMOROUS STORY ABOUT THE TOPIC BELOW.
JOKE MUST BE:
- G RATED
- WORKPLACE/FAMILY SAFE
NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY.
BE CREATIVE AND FUNNY. I WANT TO LAUGH.
+++++
{{$input}}
+++++
Note the special {{$input}}
token, which is a variable that is automatically passed when invoking the function, commonly referred to as a "function parameter".
We'll explore later how functions can accept multiple variables, as well as invoke other functions.
In the same folder you'll notice a second config.json file. The file is optional, and is used to set some parameters for large language models like Temperature, TopP, Stop Sequences, etc.
{
"schema": 1,
"description": "Generate a funny joke",
"execution_settings": [
{
"max_tokens": 1000,
"temperature": 0.9,
"top_p": 0.0,
"presence_penalty": 0.0,
"frequency_penalty": 0.0
}
]
}
Given a semantic function defined by these files, this is how to load and use a file based semantic function.
Configure and create the kernel, as usual, loading also the AI backend settings defined in the Setup notebook:
#r "nuget: Microsoft.SemanticKernel, 1.19.0"
#!import config/Settings.cs
using Microsoft.SemanticKernel;
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();
Import the plugin and all its functions:
// FunPlugin directory path
var funPluginDirectoryPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "..", "..", "prompt_template_samples", "FunPlugin");
// Load the FunPlugin from the Plugins Directory
var funPluginFunctions = kernel.ImportPluginFromPromptDirectory(funPluginDirectoryPath);
How to use the plugin functions, e.g. generate a joke about "time travel to dinosaur age":
// Construct arguments
var arguments = new KernelArguments() { ["input"] = "time travel to dinosaur age" };
// Run the Function called Joke
var result = await kernel.InvokeAsync(funPluginFunctions["Joke"], arguments);
// Return the result to the Notebook
Console.WriteLine(result);
Great, now that you know how to load a plugin from disk, let's show how you can create and run a semantic function inline.