On our previous chapter, we went through some of the basic concepts of Semantic Kernel, finishing with a working Agent that was able to respond to generic questions, but with a predefined tone and purpose using the instructions.
On this second chapter, we will add specific skills to our Librarian using Plugins.
What is a Plugin?
A allows the model to invoke native code (python, C# or Java). A plugin is represented as a class, where any function can be defined as invokable from the Agent using annotations. The developer must provide some information to the model with the annotations: name, description and arguments.
To define a Native Plugin we must only create the class and add the corresponding annotations:
from datetime import datetime
from typing import Annotated
from semantic_kernel.functions.kernel_function_decorator import kernel_function
class MyFormatterPlugin():
@kernel_function(name='format_current_date', description='Call to format current date to specific strftime format') # Define the function as invokable
def formate_current_date(
self,
strftime_format: Annotated[str, 'Format, must follow strftime syntax'] # Describe the arguments
) -> Annotated[str, 'Current date on the specified format']: # Describe the return value
return datetime.today().strftime(strftime_format)
To add a Native Plugin into the Kernel we need to create a new instance of the class:
self.kernel.add_plugin(MyFormatterPlugin(), plugin_name="my_formatter_plugin")
Function calling
Function calling, or
In Semantic Kernel, we must tell the agent to use function calling. This is done by defining an execution settings with the function choice behavior as automatic:
# Create the settings
settings = AzureChatPromptExecutionSettings()
# Set the behavior as automatic
settings.function_choice_behavior = FunctionChoiceBehavior.Auto()
# Pass the settings to the agent
self.agent = ChatCompletionAgent(
service_id='chat_completion',
kernel=self.kernel,
name='Assistant',
instructions="The prompt",
execution_settings=settings
)
It is important to emphasize that the more detailed the descriptions are, the more tokens are being used, so it is more costly. It is key to find a balance between good detailed descriptions and tokens used.
Plugins for our Librarian
Now that it is clear what a function is and its purpose, let's see how we can get the most out of it for our Librarian agent.
For learning purposes, we will define one Native Plugin and one Prompt Plugin:
Book repository plugin: it is a Native Plugin to retrive books from a repository.
Poem creator Plugin: it is a Prompt Plugin to create a poem from the first sentence of a book.
Book repository plugin
We use the and .
In the next chapter, we will include some capabilities in the chat to inspect in real time how our model calls and interacts with our plugins by creating an Inspector.
SOCIAL SHARE CARD GENERATOR