Configuring Agents with Semantic Kernel Plugins

Warning

The Semantic Kernel Agent Framework is in preview and is subject to change.

Functions and Plugins in Semantic Kernel

Function calling is a powerful tool that allows developers to add custom functionalities and expand the capabilities of AI applications. The Semantic Kernel Plugin architecture offers a flexible framework to support Function Calling. For an Agent, integrating Plugins and Function Calling is built on this foundational Semantic Kernel feature.

Once configured, an agent will choose when and how to call an available function, as it would in any usage outside of the Agent Framework.

Agents are currently unavailable in Java.

Adding Plugins to an Agent

Any Plugin available to an Agent is managed within its respective Kernel instance. This setup enables each Agent to access distinct functionalities based on its specific role.

Plugins can be added to the Kernel either before or after the Agent is created. The process of initializing Plugins follows the same patterns used for any Semantic Kernel implementation, allowing for consistency and ease of use in managing AI capabilities.

Note: For a Chat Completion Agent, the function calling mode must be explicitly enabled. Open AI Assistant agent is always based on automatic function calling.

// Factory method to product an agent with a specific role.
// Could be incorporated into DI initialization.
ChatCompletionAgent CreateSpecificAgent(Kernel kernel, string credentials)
{
    // Clone kernel instance to allow for agent specific plug-in definition
    Kernel agentKernel = kernel.Clone();

    // Initialize plug-in from type
    agentKernel.CreatePluginFromType<StatelessPlugin>();

    // Initialize plug-in from object
    agentKernel.CreatePluginFromObject(new StatefulPlugin(credentials));

    // Create the agent
    return 
        new ChatCompletionAgent()
        {
            Name = "<agent name>",
            Instructions = "<agent instructions>",
            Kernel = agentKernel,
            Arguments = new KernelArguments(
                new OpenAIPromptExecutionSettings() 
                { 
                    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() 
                })
        };
}
# Create the instance of the Kernel
kernel = Kernel()

# Define the service ID
service_id = "<service ID>"

# Add the chat completion service to the Kernel
kernel.add_service(AzureChatCompletion(service_id=service_id))

# Get the AI Service settings for the specified service_id
settings = kernel.get_prompt_execution_settings_from_service_id(service_id=service_id)

# Configure the function choice behavior to auto invoke kernel functions
settings.function_choice_behavior = FunctionChoiceBehavior.Auto()

# Add the Plugin to the Kernel
kernel.add_plugin(SamplePlugin(), plugin_name="<plugin name>")

# Create the agent
agent = ChatCompletionAgent(
    service_id=service_id, 
    kernel=kernel, 
    name=<agent name>, 
    instructions=<agent instructions>, 
    execution_settings=settings,
)

Agents are currently unavailable in Java.

Adding Functions to an Agent

A Plugin is the most common approach for configuring Function Calling. However, individual functions can also be supplied independently including prompt functions.

// Factory method to product an agent with a specific role.
// Could be incorporated into DI initialization.
ChatCompletionAgent CreateSpecificAgent(Kernel kernel)
{
    // Clone kernel instance to allow for agent specific plug-in definition
    Kernel agentKernel = kernel.Clone();

    // Initialize plug-in from a static function
    agentKernel.CreateFunctionFromMethod(StatelessPlugin.AStaticMethod);

    // Initialize plug-in from a prompt
    agentKernel.CreateFunctionFromPrompt("<your prompt instructiosn>");

    // Create the agent
    return 
        new ChatCompletionAgent()
        {
            Name = "<agent name>",
            Instructions = "<agent instructions>",
            Kernel = agentKernel,
            Arguments = new KernelArguments(
                new OpenAIPromptExecutionSettings() 
                { 
                    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() 
                })
        };
}
# Create the instance of the Kernel
kernel = Kernel()

# Define the service ID
service_id = "<service ID>"

# Add the chat completion service to the Kernel
kernel.add_service(AzureChatCompletion(service_id=service_id))

# Get the AI Service settings for the specified service_id
settings = kernel.get_prompt_execution_settings_from_service_id(service_id=service_id)

# Configure the function choice behavior to auto invoke kernel functions
settings.function_choice_behavior = FunctionChoiceBehavior.Auto()

# Add the Plugin to the Kernel
kernel.add_plugin(SamplePlugin(), plugin_name="<plugin name>")

# Create the agent
agent = ChatCompletionAgent(
    service_id=service_id, 
    kernel=kernel, 
    name=<agent name>, 
    instructions=<agent instructions>, 
    execution_settings=settings,
)

Agents are currently unavailable in Java.

Limitations for Agent Function Calling

When directly invoking aChat Completion Agent, all Function Choice Behaviors are supported. However, when using an Open AI Assistant or Agent Chat, only Automatic Function Calling is currently available.

How-To

For an end-to-end example for using function calling, see: