From Chat to Code: Building Intelligent Agents with Semantic Kernel Plugins
As .NET developers, we've long been masters of structure, types, and services. But now, with the rise of conversational AI and powerful orchestration frameworks like Microsoft Semantic Kernel, we're entering a new era—where code responds not only to structured APIs but also to human intent. We can now turn natural language into real actions through intelligent agents that bridge the gap between chat and executable logic.
In this second article of the series, we're diving into a real, working application that brings this to life. We’ll show how to integrate Semantic Kernel with your C# backend to build a plugin-enabled agent that can:
Let’s roll up our sleeves and see how chat becomes code—and code becomes intelligence.
The Vision: Intelligent Agents in .NET
What if your app could take a request like:
“Can you schedule a meeting for 3 days from now at 5 pm, and tell me the result of (5+5+5+2+210)10?”
…and automatically:
All without any hardcoded branching logic. With Semantic Kernel and function plugins, that’s exactly what we can do.
Semantic Kernel + Function Calling: What It Is
Semantic Kernel (SK) acts as an orchestration engine. It allows you to:
This turns your C# codebase into a dynamic toolkit, letting the AI mix logic with reasoning in ways traditional command handling could never achieve.
Code Walkthrough: From Setup to Intelligence
Let’s look at the heart of the application:
var builder = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion("gpt-4o",
endpoint: "https://khaledsemantickernelkh.openai.azure.com/",
apiKey: "");
var kernel = builder.Build();
kernel.ImportPluginFromObject(new MyTools(), "tools");
kernel.ImportPluginFromObject(new ScheduleMeeting(), "meetingScheduler");
var chat = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
Console.Write("Please enter your prompt: ");
var userInput = Console.ReadLine();
history.AddUserMessage(userInput);
var resultChat = await chat.GetChatMessageContentAsync(history,
new PromptExecutionSettings {
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
},
kernel);
Console.WriteLine(resultChat.Content);
Console.WriteLine($"Chat History : {JsonSerializer.Serialize(history)}");
Key Components:
Plugins: The Secret Sauce of Intelligence
Let’s unpack the plugins:
public class MyTools
{
[KernelFunction, Description("Add two integers")]
public int Add(int a, int b) => a + b;
[KernelFunction, Description("Multiply two integers")]
public int Multiply(int a, int b) => a * b;
}
public class ScheduleMeeting
{
[KernelFunction, Description("Get the current date and time")]
public DateTime Now() => DateTime.Now;
[KernelFunction, Description("Schedule a meeting with a given date and time")]
public string Schedule(string date, string time) =>
$"Meeting scheduled on {date} at {time}";
}
How Plugins Work:
Recommended by LinkedIn
FunctionChoiceBehavior.Auto(): Empowering Dynamic Execution
The real magic happens with:
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
This lets GPT-4o decide—based on the user prompt and the available plugin descriptions—what tool(s) to call.
Unlike older versions where developers had to manually choose which function to call, this new auto mode enables true orchestration:
This is what unlocks goal-driven automation.
Sample Run: From Prompt to Actions
Let's walk through this user input:
"Could you please schedule a meeting for me for the next 3 days at 5 pm and also would like to know the result of the following equations (5+5+5+2+210)10?"
Here’s what SK does under the hood:
Chat History and Memory
var history = new ChatHistory();
history.AddUserMessage(userInput);
The chat history tracks all interactions. This is essential for multi-turn conversations and memory-driven reasoning in future iterations—think “remind me to reschedule that meeting” or “repeat the math from before.”
Why This Matters: From Responding to Reasoning
What we’ve built isn’t just a chatbot.
It’s the foundation of an intelligent agent—a system that doesn’t just understand what you say, but can:
Semantic Kernel isn’t a toy—it’s a new runtime layer for smart automation. One where your C# methods are tools in a reasoning loop.
What You Can Do Next
Now that you’ve seen plugins in action, consider building:
The sky’s the limit when you combine language, logic, and plugins.
Ready to plug intelligence into your .NET codebase? Let Semantic Kernel be your AI runtime—and if you’ve already started, I’d love to hear about it. What’s been your experience with Semantic Kernel so far, and what’s the most exciting thing you’ve built using it?
Interesting 🤔