{"id":2668,"date":"2024-05-21T09:14:58","date_gmt":"2024-05-21T16:14:58","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=2668"},"modified":"2024-05-21T09:14:58","modified_gmt":"2024-05-21T16:14:58","slug":"semantic-kernel-time-plugin-with-python","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/semantic-kernel-time-plugin-with-python\/","title":{"rendered":"Semantic Kernel Time Plugin with Python"},"content":{"rendered":"<p>We are excited to showcase a Semantic Kernel sample across all 3 languages: .NET, Python and Java! Today we\u2019re going to dive into the Python version of the TimePlugin sample which is featured <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/python\/samples\/concepts\/plugins\/openai_function_calling_with_custom_plugin.py\">here.<\/a><\/p>\n<p>Prerequisites for the Azure OpenAI key or OpenAI key:<\/p>\n<p>As covered in our Python <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/python\/README.md#openai--azure-openai-api-keys\">README.md<\/a> or <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/python\/DEV_SETUP.md#llm-setup\">DEV_SETUP.md<\/a>, Python leverages Pydantic settings. This enables developers to specify environment variables or provide a <code>.env<\/code> file path containing secrets, keys, and endpoints. Alternatively, keys and secrets can be managed manually and specified directly through the constructors of Chat, Text, or Embedding modules.<\/p>\n<p>The following code shows how to configure auto function calling on the PromptExecutionSettings for specific plugins:<\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">settings.function_call_behavior = FunctionCallBehavior.EnableFunctions(\r\n    auto_invoke=True, filters={\"included_plugins\": [\"weather\", \"time\"]}\r\n)<\/code><\/pre>\n<p class=\"prettyprint language-py\">And the following code shows how to add plugins to the kernel:<\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">kernel.add_plugin(TimePlugin(), plugin_name=\"time\") \r\nkernel.add_plugin(WeatherPlugin(), plugin_name=\"weather\")<\/code><\/pre>\n<p><strong>A Glimpse into the TimePlugin<\/strong><\/p>\n<p>We\u2019ll dive into the details of the Python <code>TimePlugin<\/code> showcasing the use of plguins and how a plugin can return the time and additional items through a user to agent chat.<\/p>\n<p>When running the OpenAI function calling with custom plugin sample, you\u2019ll want to note the following plugin section:<\/p>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">class WeatherPlugin:\r\n    \"\"\"A sample plugin that provides weather information for cities.\"\"\"\r\n\r\n    @kernel_function(\r\n        name=\"get_weather_for_city\", \r\n        description=\"Get the weather for a city\"\r\n    )\r\n    def get_weather_for_city(\r\n        self, \r\n        city: Annotated[str, \"The input city\"]\r\n    ) -&gt; Annotated[str, \"The output is a string\"]:\r\n        if city == \"Boston\":\r\n            return \"61 and rainy\"\r\n        elif city == \"London\":\r\n            return \"55 and cloudy\"\r\n        elif city == \"Miami\":\r\n            return \"80 and sunny\"\r\n        elif city == \"Paris\":\r\n            return \"60 and rainy\"\r\n        elif city == \"Tokyo\":\r\n            return \"50 and sunny\"\r\n        elif city == \"Sydney\":\r\n            return \"75 and sunny\"\r\n        elif city == \"Tel Aviv\":\r\n            return \"80 and sunny\"\r\n        else:\r\n            return \"31 and snowing\"<\/code><\/pre>\n<p><strong>Getting Started<\/strong><\/p>\n<p>Steps to use the Sample.<\/p>\n<ol>\n<li>Clone the semantic kernel repository.<\/li>\n<li>Open your favorite IDE (ex. VSCode)\n<ol>\n<li>Open in the root repository folder.<\/li>\n<li>Go into VS Code and open the following sample (<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/python\/samples\/concepts\/plugins\/openai_function_calling_with_custom_plugin.py\">openai_function_calling_with_custom_plugin.py<\/a>) located in semantic-kernel in the python folder -&gt; then navigate and open the samples folder -&gt; then open the concepts folder -&gt; then open the plugins folder and navigate to the sample.<\/li>\n<li>Run or debug the sample in the IDE.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><strong>Dive Deeper<\/strong><\/p>\n<p>Please reach out if you have any questions or feedback through our <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/discussions\/categories\/general\">Semantic Kernel GitHub Discussion Channel<\/a>. We look forward to hearing from you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are excited to showcase a Semantic Kernel sample across all 3 languages: .NET, Python and Java! Today we\u2019re going to dive into the Python version of the TimePlugin sample which is featured here. Prerequisites for the Azure OpenAI key or OpenAI key: As covered in our Python README.md or DEV_SETUP.md, Python leverages Pydantic settings. [&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":[1],"tags":[48,63,67,53,9,77],"class_list":["post-2668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-semantic-kernel","tag-ai","tag-microsoft-semantic-kernel","tag-plugin","tag-python","tag-semantic-kernel","tag-timeplugin"],"acf":[],"blog_post_summary":"<p>We are excited to showcase a Semantic Kernel sample across all 3 languages: .NET, Python and Java! Today we\u2019re going to dive into the Python version of the TimePlugin sample which is featured here. Prerequisites for the Azure OpenAI key or OpenAI key: As covered in our Python README.md or DEV_SETUP.md, Python leverages Pydantic settings. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/2668","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=2668"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/2668\/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=2668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=2668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=2668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}