From Zero to AI Hero, Part 1: Jumpstart Your Journey With Semantic Kernel
Follow the steps outlined in this multi-part series to start with Semantic Kernel and integrate intelligent, context-aware functionalities into your projects.
Join the DZone community and get the full member experience.
Join For FreeGenAI and Coding: A Short Story
When I first heard about the Semantic Kernel, I was confused by the term. As a former C++ guy who has worked with Operating Systems, I thought it had something to do with it. Though I have followed the Generative AI landscape, I felt most of it was the initial hype (Garner's hype curve). Predicting tokens was lovely, but I never thought it would influence and create usable bots that could integrate with existing applications.
Then, there comes LangChain, which immediately became one of the fastest-growing open-source projects of 2023. LangChain is an AI orchestrator that helps developers sprinkle the magic of AI into their new and existing applications. LangChain came in Python, Java, and JavaScript, and there was a void for C# folks. Semantic Kernel filled this gap: although it differs from LangChain in principle and style, it was essentially built to make the .NET folks happy. Icing on the cake? It also supports Java and Python.
Semantic Kernel
What Is Semantic Kernel?
In the evolving landscape of AI, integrating intelligent, context-aware functionalities into applications has become more crucial than ever. Microsoft’s Semantic Kernel (SK) is a robust framework that allows developers to embed AI capabilities, such as natural language processing, into .NET applications. Whether you’re looking to build chatbots, automate workflows, or enhance decision-making processes, Semantic Kernel provides a robust foundation.
Semantic Kernel is an extensible framework that leverages AI models like OpenAI's GPT to enable natural language understanding and generation within .NET applications. It provides a set of APIs and tools that allow developers to create AI-driven agents capable of processing text, generating content, and meaningfully interacting with users.
At its heart, Semantic Kernel focuses on "plugins" — modular, reusable components that encapsulate specific capabilities such as understanding user intent, summarizing content, or generating responses. These can be combined and orchestrated to build sophisticated AI-driven applications.
Why Semantic Kernel?
LangChain is excellent, and Semantic Kernel is equally fabulous. Choosing one over the other should depend on your style, programming languages, or specific use cases. For example, if you need to integrate Gen AI capabilities into a browser-based solution such as ReactJS/Angular/Vue or vanilla web application, I would use LangChain, as it supports JavaScript.
Are We Only Going to Talk About Sematic Kernel?
No: in this multi-part series, though the primary focus would be on Semantic Kernel, we will still explore use cases of LangChain and use it as a cousin to Semantic Kernel for specific scenarios and use cases.
Enough talk! Let's build something with SK!
Prerequisites
Before diving into the code, ensure you have the following prerequisites:
- .NET 7.0 SDK or later: Download it from the .NET website.
- Visual Studio 2022: Ensure the ASP.NET Core workload is installed.
- Azure AI Model: It is possible to use OpenAI or Other models directly, but for this series, I will stick to AI models that are deployed in Azure as I have enough Azure credits as an MS MVP (P.S.: If you plan to use OpenAI’s models, you’ll need an API key, which you can obtain from the OpenAI website.)
Setting Up Your Project
The first step in integrating Semantic Kernel into your application is to set up the environment. Let’s start with a simple console application and then walk through adding Semantic Kernel to the mix.
1. Create a New Console Project
It's fine if you prefer to create a new console application using Visual Studio or VS Code.
dotnet new console -n sk-console
cd sk-console
2. Add the Semantic Kernel NuGet Package
Add Semantic Kernel NuGet package using the following command:
dotnet add package Microsoft.SemanticKernel
3. Setup Semantic Kernel
Open your Program.cs
file and configure the Semantic Kernel service. This service will handle interactions with the AI model. Please take a look at the function AddAzureOpenAIChatCompletion()
. As the name suggests, this function helps us integrate Open AI chat completion using the Open AI model hosted in Azure onto our Semantic Kernel Builder. The parameters' values are from my already deployed gtp-4o model on Azure AI Studio. I will write a separate article on deploying AI models using Azure AI Studio and link it here later.
var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(
deploymentName: "<Your_Deployment_Name>",
endpoint: "<Azure-Deployment-Endpoint-Ends-In:openai.azure.com>",
apiKey: "<Your_API_Key>"
);
var kernel = builder.Build();
Think of this KernelBuilder
as similar to the ASP.NET Core HostBuilder. Before the Build()
call, you would need to supply all of your plugin information(more on plugins later), so that SK would be aware of it.
4. Ask the First Question
Console.WriteLine(await kernel.InvokePromptAsync("What is Gen AI?"));
5. Running the Application
With everything configured, you’re ready to run your application. Run the below command in the terminal.
dotnet run
6. We Did It!
All is well. Our Semantic Kernel configuration used the Deployed Azure Open AI model to answer our question. Hooray!
I know, I know, this isn't much. But I still published the source code on GitHub here. This is the starting point, and we will build from here.
Conclusion
Semantic Kernel is a powerful tool for bringing advanced AI capabilities to your .NET applications. Following the steps outlined in this multi-part series, you can quickly get started with Semantic Kernel and integrate intelligent, context-aware functionalities into your projects. The possibilities are vast, from simple chatbots to complex, AI-driven workflows.
As we dive deeper, remember that the key to effectively leveraging the Semantic Kernel is in how you define and orchestrate your skills. With a solid understanding of these basics, you're well on your way to building the next generation of intelligent applications.
What's Next?
Are we done? No. Now that we know how to add Semantic Kernel in a .NET Application, it is time to take this flight off the ground. We will dig deeper and deeper as we go along with this multi-part series. In "Part 2: Understanding Plugins in Semantic Kernel, A Deep Dive," we will dive deeper into plugins in the semantic kernel. We wouldn't stop there, in the following parts, we will discuss agents, local SLMs and Ollama, Semantic Kernel on ASP.NET Core applications, mixing SK with AutoGen and LangChain, and more.
Opinions expressed by DZone contributors are their own.
Comments