Building AI-Powered Applications with C# and Semantic Kernel
Maria

Maria @chakewitz

About: I’m a code whisperer :) A software developer with a decade of experience building websites, web apps, and the occasional Frankenstein monster made entirely of JavaScript and caffeine

Joined:
May 27, 2025

Building AI-Powered Applications with C# and Semantic Kernel

Publish Date: Aug 5
2 1

Building AI-Powered Applications with C# and Semantic Kernel

In the age of artificial intelligence, developers have an unprecedented opportunity to build intelligent applications that redefine user experiences. Whether you're automating workflows, personalizing recommendations, or generating human-like content, integrating AI into your C# applications opens up exciting possibilities.

But how do you build these AI-powered applications effectively? Enter Semantic Kernel: an open-source SDK from Microsoft designed to simplify the integration of powerful large language models (LLMs) like GPT into your applications.

In this blog post, we'll explore how to use Semantic Kernel in C# to build intelligent applications, diving into concepts like prompt engineering, plugins, and best practices. Whether you're an experienced C# developer or just starting to explore AI, this guide will give you the tools and knowledge to get started.


Why Semantic Kernel?

Imagine Semantic Kernel as a bridge between your C# application and the capabilities of AI-powered LLMs. Traditionally, interacting with LLMs involved sending HTTP requests to APIs, managing prompts, and handling responses manually. Semantic Kernel abstracts much of this complexity, offering:

  • Reusable plugins for common AI functionalities (e.g., summarization, translation).
  • Prompt templates for dynamic interactions with LLMs.
  • Memory storage to retain context across conversations.

This SDK not only simplifies AI integration but also empowers developers to create applications that feel truly intelligent.


Getting Started: Setting Up Semantic Kernel

Before diving into code, let’s ensure your environment is ready.

Prerequisites

  1. C# Development Environment: Visual Studio, Rider, or VS Code.
  2. .NET 6 or higher: Semantic Kernel requires modern .NET versions.
  3. Azure OpenAI Service or OpenAI API Key: You'll need access to GPT models.

Installation

Install Semantic Kernel via NuGet. Run the following command in your project directory:

dotnet add package Microsoft.SemanticKernel
Enter fullscreen mode Exit fullscreen mode

Once installed, you're ready to start coding.


Core Concepts: Prompts, Plugins, and Memory

Let’s break down the building blocks of Semantic Kernel:

1. Prompt Engineering

Think of prompts as instructions for LLMs. They dictate the AI's behavior and output. For example, a prompt for generating code comments might look like this:

"Write concise comments for the following C# code: {code}"

Semantic Kernel allows you to create reusable prompt templates, combining dynamic variables with static text.

Code Example: Creating a Prompt

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Orchestration;

var kernel = Kernel.Builder.Build();

var promptTemplate = @"
    Write a friendly email to a customer about their recent order.
    Include the following details:
    - Customer Name: {{$name}}
    - Order ID: {{$orderId}}
";

var emailPrompt = kernel.CreatePromptTemplate(promptTemplate);

var context = new ContextVariables();
context.Set("name", "John Doe");
context.Set("orderId", "12345");

var result = await kernel.RunAsync(emailPrompt, context);

Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Prompt Template: A mix of static text and dynamic placeholders ({{$name}}).
  • Context Variables: Pass dynamic data to the placeholders at runtime.
  • Output: The AI generates an email using the provided details.

2. Plugins

Plugins are reusable units of functionality in Semantic Kernel. They encapsulate specific tasks, such as summarization, translation, or even custom workflows.

Code Example: Building a Summarization Plugin

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins;

var kernel = Kernel.Builder.Build();

var summarizationPlugin = kernel.ImportSemanticPlugin(
    "SummarizationPlugin", "./plugins/SummarizationPlugin/skprompt.txt");

var document = "C# is a modern, object-oriented programming language developed by Microsoft...";
var result = await kernel.RunAsync(summarizationPlugin["Summarize"], document);

Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Plugin File: The skprompt.txt contains the prompt template for summarization.
  • Importing Plugin: Semantic Kernel allows you to load plugins from external files for modularity.
  • Execution: Run the plugin using the RunAsync method with input data.

