Semantic Kernel Time Plugin with Python

Sophia Lagerkrans-Pandey

Evan Mattson

Eduard van Valkenburg

We are excited to showcase a Semantic Kernel sample across all 3 languages: .NET, Python and Java! Today we’re 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. This enables developers to specify environment variables or provide a .env 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.

The following code shows how to configure auto function calling on the PromptExecutionSettings for specific plugins:

settings.function_call_behavior = FunctionCallBehavior.EnableFunctions(
    auto_invoke=True, filters={"included_plugins": ["weather", "time"]}
)

And the following code shows how to add plugins to the kernel:

kernel.add_plugin(TimePlugin(), plugin_name="time") 
kernel.add_plugin(WeatherPlugin(), plugin_name="weather")

A Glimpse into the TimePlugin

We’ll dive into the details of the Python TimePlugin showcasing the use of plguins and how a plugin can return the time and additional items through a user to agent chat.

When running the OpenAI function calling with custom plugin sample, you’ll want to note the following plugin section:

class WeatherPlugin:
    """A sample plugin that provides weather information for cities."""

    @kernel_function(
        name="get_weather_for_city", 
        description="Get the weather for a city"
    )
    def get_weather_for_city(
        self, 
        city: Annotated[str, "The input city"]
    ) -> Annotated[str, "The output is a string"]:
        if city == "Boston":
            return "61 and rainy"
        elif city == "London":
            return "55 and cloudy"
        elif city == "Miami":
            return "80 and sunny"
        elif city == "Paris":
            return "60 and rainy"
        elif city == "Tokyo":
            return "50 and sunny"
        elif city == "Sydney":
            return "75 and sunny"
        elif city == "Tel Aviv":
            return "80 and sunny"
        else:
            return "31 and snowing"

Getting Started

Steps to use the Sample.

  1. Clone the semantic kernel repository.
  2. Open your favorite IDE (ex. VSCode)
    1. Open in the root repository folder.
    2. Go into VS Code and open the following sample (openai_function_calling_with_custom_plugin.py) located in semantic-kernel in the python folder -> then navigate and open the samples folder -> then open the concepts folder -> then open the plugins folder and navigate to the sample.
    3. Run or debug the sample in the IDE.

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