Overview
In this article I will show you how you can experiment with AI models hosted on GitHub. GitHub AI Models are intended for learning, experimentation and proof-of-concept activities. The feature is subject to various limits (including requests per minute, requests per day, tokens per request, and concurrent requests) and is not designed for production use cases.
Getting Started
There are many AI models from a variety of vendors that you can choose from. The starting point is to visit https://github.com/marketplace/models. At the time of writing, these are a subset of the models available:
The first thing we need to do is get a 'personal access token' by clicking on the indicated button above.
Give your token a name, set the expiration, and optionally describe the purpose of the token. Thereafter, click on the green 'Generate token' button at the bottom of the page.
Copy the newly generated token and place it is a safe place because you cannot view this token again once you leave the above page.
Let's use Semantic Kernel
In a working directory, create a C# console app named GitHubAiModelSK inside a terminal window with the following command:
dotnet new console -n GitHubAiModelSK
Change into the newly created directory GitHubAiModelSK with:
cd GitHubAiModelSK
Next, let's add two packages to our console application with:
dotnet add package Microsoft.SemanticKernel -v 1.25.0
dotnet add package Microsoft.Extensions.Configuration.Json
Open the project in VS Code and add this directive to the .csproj file right below: <Nullable>enable</Nullable>:
<NoWarn>SKEXP0010</NoWarn>
Create a file named appsettings.json. Add this to appsettings.json:
{"AI": {"Endpoint": "https://models.inference.ai.azure.com","Model": "Phi-3.5-mini-instruct","PAT": "fake-token"}}
Replace "fake-token" with the personal access token that you got from GitHub.
Next, open Program.cs in an editor and delete all contents of the file. Add this code to Program.cs:
using Microsoft.SemanticKernel;using System.Text;using Microsoft.SemanticKernel.ChatCompletion;using OpenAI;using System.ClientModel;using Microsoft.Extensions.Configuration;var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();var modelId = config["AI:Model"]!;var uri = config["AI:Endpoint"]!;var githubPAT = config["AI:PAT"]!;var client = new OpenAIClient(new ApiKeyCredential(githubPAT), new OpenAIClientOptions { Endpoint = new Uri(uri) });// Initialize the Semantic kernelvar builder = Kernel.CreateBuilder();builder.AddOpenAIChatCompletion(modelId, client);var kernel = builder.Build();// get a chat completion servicevar chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();// Create a new chat by specifying the assistantChatHistory chat = new(@"You are an AI assistant that helps people find information.The response must be brief and should not exceed one paragraph.If you do not know the answer then simply say 'I do not know the answer'.");// Instantiate a StringBuilderStringBuilder strBuilder = new();// User question & answer loopwhile (true){// Get the user's questionConsole.Write("Q: ");chat.AddUserMessage(Console.ReadLine()!);// Clear contents of the StringBuilderstrBuilder.Clear();// Get the AI response streamed back to the consoleawait foreach (var message in chatCompletionService.GetStreamingChatMessageContentsAsync(chat, kernel: kernel)){Console.Write(message);strBuilder.Append(message.Content);}Console.WriteLine();chat.AddAssistantMessage(strBuilder.ToString());Console.WriteLine();}
Run the application:
I asked the question "How many pyramids are there in Egypt?" and the AI answered as shown above.
Using a different model
How about we use a different AI model. For example, I will try the 'Meta-Llama-3.1-405B-Instruct' model. We need to get the model ID. Click on the model on the https://github.com/marketplace/models page.
Change Model in appsettings.json to "Meta-Llama-3.1-405B-Instruct".Run the application again. This is what I experienced with the AI model meta-llama-3.1-405b-instruct:
Conclusion
GitHub AI models are easy to access. I hope you come up with great AI driven applications that make a difference to our world.
No comments:
Post a Comment