{"id":500,"date":"2023-05-24T11:57:20","date_gmt":"2023-05-24T18:57:20","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=500"},"modified":"2023-05-24T12:05:25","modified_gmt":"2023-05-24T19:05:25","slug":"how-to-use-plugins-with-semantic-kernel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/how-to-use-plugins-with-semantic-kernel\/","title":{"rendered":"How to use plugins with Semantic Kernel"},"content":{"rendered":"<h2><a href=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-89\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge.png\" alt=\"Image skpatternlarge\" width=\"1638\" height=\"136\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge.png 1638w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-300x25.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-1024x85.png 1024w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-768x64.png 768w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternlarge-1536x128.png 1536w\" sizes=\"(max-width: 1638px) 100vw, 1638px\" \/><\/a><\/h2>\n<p>Have you wondered how you can use plugins with Semantic Kernel? Or maybe you want to use plugins for your own co-pilot chat app just like our <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/samples\/apps\/copilot-chat-app\/README.md\">sample<\/a> app?<\/p>\n<p>Well, I have answers for you. I will guide you an example that uses Semantic Kernel with a GitHub <span style=\"font-size: 1rem;\">Open API plug-in to build a <\/span><span style=\"font-size: 1rem;\">console chat experience using action <\/span><span style=\"font-size: 1rem;\">planner and a chat completion skill.\u00a0<\/span><\/p>\n<p>To get the best of this blog, go ahead and clone the Semantic <a href=\"https:\/\/aka.ms\/skrepo\">repo<\/a> and hit that star button!<\/p>\n<p>Let&#8217;s get started. Once you have cloned the repository navigate to &#8216;semantic-kernel\/samples\/dotnet\/openapi-skills&#8217;. The example requires a small setup, which is all defined in the appsettings.json file. You need to update AIService and GitHub plug-in parameters. This defines if you will be using Azure OpenAI or OpenAI and which GitHub repo you want to use along with the GitHub access token. If you don&#8217;t have an access token, you can get one from <a href=\"https:\/\/docs.github.com\/en\/authentication\/keeping-your-account-and-data-secure\/creating-a-personal-access-token\">here.<\/a><\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">\"AIService\": {\r\n    \"Type\": \"AzureOpenAI\", \/\/ \r\n    \"Endpoint\": \"\", \/\/ ignored when AIService is \"OpenAI\"\r\n    \"CompletionTokenLimit\": 4096,\r\n    \/\/ \"Key\": \"\"\r\n    \"Models\": {\r\n      \"Completion\": \"gpt-35-turbo\" \/\/ For OpenAI, change to 'gpt-3.5-turbo' (with a period).\r\n    }\r\n  },<\/code><\/pre>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\"> \"GitHubSkill\": {\r\n    \"Owner\": \"microsoft\",\r\n    \"Repository\": \"semantic-kernel\"\r\n    \/\/ \"Key\": \"\"\r\n  },<\/code><\/pre>\n<p>If you are <span style=\"font-size: 1rem;\">Visual Studio (2019 or newer), right-click on the <\/span><code>OpenApiSkillsExample<\/code><span style=\"font-size: 1rem;\">\u00a0project, select &#8220;Set as Startup Project&#8221;, then press\u00a0<\/span><code>F5<\/code><span style=\"font-size: 1rem;\"> to run and debug the application. Or <\/span>open a terminal window, change directory to the\u00a0<code>OpenApiSkillsExample<\/code>\u00a0project, then run\u00a0<code>dotnet run.<\/code><\/p>\n<p>If you are interested to learn more about how these settings are used, refer to the Program.cs file, where the &#8216;magic&#8217; happens. Let&#8217;s step through what is going on.<\/p>\n<p>The top of the file loads assembles, and dependencies, followed by loading configuration from appsettings.json and loading logger for debugging.<\/p>\n<p>Then the Semantic Kernel is initialized utilizing the preferred AIService with some validation.<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">        IKernel kernel = Kernel.Builder\r\n            .WithLogger(loggerFactory.CreateLogger&lt;IKernel&gt;())\r\n            .Configure(c =&gt;\r\n            {\r\n                switch (aiOptions.Type)\r\n                {\r\n                    case AIServiceOptions.AIServiceType.AzureOpenAI:\r\n                        c.AddAzureChatCompletionService(aiOptions.Models.Completion, aiOptions.Endpoint, aiOptions.Key);\r\n                        break;\r\n                    case AIServiceOptions.AIServiceType.OpenAI:\r\n                        c.AddOpenAIChatCompletionService(aiOptions.Models.Completion, aiOptions.Key);\r\n                        break;\r\n                    default:\r\n                        throw new InvalidOperationException($\"Unhandled AI service type {aiOptions.Type}\");\r\n                }\r\n            })\r\n            .Build();<\/code><\/pre>\n<p>Next we register the GitHub plug-in using an OpenAPI definition, and for this example we are referencing the pull request GET Operations. Feel free to experiment by adding other GitHub operations to get more familar with this concept.<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">        GitHubSkillOptions gitHubOptions = configuration.GetRequiredSection(GitHubSkillOptions.PropertyName).Get&lt;GitHubSkillOptions&gt;()\r\n            ?? throw new InvalidOperationException($\"Missing configuration for {GitHubSkillOptions.PropertyName}.\");\r\n\r\n        BearerAuthenticationProvider authenticationProvider = new(() =&gt; Task.FromResult(gitHubOptions.Key));\r\n\r\n        await kernel.ImportOpenApiSkillFromFileAsync(\r\n            skillName: \"GitHubSkill\",\r\n            filePath: Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, \"GitHubSkill\/openapi.json\"),\r\n            authCallback: authenticationProvider.AuthenticateRequestAsync);<\/code><\/pre>\n<p>Now we have our plug-in defined we can setup the Semantic Kernel planner to invoke the GitHub plug-in. We will use the action planner which is great for 0 or 1 step plans. If you need to use chaining or want to have more steps in your plan the <a href=\"https:\/\/aka.ms\/sk-repo-sequential-planner\">Sequential Planner<\/a> would be great alternative.<\/p>\n<p>We create a new chat instance and provide a prompt to define the experience we want from the Bot.<\/p>\n<p><code class=\"language-cs language-csharp\">        ActionPlanner planner = new(kernel, logger: logger);<\/code><\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">\/\/ Chat loop\r\nIChatCompletion chatGPT = kernel.GetService&lt;IChatCompletion&gt;();\r\nOpenAIChatHistory chatHistory = (OpenAIChatHistory)chatGPT.CreateNewChat(\"You are a helpful, friendly, intelligent assistant that is good at conversation.\");\r\nwhile (true)\r\n{\r\nConsole.WriteLine(\"----------------\");\r\nConsole.Write(\"Input: \");\r\nstring? input = Console.ReadLine();\r\n\r\nif (string.IsNullOrWhiteSpace(input))\r\n{\r\ncontinue;\r\n}\r\n\r\n\/\/ Add GitHub's response, if any, to the chat history.\r\nint planResultTokenAllowance = (int)(aiOptions.TokenLimit * 0.25); \/\/ Allow up to 25% of our token limit to be from GitHub.\r\nstring planResult = await PlanGitHubSkill(gitHubOptions, planner, chatHistory, input, planResultTokenAllowance);\r\nif (!string.IsNullOrWhiteSpace(planResult))\r\n{\r\nchatHistory.AddUserMessage(planResult);\r\n}\r\n\r\n\/\/ Add the user's input to the chat history.\r\nchatHistory.AddUserMessage(input);<\/code><\/pre>\n<p>That&#8217;s it! We now have a console chat bot using an OpenAI plug-in with Semantic Kernel! Enjoy!<\/p>\n<p>Please share in the comments what you think and what plugins you tried!<\/p>\n<h2><strong>Next Steps:<\/strong><\/h2>\n<p>Try the Co-pilot <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/blob\/main\/samples\/apps\/copilot-chat-app\/README.md\">Sample<\/a> Application and explore with other plug-ins such Jira, Klarna Shopping, Microsoft Graph and GitHub.<\/p>\n<p>Join the community and let us know what you think:\u00a0<a href=\"https:\/\/aka.ms\/sk\/discord\" target=\"_blank\" rel=\"noopener\">https:\/\/aka.ms\/sk\/discord<\/a><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-92\" src=\"https:\/\/devblogs.microsoft.com\/semantic-kernel\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw.png\" alt=\"Image skpatternsmallbw\" width=\"1211\" height=\"137\" srcset=\"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw.png 1211w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw-300x34.png 300w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw-1024x116.png 1024w, https:\/\/devblogs.microsoft.com\/agent-framework\/wp-content\/uploads\/sites\/78\/2023\/03\/skpatternsmallbw-768x87.png 768w\" sizes=\"(max-width: 1211px) 100vw, 1211px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you wondered how you can use plugins with Semantic Kernel? Or maybe you want to use plugins for your own co-pilot chat app just like our sample app? Well, I have answers for you. I will guide you an example that uses Semantic Kernel with a GitHub Open API plug-in to build a console [&hellip;]<\/p>\n","protected":false},"author":116113,"featured_media":94,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17,2,1],"tags":[],"class_list":["post-500","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcements","category-samples","category-semantic-kernel"],"acf":[],"blog_post_summary":"<p>Have you wondered how you can use plugins with Semantic Kernel? Or maybe you want to use plugins for your own co-pilot chat app just like our sample app? Well, I have answers for you. I will guide you an example that uses Semantic Kernel with a GitHub Open API plug-in to build a console [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/116113"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=500"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/500\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/94"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}