3. Memory

AI applications often require context to deliver meaningful results. Semantic Kernel’s memory feature enables storing and retrieving information across interactions.

Code Example: Using Memory

using Microsoft.SemanticKernel;

var kernel = Kernel.Builder.WithMemoryStorage().Build();

// Save memory
await kernel.Memory.SaveInformationAsync(
    "customerInteractions", "JohnDoe", "John Doe placed an order on October 12.");

// Retrieve memory
var memoryResults = await kernel.Memory.SearchAsync("customerInteractions", "John Doe");
foreach (var result in memoryResults)
{
    Console.WriteLine(result.Metadata.Text);
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Memory Storage: Semantic Kernel supports storing information tied to specific keys (JohnDoe).
  • Search: Retrieve relevant context using search queries.

Common Pitfalls (And How to Avoid Them)

Building AI-powered applications isn’t always smooth sailing. Here are some pitfalls to watch out for:

1. Overloading Prompts with Data

Passing excessive or irrelevant data in prompts can confuse the LLM, leading to poor responses. Keep prompts concise and focused.

Solution:

Break large tasks into smaller ones. For example, instead of asking the AI to summarize and translate in one step, create separate plugins for each.

2. Ignoring Memory Management

Storing too much information in memory can bloat your application and degrade performance.

Solution:

Implement expiration policies for stored memory and only save what's strictly necessary.

3. Misaligned Expectations

Remember, LLMs aren't magic—they’re probabilistic models. They may occasionally generate incorrect or irrelevant responses.

Solution:

Validate AI outputs and implement fallback mechanisms in your application.


Real-World Use Case: Intelligent Customer Support

Let’s combine everything we’ve learned into a practical example: building an intelligent customer support bot.

Code Example: Customer Support Bot

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins;
using Microsoft.SemanticKernel.Orchestration;

var kernel = Kernel.Builder.WithMemoryStorage().Build();

// Load plugins
var summarizationPlugin = kernel.ImportSemanticPlugin(
    "SummarizationPlugin", "./plugins/SummarizationPlugin/skprompt.txt");
var translationPlugin = kernel.ImportSemanticPlugin(
    "TranslationPlugin", "./plugins/TranslationPlugin/skprompt.txt");

// Store customer interaction
await kernel.Memory.SaveInformationAsync(
    "customerInteractions", "JohnDoe", "John Doe reported an issue with order #12345.");

// Generate response
var context = new ContextVariables();
context.Set("name", "John Doe");

var emailPrompt = @"
    Write a friendly response to a customer query.
    Include memory from prior interactions if relevant.
";

var result = await kernel.RunAsync(emailPrompt, context);

Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

What’s Happening?

  • Plugins: Summarization and translation plugins are ready for use as needed.
  • Memory: The bot retrieves prior interactions for context.
  • Prompt Template: Generates a dynamic response based on customer details and memory.

Key Takeaways

  1. Semantic Kernel Simplifies AI: It abstracts complex tasks like prompt engineering, memory management, and API calls.
  2. Reusable Components: Plugins and prompt templates make your code modular and maintainable.
  3. Context Matters: Use memory intelligently to retain meaningful interactions.
  4. Be Mindful of Pitfalls: Validate outputs, manage memory efficiently, and set realistic expectations for AI.

Next Steps

Ready to dive deeper? Here's what you can do:

  1. Explore the Semantic Kernel GitHub repository for extensive examples and documentation.
  2. Experiment with building custom plugins tailored to your application's needs.
  3. Learn more about prompt engineering to craft effective instructions for LLMs.

AI-powered applications are the future, and with Semantic Kernel, you’re equipped to build them efficiently. Whether you’re crafting enterprise solutions or innovative side projects, the combination of C# and Semantic Kernel can unlock new possibilities.

Happy coding! 🚀

Comments 1 total

  • peterkmx
    peterkmxAug 8, 2025

    Great, upvoted! However, I am a bit curious about skprompt.txt. Many thanks in advance...

Add comment