2 min read
Implementing Semantic Kernel Plugins for Enterprise Applications
Plugins are the building blocks of Semantic Kernel applications. They enable LLMs to interact with external systems, databases, and APIs, transforming AI from a conversation tool into an action-oriented assistant.
Creating Native Function Plugins
Native plugins expose C# methods to the kernel, allowing the LLM to invoke them when appropriate.
using Microsoft.SemanticKernel;
using System.ComponentModel;
public class CustomerPlugin
{
private readonly ICustomerRepository _repository;
public CustomerPlugin(ICustomerRepository repository)
{
_repository = repository;
}
[KernelFunction("get_customer_orders")]
[Description("Retrieves recent orders for a customer by their email address")]
public async Task<string> GetCustomerOrdersAsync(
[Description("Customer email address")] string email,
[Description("Number of orders to retrieve")] int limit = 5)
{
var customer = await _repository.GetByEmailAsync(email);
if (customer == null)
return "Customer not found.";
var orders = await _repository.GetOrdersAsync(customer.Id, limit);
return System.Text.Json.JsonSerializer.Serialize(orders,
new JsonSerializerOptions { WriteIndented = true });
}
[KernelFunction("update_customer_preferences")]
[Description("Updates customer communication preferences")]
public async Task<string> UpdatePreferencesAsync(
[Description("Customer email")] string email,
[Description("Email opt-in preference")] bool emailOptIn,
[Description("SMS opt-in preference")] bool smsOptIn)
{
var result = await _repository.UpdatePreferencesAsync(
email, emailOptIn, smsOptIn);
return result ? "Preferences updated successfully." : "Update failed.";
}
}
Registering Plugins with the Kernel
var kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(deploymentName, endpoint, apiKey)
.Build();
// Register the plugin
kernel.Plugins.AddFromObject(
new CustomerPlugin(customerRepository),
"CustomerService");
// Enable automatic function calling
var settings = new OpenAIPromptExecutionSettings
{
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
};
var result = await kernel.InvokePromptAsync(
"Check recent orders for customer@example.com",
new KernelArguments(settings));
Well-designed plugins make your AI applications genuinely useful by connecting natural language understanding to real business actions.