{"id":54132,"date":"2024-10-31T10:05:00","date_gmt":"2024-10-31T17:05:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=54132"},"modified":"2024-10-31T10:05:00","modified_gmt":"2024-10-31T17:05:00","slug":"github-ai-models-dotnet-semantic-kernel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/github-ai-models-dotnet-semantic-kernel\/","title":{"rendered":"Unlocking the Power of GitHub Models in .NET with Semantic Kernel"},"content":{"rendered":"<p>Explore how to integrate GitHub&#8217;s AI models, like GPT, Llama and Phi, into your .NET apps using Microsoft&#8217;s Semantic Kernel for intelligent applications.<\/p>\n<h2>Unlocking the Power of GitHub Models in .NET with Semantic Kernel<\/h2>\n<p>The world of AI continues to evolve rapidly, and GitHub has joined the race by introducing a set of popular Large Language Models (LLMs), such as GPT, Llama and Phi, available on the <strong>GitHub Marketplace<\/strong>. These models can help developers build powerful AI-driven applications with ease. In this post, we&#8217;ll explore how .NET programmers can take advantage of these models and integrate them into their applications using <strong>Semantic Kernel<\/strong>.<\/p>\n<h2>Introduction to GitHub Models<\/h2>\n<p>GitHub has expanded its toolkit by launching <a href=\"https:\/\/github.blog\/news-insights\/product-news\/introducing-github-models\">GitHub Models<\/a>, a suite of industry-leading AI Models designed to enable more than 100 million developers to become AI engineers. These models, like Llama 3.1, GPT-4o and Phi-3.5, are particularly helpful for tasks that involve natural language processing (NLP). Available in the GitHub Marketplace, they provide developers a built-in playground that lets them test different prompts and model parameters, for free, right in GitHub.<\/p>\n<p>For .NET developers, these models unlock new possibilities to create intelligent applications that can understand and generate human language or even code, making it easier to streamline various tasks and processes.<\/p>\n<h2>Semantic Kernel: A Brief Overview<\/h2>\n<p><a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\">Semantic Kernel<\/a> is a lightweight, extensible framework from Microsoft that allows developers to create sophisticated AI applications that leverage LLMs and other cloud services like Azure AI Search. It integrates easily into your .NET applications, making it possible to incorporate natural language understanding and generation features.<\/p>\n<p>With Semantic Kernel, you can define workflows, apply reasoning over the outputs of LLMs, and chain together models to create more complex AI-driven experiences. It acts as a bridge between large language models and your application logic.<\/p>\n<h2>Using GitHub Models with Semantic Kernel<\/h2>\n<p>To give you a practical example, let&#8217;s explore how you can integrate GitHub Models into a C# application using Semantic Kernel. There\u2019s a <a href=\"https:\/\/github.com\/elbruno\/GithubModelsAndSemanticKernel\">GitHub repository<\/a> that provides a working sample of how this integration can be achieved.<\/p>\n<p>Here\u2019s a quick step-by-step guide to get started:<\/p>\n<h3>Step 1: Install the necessary NuGet packages<\/h3>\n<p>First, ensure you have the required NuGet packages in your C# project:<\/p>\n<pre><code class=\"language-bash\">dotnet add package Microsoft.SemanticKernel --version 1.18.2\ndotnet add package Microsoft.Extensions.Configuration.UserSecrets --version 9.0.0-rc.1.24431.7<\/code><\/pre>\n<p>The Semantic Kernel package allows you to interact with the GitHub Models through the API.\nMicrosoft Configuration User Secrets is used to store and retrieve the required GitHub Token.<\/p>\n<h3>Step 2: Setup project secrets with your GitHub Personal Access Token<\/h3>\n<p>Generate a new <a href=\"https:\/\/github.com\/settings\/tokens\">GitHub Personal Access Token<\/a>. Navigate to the root of your C# project and run these commands to add the Token.<\/p>\n<pre><code class=\"language-bash\">dotnet user-secrets init\ndotnet user-secrets set \"GH_PAT\" \"&lt; PAT &gt;\"<\/code><\/pre>\n<p>In the repository <a href=\"https:\/\/github.com\/elbruno\/GithubModelsAndSemanticKernel\/blob\/main\/src\/Program.cs\">Sample console application<\/a>, these code is used to retrieve:<\/p>\n<ul>\n<li>GitHub Models, model name<\/li>\n<li>GitHub Models, model endpoint<\/li>\n<li>GitHub Personal Access Token<\/li>\n<\/ul>\n<pre><code class=\"language-csharp\">var config = new ConfigurationBuilder().AddUserSecrets&lt;Program&gt;().Build();\nvar modelId = \"Phi-3.5-mini-instruct\";\nvar uri = \"https:\/\/models.inference.ai.azure.com\";\nvar githubPAT = config[\"GH_PAT\"];<\/code><\/pre>\n<p>This is an example of how to set the modelId and the uri, and the GitHub PAT using secrets:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2024\/10\/10SampleCode.png\" alt=\"an example of how to set the modelId and the uri, without using secrets\" \/><\/p>\n<h3>Step 3: Configure the Semantic Kernel client to use GitHub Models<\/h3>\n<p>Next, set up the Semantic Kernel to integrate with the GitHub models API:<\/p>\n<pre><code class=\"language-csharp\">\/\/ create client\nvar client = new OpenAIClient(new ApiKeyCredential(githubPAT), new OpenAIClientOptions { Endpoint = new Uri(uri) });\n\n\/\/ Create a chat completion service\nvar builder = Kernel.CreateBuilder();\nbuilder.AddOpenAIChatCompletion(modelId, client);\n\n\/\/ Get the chat completion service\nKernel kernel = builder.Build();\nvar chat = kernel.GetRequiredService&lt;IChatCompletionService&gt;();<\/code><\/pre>\n<h3>Step 4: Run the App<\/h3>\n<p>Now, define the task you want the GitHub model to perform. The sample console app, is a standard Q&amp;A chat that runs in the console:<\/p>\n<pre><code class=\"language-csharp\">var history = new ChatHistory();\nhistory.AddSystemMessage(\"You are a useful chatbot. If you don't know an answer, say 'I don't know!'. Always reply in a funny way. Use emojis if possible.\");\n\nwhile (true)\n{\n    Console.Write(\"Q: \");\n    var userQ = Console.ReadLine();\n    if (string.IsNullOrEmpty(userQ))\n    {\n        break;\n    }\n    history.AddUserMessage(userQ);\n\n    var sb = new StringBuilder();\n    var result = chat.GetStreamingChatMessageContentsAsync(history);\n    Console.Write(\"AI: \");\n    await foreach (var item in result)\n    {\n        sb.Append(item);\n        Console.Write(item.Content);\n    }\n    Console.WriteLine();\n\n    history.AddAssistantMessage(sb.ToString());\n}<\/code><\/pre>\n<p>Optional: The repo is ready to run the sample project using Codespaces. The chat demo application should look like these:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2024\/10\/15Samplerun.png\" alt=\"Sample chat console application running in Codespaces\" \/><\/p>\n<h2>Summary<\/h2>\n<p>Integrating GitHub Models into your .NET applications using Semantic Kernel opens up exciting possibilities for building AI-driven applications. With tools like Semantic Kernel, you can streamline your development process and create smarter applications.<\/p>\n<p>If you\u2019re looking to dive deeper into this topic, check out the following resources:<\/p>\n<ul>\n<li><a href=\"https:\/\/github.blog\/news-insights\/product-news\/introducing-github-models\">Introducing GitHub Models<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/elbruno\/GithubModelsAndSemanticKernel\">GitHub Repository with Semantic Kernel Integration<\/a><\/li>\n<\/ul>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explore how to integrate GitHub&#8217;s AI models, like GPT, Llama and Phi, into your .NET apps using Microsoft&#8217;s Semantic Kernel for intelligent applications<\/p>\n","protected":false},"author":120281,"featured_media":54133,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,7781,756],"tags":[4,568,46],"class_list":["post-54132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-ai","category-csharp","tag-net","tag-ai","tag-c"],"acf":[],"blog_post_summary":"<p>Explore how to integrate GitHub&#8217;s AI models, like GPT, Llama and Phi, into your .NET apps using Microsoft&#8217;s Semantic Kernel for intelligent applications<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/54132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/120281"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=54132"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/54132\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/54133"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=54132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=54132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=54132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}