Thursday, January 22, 2026

Using Microsoft Agent Framework with AI models hosted on GitHub

Overview

In this article I will show you how you can use the Microsoft Agent Framework to experiment with AI models hosted on GitHub. GitHub AI Models are intended for learning, experimentation and proof-of-concept activities. The GitHub Models 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?type=models to work with free GitHub AI Models. At the time of writing, these are a subset of the models available:


For this article, I will use the "OpenAI GPT-4o" model highlighted above. 

Selecting the "OpenAI GPT-4o" leads you to the page below. Click on the "<> Use this model" button.

On the dialog that pops up, select C#, then click on "Run a basic code sample".

On the next dialog, you will see the signature of the model. In our case it is "openai/gpt-4o".


Click on "1. Configure authentication".

Next, click on the "Create Personal Access Token" button. 

You may need to go through a verification process.

Make your selections.

On the next pop-up, click on "Generate token".

Copy the newly generated token and place it is a safe place because you cannot view this token again once you leave this page. 

Let's use Microsoft Agent Framework (MAF)

In a working directory, create a C# console app named GitHubAiModelMAF inside a terminal window with the following command:

dotnet new console -n GitHubAiModelMAF

Change into the newly created directory GitHubAiModelSK with:

cd GitHubAiModelMAF

Next, let's add two packages to our console application with:

dotnet add package Azure.AI.OpenAI
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI -v 1.0.0-preview.251114.1
dotnet add package Microsoft.Extensions.Configuration.Json

Create a file named appsettings.json. Add this to appsettings.json:

{
    "GitHub": {
        "Token": "PUT-PERSONAL-ACCESS-TOKEN-HERE",
        "ApiEndpoint": "https://models.github.ai/inference",
        "Model": "openai/gpt-4o"
    }
}

Replace "PUT-PERSONAL-ACCESS-TOKEN-HERE" 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 System.Text;
using Azure;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using OpenAI;
using OpenAI.Chat;

var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .Build();

string? apiKey = config["GitHub:Token"];
string? model = config["GitHub:Model"] ?? "openai/gpt-4o-mini";
string? endpoint = config["GitHub:ApiEndpoint"] ?? "https://models.github.ai/inference";

IChatClient chatClient = new ChatClient(
    model,
    new AzureKeyCredential(apiKey!),
    new OpenAIClientOptions
    {
        Endpoint = new Uri(endpoint)
    }
)
.AsIChatClient();

var instructions = @"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'.";

var agent = chatClient
    .CreateAIAgent(
        instructions: instructions,
        name: "Assistant"
    );

// Instantiate a StringBuilder
StringBuilder strBuilder = new();

// User question & answer loop
while (true)
{
    Console.Write("Q: ");
    var result = await agent.RunAsync(Console.ReadLine()!);
    Console.WriteLine(result);
}

Run the application:

I asked the question "What is the longest river in the world?" and the AI answered as shown above. 

Conclusion

GitHub AI models are easy to access with the new Microsoft Afent Framework (MSF). I hope you come up with great AI driven applications that make a difference to our world.


No comments:

Post a Comment