GitHub AI Models usage in Java

This simple Java code will demonstrate the usage of GitHub AI models through the Azure AI SDK, showcasing how to seamlessly integrate Microsoft's powerful AI infrastructure with GitHub's development ecosystem.

This simple Java code will demonstrate the usage of GitHub AI models through the Azure AI SDK, showcasing how to seamlessly integrate Microsoft’s powerful AI infrastructure with GitHub’s development ecosystem.

Example of GitHub AI Models Usage

Model : gpt-4o-mini

java

package com.ai.java.geek.github.models;

import com.azure.ai.inference.ChatCompletionsClient;
import com.azure.ai.inference.ChatCompletionsClientBuilder;
import com.azure.ai.inference.models.ChatCompletions;
import com.azure.ai.inference.models.ChatCompletionsOptions;
import com.azure.ai.inference.models.ChatRequestMessage;
import com.azure.ai.inference.models.ChatRequestSystemMessage;
import com.azure.core.credential.AzureKeyCredential;

import java.util.Arrays;
import java.util.List;

public final class GitHubAIModelsExample {
    public static void main(String[] args) {
        // To authenticate with the model you will need to generate a personal access token (PAT) in your GitHub settings.
        // Create your PAT token by following instructions here: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
        String key = "GitHubAccessTokenHere";
        String endpoint = "https://models.inference.ai.azure.com";
        String model = "gpt-4o-mini";

        ChatCompletionsClient client = new ChatCompletionsClientBuilder()
                .credential(new AzureKeyCredential(key))
                .endpoint(endpoint)
                .buildClient();

        List<ChatRequestMessage> chatMessages = Arrays.asList(
                new ChatRequestSystemMessage("What is java?")
        );

        ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions(chatMessages);
        chatCompletionsOptions.setModel(model);

        ChatCompletions completions = client.complete(chatCompletionsOptions);

        System.out.printf("%s.%n", completions.getChoice().getMessage().getContent());
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ai.java.geek.github.models</groupId>
    <artifactId>TestGenAIMaven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-ai-inference</artifactId>
            <version>1.0.0-beta.2</version>
        </dependency>
    </dependencies>
</project>

Written by Meera Prince