Home/Agentic AI/Semantic Kernel/Plugins & Functions

Microsoft Semantic Kernel

Master Semantic Kernel for building enterprise-grade AI agents with plugin architecture

Plugins & Functions

Semantic Kernel's power comes from its plugin architecture. Plugins are collections of functions that extend your kernel's capabilities. SK treats AI functions (prompts) and native functions (code) identically - both are just functions the kernel can call.

Interactive: Semantic vs Native Functions

Semantic Function (AI Prompt)

Uses LLM to process input. Define prompt template with variables.

# config.json - Configuration
{
  "schema": 1,
  "description": "Summarize text concisely",
  "execution_settings": {
    "default": {
      "max_tokens": 200,
      "temperature": 0.7
    }
  }
}

# skprompt.txt - Prompt Template
Summarize the following text in {{$style}} style:

---
{{$input}}
---

Summary:
Usage:
// Use the function
var result = await kernel.InvokeAsync(
    "SummarizePlugin",
    "Summarize",
    new() {
        ["input"] = longText,
        ["style"] = "bullet points"
    }
);

🔧 Creating and Using Plugins

1. Create a Native Plugin (C#)
using Microsoft.SemanticKernel;

public class EmailPlugin
{
    [KernelFunction]
    [Description("Send an email")]
    public async Task<string> SendEmail(
        [Description("Recipient email")] string to,
        [Description("Email subject")] string subject,
        [Description("Email body")] string body
    )
    {
        // Your email sending logic
        await emailService.SendAsync(to, subject, body);
        return "Email sent successfully";
    }
}
2. Register Plugin with Kernel
var kernel = Kernel.CreateBuilder()
    .AddAzureOpenAIChatCompletion(endpoint, apiKey, model)
    .Build();

// Import plugin
kernel.ImportPluginFromType<EmailPlugin>("EmailPlugin");

// Or import from directory (for semantic functions)
kernel.ImportPluginFromPromptDirectory("./Plugins/WriterPlugin");
3. Invoke Functions
// Direct invocation
var result = await kernel.InvokeAsync(
    "EmailPlugin",
    "SendEmail",
    new() {
        ["to"] = "user@example.com",
        ["subject"] = "Hello",
        ["body"] = "Test email"
    }
);

// Or let planner decide when to call it
var plan = await planner.CreatePlanAsync("Send a welcome email to new users");
await plan.InvokeAsync(kernel);

✨ Function Attributes Explained

[KernelFunction]

Marks a method as a function that SK can invoke. Without this, SK won't recognize the function.

[Description("...")]

Describes what the function does. Used by planners to decide when to call this function. Be descriptive and specific.

💡 Good: "Calculate compound interest given principal, rate, and years"

Parameter Descriptions

Add [Description] to parameters so planners know what values to pass. Helps with automatic function selection.

💡 Example: [Description("The year to calculate interest for")] int year

🎯 Plugin Best Practices

  • Single responsibility: Each plugin should focus on one domain (Email, Math, Weather)
  • Descriptive names: Use clear function names and detailed descriptions for planner
  • Mix AI and native: Combine semantic functions (summarize) with native (send email)
  • Return useful values: Functions should return data that other functions can use
Prev