Plugins are a key element when building agents in Semantic Kernel. They allow you to extend the capabilities of your Large Language Model with extra functions. This post is not about writing your own plugin but
Just as a reminder, registering a plugin can be done like this:
var builder = Kernel.CreateBuilder() | |
.AddOpenAIChatCompletion( | |
modelId: "phi3", | |
apiKey: null, | |
endpoint: new Uri("http://localhost:11434"));// With Ollama OpenAI API endpoint | |
builder.Services.AddLogging(c => c.SetMinimumLevel(LogLevel.Debug)); | |
builder.Plugins | |
.AddFromType<MathPlugin>() | |
.AddFromType<ConversationSummaryPlugin>() | |
.AddFromType<TimePlugin>("time"); |
Here is the code I used to try to invoke the TimePlugin in a prompt:
const string promptTemplate = @" | |
Today is: {{TimePlugin.Date}} | |
Current time is: {{TimePlugin.Time}} | |
Answer to the following questions using JSON syntax, including the data used. | |
Is it morning, afternoon, evening, or night (morning/afternoon/evening/night)? | |
Is it weekend time (weekend/not weekend)?"; | |
var results = await kernel.InvokePromptAsync(promptTemplate); | |
Console.WriteLine(results); |
However when I did this the following error was returned:
System.Collections.Generic.KeyNotFoundException: 'The plugin collection does not contain a plugin and/or function with the specified names. Plugin name – 'time', function name - 'Date'.'
The problem is caused because I had configured an alias name for the plugin but I was using the plugin name in the prompt.
I have 2 ways to fix the issue above.
Either I update the prompt to point to the configured alias:
const string promptTemplate = @" | |
Today is: {{time.Date}} | |
Current time is: {{time.Time}} | |
Answer to the following questions using JSON syntax, including the data used. | |
Is it morning, afternoon, evening, or night (morning/afternoon/evening/night)? | |
Is it weekend time (weekend/not weekend)?"; | |
var results = await kernel.InvokePromptAsync(promptTemplate); | |
Console.WriteLine(results); |
Or I remove the alias from my configuration:
var builder = Kernel.CreateBuilder() | |
.AddOpenAIChatCompletion( | |
modelId: "phi3", | |
apiKey: null, | |
endpoint: new Uri("http://localhost:11434"));// With Ollama OpenAI API endpoint | |
builder.Services.AddLogging(c => c.SetMinimumLevel(LogLevel.Debug)); | |
builder.Plugins | |
.AddFromType<MathPlugin>() | |
.AddFromType<ConversationSummaryPlugin>() | |
.AddFromType<TimePlugin>(); //Use 'TimePlugin' to refer to the plugin |
Hope that helps…