How to Implement AI in Your Code Using Semantic Kernel – in Under 30 Minutes

How to Implement AI in Your Code Using Semantic Kernel - in Under 30 Minutes

If you’re a .NET developer, you’ve probably noticed that most AI frameworks are built for Python. LangChain, AutoGen, CrewAI, they’re all Python-first.

But what about us, the C# developers who want to add AI capabilities to our applications?

In my last blog post, we explored Microsoft.Extensions.AI and how it provides a unified abstraction layer for working with AI services in .NET applications. That approach works great for straightforward scenarios where you need a clean, standardized way to interact with different AI providers.

But what about advanced scenarios?

That’s where Semantic Kernel comes in…

Semantic Kernel is Microsoft’s open-source SDK that brings AI orchestration to .NET. It’s not just another wrapper around OpenAI’s API. It’s a complete framework for building AI-powered applications with the patterns and practices we already use in our .NET projects.

In this tutorial, I’ll show you how to implement AI in your code using Semantic Kernel in under 30 minutes.

What Makes Semantic Kernel Different?

Kernel Architecture: The kernel acts as the brain of your AI application, managing services, plugins, and memory in a coordinated way.

Semantic Functions: These are more than just prompts, they’re reusable, testable, and composable AI components that can be version-controlled and shared across your team.

Native Functions: Write C# methods that your AI can call directly. This bidirectional interaction between AI and code opens up powerful possibilities.

Planners: Let the AI break down complex tasks into steps automatically. Instead of hardcoding workflows, the AI determines the best sequence of operations.

Memory Stores: Built-in support for vector databases and semantic search, allowing your AI to have long-term memory and retrieve relevant information efficiently.

Building Your First AI-Powered API with Semantic Kernel and Ollama

Now that we understand what Semantic Kernel brings to the table, let’s build something real. We’re going to create a chat completion API that runs entirely on your local machine using Ollama and Llama 3.2.

No cloud dependencies. No API costs. Just pure AI running on your hardware.

First, let’s configure Semantic Kernel to work with Ollama. If you haven’t installed Ollama yet, grab it from ollama.ai and pull the Llama 3.2 model:

				ollama pull llama3.2:latest
			

For this project we will be using the following semantic kernel packages:

				Microsoft.SemanticKernel.Connectors.Ollama
Microsoft.SemanticKernel
			

Now, let’s configure our kernel in Program.cs:

C#
				const string ollamaModel = "llama3.2:latest";
var ollamaUri = new Uri("http://localhost:11434");

var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOllamaChatCompletion(
    modelId: ollamaModel,
    endpoint: ollamaUri);

var kernel = kernelBuilder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

builder.Services.AddSingleton(kernel);
builder.Services.AddSingleton(chatCompletionService);
			

What’s happening here?

We’re building a Semantic Kernel instance that will orchestrate all AI operations.

The beauty of this approach is that you can swap Ollama for Azure OpenAI or any other provider by changing just these few lines. The rest of your code remains unchanged.

Creating the Chat Endpoint with Carter

For our API, we’re using Carter, a thin layer on top of Minimal APIs that gives us better organization without the ceremony. Here’s our chat endpoint:

C#
				public class ChatEndpoints : CarterModule
{
    public ChatEndpoints() : base("/chat")
    {
        this.WithTags("Chat");
        this.WithSummary("Handles AI chat completion operations using Semantic Kernel");
    }

    public override void AddRoutes(IEndpointRouteBuilder app)
    {
        app.MapPost("/",
            async (ChatRequest request, IChatCompletionService chatService, Kernel kernel, CancellationToken ct) =>
            {
                if (string.IsNullOrWhiteSpace(request.Prompt))
                {
                    return Results.BadRequest("Prompt cannot be empty");
                }

                var result =
                    await chatService.GetChatMessageContentAsync(request.Prompt, kernel: kernel,
                        cancellationToken: ct);

                var content = result.Content ?? result.ToString() ?? "No response generated";

                return Results.Ok(new ChatResponse(content));
            })
        .WithName("ProcessChat")
        .WithSummary("Process a chat message using Semantic Kernel")
        .WithDescription("Sends a chat prompt to the AI service and returns the response")
        .Produces<ChatResponse>(200)
        .Produces(400);
    }
}
			
 

The Kernel is the central orchestrator. It manages everything, this is just a simple implementation to get you started with Semantic Kernel. In reality, Semantic Kernel’s capabilities go far beyond basic chat completion.

Prompt
				What is Semantic Kernel?
			
