Integrating AI Power into Your .NET Applications with the Semantic Kernel Toolkit – an Early View

With the rise of powerful AI models and services, questions come up on how to integrate those into our applications and make reasonable use of them. While other languages like Python already have popular and feature-rich libraries like LangChain, we are missing these in .NET and C#. But there is a new kid on the block that might change this situation. Welcome Semantic Kernel by Microsoft!

In diesem Artikel:

Version information: Semantic Kernel .NET 0.13.277.1-preview

What is Semantic Kernel?

Semantic Kernel (SK) is an SDK, available for .NET and Python, that lets you easily integrate Large Language Models (LLM) in your application. It comes with building blocks and abstractions like

  • Memory: for storing and querying embeddings
  • Skills: let you define AI prompts or connect to external systems
  • Planner: for building AI agents that can execute multiple steps

Since SK is still in early development (at the time of writing, the .NET version is at 0.13.277.1-preview) it currently lacks support for a number of things that tools like LangChain are already offering. Currently, the only supported Vector Database is Qdrant (they are working on supporting many more, though), and it only supports remote models like the OpenAI APIAzure OpenAI API, and Huggingface Inference API.

Using Semantic Kernel: first steps

So, let’s check out how we can use SK in .NET with C#. Before we start, we need to add the Microsoft.SemanticKernel NuGet package:
				
					dotnet add package Microsoft.SemanticKernel --prerelease
				
			

After that, we are able to create a kernel object, which is the orchestrator of our AI application. In this example, we use the OpenAI API Text-Completion API.

				
					var kernel = new KernelBuilder()
    .Configure(c => c.AddOpenAITextCompletionService("davinci-openai", "text-davinci-003", config["OpenAi:ApiKey"]))
    .Build();
				
			

Having the kernel at hand, we now need to define a prompt and create a so called semantic function which let’s us use the prompt.

				
					var prompt = @"Create a single sentence TLDR for the input.
Input: {{$input}}";

var tldr = kernel.CreateSemanticFunction(prompt);
				
			

We can now use the tldr object and invoke the prompt with input:

				
					var input = @"With SK, you can now build AI-first apps faster by design while also having a front-row peek at how 
the SDK is being built. SK has been released as open-source so that more pioneering developers can join us in crafting 
the future of this landmark moment in the history of computing. SK has been engineered to flexibly integrate LLM AI 
into existing apps. With SK, it's easier to accelerate your innovations' time to market, and to manage for reliability 
and performance in the long run.";

Console.WriteLine(await tldr.InvokeAsync(input));
				
			

As you can see, with these four simple steps, we are able to create a simple summarizer application.

Advanced sample: Question & Answer flow with Semantic Kernel

While this example shows how easily we can integrate an LLM into our application, a lot of integration scenarios will have the need to work with data present in the application. A more advanced example can be found in this GitHub repo, which implements a simple question & answer flow. In that example, we use text embeddings to find text that is related to a user’s question. After that, the text will be sent to OpenAI in order to create an answer to the question.

To be able to use embeddings and a store to store them, we need to adjust the kernel like this:

				
					var store = await SqliteMemoryStore.ConnectAsync("index.db");
var kernel = new KernelBuilder()
    .Configure(c =>
    {
        c.AddOpenAITextEmbeddingGenerationService("ada", "text-embedding-ada-002", config["OpenAi:ApiKey"]);
        c.AddOpenAITextCompletionService("davinci-openai", "text-davinci-003", config["OpenAi:ApiKey"]);
    })
    .WithMemoryStorage(store)
    .Build();
				
			

Our kernel now has a memory, and we are now able to store text in this memory. The following method downloads an HTML page, extracts the main contents of the page, and stores the contents in the kernel’s memory.

				
					async Task IndexUrl(HttpClient client, string url, string contentSelector)
{
    var content = await client.GetStringAsync(url);
    var title = string.Empty;
    
    var doc = new HtmlDocument();
    doc.LoadHtml(content);
    var mainElement = doc.DocumentNode.SelectSingleNode(contentSelector);
    title = mainElement.SelectSingleNode("//h1").InnerText;
    content = mainElement.InnerText

    await kernel.Memory.SaveInformationAsync(COLLECTION, content, url, title);
}
				
			

Once we have the data in the memory, we again need to define a prompt. In this example, the prompt is stored in a skill in the repo and will be imported with this line:

				
					kernel.ImportSemanticSkillFromDirectory("skills", "qa");
				
			

