Перейти к содержимому

GitHub Models integration

Это содержимое пока не доступно на вашем языке.

GitHub logo

GitHub Models provides access to various AI models including OpenAI’s GPT models, DeepSeek, Microsoft’s Phi models, and other leading AI models, all accessible through GitHub’s infrastructure. The Aspire GitHub Models integration enables you to connect to GitHub Models from your applications for prototyping and production scenarios.

The Aspire GitHub Models hosting integration models GitHub Models resources as GitHubModelResource. To access these types and APIs, install the 📦 Aspire.Hosting.GitHub.Models NuGet package:

Aspire CLI — Добавить пакет Aspire.Hosting.GitHub.Models
aspire add github-models

Aspire CLI интерактивен; выберите подходящий результат поиска при запросе:

Aspire CLI — Пример вывода
Select an integration to add:
> github-models (Aspire.Hosting.GitHub.Models)
> Other results listed as selectable options...

To add a GitHubModelResource to your AppHost project, call the AddGitHubModel method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var model = GitHubModel.OpenAI.OpenAIGpt4oMini;
var chat = builder.AddGitHubModel("chat", model);
builder.AddProject<Projects.ExampleProject>()
.WithReference(chat);
builder.Build().Run();

The preceding code adds a GitHub Model resource named chat using the GitHubModel constant for OpenAI’s GPT-4o-mini model. The WithReference method passes the connection information to the ExampleProject project.

For organization-specific requests, you can specify an organization parameter:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var organization = builder.AddParameter("github-org");
var model = GitHubModel.OpenAI.OpenAIGpt4oMini;
var chat = builder.AddGitHubModel("chat", model, organization);
builder.AddProject<Projects.ExampleProject>()
.WithReference(chat);
builder.Build().Run();

When an organization is specified, the token must be attributed to that organization in GitHub.

The GitHub Models integration supports multiple ways to configure authentication:

By default, the integration creates a parameter named {resource_name}-gh-apikey that automatically falls back to the GITHUB_TOKEN environment variable:

var model = GitHubModel.OpenAI.OpenAIGpt4oMini;
var chat = builder.AddGitHubModel("chat", model);

Then in user secrets:

{
"Parameters": {
"chat-gh-apikey": "YOUR_GITHUB_TOKEN_HERE"
}
}

You can also specify a custom parameter for the API key:

var apiKey = builder.AddParameter("my-api-key", secret: true);
var model = GitHubModel.OpenAI.OpenAIGpt4oMini;
var chat = builder.AddGitHubModel("chat", model)
.WithApiKey(apiKey);

Then in user secrets:

{
"Parameters": {
"my-api-key": "YOUR_GITHUB_TOKEN_HERE"
}
}

You can add health checks to verify the GitHub Models endpoint accessibility and API key validity:

var model = GitHubModel.OpenAI.OpenAIGpt4oMini;
var chat = builder.AddGitHubModel("chat", model)
.WithHealthCheck();

GitHub Models supports various AI models. Use the strongly-typed GitHubModel constants for the most up-to-date list of available models. Some popular options include:

  • GitHubModel.OpenAI.OpenAIGpt4oMini
  • GitHubModel.OpenAI.OpenAIGpt41Mini
  • GitHubModel.DeepSeek.DeepSeekV30324
  • GitHubModel.Microsoft.Phi4MiniInstruct

Check the GitHub Models documentation for more information about these models and their capabilities.

When you reference a GitHub Model resource using WithReference, the following connection properties are made available to the consuming project:

The GitHub Model resource exposes the following connection properties:

Property NameDescription
UriThe GitHub Models inference endpoint URI, with the format https://models.github.ai/inference
KeyThe API key (PAT or GitHub App token) for authentication
ModelNameThe model identifier for inference requests, for instance openai/gpt-4o-mini
OrganizationThe organization attributed to the request (available when configured)

Example properties:

Uri: https://models.github.ai/inference
ModelName: openai/gpt-4o-mini

