Getting Started with Semantic Kernel in .NET: A Simple Guide for Developers
Introduction
If you're a .NET developer stepping into the world of AI, you've probably heard about integrating large language models (LLMs) into applications but doing that in a structured, maintainable, and scalable way can feel overwhelming.
This is where Semantic Kernel becomes extremely useful.
Semantic Kernel is an open-source SDK that helps you integrate AI (like OpenAI models) into your .NET applications in a clean, organized, and developer-friendly way.
Instead of treating AI like a random external service, Semantic Kernel helps you structure AI like part of your application architecture.
What Problem Does Semantic Kernel Solve?
Before jumping into code, let’s understand the real issue.
Without Semantic Kernel
You might:
With Semantic Kernel
You:
Think of it as dependency injection + service layer, but for AI
Step-by-Step Code with Explanation
Let’s build a simple feature: Text Summarizer
Step 1: Install Package
dotnet add package Microsoft.SemanticKernel
Why?
This package gives you:
Without this, you'd manually manage HTTP calls and JSON parsing.
Step 2: Create the Kernel
using Microsoft.SemanticKernel;
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion(
modelId: "gpt-4",
apiKey: "your-api-key"
);
var kernel = builder.Build();
What’s Happening Here?
Let’s break it down:
1. Kernel.CreateBuilder()
2. AddOpenAIChatCompletion(...)
Reason: This follows Dependency Injection principles. Instead of calling APIs directly, you let the kernel manage AI communication.
3. builder.Build()
Think of kernel as your AI runtime engine
Step 3: Create a Semantic Function
var summarizeFunction = kernel.CreateFunctionFromPrompt(
"Summarize the following text:\n{{$input}}"
);
What’s Happening?
This is where AI becomes “programmable”.
Key Concept: Prompt Template
Summarize the following text:
{{$input}}
Why This Design?
Instead of writing:
string prompt = "Summarize: " + text;
You:
This is similar to parameterized SQL queries (cleaner, safer, reusable)
Step 4: Execute the Function
var result = await kernel.InvokeAsync(
summarizeFunction,
new() { ["input"] = "Long text here..." }
);
Console.WriteLine(result);
Explanation
1. InvokeAsync(...)
2. new() { ["input"] = "..." }
This is like calling:
Recommended by LinkedIn
MyMethod(input: "value");
Why This Approach is Powerful
1. Reusability
You can reuse the same function:
await kernel.InvokeAsync(summarizeFunction, new() { ["input"] = article1 });
await kernel.InvokeAsync(summarizeFunction, new() { ["input"] = article2 });
No need to rewrite prompts.
2. Clean Separation
This avoids messy string concatenation everywhere.
3. Easy Testing
You can:
Going One Level Deeper: Native Functions
You can mix AI with normal C# code.
Example
public class DiscountPlugin
{
public double CalculateDiscount(double price)
{
return price * 0.1;
}
}
Register it:
kernel.ImportPluginFromObject(new DiscountPlugin(), "Discount");
Why This Matters?
Now you can:
Example flow:
Understanding the Planner
Instead of writing:
await summarize();
await translate();
await format();
You say:
“Summarize and translate this text”
Planner:
This is like giving AI decision-making ability
Memory
Semantic Kernel can store context using embeddings.
Why useful?
Real Example Architecture
In a real .NET app:
Treat Semantic Kernel like a service layer for AI
Common Mistakes
1. Mixing Prompts Everywhere
Don’t do this:
var prompt1 = "...";
var prompt2 = "...";
Use functions instead.
2. Overusing AI
Not everything needs AI:
3. Ignoring Structure
If you don’t organize plugins:
Semantic Kernel is not just about calling AI—it’s about engineering AI into your application properly.