{"id":2675,"date":"2024-05-17T10:38:02","date_gmt":"2024-05-17T17:38:02","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=2675"},"modified":"2024-05-17T10:38:02","modified_gmt":"2024-05-17T17:38:02","slug":"using-semantic-kernel-to-create-a-time-plugin-with-java","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/using-semantic-kernel-to-create-a-time-plugin-with-java\/","title":{"rendered":"Using Semantic Kernel to create a Time Plugin with Java"},"content":{"rendered":"<p><span data-contrast=\"auto\">Plugins are one of the most powerful features of Semantic Kernel and in this demo <\/span><span data-contrast=\"auto\">we show how you can easily use Plugins with the power of Auto Function Calling from AI Models with Java <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/java-v1\/java\/samples\/sample-code\/src\/main\/java\/com\/microsoft\/semantickernel\/samples\/plugins\/TimePlugin.java\">here<\/a>.<\/span><\/p>\n<p aria-level=\"2\"><strong>A Glimpse into the Demonstration\u00a0<\/strong><\/p>\n<p aria-level=\"3\"><strong>Time Information Plugin\u00a0<\/strong><\/p>\n<p><span data-contrast=\"auto\">In the demo we implement a simple class TimePlugin with one function to retrieve the UTC time in String format.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint language-js\"><code class=\"language-js\">public class TimePlugin {\r\n    public static final String DAY_MONTH_DAY_YEAR = \"EEEE, MMMM d, yyyy\";\r\n\r\n    \/**\r\n     * Get the current date and time for the system default timezone.\r\n     *\r\n     * @return a ZonedDateTime object with the current date and time.\r\n     *\/\r\n    public ZonedDateTime now() {\r\n        return ZonedDateTime.now(ZoneId.systemDefault());\r\n    }\r\n\r\n    \/**\r\n     * Get the current date.\r\n     *\r\n     * &lt;p&gt;Example: {{time.date}} =&gt; Sunday, January 12, 2025\r\n     *\r\n     * @return The current date.\r\n     *\/\r\n    @DefineKernelFunction(\r\n          name = \"date\", \r\n          description = \"Get the current date\")\r\n    public String date(\r\n        @KernelFunctionParameter(\r\n           name = \"locale\", \r\n           description = \"Locale to use when formatting the date\",\r\n           required = false) \r\n        String locale) {\r\n        \/\/ Example: Sunday, 12 January, 2025\r\n        return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR)\r\n            .withLocale(parseLocale(locale))\r\n            .format(now());\r\n    }\r\n\r\n    \/**\r\n     * Get the current time.\r\n     *\r\n     * &lt;p&gt;Example: {{time.time}} =&gt; 9:15:00 AM\r\n     *\r\n     * @return The current time.\r\n     *\/\r\n    @DefineKernelFunction(\r\n        name = \"time\", \r\n        description = \"Get the current time\")\r\n    public String time(\r\n        @KernelFunctionParameter(\r\n             name = \"locale\", \r\n             description = \"Locale to use when formatting the date\", \r\n             required = false) \r\n        String locale) {\r\n        \/\/ Example: 09:15:07 PM\r\n        return DateTimeFormatter.ofPattern(\"hh:mm:ss a\")\r\n            .withLocale(parseLocale(locale))\r\n            .format(now());\r\n    }\r\n}<\/code><\/pre>\n<p>The above code provides a Time Plugin, that allows retrieving the current time so that an agent may relay it to a user.<\/p>\n<p>This plugin can then be added to a Kernel.<\/p>\n<pre>KernelPlugin timePlugin = KernelPluginFactory.createFromObject(new TimePlugin(),\"TimePlugin\");\r\n\r\nvar kernel = Kernel.builder()\r\n    .withAIService(ChatCompletionService.class, openAIChatCompletion)\r\n    .withPlugin(timePlugin)\r\n    .build();\r\n\r\n<\/pre>\n<p>Once added to a Kernel its functions can be invoked from a prompt to add dynamic content to your prompt.<\/p>\n<pre>\u00a0KernelFunction&lt;String&gt; prompt = KernelFunctionFromPrompt\r\n    .&lt;String&gt;createFromPrompt(\"\"\"\r\n        Translate the date {{TimePlugin.date locale=\"English\"}} to French.\r\n    \"\"\")\r\n    .build();\r\n\r\n    FunctionResult&lt;String&gt; timeInFrench = prompt.invokeAsync(kernel).block();\r\n\r\n    System.out.println(timeInFrench.getResult());<\/pre>\n<p aria-level=\"2\">Produces<\/p>\n<pre>Translate the date Wed, May 15, 2024 to French.\r\nMercredi, 15 mai 2024.\r\n\r\n<\/pre>\n<p aria-level=\"2\">Plugins also can be invoked by the Kernel as part of a tool call<\/p>\n<pre>\/\/ What is the current time at UTC?\r\nKernelFunction&lt;String&gt; promptWithToolCall = KernelFunctionFromPrompt\r\n    .&lt;String&gt;createFromPrompt(\"\"\"\r\n        Quelle est l'heure actuelle \u00e0 UTC.\r\n    \"\"\")\r\n    .build();\r\n\r\nFunctionResult&lt;String&gt; timeAtUtcInFrench = promptWithToolCall\r\n    .invokeAsync(kernel)\r\n    .withToolCallBehavior(ToolCallBehavior.allowAllKernelFunctions(true))\r\n    .block();\r\n\r\nSystem.out.println(timeAtUtcInFrench.getResult());<\/pre>\n<p>Produces:<\/p>\n<pre>Quelle est l'heure actuelle \u00e0 UTC.\r\nL'heure actuelle \u00e0 UTC est 01:58:50 PM.<\/pre>\n<p>Due to passing <code>withToolCallBehavior(ToolCallBehavior.allowAllKernelFunctions(true))<\/code> 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 <code>TimePlugin.time(\"utc\")<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p aria-level=\"2\"><b><span data-contrast=\"none\">Dive Deeper<\/span><\/b><span data-contrast=\"none\">\u202f<\/span><span data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Please reach out if you have any questions or feedback through our <a class=\"Hyperlink SCXW128429093 BCX8\" href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/discussions\/categories\/general\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW128429093 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW128429093 BCX8\" data-ccp-charstyle=\"normaltextrun\">Semantic Kernel GitHub Discussion Channel<\/span><\/span><\/a><\/span><span class=\"TextRun SCXW236774567 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW236774567 BCX8\" data-ccp-charstyle=\"normaltextrun\">. We look forward to hearing from you!<\/span><span class=\"NormalTextRun SCXW236774567 BCX8\" data-ccp-charstyle=\"eop\" data-ccp-charstyle-defn=\"{&quot;ObjectId&quot;:&quot;a76df39c-72df-4e55-b0d4-80f5cc09615e|3&quot;,&quot;ClassId&quot;:1073872969,&quot;Properties&quot;:[201342446,&quot;1&quot;,201342447,&quot;5&quot;,201342448,&quot;3&quot;,201342449,&quot;1&quot;,469777841,&quot;Aptos&quot;,469777842,&quot;Arial&quot;,469777843,&quot;\uff2d\uff33 \u660e\u671d&quot;,469777844,&quot;Aptos&quot;,201341986,&quot;1&quot;,469769226,&quot;Aptos,Arial,\uff2d\uff33 \u660e\u671d&quot;,268442635,&quot;24&quot;,335559704,&quot;1025&quot;,335559705,&quot;1041&quot;,335551547,&quot;1033&quot;,335559740,&quot;279&quot;,201341983,&quot;0&quot;,335559739,&quot;160&quot;,469775450,&quot;eop&quot;,201340122,&quot;1&quot;,134233614,&quot;true&quot;,469778129,&quot;eop&quot;,335572020,&quot;1&quot;,469778324,&quot;Default Paragraph Font&quot;]}\">\u202f\u00a0<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0 Time Information Plugin\u00a0 In the demo we implement a simple class TimePlugin with one [&hellip;]<\/p>\n","protected":false},"author":149071,"featured_media":2365,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[24,2,1],"tags":[48,20,63,9],"class_list":["post-2675","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-samples","category-semantic-kernel","tag-ai","tag-java","tag-microsoft-semantic-kernel","tag-semantic-kernel"],"acf":[],"blog_post_summary":"<p>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\u00a0 Time Information Plugin\u00a0 In the demo we implement a simple class TimePlugin with one [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/2675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/149071"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=2675"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/2675\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/2365"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=2675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=2675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=2675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}