To get started with the Aspire GitHub Models client integration, you can use either the Azure AI Inference client or the OpenAI client, depending on your needs and model compatibility.

Install the 📦 Aspire.Azure.AI.Inference NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.AI.Inference package
dotnet add package Aspire.Azure.AI.Inference

In the Program.cs file of your client-consuming project, use the AddAzureChatCompletionsClient method to register a ChatCompletionsClient for dependency injection:

builder.AddAzureChatCompletionsClient("chat");

You can then retrieve the ChatCompletionsClient instance using dependency injection:

public class ExampleService(ChatCompletionsClient client)
{
public async Task<string> GetResponseAsync(string prompt)
{
var response = await client.GetChatCompletionsAsync(
new[]
{
new ChatMessage(ChatRole.User, prompt)
});
return response.Value.Choices[0].Message.Content;
}
}

Add ChatCompletionsClient with registered IChatClient

Section titled “Add ChatCompletionsClient with registered IChatClient”

If you’re using the Microsoft.Extensions.AI abstractions, you can register an IChatClient:

builder.AddAzureChatCompletionsClient("chat")
.AddChatClient();

Then use it in your services:

public class StoryService(IChatClient chatClient)
{
public async Task<string> GenerateStoryAsync(string prompt)
{
var response = await chatClient.GetResponseAsync(prompt);
return response.Text;
}
}

For models compatible with the OpenAI API (such as openai/gpt-4o-mini), you can use the OpenAI client. Install the 📦 Aspire.OpenAI NuGet package:

.NET CLI — Add Aspire.OpenAI package
dotnet add package Aspire.OpenAI
builder.AddOpenAIClient("chat");

You can then use the OpenAI client:

public class ChatService(OpenAIClient client)
{
public async Task<string> GetChatResponseAsync(string prompt)
{
var chatClient = client.GetChatClient(GitHubModel.OpenAI.OpenAIGpt4oMini);
var response = await chatClient.CompleteChatAsync(
new[]
{
new UserChatMessage(prompt)
});
return response.Value.Content[0].Text;
}
}

Add OpenAI client with registered IChatClient

Section titled “Add OpenAI client with registered IChatClient”
builder.AddOpenAIClient("chat")
.AddChatClient();

The GitHub Models integration supports configuration through user secrets, environment variables, or app settings. The integration automatically uses the GITHUB_TOKEN environment variable if available, or you can specify a custom API key parameter.

The GitHub Models integration requires a GitHub personal access token with models: read permission. The token can be provided in several ways:

Environment variables in Codespaces and GitHub Actions
Section titled “Environment variables in Codespaces and GitHub Actions”

When running an app in GitHub Codespaces or GitHub Actions, the GITHUB_TOKEN environment variable is automatically available and can be used without additional configuration. This token has the necessary permissions to access GitHub Models for the repository context.

// No additional configuration needed in Codespaces/GitHub Actions
var model = GitHubModel.OpenAI.OpenAIGpt4oMini;
var chat = builder.AddGitHubModel("chat", model);
Personal access tokens for local development
Section titled “Personal access tokens for local development”

For local development, you need to create a fine-grained personal access token with the models: read scope and configure it in user secrets:

{
"Parameters": {
"chat-gh-apikey": "github_pat_YOUR_TOKEN_HERE"
}
}

The connection string follows this format:

Endpoint=https://models.github.ai/inference;Key={api_key};Model={model_name};DeploymentId={model_name}

For organization-specific requests:

Endpoint=https://models.github.ai/orgs/{organization}/inference;Key={api_key};Model={model_name};DeploymentId={model_name}

The dotnet/aspire repo contains an example application demonstrating the GitHub Models integration. You can find the sample in the Aspire GitHub repository.

The GitHub Models integration uses standard HTTP client logging categories:

  • System.Net.Http.HttpClient
  • Microsoft.Extensions.Http

HTTP requests to the GitHub Models API are automatically traced when using the Azure AI Inference or OpenAI clients.

Вопросы & ответыСотрудничатьСообществоОбсуждатьСмотреть