Response
JSON
				{
    "answer": "A Semantic Kernel (SK) is a software component that acts as the core of a database management system (DBMS), providing a common interface for data manipulation and querying. It is designed to be highly customizable, allowing users to define their own domain-specific languages (DSLs) and rules engines.\n\nIn traditional DBMS architecture, the kernel is responsible for managing storage, retrieval, and caching of data. However, a Semantic Kernel takes it a step further by adding semantic capabilities to the database system. It enables the creation of domain-specific data models, which are tailored to specific industries or applications, such as finance, healthcare, or manufacturing.\n\nThe key features of a Semantic Kernel include:\n\n1. **Domain-specific language (DSL)**: A SK allows users to define their own DSL, which is used to describe the structure and semantics of data in their domain.\n2. **Rules engine**: The kernel includes a rules engine that can be used to enforce business rules, validate data, and perform complex queries.\n3. **Ontology management**: A SK supports the creation and management of ontologies, which are formal representations of knowledge that describe concepts, relationships, and vocabularies in a specific domain.\n4. **Data integration**: The kernel provides mechanisms for integrating data from multiple sources, including external systems, files, and other databases.\n5. **Customizable**: A SK is designed to be highly customizable, allowing users to adapt the database system to their specific needs.\n\nThe benefits of a Semantic Kernel include:\n\n1. **Improved domain-specific knowledge management**: By creating a custom DSL and rules engine, organizations can better manage their domain-specific knowledge and expertise.\n2. **Enhanced data quality and validation**: The kernel's rules engine ensures that data is consistent and valid within the context of the specific domain.\n3. **Increased flexibility and adaptability**: A SK allows users to easily modify the database system to accommodate changing business requirements or new technologies.\n\nExamples of Semantic Kernels include:\n\n1. **Red Hat JBoss Data Grid**: An open-source, in-memory data grid that supports a domain-specific language for defining data models and rules engines.\n2. **Oracle WebLogic Data Grid**: A commercial, in-memory data grid that includes a semantic kernel for managing domain-specific data models and rules engines.\n3. **Microsoft Azure Cosmos DB**: A cloud-based NoSQL database service that supports a domain-specific language for defining data models and rules engines.\n\nIn summary, a Semantic Kernel is a software component that provides a highly customizable and adaptive architecture for managing domain-specific data and knowledge in a database system."
}
			

Wow, what a huge answer, you can also limit the amount of characters in your prompt

Adding Conversation Memory: Making Your AI Remember Context

One of the biggest limitations of basic AI implementations is that they treat each request in isolation. Your AI doesn’t remember what you talked about just moments ago. Let’s fix that by implementing chat history using redis.

Semantic Kernel provides a built-in ChatHistory class that maintains conversation context. Here’s how we integrate it into our application:

C#
				public async Task SaveChatHistoryAsync(string sessionId, ChatHistory chatHistory)
{
    var redisKey = $"chat_history:{sessionId}";
    var serializedHistory = JsonSerializer.Serialize(chatHistory.ToList());
    await _database.StringSetAsync(redisKey, serializedHistory, TimeSpan.FromHours(24));
}

public async Task ClearChatHistoryAsync(string sessionId)
{
    var redisKey = $"chat_history:{sessionId}";
    await _database.KeyDeleteAsync(redisKey);
}
			

Our endpoint now maintains conversation continuity:

C#
				var sessionId = request.SessionId ?? "default";

var chatHistory = await cacheService.GetChatHistoryAsync(sessionId);

chatHistory.AddUserMessage(request.Prompt);

var result = await chatService.GetChatMessageContentAsync(chatHistory, kernel: kernel, cancellationToken: ct);
var content = result.Content ?? result.ToString() ?? "No response generated";

chatHistory.AddAssistantMessage(content);

await cacheService.SaveChatHistoryAsync(sessionId, chatHistory);

return Results.Ok(new ChatResponse(content));
			

When you pass this to the chat service, it sends the entire conversation to the model, allowing it to understand context and provide relevant responses.

This is just one way where you store history per session, there are several ways to do this, such as storing it by token, context window, userid, etc. Additionally, you should think about how much history you want to store, the number of tokens your model supports, the cost this will generate, response time, how much memory you want to spend and a expiration time.

While ChatHistory handles conversation continuity, with Semantic Kernel you can implement even more sophisticated memory patterns:

  • Semantic Memory: Store and retrieve information based on meaning, not just sequence
  • Working Memory: Temporary storage for complex multi-step operations
  • Long-term Memory: Persistent storage across sessions using vector databases
  • Episodic Memory: Remember specific events and when they occurred
Prompt
				My name is Pedro, What is Semantic Kernel?
			
Output
				Semantic Kernel is Microsoft's open-source SDK that enables .NET developers to integrate Large Language Models (LLMs) like GPT-4, Claude, or local models directly into their C# applications. It acts as an orchestration layer between your code and AI services, providing not just simple API calls but a complete framework with features like plugin architecture (where AI can call your C# methods), semantic memory for context retention, function chaining for complex workflows, and support for multiple AI providers simultaneously. Unlike Python-centric frameworks, Semantic Kernel is built specifically for .NET, leveraging familiar patterns like dependency injection, async/await, and strong typing, making it the natural choice for C# developers who want to add AI capabilities to their applications without leaving their ecosystem. Pedro, think of it as the missing link that transforms AI from an external service into a first-class citizen in your .NET architecture.
			

Now let’s test the history:

Prompt
				Who am I?
			
API Response
JSON
				{
    "answer": "Pedro! You are the person who asked me \"What is Semantic Kernel?\" earlier."
}
			

Wow, in less than 30 minutes, we’ve transformed a basic .NET application into an AI-powered system using Semantic Kernel. We started with a simple chat endpoint, added conversation memory using chat history.

Conclusion

We’ve barely scratched the surface of what Semantic Kernel can do.

What we built today is like learning to write “Hello World” when you’re getting started with programming. It’s essential, it works, and it gives you a foundation to build upon. But Semantic Kernel’s true power lies in its advanced capabilities that transform simple AI calls into intelligent, autonomous systems.

In my upcoming blog posts, I’ll dive deeper into Semantic Kernel’s advanced features.

Until then, take the code from this tutorial, experiment with it, and start imagining the possibilities. The best way to learn Semantic Kernel is to build with it.

Thank you for reading.

See you next time!

Share the Post:
plugins premium WordPress