In this walkthrough, I will show you how easy it is to use the 'Semantic Kernel Tool' in Visual Studio Code to create a cake baking skill without a single line of code. We will then build a C# console application that uses the skill.
Source: https://github.com/medhatelmasry/sk-library
Companion Video: https://youtu.be/eI5Pr58gFZg
What is Semantic Kernel?
This is the official definition obtained from Create AI agents with Semantic Kernel | Microsoft Learn:
Semantic Kernel is an open-source SDK that lets you easily build agents that can call your existing code. As a highly extensible SDK, you can use Semantic Kernel with models from OpenAI, Azure OpenAI, Hugging Face, and more!
We now have an extension for Visual Studio Code that makes it very easy to build AI apps that use the large language models (LLMs) available through OpenAI.
In order to proceed with this tutorial, you will need the following prerequisites:
- .NET 8.0 Framework
- Visual Studio Code
- Access to Azure OpenAI
- Install the 'Semantic Kernel Tool' extension into Visual Studio Code.
Getting Started
In a suitable working directory, create a folder named sk-library, change directory into the new folder, then start Visual Studio Code with the following terminal commands:
mkdir sk-librarycd sk-librarycode .
In Visual Studio Code, select: View >> Command Palette...
The next ask is the name of the model that was created on the Azure portal in the AzureOpenAI service. A suitable model for this completion task is text-davinci-003.We will be asked to enter the Azure OpenAI Endpoint, which you can obtain from Azure.Finally, we must enter the Azure OpenAI Key.
If all goes well, you will receive this comforting message.
Create a skill without any coding
We can now create a skill without a single line of code. Create sub-folders Skills/Baking with the following terminal commands:
mkdir Skillscd Skillsmkdir Bakingcd ..
Start the "Semantic Kernel" view in Visual Studio Code.
Click on "Add Semantic Skill" tool beside Functions.Click on "Create a new skill folder for the function".
Choose the Skills/Baking folder.
Enter CakeRecipe for the function name.
A description for the function is required. Enter "Recipe for making a cake" for the description.Two files get created in the Skills/Baking/CakeRecipe folder: skprompt.txt and config.json.skprompt.txt
config.json
Replace contents of skprompt.txt with the following:I want to bake a fabulous cake. Give me a recipe using the input provided. The cake must be easy, tasty, and cheap. I don't want to spend more than $10 on ingredients. I don't want to spend more than 30 minutes preparing the cake. I don't want to spend more than 30 minutes baking the cake.
[INPUT]
{{$input}}
[END INPUT]
Testing our baking skill
Creating a skill with the Visual Studio Code 'Semantic Kernel Tool' is painless. We can now test our baking skill. Click on the arrow on the top-right of the panel.
You will be asked to enter a type of cake that you are interested in baking. I entered: chocolate.
If you check the OUTPUT tab at the bottom of Visual Studio Code, you will see the results.
The actual output I received was:
AI Provider: AzureOpenAIModel: text-davinci-003Execute: Baking.CakeRecipeParameters:input: chocolatePrompt:I want to bake a fabulous cake. Give me a recipe using the inputprovided. The cake must be easy, tasty and cheap. I don't want to spend more than$10 on ingredients. I don't want to spend more than 30 minutes preparing thecake. I don't want to spend more than 30 minutes baking the cake.[INPUT]chocolate[END INPUT]Result:Easy Chocolate Cake RecipeIngredients:- 1 ½ cups all-purpose flour- 1 cup granulated sugar- ¾ cup cocoa powder- 1 teaspoon baking soda- ½ teaspoon baking powder- ½ teaspoon salt- 2 eggs- 1 cup buttermilk- ½ cup vegetable oil- 1 teaspoon vanilla extractInstructions:1. Preheat oven to 350°F. Grease and flour a 9-inch round cake pan.2. In a large bowl, whisk together the flour, sugar, cocoa powder, baking soda, baking powder, and salt.3. In a separate bowl, whisk together the eggs, buttermilk, oil, and vanilla extract.4. Pour the wet ingredients into the dry ingredients and mix until just combined.5. Pour the batter into the prepared cake pan and bake for 25-30 minutes, or until a toothpick inserted into the center comes out clean.6. Allow the cake to cool in the pan for 10 minutes before transferring to a wire rack to cool completely.7. Serve and enjoy!Tokens:Input tokens: 88Output tokens: 237Total: 325Duration:00:00:11.971========== Function execution was finished. ==========
Using our baking skill in C# console app
Let us first create a console application in the root sk-library folder, with:
dotnet new console
We need to add two packages. One for SemanticKernel and the other is ConfigurationManager that allows us to read from settings in the App.config XML file.
dotnet add package Microsoft.SemanticKerneldotnet add package System.Configuration.ConfigurationManager
Create a file named App.config in the root folder of the console application and add to it the important parameters that allow access to your Azure OpenAI service. Contents of App.config are like the following:
<?xml version="1.0"?><configuration><appSettings><add key="endpoint" value="https://fake.openai.azure.com/" /><add key="api-key" value="fakekey-fakekey-fakekey-fakekey" /><add key="deployment-name" value="text-davinci-003" /></appSettings></configuration>
NOTE: Since I cannot share the endpoint and apiKey, I have fake values for these settings.
Replace the code in Program.cs with the following code:
using System.Configuration;using Microsoft.SemanticKernel;string _endpoint = ConfigurationManager.AppSettings["endpoint"]!;string _apikey = ConfigurationManager.AppSettings["api-key"]!;string _deploymentname = ConfigurationManager.AppSettings["deployment-name"]!;var builder = Kernel.CreateBuilder();builder.Services.AddAzureOpenAITextGeneration(_deploymentname, _endpoint, _apikey);var kernel = builder.Build();var functionDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Skills", "Baking");var semanticFunctions = kernel.ImportPluginFromPromptDirectory(functionDirectory);/* request user for input */Console.WriteLine("Enter a cake type you want to bake:");var cakeType = Console.ReadLine();var functionResult = await kernel.InvokeAsync(semanticFunctions["CakeRecipe"],new KernelArguments {{ "input", cakeType }});Console.WriteLine(functionResult);Console.WriteLine();
Run the app with:
dotnet run
You will be asked to enter a type of cake. I entered: lemon.
This was the output given by the AI.
Lemon Sponge CakeIngredients:- 2 cups all-purpose flour- 2 teaspoons baking powder- ½ teaspoon salt- 4 tablespoons butter- 1 cup sugar- 2 eggs- 1 cup milk- juice and zest of one lemonInstructions:1. Preheat oven to 350°F (175°C). Grease and flour an 8-inch cake pan.2. In a medium bowl, sift together the flour, baking powder, and salt.3. In a large bowl, beat the butter and sugar together until light and fluffy.4. Beat in the eggs, one at a time.5. Beat in the flour mixture alternately with the milk, beginning and ending with the flour mixture.6. Stir in the lemon juice and zest.7. Pour the batter into the prepared cake pan.8. Bake for 25-30 minutes or until a toothpick inserted into the center comes out clean.9. Allow the cake to cool in the pan before serving.
You can build applications with a variety of AI skills.
Happy Coding.
No comments:
Post a Comment