MAF

2. GitHub Models Connection

In this module, youโ€™ll set up access to GitHub Models and make your first AI API request using Python.

Learning Goals

After completing this section, you should be able to:

  • Understand what GitHub Models is
  • Generate a GitHub Personal Access Token (PAT)
  • Configure environment variables securely
  • Connect to an AI model using the OpenAI SDK
  • Send and receive responses from a model

Introduction to GitHub Models

GitHub provides a hosted AI inference service called GitHub Models, which gives developers access to several popular models through an OpenAI-compatible API.

Highlights

CapabilityDescription
AI ModelsGPT-4o, GPT-4o-mini, o3-mini, and more
API StyleOpenAI-compatible
AuthenticationGitHub Personal Access Token
PricingIncludes a free usage tier

API Endpoint

https://models.github.ai/inference

Because the API follows the OpenAI format, you can use the standard OpenAI Python library without major changes.

Step 1 - Generate a GitHub Access Token

To use GitHub Models, youโ€™ll need a Personal Access Token.

Instructions

  1. Open GitHub token settings: https://github.com/settings/tokens
  2. Select:
Generate new token -> Generate new token (classic)
  1. Configure the token:
SettingSuggested Value
Namemicrosoft-agent-framework-workshop-token
Expiration30 days
ScopesNo additional scopes needed
  1. Click Generate token
  2. Copy the token immediately and store it safely

[!IMPORTANT] GitHub only shows the token once.

Step 2 - Store the Token Securely

Create or update a .env file in your project root.

GITHUB_TOKEN=your_token_here
GITHUB_MODEL=gpt-4o-mini

This keeps secrets outside your source code.

Step 3 - Create a Test Script

Create a new Python file inside the current project root (lab/) for testing GitHub Models.

touch test_github_models_connection.py

Now add the following Python code.

"""
Module 2 - Test GitHub Models Connection

Run:
    python test_github_models_connection.py
    or
    uv run test_github_models_connection.py
"""

import asyncio
import os

from dotenv import load_dotenv
from openai import AsyncOpenAI

load_dotenv()


async def main():
    """Send a simple request to GitHub Models."""

    print("๐Ÿ”Œ Connecting to GitHub Models...")

    client = AsyncOpenAI(
        api_key=os.getenv("GITHUB_TOKEN"),
        base_url="https://models.github.ai/inference",
    )

    print("๐Ÿ“จ Sending request...")

    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "user",
                "content": "Reply with: Hello from GitHub Models!",
            }
        ],
    )

    message = response.choices[0].message.content

    print("\n๐Ÿ’ฌ Model Response:", message)


if __name__ == "__main__":
    asyncio.run(main())

Step 4 - Execute the Script

Run the test file:

python test_github_models_connection.py
# or
uv run test_github_models_connection.py

Example Output

๐Ÿ”Œ Connecting to GitHub Models...
๐Ÿ“จ Sending request...

๐Ÿ’ฌ Model Response: Hello from GitHub Models!

If you receive a valid response, your setup is working correctly.

Code Walkthrough

Creating the Client

client = AsyncOpenAI(
    api_key=os.getenv("GITHUB_TOKEN"),
    base_url="https://models.github.ai/inference",
)

What this does

ParameterPurpose
api_keyUses your GitHub token
base_urlRedirects requests to GitHub Models

Sending a Chat Request

response = await client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {
            "role": "user",
            "content": "Reply with: Hello from GitHub Models!",
        }
    ],
)

This sends a standard OpenAI-style chat request.

Reading the Response

message = response.choices[0].message.content

The generated text is stored inside the response object.

ModelBest Use Case
gpt-4oHigh-quality outputs
gpt-4o-miniFast and lightweight tasks
o3-miniReasoning-focused workflows

For most workshop examples, gpt-4o-mini is a good default because it is fast and cost-efficient.

Suggested Project Layout

โ”œโ”€โ”€ lab
โ”‚ย ย  โ”œโ”€โ”€ main.py
โ”‚ย ย  โ”œโ”€โ”€ pyproject.toml
โ”‚ย ย  โ”œโ”€โ”€ README.md
โ”‚ย ย  โ”œโ”€โ”€ requirements.txt
โ”‚ย ย  โ”œโ”€โ”€ test_github_models_connection.py
โ”‚ย ย  โ””โ”€โ”€ uv.lock

Completion Checklist

TaskDone
GitHub token createdโ˜
Token added to .envโ˜
Test file createdโ˜
Script executed successfullyโ˜
AI response receivedโ˜

Next

Continue to 3. Microsoft Agent Framework Agents.