- Why is an SDK like Semantic Kernel Needed?
- What is required to build a basic Chat Bot with Semantic Kernel?
It's important to understand why a framework is needed for AI based solutions and what are the fundemental building blocks needed for a basic Semantic Kernel Chat Bot. Once you understand this basics, you are start to leverage the more complex features.
-
Create a Kernel Builder
var builder = Kernel.CreateBuilder();
-
Load you AI Endpoint Details
var openAiDeployment = ConfigurationManager.AppSettings.Get("AzureOpenAIModel"); var openAiUri = ConfigurationManager.AppSettings.Get("AzureOpenAIEndpoint"); var openAiApiKey = ConfigurationManager.AppSettings.Get("AzureOpenAIKey");
-
Add the Chat Completion Service
builder.Services.AddAzureOpenAIChatCompletion( deploymentName: openAiDeployment!, endpoint: openAiUri!, apiKey: openAiApiKey!);
-
Construct the Kernel, ChatHistory, get instance of the ChatCompletion Service
var kernel = builder.Build(); ChatHistory history = []; var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
-
Send a prompt or ChatHistory and get a response from LLM
var prompt = "Why is the Sky blue?"; var result = await chatCompletionService.GetChatMessageContentAsync(prompt); Console.WriteLine(result);
-
Create a new .NET Core Console App in Visual Studio and name it SK-Lesson-1
-
Add the Semantic Kernel Package to the project.
- Click on Dependecies->Manage NuGet Packages and inssue the package below
Packages (click to expand)
Microsoft.SemanticKernel 1.6.3 or better
-
Come up with some way to store and read the Model Name, AI Key and AI Endpoint. Here is an example of using the System.Configuration class and an App.Config file.
- Create an App.Config file in the root of the project using the following format and replace the values to point to your AI Endpoint
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="AzureOpenAIEndpoint" value="AzureOpenAI-Endpoint-URI" /> <add key="AzureOpenAIKey" value="AzureOpenAI KEY" /> <add key="AzureOpenAIModel" value="AzureOpenAI Model Name" /> </appSettings> </configuration>
- Add a reference to the System.Configuration assembly to the program.cs file
-
Using the steps outlined in the 5 Simple steps build and test your first app.
- If needed you can take a look at the soltion to see a fishined project, but ideally you should put hands on keyboard and work through any issues you come across.