Using Semantic Kernel to create a Time Plugin with Java

Sophia Lagerkrans-Pandey

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 with Java here.

A Glimpse into the Demonstration 

Time Information Plugin 

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

public class TimePlugin {
    public static final String DAY_MONTH_DAY_YEAR = "EEEE, MMMM d, yyyy";

    /**
     * Get the current date and time for the system default timezone.
     *
     * @return a ZonedDateTime object with the current date and time.
     */
    public ZonedDateTime now() {
        return ZonedDateTime.now(ZoneId.systemDefault());
    }

    /**
     * Get the current date.
     *
     * <p>Example: {{time.date}} => Sunday, January 12, 2025
     *
     * @return The current date.
     */
    @DefineKernelFunction(
          name = "date", 
          description = "Get the current date")
    public String date(
        @KernelFunctionParameter(
           name = "locale", 
           description = "Locale to use when formatting the date",
           required = false) 
        String locale) {
        // Example: Sunday, 12 January, 2025
        return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR)
            .withLocale(parseLocale(locale))
            .format(now());
    }

    /**
     * Get the current time.
     *
     * <p>Example: {{time.time}} => 9:15:00 AM
     *
     * @return The current time.
     */
    @DefineKernelFunction(
        name = "time", 
        description = "Get the current time")
    public String time(
        @KernelFunctionParameter(
             name = "locale", 
             description = "Locale to use when formatting the date", 
             required = false) 
        String locale) {
        // Example: 09:15:07 PM
        return DateTimeFormatter.ofPattern("hh:mm:ss a")
            .withLocale(parseLocale(locale))
            .format(now());
    }
}

The above code provides a Time Plugin, that allows retrieving the current time so that an agent may relay it to a user.

This plugin can then be added to a Kernel.

KernelPlugin timePlugin = KernelPluginFactory.createFromObject(new TimePlugin(),"TimePlugin");

var kernel = Kernel.builder()
    .withAIService(ChatCompletionService.class, openAIChatCompletion)
    .withPlugin(timePlugin)
    .build();

Once added to a Kernel its functions can be invoked from a prompt to add dynamic content to your prompt.

 KernelFunction<String> prompt = KernelFunctionFromPrompt
    .<String>createFromPrompt("""
        Translate the date {{TimePlugin.date locale="English"}} to French.
    """)
    .build();

    FunctionResult<String> timeInFrench = prompt.invokeAsync(kernel).block();

    System.out.println(timeInFrench.getResult());

Produces

Translate the date Wed, May 15, 2024 to French.
Mercredi, 15 mai 2024.

Plugins also can be invoked by the Kernel as part of a tool call

// What is the current time at UTC?
KernelFunction<String> promptWithToolCall = KernelFunctionFromPrompt
    .<String>createFromPrompt("""
        Quelle est l'heure actuelle à UTC.
    """)
    .build();

FunctionResult<String> timeAtUtcInFrench = promptWithToolCall
    .invokeAsync(kernel)
    .withToolCallBehavior(ToolCallBehavior.allowAllKernelFunctions(true))
    .block();

System.out.println(timeAtUtcInFrench.getResult());

Produces:

Quelle est l'heure actuelle à UTC.
L'heure actuelle à UTC est 01:58:50 PM.

Due to passing withToolCallBehavior(ToolCallBehavior.allowAllKernelFunctions(true)) to our invocation, this allows the kernel to invoke functions it needs to achieve a task, in this case it made the choice to obtain the current time via invoking TimePlugin.time("utc").

 

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!  

0 comments

Leave a comment

Feedback usabilla icon