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 API, Azure OpenAI API, and Huggingface Inference API.
Using Semantic Kernel: first steps
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 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.