This package provides a deprecated integration module that enables Java applications to interact with GitHub Models through the LangChain4j framework. It offers chat models (both synchronous and streaming), embedding models, and support for AI services with tool integration, JSON schema responses, and responsible AI features. The module wraps Azure AI Inference SDK to provide a unified API for accessing various language models hosted on GitHub Models, including chat completion capabilities, embeddings generation, and content filtering management. As of version 1.10.0, this module has been marked for deprecation and future removal, with users recommended to migrate to the langchain4j-openai-official module for enhanced functionality and better integration. The library is designed for reusability as a foundational component in LLM-powered Java applications that need to leverage GitHub-hosted AI models, offering builder patterns for configuration, support for proxy options, custom timeouts, and comprehensive model service versioning capabilities.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
How to configure GitHub personal access token authentication.
All models require a GitHub personal access token with access to GitHub Models.
.gitHubToken(String gitHubToken)Recommended: Never hardcode tokens
// ✅ Best practice
GitHubModelsChatModel model = GitHubModelsChatModel.builder()
.gitHubToken(System.getenv("GITHUB_TOKEN"))
.modelName("gpt-4o")
.build();Linux/macOS:
export GITHUB_TOKEN=ghp_your_token_hereWindows (Command Prompt):
set GITHUB_TOKEN=ghp_your_token_hereWindows (PowerShell):
$env:GITHUB_TOKEN="ghp_your_token_here"application.properties:
github.token=${GITHUB_TOKEN}Load in code:
Properties props = new Properties();
props.load(new FileInputStream("application.properties"));
GitHubModelsChatModel model = GitHubModelsChatModel.builder()
.gitHubToken(props.getProperty("github.token"))
.modelName("gpt-4o")
.build();application.yml:
github:
models:
token: ${GITHUB_TOKEN}Spring Bean:
@Configuration
public class ModelConfig {
@Value("${github.models.token}")
private String token;
@Bean
public GitHubModelsChatModel chatModel() {
return GitHubModelsChatModel.builder()
.gitHubToken(token)
.modelName("gpt-4o")
.build();
}
}import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
public class SecretsConfig {
private static final SecretsManagerClient client = SecretsManagerClient.create();
public static String getGitHubToken() {
GetSecretValueRequest request = GetSecretValueRequest.builder()
.secretId("github-models-token")
.build();
return client.getSecretValue(request).secretString();
}
}
// Usage
GitHubModelsChatModel model = GitHubModelsChatModel.builder()
.gitHubToken(SecretsConfig.getGitHubToken())
.modelName("gpt-4o")
.build();import io.github.jopenlibs.vault.Vault;
import io.github.jopenlibs.vault.VaultConfig;
public class VaultConfig {
public static String getGitHubToken() throws VaultException {
VaultConfig config = new VaultConfig()
.address("https://vault.example.com")
.token(System.getenv("VAULT_TOKEN"))
.build();
Vault vault = new Vault(config);
return vault.logical()
.read("secret/github-models")
.getData()
.get("token");
}
}
// Usage
GitHubModelsChatModel model = GitHubModelsChatModel.builder()
.gitHubToken(VaultConfig.getGitHubToken())
.modelName("gpt-4o")
.build();import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
public class KeyVaultConfig {
public static String getGitHubToken() {
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl("https://your-vault.vault.azure.net/")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
return secretClient.getSecret("github-models-token").getValue();
}
}
// Usage
GitHubModelsChatModel model = GitHubModelsChatModel.builder()
.gitHubToken(KeyVaultConfig.getGitHubToken())
.modelName("gpt-4o")
.build();public class TokenProvider {
public static String getToken() {
String env = System.getenv("APP_ENV");
if ("production".equals(env)) {
return System.getenv("GITHUB_TOKEN_PROD");
} else if ("staging".equals(env)) {
return System.getenv("GITHUB_TOKEN_STAGING");
} else {
return System.getenv("GITHUB_TOKEN_DEV");
}
}
}
GitHubModelsChatModel model = GitHubModelsChatModel.builder()
.gitHubToken(TokenProvider.getToken())
.modelName("gpt-4o")
.build();For tests, use a separate test token or mock the authentication:
public class TestConfig {
public static String getTestToken() {
// Option 1: Separate test token
String testToken = System.getenv("GITHUB_TOKEN_TEST");
if (testToken != null) {
return testToken;
}
// Option 2: Fallback to development token
return System.getenv("GITHUB_TOKEN");
}
}@Test
public void testWithMockClient() {
ChatCompletionsClient mockClient = mock(ChatCompletionsClient.class);
GitHubModelsChatModel model = GitHubModelsChatModel.builder()
.chatCompletionsClient(mockClient) // Bypasses token requirement
.modelName("test-model")
.build();
// Configure mock and test
}✅ Do:
❌ Don't:
# Ensure these are ignored
.env
.env.local
secrets.properties
application-secrets.ymlpublic static GitHubModelsChatModel createModel() {
String token = System.getenv("GITHUB_TOKEN");
if (token == null || token.trim().isEmpty()) {
throw new IllegalStateException(
"GITHUB_TOKEN environment variable is required. " +
"Set it with: export GITHUB_TOKEN=your-token"
);
}
return GitHubModelsChatModel.builder()
.gitHubToken(token)
.modelName("gpt-4o")
.build();
}When rotating tokens:
// Use configuration reload for zero-downtime rotation
public class RefreshableTokenProvider {
private volatile String cachedToken;
private long lastRefresh;
private static final long REFRESH_INTERVAL = Duration.ofHours(1).toMillis();
public String getToken() {
long now = System.currentTimeMillis();
if (now - lastRefresh > REFRESH_INTERVAL) {
cachedToken = loadTokenFromSecretStore();
lastRefresh = now;
}
return cachedToken;
}
}try {
String token = System.getenv("GITHUB_TOKEN");
if (token == null) {
throw new IllegalStateException("GITHUB_TOKEN not set");
}
GitHubModelsChatModel model = GitHubModelsChatModel.builder()
.gitHubToken(token)
.modelName("gpt-4o")
.build();
} catch (IllegalStateException e) {
System.err.println("Configuration error: " + e.getMessage());
System.err.println("Please set GITHUB_TOKEN environment variable");
System.exit(1);
}import com.azure.core.exception.HttpResponseException;
try {
ChatResponse response = model.chat(request);
} catch (HttpResponseException e) {
if (e.getResponse().getStatusCode() == 401) {
System.err.println("Authentication failed. Check your GitHub token.");
} else if (e.getResponse().getStatusCode() == 403) {
System.err.println("Access forbidden. Token may lack required permissions.");
}
}Centralize token loading in SPI factory:
import dev.langchain4j.model.github.spi.GitHubModelsChatModelBuilderFactory;
public class TokenManagingFactory implements GitHubModelsChatModelBuilderFactory {
@Override
public GitHubModelsChatModel.Builder get() {
String token = loadTokenSecurely();
return GitHubModelsChatModel.builder()
.gitHubToken(token);
}
private String loadTokenSecurely() {
// Load from secret management system
String token = System.getenv("GITHUB_TOKEN");
if (token == null) {
throw new IllegalStateException(
"GITHUB_TOKEN environment variable is required"
);
}
return token;
}
}Install with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-github-models@1.11.0docs