2. GitHub Models Connection
- 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
| Capability | Description |
|---|---|
| AI Models | GPT-4o, GPT-4o-mini, o3-mini, and more |
| API Style | OpenAI-compatible |
| Authentication | GitHub Personal Access Token |
| Pricing | Includes 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
- Open GitHub token settings:
https://github.com/settings/tokens - Select:
Generate new token -> Generate new token (classic)
- Configure the token:
| Setting | Suggested Value |
|---|---|
| Name | microsoft-agent-framework-workshop-token |
| Expiration | 30 days |
| Scopes | No additional scopes needed |
- Click Generate token
- 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
| Parameter | Purpose |
|---|---|
api_key | Uses your GitHub token |
base_url | Redirects 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.
Recommended Models
| Model | Best Use Case |
|---|---|
gpt-4o | High-quality outputs |
gpt-4o-mini | Fast and lightweight tasks |
o3-mini | Reasoning-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
| Task | Done |
|---|---|
| GitHub token created | โ |
Token added to .env | โ |
| Test file created | โ |
| Script executed successfully | โ |
| AI response received | โ |
Next
Continue to 3. Microsoft Agent Framework Agents.