Jump to content
Xtreme .Net Talk

Unlocking the Power of GitHub Models in .NET with Semantic Kernel


Recommended Posts

Guest Bruno Capuano
Posted

Explore how to integrate GitHub’s AI models, like GPT, Llama and Phi, into your .NET apps using Microsoft’s Semantic Kernel for intelligent applications.

 

[HEADING=1]Unlocking the Power of GitHub Models in .NET with Semantic Kernel[/HEADING]

 

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 GitHub Marketplace. These models can help developers build powerful AI-driven applications with ease. In this post, we’ll explore how .NET programmers can take advantage of these models and integrate them into their applications using Semantic Kernel.

 

[HEADING=1]Introduction to GitHub Models[/HEADING]

 

GitHub has expanded its toolkit by launching GitHub Models, 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.

 

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.

 

[HEADING=1]Semantic Kernel: A Brief Overview[/HEADING]

 

Semantic Kernel 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.

 

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.

 

[HEADING=1]Using GitHub Models with Semantic Kernel[/HEADING]

 

To give you a practical example, let’s explore how you can integrate GitHub Models into a C# application using Semantic Kernel. There’s a GitHub repository that provides a working sample of how this integration can be achieved.

 

Here’s a quick step-by-step guide to get started:

 

[HEADING=2]Step 1: Install the necessary NuGet packages[/HEADING]

 

First, ensure you have the required NuGet packages in your C# project:

 

 

dotnet add package Microsoft.SemanticKernel --version 1.18.2dotnet add package Microsoft.Extensions.Configuration.UserSecrets --version 9.0.0-rc.1.24431.7

 

 

The Semantic Kernel package allows you to interact with the GitHub Models through the API. Microsoft Configuration User Secrets is used to store and retrieve the required GitHub Token.

 

[HEADING=2]Step 2: Setup project secrets with your GitHub Personal Access Token[/HEADING]

 

Generate a new GitHub Personal Access Token. Navigate to the root of your C# project and run these commands to add the Token.

 

 

dotnet user-secrets initdotnet user-secrets set "GH_PAT" "< PAT >"

 

 

In the repository Sample console application, these code is used to retrieve:

 

 

  • GitHub Models, model name
  • GitHub Models, model endpoint
  • GitHub Personal Access Token

 

 

 

var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();var modelId = "Phi-3.5-mini-instruct";var uri = "https://models.inference.ai.azure.com";var githubPAT = config["GH_PAT"];

 

 

This is an example of how to set the modelId and the uri, and the GitHub PAT using secrets:

 

[ATTACH type=full" alt="an example of how to set the modelId and the uri, without using secrets]6031[/ATTACH]

 

[HEADING=2]Step 3: Configure the Semantic Kernel client to use GitHub Models[/HEADING]

 

Next, set up the Semantic Kernel to integrate with the GitHub models API:

 

 

// create clientvar client = new OpenAIClient(new ApiKeyCredential(githubPAT), new OpenAIClientOptions { Endpoint = new Uri(uri) });// Create a chat completion servicevar builder = Kernel.CreateBuilder();builder.AddOpenAIChatCompletion(modelId, client);// Get the chat completion serviceKernel kernel = builder.Build();var chat = kernel.GetRequiredService<IChatCompletionService>();

 

[HEADING=2]Step 4: Run the App[/HEADING]

 

Now, define the task you want the GitHub model to perform. The sample console app, is a standard Q&A chat that runs in the console:

 

 

var history = new ChatHistory();history.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.");while (true){   Console.Write("Q: ");   var userQ = Console.ReadLine();   if (string.IsNullOrEmpty(userQ))   {       break;   }   history.AddUserMessage(userQ);   var sb = new StringBuilder();   var result = chat.GetStreamingChatMessageContentsAsync(history);   Console.Write("AI: ");   await foreach (var item in result)   {       sb.Append(item);       Console.Write(item.Content);   }   Console.WriteLine();   history.AddAssistantMessage(sb.ToString());}

 

 

Optional: The repo is ready to run the sample project using Codespaces. The chat demo application should look like these:

 

[ATTACH type=full" alt="Sample chat console application running in Codespaces]6032[/ATTACH]

 

[HEADING=1]Summary[/HEADING]

 

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.

 

If you’re looking to dive deeper into this topic, check out the following resources:

 

 

 

 

Happy coding!

 

The post Unlocking the Power of GitHub Models in .NET with Semantic Kernel appeared first on .NET Blog.

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...