GenAIHub
← Back to Technical Section

Semantic Kernel

Microsoft's Enterprise AI Orchestration SDK

What is Semantic Kernel?

Semantic Kernel is an open-source SDK by Microsoft that acts as an AI orchestration layer, enabling developers to integrate LLMs (OpenAI, Azure OpenAI, Hugging Face) with traditional programming languages like C#, Python, and Java. It connects your existing code to AI models through plugins, enabling the creation of enterprise-grade, agentic AI applications.

"Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your C#, Python, or Java codebase."

Plugins

Extend AI capabilities

Planners

Auto orchestration

Memory

Context persistence

Enterprise

Production ready

Core Concepts

Kernel

The central orchestrator that manages AI services, plugins, and memory. Think of it as the "brain" that coordinates all components and routes requests to the appropriate handlers.

Plugins

Encapsulated groups of functions that expose APIs and capabilities to AI. Plugins contain Kernel Functions that can be native code or prompt-based. The AI can discover and invoke these functions automatically.

Native Functions
Prompt Functions
OpenAPI Plugins

Planners

AI-powered components that interpret user requests ("asks") and dynamically select and combine plugins into a sequence of steps. Planners enable goal-driven, autonomous execution.

Memory

Semantic memory stores facts and context using embeddings and vector databases. Supports Azure AI Search, Chroma, Pinecone, Qdrant, and more for RAG patterns.

Quick Start: Python

Create a simple chat completion with Semantic Kernel:

# Install Semantic Kernel
# pip install semantic-kernel

import asyncio
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

async def main():
    # Create kernel
    kernel = Kernel()
    
    # Add AI service
    kernel.add_service(
        OpenAIChatCompletion(
            service_id="chat",
            ai_model_id="gpt-4o-mini"
        )
    )
    
    # Simple prompt
    result = await kernel.invoke_prompt(
        "What is the capital of France?"
    )
    
    print(result)

asyncio.run(main())

Expected Output

"The capital of France is Paris."

Example: Creating a Plugin

Define custom functions that AI can discover and call:

from semantic_kernel import Kernel
from semantic_kernel.functions import kernel_function
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior

# Define a plugin with functions
class WeatherPlugin:
    """Plugin for weather-related functions."""
    
    @kernel_function(
        name="get_weather",
        description="Gets the weather for a given city"
    )
    def get_weather(self, city: str) -> str:
        """Get weather for a city."""
        # In production, call a real weather API
        return f"The weather in {city} is 22°C and sunny."
    
    @kernel_function(
        name="get_forecast",
        description="Gets the 5-day forecast for a city"
    )
    def get_forecast(self, city: str, days: int = 5) -> str:
        """Get forecast for a city."""
        return f"The {days}-day forecast for {city}: Sunny with highs of 24°C."

async def main():
    kernel = Kernel()
    
    # Add AI service with auto function calling
    service = OpenAIChatCompletion(service_id="chat", ai_model_id="gpt-4o")
    kernel.add_service(service)
    
    # Register the plugin
    kernel.add_plugin(WeatherPlugin(), plugin_name="Weather")
    
    # Enable automatic function calling
    settings = kernel.get_prompt_execution_settings_class(service_id="chat")()
    settings.function_choice_behavior = FunctionChoiceBehavior.Auto()
    
    # AI will automatically call the plugin!
    result = await kernel.invoke_prompt(
        "What's the weather like in London?",
        settings=settings
    )
    print(result)

Expected Output

[Function Call: Weather.get_weather(city="London")]
"The weather in London is currently 22°C and sunny."

Example: C# / .NET

Semantic Kernel has first-class support for C#:

// Install: dotnet add package Microsoft.SemanticKernel

using Microsoft.SemanticKernel;
using System.ComponentModel;

// Create the kernel
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("gpt-4o", apiKey);
var kernel = builder.Build();

// Define a plugin class
public class MathPlugin
{
    [KernelFunction, Description("Adds two numbers")]
    public int Add([Description("First number")] int a, 
                    [Description("Second number")] int b)
    {
        return a + b;
    }
    
    [KernelFunction, Description("Multiplies two numbers")]
    public int Multiply(int a, int b) => a * b;
}

// Register and use
kernel.ImportPluginFromType<MathPlugin>("Math");

var settings = new OpenAIPromptExecutionSettings {
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};

var result = await kernel.InvokePromptAsync(
    "What is 42 + 58?", 
    new(settings)
);

Console.WriteLine(result); // "42 + 58 = 100"

Multi-Language Support

C#

.NET / C#

Primary platform with full features

Microsoft.SemanticKernel
Py

Python

Full feature parity with .NET

semantic-kernel

Java

Growing feature set

semantic-kernel-java

Supported AI Services

OpenAI

GPT-4o, GPT-4

Azure OpenAI

Enterprise

Google

Gemini

Hugging Face

Open models

Anthropic

Claude

Ollama

Local models

Mistral

Mixtral

Local LLMs

Phi, Llama

Semantic Kernel vs Others

Aspect Semantic Kernel LangChain AutoGen
Backed By Microsoft LangChain Inc Microsoft
Primary Language C# / .NET Python Python
Architecture Plugins + Planners Chains + Agents Conversations
Enterprise Focus Strong Medium Medium
Best For .NET enterprise apps Python AI apps Multi-agent chat

Part of the Copilot Stack

Microsoft's AI Orchestration Layer

Semantic Kernel is the same technology that powers Microsoft 365 Copilot, Bing Chat, and other Microsoft AI products. Build enterprise AI applications with the same foundation as Microsoft's flagship products.

Resources & References

Related Topics