May 14th, 2024

Using Semantic Kernel to create a Time Plugin with .NET

Plugins are one of the most powerful features of Semantic Kernel and in this demo we show how you can easily use Plugins with the power of Auto Function Calling from AI Models.  

A Glimpse into the Demonstration 

Time Information Plugin 

In the demo we implement a simple class TimeInformantionPlugin with one function to retrieve the UTC time in string format. 

public class TimeInformationPlugin 
{ 
    [KernelFunction] 
    [Description("Retrieves the current time in UTC.")] 
    public string GetCurrentUtcTime() => DateTime.UtcNow.ToString("R"); 
}

C# functions that can be imported to Plugins are marked with KernelFunction that will instruct the Kernel what are going to be imported into Plugins using Description attribute will be used on how those functions will be detailed to the AI Model for calling so it will have a better understanding on when to use your functions. 

When implementing functions in your plugins you can use many different signatures, including asynchronous tasks and different types, check our [Concept – Method Function Types]( semantic-kernel/dotnet/samples/Concepts/Functions/MethodFunctions_Types.cs at main · microsoft/semantic-kernel (github.com)) sample for more details. 

  • Info: Is very important providing descriptions to your functions and arguments when using them against the AI for best results. 

Configuring the Kernel 

In the code below we use the configuration extensions from .Net to get the current OpenAI configuration details to setup and initialize the kernel with an OpenAI connector and our TimeInformationPlugin ready to use. 

var config = new ConfigurationBuilder() 
    .AddUserSecrets<Program>() 
    .AddEnvironmentVariables() 
    .Build() 

var kernelBuilder = Kernel.CreateBuilder().AddOpenAIChatCompletion( 
    modelId: config["OpenAI:ChatModelId"]!, 
    apiKey: config["OpenAI:ApiKey"]!); 

kernelBuilder.Plugins.AddFromType<TimeInformationPlugin>(); 

var kernel = kernelBuilder.Build();

Using Auto Function Calling 

With the kernel instance is possible to get the completion service and use it to call the AI. Is important to configure your execution settings to enable AutoInvocation as this is not a default configuration and won’t expose your plugins inadvertently to AI. 

var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>(); 

OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() 
{ 
    // Enable auto function calling 
    ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions 
};

Is also important to create and pass the chat history so it keeps the track of the conversation with the AI in a loop and it improves the overall results. 

chatHistory.AddUserMessage(input); 

var chatResult = await chatCompletionService.GetChatMessageContentAsync(chatHistory, openAIPromptExecutionSettings, kernel); 
Console.Write($"\nAssistant > {chatResult}\n");

For each iteration in this loop, the assistant will get an instruction from the user, and whenever the instruction suggest a need to get the current time, it will be invoked by the AI automatically. 

Dive Deeper 

Please reach out if you have any questions or feedback through our Semantic Kernel GitHub Discussion Channel. We look forward to hearing from you!

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Chris

    I do not see `ToolCallBehavior` in the `HuggingFacePromptExecutionSettings`. When running the application without this setting the time is not returned, which sounds like is the expected functionality when the setting is not used. Is it possible to use HuggingFace with kernel functions?