Beginner's Guide To Semantic Kernel In C#
January 1, 2025
• 8,354 views
csharpdotnet.net.net coredotnet coresemantic kernelai agentsopenailangchaincopilotsemantic kernel skillssemantic kernel plannersemantic kernel tutorialazure openaiintroduction to semantic kernelmicrosoft semantic kernelazure ai studiointroduction to memories in semantic kernelsemantic kernel demosemantic kernel pluginsemantic kernel toolssemantic kernel microsoftsemantic kernel copilot chatsemantic kernel embeddingssemantic kernel C#
Want to build more interesting AI applications using C#? Let's dive right into Semantic Kernel and see all of the awesome functionality that the framework has to offer!
View Transcript
so it's been a minute since I've made a video about using C with different AI tools that we have available to us over the past year things have changed dramatically so I thought it would be a really cool opportunity to revisit some of the new things that we have access to hi my name is Nick centino and I'm a principal soft engineering manager at Microsoft in this video we're going to be looking at using semantic kernel in C and connecting that with Azure open AI this will be an introductory video to these Concepts so if you haven't played with these things together this video should help you get set up with that however if you're more advanced working with this stuff this might seem a little bit basic for where you're at if that sounds interesting just a reminder to subscribe to the channel and
check out that pin comment for my courses on dome train now let's jump over to visual studio and get set up with semantic kernel all right to kick things off I have Visual Studio pulled up here with a very simple semantic kernel application but I wanted to start by walking us through the different Nate packages that we're going to want to work with so if I jump over to the project file just to make it very simple the first three things that we see here aren't even semantic kernel related I just wanted to be able to pull in some logging and then show you how you can configure this kind of stuff when you're working with the builder for a semantic kernel but the two things that we're really going to need are down here and that's going to be semantic kernel itself and then
the connectors that we're going to be using for Azure open AI I should make a note here that if you're not interested in using Azure open aai there are other connectors you can use but in this tutorial we are going to be going through Azure open AI now jumping back over to the code here I do also want to call out that you're going to want to set these things up in your Azure portal I'm not going to walk through that in this tutorial there are Guides Online for how to get set up in the Azure portal and make a deployment for the models that you want to use this video tutorial will assume that you have that set up in your Azure portal so if you haven't done that make sure you pause this video go get that set up and then come back
and continue on with this tutorial one of the things that I really appreciate but working with semantic kernel is that we get this very similar Builder setup that we all know and love from working with asp.net core so you can see that we start off by saying konel create Builder and then very much like asp.net core we're going to start working with that Builder and setting some things up so we can see right away we're on line1 19 we're adding add Azure open AI chat completion that we're going to pass in that information that we've defined above I've actually entered my information on line 11 you can't see it I'm hiding it from you but if you're interested in knowing how to get that kind of information set up in your Visual Studio project you can check out this link up here and I have
a video on working with configuration files where you can pull that kind of stuff in without having it directly in your code I'm taking a bit of a shortcut here next up you can see on line 20 this is where I'm adding in logging you can configure this to be different log levels but this part isn't really having anything to do with semantic kernel specifically I just wanted to show you that you can configure other services on on here like logging so if you want that you can go ahead and use it I put this to warning so that we don't get very spammy you'll notice that if you try this whole application out and you put it to something like Trace it's going to be very noisy but warning should at least let us see something that's interesting if we have any problems this
is just a quick Interruption from this video sponsor which is packed publishing now P has sent me over this book from Mark J Price which is C13 net9 modern crossplatform development fundamentals and I have the previous edition of this book sitting on my bookshelf as well and now I have this one which is super exciting because this book is packed with tons of awesome examples that guide you through the different functionalities of the language now I think that this is an awesome reference guide I think that you'll have tons of things that you can walk through from the very beginning to learn about C but one of the best parts about this is that it's not just limited to looking at the language you'll get to see things like asp.net core Entity framework as well as Blazer all in action with practical examples in the
book I personally cannot recommend Mark J Price's books enough like I said he's got tons of awesome examples and he has other books as well from pack that I do highly recommend you check out so if you're interested in this you can check out the link in the description and I'll have a link up here as well that you can check out thanks and now back to the video next up on line 23 we're going to build the kernel so for this tutorial it's very simple right we've only looked at a few lines of code and we have a kernel to work with which is awesome so what we're going to do now with the kernel that we have is we're going to work with chat completions and this is a pretty popular use case right I think when chat GPT became available and people
were trying it out everyone was trying to make chat Bots and this is essentially what we're going to be able to do with a few more lines of code in this video so we're going to ask the colonel for this required service again very much like in asp.net core working with the dependency injection framework we say I want the I chat completion service and we're going to assign that to a variable next up we're going to configure The Prompt settings and for what we have going on right now this isn't super important but in a follow-up video this will make a little bit more sense and we'll see how we can use something like plugins inside of semantic kernel for now we're just going to focus on chat though on line 37 we're going to prompt the user that they need to enter a message
because they're going to start chatting with the AI bot and what we have beyond that is really just a simple Loop for chatting so if I read through this pretty quick it's because it is really that basic just a while true loop we're going to get the user input if it's null or empty we're just going to break out what we're going to do though is add the message to the history and then essentially ask the AI service so uh providing the kernel to that AI service to say go get me the completions so this sends it off to Azure open aai and then from there we're going to get the completion back so we'll add that into the chat history and print that message out to the console so essentially what we're doing here is continuing to build up this chat history and that
way every time the user is typing in something we'll add it in to the current history and every time we get a result back we'll add the ai's response also into that history pretty straightforward you've probably seen things like this with chat gbt or other simple tutorials but this is how we can make it work with semantic kernel let's go ahead and run it and see if we can start chatting with our AI bot so kicking things off write your message to the AI bot I will say hey my name is Dev leader enter hello Dev leader how can I assist you today so this is coming back again from Azure open AI um what is your favorite color as an AI you don't have personal preferences or the ability to perceive colors however I'm here to help you questions or tasks you have how
can I assist you today so I'm not going to keep having a conversation with this AI bot because you already know how to have conversations and prompt with LMS but this is a very simple tutorial for how you can get set up with semantic kernel so just a brief recap we added the necessary nougat packages we have to make sure that we have our Azure portal set up with the right information so we have to go make sure we do that deployment and then get the information for our API key and the endpoint that we want to work with because we need the model endpoint and API key to configure this properly next up we're going to end up building the kernel we ask for the the chat completion service and from there essentially we just have this Loop where we're adding messages into the
history and asking for the chat completions pretty simple pretty straightforward not many lines of code these days to be able to have a chat bot set up compared to where we started off around a year ago so just a reminder if you thought this video was interesting and you want to see how you can start working with plugins in semantic kernel you can check out this video next when it's uploaded thanks and I'll see you next time
Frequently Asked Questions
What is Semantic Kernel and how is it used in this tutorial?
In this tutorial, I introduce Semantic Kernel as a framework for building applications that can interact with AI tools, specifically using C#. I demonstrate how to set it up in Visual Studio and connect it with Azure OpenAI to create a simple chat bot.
Do I need to set up anything in Azure before following this tutorial?
Yes, you need to set up your Azure portal and deploy the models you want to use. This tutorial assumes you have that set up, so I recommend pausing the video to complete that before continuing.
What are the main steps to create a chat bot using Semantic Kernel?
These FAQs were generated by AI from the video transcript.The main steps include adding the necessary NuGet packages, configuring the Azure OpenAI service, building the kernel, and implementing a simple loop to handle user input and AI responses. I walk through these steps in the video.