After we import the text into the memory and skill into the kernel, we can query the memory and let us create an answer to a question based on the stored text.

				
					async Task<string> Answer(string question)
{
    var results = await kernel.Memory.SearchAsync(COLLECTION, question, limit: 2).ToListAsync();
    var variables = new ContextVariables(question)
    {
        ["context"] = results.Any() 
            ? string.Join("\n", results.Select(r => r.Metadata.Text)) 
            : "No context found for this question."
    };
    
    var result = await kernel.RunAsync(variables, kernel.Skills.GetFunction("qa", "answer"));
    return result.Result;    
}
				
			

In the code, first, we do a semantic search and retrieve the top two results. These are concatenated and will be used as the context for the AI to answer the question of the user.

Conclusion

Although Semantic Kernel (SK) is in a very early stage of development and still needs to fill a lot of gaps, we are already able to use this library and integrate Large Language Models (LLMs) into our .NET applications. This lets application developers use the power of AI and natural language processing (NLP) without the need to build LLMs from scratch. With SK, developers can focus on building and improving their applications, while SK handles the heavy lifting of language processing.

Furthermore, SK allows developers to work with application data to fulfill AI tasks, as demonstrated in the advanced example. This means that developers can use their existing data to improve the natural language processing capabilities of their applications and enable powerful features like semantic search and question-answering.

Overall, SK shows great potential for integrating LLMs into .NET and C# applications, and its early development stage should not discourage developers from exploring its capabilities. As SK continues to evolve and mature, we can expect it to become an increasingly valuable tool for building advanced AI applications.

Mehr Artikel zu .NET, AI, LLM, Semantic Kernel
Kostenloser
Newsletter

Aktuelle Artikel, Screencasts, Webinare und Interviews unserer Experten für Sie

Verpassen Sie keine Inhalte zu Angular, .NET Core, Blazor, Azure und Kubernetes und melden Sie sich zu unserem kostenlosen monatlichen Dev-Newsletter an.

Diese Artikel könnten Sie interessieren
.NET
Incremental Roslyn Source Generators: High-Level API – ForAttributeWithMetadataName – Part 8

Incremental Roslyn Source Generators: High-Level API – ForAttributeWithMetadataName – Part 8

With the version 4.3.1 of Microsoft.CodeAnalysis.* Roslyn provides a new high-level API - the method "ForAttributeWithMetadataName". Although it is just 1 method, still, it addresses one of the biggest performance issue with Source Generators.
16.05.2023
.NET
.NET 7 Performance: Regular Expressions – Part 2

.NET 7 Performance: Regular Expressions – Part 2

There is this popular quote by Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."

In this second article of our short performance series, we want to look at the latter one of those problems.
25.04.2023
.NET
.NET 7 Performance: Introduction and Runtime Optimizations – Part 1

.NET 7 Performance: Introduction and Runtime Optimizations – Part 1

.NET 7 is fast. Superfast. All the teams at Microsoft working on .NET are keen to improve the performance and do so every year with each new .NET release. Though this time the achievements are really impressive. In this series of short articles, we want to explore some of the most significant performance updates in .NET and look at how that may affect our own projects. This first article is taking a deep look under the hood of the compiler and the runtime to look for some remarkably interesting and significant updates.
28.03.2023
.NET
Incremental Roslyn Source Generators: Using Additional Files – Part 7

Incremental Roslyn Source Generators: Using Additional Files – Part 7

In the previous article the Source Generator itself needed a 3rd-party library Newtonsoft.Json in order to generate new source code. The JSON-strings were hard-coded inside the Source Generator for simplicity reasons. In this article we will see how to process not just .NET code, but also other files, like JSON or XML.
21.03.2023
.NET
Understanding and Controlling the Blazor WebAssembly Startup Process

Understanding and Controlling the Blazor WebAssembly Startup Process

There are a lot of things going on in the background, when a Blazor WebAssembly application is being started. In some cases you might want to take a bit more control over that process. One example might be the wish to display a loading screen for applications that take some time for initial preparation, or when users are on a slow internet connection. However, in order to control something, we need to understand what is happening first. This article takes you down the rabbit hole of how a Blazor WASM application starts up.
07.03.2023
.NET
Adding Superpowers to your Blazor WebAssembly App with Project Fugu APIs

Adding Superpowers to your Blazor WebAssembly App with Project Fugu APIs

Blazor WebAssembly is a powerful framework for building web applications that run on the client-side. With Project Fugu APIs, you can extend the capabilities of these apps to access new device features and provide an enhanced user experience. In this article, learn about the benefits of using Project Fugu APIs, the wrapper packages that are available for Blazor WebAssembly, and how to use them in your application.

Whether you're a seasoned Blazor developer or just getting started, this article will help you add superpowers to your Blazor WebAssembly app.
28.02.2023